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