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