Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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_TINT_PROGRAM_H_ |
| 16 | #define SRC_TINT_PROGRAM_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <unordered_set> |
| 20 | |
| 21 | #include "src/tint/ast/function.h" |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 22 | #include "src/tint/constant/manager.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | #include "src/tint/program_id.h" |
| 24 | #include "src/tint/sem/info.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 25 | #include "src/tint/symbol_table.h" |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 26 | #include "src/tint/type/manager.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | 8155b9d | 2022-04-07 19:10:25 +0000 | [diff] [blame] | 28 | // Forward Declarations |
Ben Clayton | a7230f0 | 2022-04-11 14:37:21 +0000 | [diff] [blame] | 29 | namespace tint { |
| 30 | class CloneContext; |
| 31 | } // namespace tint |
dan sinclair | 8155b9d | 2022-04-07 19:10:25 +0000 | [diff] [blame] | 32 | namespace tint::ast { |
| 33 | class Module; |
| 34 | } // namespace tint::ast |
| 35 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 36 | namespace tint { |
| 37 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | /// Program holds the AST, Type information and SymbolTable for a tint program. |
| 39 | class Program { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | public: |
| 41 | /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node> |
| 42 | using ASTNodeAllocator = utils::BlockAllocator<ast::Node>; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | /// SemNodeAllocator is an alias to BlockAllocator<sem::Node> |
| 45 | using SemNodeAllocator = utils::BlockAllocator<sem::Node>; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 46 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 47 | /// Constructor |
| 48 | Program(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | /// Move constructor |
| 51 | /// @param rhs the Program to move |
| 52 | Program(Program&& rhs); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 53 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | /// Move constructor from builder |
| 55 | /// @param builder the builder used to construct the program |
| 56 | explicit Program(ProgramBuilder&& builder); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 57 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | /// Destructor |
| 59 | ~Program(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 61 | /// Move assignment operator |
| 62 | /// @param rhs the Program to move |
| 63 | /// @return this Program |
| 64 | Program& operator=(Program&& rhs); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 65 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 66 | /// @returns the unique identifier for this program |
| 67 | ProgramID ID() const { return id_; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 68 | |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 69 | /// @returns the last allocated (numerically highest) AST node identifier. |
| 70 | ast::NodeID HighestASTNodeID() const { return highest_node_id_; } |
| 71 | |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 72 | /// @returns a reference to the program's constants |
| 73 | const constant::Manager& Constants() const { |
| 74 | AssertNotMoved(); |
| 75 | return constants_; |
| 76 | } |
| 77 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 78 | /// @returns a reference to the program's types |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 79 | const type::Manager& Types() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 80 | AssertNotMoved(); |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 81 | return constants_.types; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 82 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 84 | /// @returns a reference to the program's AST nodes storage |
| 85 | const ASTNodeAllocator& ASTNodes() const { |
| 86 | AssertNotMoved(); |
| 87 | return ast_nodes_; |
| 88 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 89 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 90 | /// @returns a reference to the program's semantic nodes storage |
| 91 | const SemNodeAllocator& SemNodes() const { |
| 92 | AssertNotMoved(); |
| 93 | return sem_nodes_; |
| 94 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 95 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | /// @returns a reference to the program's AST root Module |
| 97 | const ast::Module& AST() const { |
| 98 | AssertNotMoved(); |
| 99 | return *ast_; |
| 100 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 101 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 102 | /// @returns a reference to the program's semantic info |
| 103 | const sem::Info& Sem() const { |
| 104 | AssertNotMoved(); |
| 105 | return sem_; |
| 106 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 107 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 108 | /// @returns a reference to the program's SymbolTable |
| 109 | const SymbolTable& Symbols() const { |
| 110 | AssertNotMoved(); |
| 111 | return symbols_; |
| 112 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 113 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | /// @returns a reference to the program's diagnostics |
| 115 | const diag::List& Diagnostics() const { |
| 116 | AssertNotMoved(); |
| 117 | return diagnostics_; |
| 118 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 119 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 120 | /// Performs a deep clone of this program. |
| 121 | /// The returned Program will contain no pointers to objects owned by this |
| 122 | /// Program, and so after calling, this Program can be safely destructed. |
| 123 | /// @return a new Program copied from this Program |
| 124 | Program Clone() const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 125 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 126 | /// Performs a deep clone of this Program's AST nodes, types and symbols into |
| 127 | /// a new ProgramBuilder. Semantic nodes are not cloned, as these will be |
| 128 | /// rebuilt when the ProgramBuilder builds its Program. |
| 129 | /// The returned ProgramBuilder will contain no pointers to objects owned by |
| 130 | /// this Program, and so after calling, this Program can be safely destructed. |
| 131 | /// @return a new ProgramBuilder copied from this Program |
| 132 | ProgramBuilder CloneAsBuilder() const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 133 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 134 | /// @returns true if the program has no error diagnostics and is not missing |
| 135 | /// information |
| 136 | bool IsValid() const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 137 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 138 | /// Helper for returning the resolved semantic type of the expression `expr`. |
| 139 | /// @param expr the AST expression |
| 140 | /// @return the resolved semantic type for the expression, or nullptr if the |
| 141 | /// expression has no resolved type. |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 142 | const type::Type* TypeOf(const ast::Expression* expr) const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 143 | |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 144 | /// Helper for returning the resolved semantic type of the variable `var`. |
| 145 | /// @param var the AST variable |
| 146 | /// @return the resolved semantic type for the variable, or nullptr if the |
| 147 | /// variable has no resolved type. |
| 148 | const type::Type* TypeOf(const ast::Variable* var) const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 149 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 150 | /// Helper for returning the resolved semantic type of the AST type |
| 151 | /// declaration `type_decl`. |
| 152 | /// @param type_decl the AST type declaration |
| 153 | /// @return the resolved semantic type for the type declaration, or nullptr if |
| 154 | /// the type declaration has no resolved type. |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 155 | const type::Type* TypeOf(const ast::TypeDecl* type_decl) const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 156 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 157 | /// A function that can be used to print a program |
| 158 | using Printer = std::string (*)(const Program*); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 159 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 160 | /// The Program printer used for testing and debugging. |
| 161 | static Printer printer; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 162 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 163 | private: |
| 164 | Program(const Program&) = delete; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 165 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 166 | /// Asserts that the program has not been moved. |
| 167 | void AssertNotMoved() const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 168 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 169 | ProgramID id_; |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 170 | ast::NodeID highest_node_id_; |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 171 | constant::Manager constants_; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 172 | ASTNodeAllocator ast_nodes_; |
| 173 | SemNodeAllocator sem_nodes_; |
| 174 | ast::Module* ast_ = nullptr; |
| 175 | sem::Info sem_; |
| 176 | SymbolTable symbols_{id_}; |
| 177 | diag::List diagnostics_; |
| 178 | bool is_valid_ = false; // Not valid until it is built |
| 179 | bool moved_ = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /// @param program the Program |
| 183 | /// @returns the ProgramID of the Program |
| 184 | inline ProgramID ProgramIDOf(const Program* program) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 185 | return program->ID(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace tint |
| 189 | |
| 190 | #endif // SRC_TINT_PROGRAM_H_ |