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 | #include "src/tint/program.h" |
| 16 | |
| 17 | #include <utility> |
| 18 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 19 | #include "src/tint/resolver/resolver.h" |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 20 | #include "src/tint/sem/type_expression.h" |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 21 | #include "src/tint/sem/value_expression.h" |
Ben Clayton | 23946b3 | 2023-03-09 16:50:19 +0000 | [diff] [blame] | 22 | #include "src/tint/switch.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | |
| 24 | namespace tint { |
| 25 | namespace { |
| 26 | |
| 27 | std::string DefaultPrinter(const Program*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 28 | return "<no program printer assigned>"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | Program::Printer Program::printer = DefaultPrinter; |
| 34 | |
| 35 | Program::Program() = default; |
| 36 | |
| 37 | Program::Program(Program&& program) |
| 38 | : id_(std::move(program.id_)), |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 39 | highest_node_id_(std::move(program.highest_node_id_)), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 40 | types_(std::move(program.types_)), |
| 41 | ast_nodes_(std::move(program.ast_nodes_)), |
| 42 | sem_nodes_(std::move(program.sem_nodes_)), |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 43 | constant_nodes_(std::move(program.constant_nodes_)), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 44 | ast_(std::move(program.ast_)), |
| 45 | sem_(std::move(program.sem_)), |
| 46 | symbols_(std::move(program.symbols_)), |
| 47 | diagnostics_(std::move(program.diagnostics_)), |
| 48 | is_valid_(program.is_valid_) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 49 | program.AssertNotMoved(); |
| 50 | program.moved_ = true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Program::Program(ProgramBuilder&& builder) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | id_ = builder.ID(); |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 55 | highest_node_id_ = builder.LastAllocatedNodeID(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 57 | is_valid_ = builder.IsValid(); |
| 58 | if (builder.ResolveOnBuild() && builder.IsValid()) { |
| 59 | resolver::Resolver resolver(&builder); |
| 60 | if (!resolver.Resolve()) { |
| 61 | is_valid_ = false; |
| 62 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 63 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 65 | // The above must be called *before* the calls to std::move() below |
| 66 | types_ = std::move(builder.Types()); |
| 67 | ast_nodes_ = std::move(builder.ASTNodes()); |
| 68 | sem_nodes_ = std::move(builder.SemNodes()); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 69 | constant_nodes_ = std::move(builder.ConstantNodes()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 70 | ast_ = &builder.AST(); // ast::Module is actually a heap allocation. |
| 71 | sem_ = std::move(builder.Sem()); |
| 72 | symbols_ = std::move(builder.Symbols()); |
| 73 | diagnostics_.add(std::move(builder.Diagnostics())); |
| 74 | builder.MarkAsMoved(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 75 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 76 | if (!is_valid_ && !diagnostics_.contains_errors()) { |
| 77 | // If the builder claims to be invalid, then we really should have an error |
| 78 | // message generated. If we find a situation where the program is not valid |
| 79 | // and there are no errors reported, add one here. |
| 80 | diagnostics_.add_error(diag::System::Program, "invalid program generated"); |
| 81 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Program::~Program() = default; |
| 85 | |
| 86 | Program& Program::operator=(Program&& program) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 87 | program.AssertNotMoved(); |
| 88 | program.moved_ = true; |
| 89 | moved_ = false; |
| 90 | id_ = std::move(program.id_); |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 91 | highest_node_id_ = std::move(program.highest_node_id_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 92 | types_ = std::move(program.types_); |
| 93 | ast_nodes_ = std::move(program.ast_nodes_); |
| 94 | sem_nodes_ = std::move(program.sem_nodes_); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 95 | constant_nodes_ = std::move(program.constant_nodes_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | ast_ = std::move(program.ast_); |
| 97 | sem_ = std::move(program.sem_); |
| 98 | symbols_ = std::move(program.symbols_); |
| 99 | diagnostics_ = std::move(program.diagnostics_); |
| 100 | is_valid_ = program.is_valid_; |
| 101 | return *this; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | Program Program::Clone() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 105 | AssertNotMoved(); |
| 106 | return Program(CloneAsBuilder()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | ProgramBuilder Program::CloneAsBuilder() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 110 | AssertNotMoved(); |
| 111 | ProgramBuilder out; |
| 112 | CloneContext(&out, this).Clone(); |
| 113 | return out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool Program::IsValid() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 117 | AssertNotMoved(); |
| 118 | return is_valid_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 119 | } |
| 120 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 121 | const type::Type* Program::TypeOf(const ast::Expression* expr) const { |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 122 | return tint::Switch( |
| 123 | Sem().Get(expr), // |
| 124 | [](const sem::ValueExpression* ty_expr) { return ty_expr->Type(); }, |
| 125 | [](const sem::TypeExpression* ty_expr) { return ty_expr->Type(); }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 128 | const type::Type* Program::TypeOf(const ast::Variable* var) const { |
| 129 | auto* sem = Sem().Get(var); |
| 130 | return sem ? sem->Type() : nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 131 | } |
| 132 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 133 | const type::Type* Program::TypeOf(const ast::TypeDecl* type_decl) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 134 | return Sem().Get(type_decl); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void Program::AssertNotMoved() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 138 | TINT_ASSERT(Program, !moved_); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } // namespace tint |