Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +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/program_builder.h" |
| 16 | |
| 17 | #include <assert.h> |
| 18 | |
| 19 | #include <sstream> |
| 20 | |
| 21 | #include "src/clone_context.h" |
Ben Clayton | 708dc2d | 2021-01-29 11:22:40 +0000 | [diff] [blame] | 22 | #include "src/demangler.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 23 | #include "src/type/struct_type.h" |
| 24 | |
| 25 | namespace tint { |
| 26 | |
| 27 | ProgramBuilder::ProgramBuilder() |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 28 | : ty(this), ast_(ast_nodes_.Create<ast::Module>(Source{})) {} |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 29 | |
| 30 | ProgramBuilder::ProgramBuilder(ProgramBuilder&& rhs) |
| 31 | : ty(std::move(rhs.ty)), |
| 32 | types_(std::move(rhs.types_)), |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 33 | ast_nodes_(std::move(rhs.ast_nodes_)), |
| 34 | sem_nodes_(std::move(rhs.sem_nodes_)), |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 35 | ast_(rhs.ast_), |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 36 | sem_(std::move(rhs.sem_)), |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 37 | symbols_(std::move(rhs.symbols_)) { |
| 38 | rhs.MarkAsMoved(); |
| 39 | } |
| 40 | |
| 41 | ProgramBuilder::~ProgramBuilder() = default; |
| 42 | |
| 43 | ProgramBuilder& ProgramBuilder::operator=(ProgramBuilder&& rhs) { |
| 44 | rhs.MarkAsMoved(); |
| 45 | AssertNotMoved(); |
| 46 | ty = std::move(rhs.ty); |
| 47 | types_ = std::move(rhs.types_); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 48 | ast_nodes_ = std::move(rhs.ast_nodes_); |
| 49 | sem_nodes_ = std::move(rhs.sem_nodes_); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 50 | ast_ = rhs.ast_; |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 51 | sem_ = std::move(rhs.sem_); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 52 | symbols_ = std::move(rhs.symbols_); |
| 53 | return *this; |
| 54 | } |
| 55 | |
Ben Clayton | e43c830 | 2021-01-29 11:59:32 +0000 | [diff] [blame] | 56 | ProgramBuilder ProgramBuilder::Wrap(const Program* program) { |
| 57 | ProgramBuilder builder; |
| 58 | builder.types_ = type::Manager::Wrap(program->Types()); |
| 59 | builder.ast_ = builder.create<ast::Module>( |
| 60 | program->AST().source(), program->AST().ConstructedTypes(), |
| 61 | program->AST().Functions(), program->AST().GlobalVariables()); |
| 62 | builder.sem_ = semantic::Info::Wrap(program->Sem()); |
| 63 | builder.symbols_ = program->Symbols(); |
| 64 | builder.diagnostics_ = program->Diagnostics(); |
| 65 | return builder; |
| 66 | } |
| 67 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 68 | bool ProgramBuilder::IsValid() const { |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 69 | return !diagnostics_.contains_errors() && ast_->IsValid(); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ben Clayton | 708dc2d | 2021-01-29 11:22:40 +0000 | [diff] [blame] | 72 | std::string ProgramBuilder::str(const ast::Node* node) const { |
| 73 | return Demangler().Demangle(Symbols(), node->str(Sem())); |
| 74 | } |
| 75 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 76 | void ProgramBuilder::MarkAsMoved() { |
| 77 | AssertNotMoved(); |
| 78 | moved_ = true; |
| 79 | } |
| 80 | |
| 81 | void ProgramBuilder::AssertNotMoved() const { |
| 82 | assert(!moved_); |
| 83 | } |
| 84 | |
| 85 | ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {} |
| 86 | |
| 87 | ast::Variable* ProgramBuilder::Var(const std::string& name, |
| 88 | ast::StorageClass storage, |
| 89 | type::Type* type) { |
| 90 | return Var(name, storage, type, nullptr, {}); |
| 91 | } |
| 92 | |
| 93 | ast::Variable* ProgramBuilder::Var(const std::string& name, |
| 94 | ast::StorageClass storage, |
| 95 | type::Type* type, |
| 96 | ast::Expression* constructor, |
| 97 | ast::VariableDecorationList decorations) { |
| 98 | auto* var = create<ast::Variable>(Symbols().Register(name), storage, type, |
| 99 | false, constructor, decorations); |
| 100 | OnVariableBuilt(var); |
| 101 | return var; |
| 102 | } |
| 103 | |
| 104 | ast::Variable* ProgramBuilder::Var(const Source& source, |
| 105 | const std::string& name, |
| 106 | ast::StorageClass storage, |
| 107 | type::Type* type, |
| 108 | ast::Expression* constructor, |
| 109 | ast::VariableDecorationList decorations) { |
| 110 | auto* var = create<ast::Variable>(source, Symbols().Register(name), storage, |
| 111 | type, false, constructor, decorations); |
| 112 | OnVariableBuilt(var); |
| 113 | return var; |
| 114 | } |
| 115 | |
| 116 | ast::Variable* ProgramBuilder::Const(const std::string& name, |
| 117 | ast::StorageClass storage, |
| 118 | type::Type* type) { |
| 119 | return Const(name, storage, type, nullptr, {}); |
| 120 | } |
| 121 | |
| 122 | ast::Variable* ProgramBuilder::Const(const std::string& name, |
| 123 | ast::StorageClass storage, |
| 124 | type::Type* type, |
| 125 | ast::Expression* constructor, |
| 126 | ast::VariableDecorationList decorations) { |
| 127 | auto* var = create<ast::Variable>(Symbols().Register(name), storage, type, |
| 128 | true, constructor, decorations); |
| 129 | OnVariableBuilt(var); |
| 130 | return var; |
| 131 | } |
| 132 | |
| 133 | ast::Variable* ProgramBuilder::Const(const Source& source, |
| 134 | const std::string& name, |
| 135 | ast::StorageClass storage, |
| 136 | type::Type* type, |
| 137 | ast::Expression* constructor, |
| 138 | ast::VariableDecorationList decorations) { |
| 139 | auto* var = create<ast::Variable>(source, Symbols().Register(name), storage, |
| 140 | type, true, constructor, decorations); |
| 141 | OnVariableBuilt(var); |
| 142 | return var; |
| 143 | } |
| 144 | |
| 145 | } // namespace tint |