1 // Copyright 2021 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_MODULE_H_ 16 #define SRC_AST_MODULE_H_ 17 18 #include <string> 19 #include <vector> 20 21 #include "src/ast/function.h" 22 #include "src/ast/type.h" 23 24 namespace tint { 25 namespace ast { 26 27 class TypeDecl; 28 29 /// Module holds the top-level AST types, functions and global variables used by 30 /// a Program. 31 class Module : public Castable<Module, Node> { 32 public: 33 /// Constructor 34 /// @param pid the identifier of the program that owns this node 35 /// @param src the source of this node 36 Module(ProgramID pid, const Source& src); 37 38 /// Constructor 39 /// @param pid the identifier of the program that owns this node 40 /// @param src the source of this node 41 /// @param global_decls the list of global types, functions, and variables, in 42 /// the order they were declared in the source program 43 Module(ProgramID pid, 44 const Source& src, 45 std::vector<const Node*> global_decls); 46 47 /// Destructor 48 ~Module() override; 49 50 /// @returns the ordered global declarations for the translation unit GlobalDeclarations()51 const std::vector<const Node*>& GlobalDeclarations() const { 52 return global_declarations_; 53 } 54 55 /// Add a global variable to the Builder 56 /// @param var the variable to add 57 void AddGlobalVariable(const Variable* var); 58 59 /// @returns true if the module has the global declaration `decl` 60 /// @param decl the declaration to check HasGlobalDeclaration(Node * decl)61 bool HasGlobalDeclaration(Node* decl) const { 62 for (auto* d : global_declarations_) { 63 if (d == decl) { 64 return true; 65 } 66 } 67 return false; 68 } 69 70 /// @returns the global variables for the translation unit GlobalVariables()71 const VariableList& GlobalVariables() const { return global_variables_; } 72 73 /// @returns the global variables for the translation unit GlobalVariables()74 VariableList& GlobalVariables() { return global_variables_; } 75 76 /// Adds a type declaration to the Builder. 77 /// @param decl the type declaration to add 78 void AddTypeDecl(const TypeDecl* decl); 79 80 /// @returns the TypeDecl registered as a TypeDecl() 81 /// @param name the name of the type to search for 82 const TypeDecl* LookupType(Symbol name) const; 83 84 /// @returns the declared types in the translation unit TypeDecls()85 const std::vector<const TypeDecl*>& TypeDecls() const { return type_decls_; } 86 87 /// Add a function to the Builder 88 /// @param func the function to add 89 void AddFunction(const Function* func); 90 91 /// @returns the functions declared in the translation unit Functions()92 const FunctionList& Functions() const { return functions_; } 93 94 /// Clones this node and all transitive child nodes using the `CloneContext` 95 /// `ctx`. 96 /// @param ctx the clone context 97 /// @return the newly cloned node 98 const Module* Clone(CloneContext* ctx) const override; 99 100 /// Copy copies the content of the Module src into this module. 101 /// @param ctx the clone context 102 /// @param src the module to copy into this module 103 void Copy(CloneContext* ctx, const Module* src); 104 105 private: 106 std::vector<const Node*> global_declarations_; 107 std::vector<const TypeDecl*> type_decls_; 108 FunctionList functions_; 109 VariableList global_variables_; 110 }; 111 112 } // namespace ast 113 } // namespace tint 114 115 #endif // SRC_AST_MODULE_H_ 116