blob: 5f217432cdd93e6a41af56984657fb1e5b97d9f9 [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_),
Ben Claytondd1b6fc2021-01-29 10:55:40 +000034 sem_(std::move(rhs.sem_)),
Ben Claytona6b9a8e2021-01-26 16:57:10 +000035 symbols_(std::move(rhs.symbols_)) {
36 rhs.MarkAsMoved();
37}
38
39ProgramBuilder::~ProgramBuilder() = default;
40
41ProgramBuilder& 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 Claytondd1b6fc2021-01-29 10:55:40 +000048 sem_ = std::move(rhs.sem_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000049 symbols_ = std::move(rhs.symbols_);
50 return *this;
51}
52
53bool ProgramBuilder::IsValid() const {
Ben Clayton844217f2021-01-27 18:49:05 +000054 return !diagnostics_.contains_errors() && ast_->IsValid();
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055}
56
57void ProgramBuilder::MarkAsMoved() {
58 AssertNotMoved();
59 moved_ = true;
60}
61
62void ProgramBuilder::AssertNotMoved() const {
63 assert(!moved_);
64}
65
66ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {}
67
68ast::Variable* ProgramBuilder::Var(const std::string& name,
69 ast::StorageClass storage,
70 type::Type* type) {
71 return Var(name, storage, type, nullptr, {});
72}
73
74ast::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
85ast::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
97ast::Variable* ProgramBuilder::Const(const std::string& name,
98 ast::StorageClass storage,
99 type::Type* type) {
100 return Const(name, storage, type, nullptr, {});
101}
102
103ast::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
114ast::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