1 // Copyright 2020 The Tint Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_AST_FUNCTION_H_ 16 #define SRC_AST_FUNCTION_H_ 17 18 #include <string> 19 #include <tuple> 20 #include <utility> 21 #include <vector> 22 23 #include "src/ast/binding_decoration.h" 24 #include "src/ast/block_statement.h" 25 #include "src/ast/builtin_decoration.h" 26 #include "src/ast/decoration.h" 27 #include "src/ast/group_decoration.h" 28 #include "src/ast/location_decoration.h" 29 #include "src/ast/pipeline_stage.h" 30 #include "src/ast/variable.h" 31 32 namespace tint { 33 namespace ast { 34 35 /// A Function statement. 36 class Function : public Castable<Function, Node> { 37 public: 38 /// Create a function 39 /// @param program_id the identifier of the program that owns this node 40 /// @param source the variable source 41 /// @param symbol the function symbol 42 /// @param params the function parameters 43 /// @param return_type the return type 44 /// @param body the function body 45 /// @param decorations the function decorations 46 /// @param return_type_decorations the return type decorations 47 Function(ProgramID program_id, 48 const Source& source, 49 Symbol symbol, 50 VariableList params, 51 const Type* return_type, 52 const BlockStatement* body, 53 DecorationList decorations, 54 DecorationList return_type_decorations); 55 /// Move constructor 56 Function(Function&&); 57 58 ~Function() override; 59 60 /// @returns the functions pipeline stage or None if not set 61 ast::PipelineStage PipelineStage() const; 62 63 /// @returns true if this function is an entry point IsEntryPoint()64 bool IsEntryPoint() const { return PipelineStage() != PipelineStage::kNone; } 65 66 /// Clones this node and all transitive child nodes using the `CloneContext` 67 /// `ctx`. 68 /// @param ctx the clone context 69 /// @return the newly cloned node 70 const Function* Clone(CloneContext* ctx) const override; 71 72 /// The function symbol 73 const Symbol symbol; 74 75 /// The function params 76 const VariableList params; 77 78 /// The function return type 79 const Type* const return_type; 80 81 /// The function body 82 const BlockStatement* const body; 83 84 /// The decorations attached to this function 85 const DecorationList decorations; 86 87 /// The decorations attached to the function return type. 88 const DecorationList return_type_decorations; 89 }; 90 91 /// A list of functions 92 class FunctionList : public std::vector<const Function*> { 93 public: 94 /// Appends f to the end of the list 95 /// @param f the function to append to this list Add(const Function * f)96 void Add(const Function* f) { this->emplace_back(f); } 97 98 /// Returns the function with the given name 99 /// @param sym the function symbol to search for 100 /// @returns the associated function or nullptr if none exists 101 const Function* Find(Symbol sym) const; 102 103 /// Returns the function with the given name 104 /// @param sym the function symbol to search for 105 /// @param stage the pipeline stage 106 /// @returns the associated function or nullptr if none exists 107 const Function* Find(Symbol sym, PipelineStage stage) const; 108 109 /// @param stage the pipeline stage 110 /// @returns true if the Builder contains an entrypoint function with 111 /// the given stage 112 bool HasStage(PipelineStage stage) const; 113 }; 114 115 } // namespace ast 116 } // namespace tint 117 118 #endif // SRC_AST_FUNCTION_H_ 119