2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SRC_AST_FUNCTION_H_
|
|
|
|
#define SRC_AST_FUNCTION_H_
|
|
|
|
|
|
|
|
#include <string>
|
2020-11-03 16:26:09 +00:00
|
|
|
#include <tuple>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-07-20 13:46:32 +00:00
|
|
|
#include "src/ast/binding_decoration.h"
|
2020-07-27 15:25:00 +00:00
|
|
|
#include "src/ast/block_statement.h"
|
2020-07-15 18:26:25 +00:00
|
|
|
#include "src/ast/builtin_decoration.h"
|
2021-03-11 17:39:32 +00:00
|
|
|
#include "src/ast/decoration.h"
|
2021-01-15 12:22:16 +00:00
|
|
|
#include "src/ast/group_decoration.h"
|
2020-07-15 18:26:25 +00:00
|
|
|
#include "src/ast/location_decoration.h"
|
2020-09-21 17:51:31 +00:00
|
|
|
#include "src/ast/pipeline_stage.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/ast/variable.h"
|
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace ast {
|
|
|
|
|
|
|
|
/// A Function statement.
|
2020-11-30 23:30:58 +00:00
|
|
|
class Function : public Castable<Function, Node> {
|
2020-03-02 20:47:43 +00:00
|
|
|
public:
|
|
|
|
/// Create a function
|
2021-04-13 23:27:27 +00:00
|
|
|
/// @param program_id the identifier of the program that owns this node
|
2020-03-02 20:47:43 +00:00
|
|
|
/// @param source the variable source
|
2020-12-11 18:24:53 +00:00
|
|
|
/// @param symbol the function symbol
|
2020-03-02 20:47:43 +00:00
|
|
|
/// @param params the function parameters
|
|
|
|
/// @param return_type the return type
|
2020-11-14 01:13:04 +00:00
|
|
|
/// @param body the function body
|
2020-12-07 20:45:14 +00:00
|
|
|
/// @param decorations the function decorations
|
2021-03-15 17:01:34 +00:00
|
|
|
/// @param return_type_decorations the return type decorations
|
2021-04-13 23:27:27 +00:00
|
|
|
Function(ProgramID program_id,
|
|
|
|
const Source& source,
|
2020-12-11 18:24:53 +00:00
|
|
|
Symbol symbol,
|
2020-04-06 19:37:37 +00:00
|
|
|
VariableList params,
|
2021-05-05 09:09:41 +00:00
|
|
|
ast::Type* return_type,
|
2020-12-07 20:45:14 +00:00
|
|
|
BlockStatement* body,
|
2021-03-15 17:01:34 +00:00
|
|
|
DecorationList decorations,
|
|
|
|
DecorationList return_type_decorations);
|
2020-03-02 20:47:43 +00:00
|
|
|
/// Move constructor
|
2020-04-09 18:52:06 +00:00
|
|
|
Function(Function&&);
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
~Function() override;
|
|
|
|
|
2020-09-21 17:51:31 +00:00
|
|
|
/// @returns the functions pipeline stage or None if not set
|
2021-10-15 17:33:10 +00:00
|
|
|
ast::PipelineStage PipelineStage() const;
|
2020-09-21 17:51:31 +00:00
|
|
|
|
2020-09-21 18:49:01 +00:00
|
|
|
/// @returns true if this function is an entry point
|
2021-10-15 17:33:10 +00:00
|
|
|
bool IsEntryPoint() const { return PipelineStage() != PipelineStage::kNone; }
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
/// Clones this node and all transitive child nodes using the `CloneContext`
|
|
|
|
/// `ctx`.
|
|
|
|
/// @param ctx the clone context
|
|
|
|
/// @return the newly cloned node
|
|
|
|
Function* Clone(CloneContext* ctx) const override;
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
/// The function symbol
|
|
|
|
Symbol const symbol;
|
|
|
|
|
|
|
|
/// The function params
|
|
|
|
VariableList const params;
|
|
|
|
|
|
|
|
/// The function return type
|
|
|
|
ast::Type* const return_type;
|
|
|
|
|
|
|
|
/// The function body
|
|
|
|
BlockStatement* const body;
|
|
|
|
|
|
|
|
/// The decorations attached to this function
|
|
|
|
DecorationList const decorations;
|
|
|
|
|
|
|
|
/// The decorations attached to the function return type.
|
|
|
|
DecorationList const return_type_decorations;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
/// A list of functions
|
2021-01-21 18:45:50 +00:00
|
|
|
class FunctionList : public std::vector<Function*> {
|
|
|
|
public:
|
|
|
|
/// Appends f to the end of the list
|
|
|
|
/// @param f the function to append to this list
|
|
|
|
void Add(Function* f) { this->emplace_back(f); }
|
|
|
|
|
|
|
|
/// Returns the function with the given name
|
|
|
|
/// @param sym the function symbol to search for
|
|
|
|
/// @returns the associated function or nullptr if none exists
|
|
|
|
Function* Find(Symbol sym) const;
|
|
|
|
|
|
|
|
/// Returns the function with the given name
|
|
|
|
/// @param sym the function symbol to search for
|
|
|
|
/// @param stage the pipeline stage
|
|
|
|
/// @returns the associated function or nullptr if none exists
|
|
|
|
Function* Find(Symbol sym, PipelineStage stage) const;
|
|
|
|
|
|
|
|
/// @param stage the pipeline stage
|
|
|
|
/// @returns true if the Builder contains an entrypoint function with
|
|
|
|
/// the given stage
|
|
|
|
bool HasStage(PipelineStage stage) const;
|
|
|
|
};
|
2020-04-06 19:37:37 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_AST_FUNCTION_H_
|