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