blob: 8286958daea6e3b3b78e1539a0988d48d960ea58 [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"
Ben Clayton708dc2d2021-01-29 11:22:40 +000022#include "src/demangler.h"
Ben Clayton33352542021-01-29 16:43:41 +000023#include "src/semantic/expression.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000024#include "src/type/struct_type.h"
25
26namespace tint {
27
28ProgramBuilder::ProgramBuilder()
Ben Clayton7fdfff12021-01-29 15:17:30 +000029 : ty(this), ast_(ast_nodes_.Create<ast::Module>(Source{})) {}
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030
31ProgramBuilder::ProgramBuilder(ProgramBuilder&& rhs)
32 : ty(std::move(rhs.ty)),
33 types_(std::move(rhs.types_)),
Ben Clayton7fdfff12021-01-29 15:17:30 +000034 ast_nodes_(std::move(rhs.ast_nodes_)),
35 sem_nodes_(std::move(rhs.sem_nodes_)),
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036 ast_(rhs.ast_),
Ben Claytondd1b6fc2021-01-29 10:55:40 +000037 sem_(std::move(rhs.sem_)),
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038 symbols_(std::move(rhs.symbols_)) {
39 rhs.MarkAsMoved();
40}
41
42ProgramBuilder::~ProgramBuilder() = default;
43
44ProgramBuilder& ProgramBuilder::operator=(ProgramBuilder&& rhs) {
45 rhs.MarkAsMoved();
46 AssertNotMoved();
47 ty = std::move(rhs.ty);
48 types_ = std::move(rhs.types_);
Ben Clayton7fdfff12021-01-29 15:17:30 +000049 ast_nodes_ = std::move(rhs.ast_nodes_);
50 sem_nodes_ = std::move(rhs.sem_nodes_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051 ast_ = rhs.ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +000052 sem_ = std::move(rhs.sem_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053 symbols_ = std::move(rhs.symbols_);
54 return *this;
55}
56
Ben Claytone43c8302021-01-29 11:59:32 +000057ProgramBuilder ProgramBuilder::Wrap(const Program* program) {
58 ProgramBuilder builder;
59 builder.types_ = type::Manager::Wrap(program->Types());
60 builder.ast_ = builder.create<ast::Module>(
61 program->AST().source(), program->AST().ConstructedTypes(),
62 program->AST().Functions(), program->AST().GlobalVariables());
63 builder.sem_ = semantic::Info::Wrap(program->Sem());
64 builder.symbols_ = program->Symbols();
65 builder.diagnostics_ = program->Diagnostics();
66 return builder;
67}
68
Ben Claytona6b9a8e2021-01-26 16:57:10 +000069bool ProgramBuilder::IsValid() const {
Ben Clayton844217f2021-01-27 18:49:05 +000070 return !diagnostics_.contains_errors() && ast_->IsValid();
Ben Claytona6b9a8e2021-01-26 16:57:10 +000071}
72
Ben Clayton708dc2d2021-01-29 11:22:40 +000073std::string ProgramBuilder::str(const ast::Node* node) const {
74 return Demangler().Demangle(Symbols(), node->str(Sem()));
75}
76
Ben Claytona6b9a8e2021-01-26 16:57:10 +000077void ProgramBuilder::MarkAsMoved() {
78 AssertNotMoved();
79 moved_ = true;
80}
81
82void ProgramBuilder::AssertNotMoved() const {
83 assert(!moved_);
84}
85
Ben Clayton33352542021-01-29 16:43:41 +000086type::Type* ProgramBuilder::TypeOf(ast::Expression* expr) const {
87 auto* sem = Sem().Get(expr);
88 return sem ? sem->Type() : nullptr;
89}
90
Ben Claytona6b9a8e2021-01-26 16:57:10 +000091ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {}
92
93ast::Variable* ProgramBuilder::Var(const std::string& name,
94 ast::StorageClass storage,
95 type::Type* type) {
96 return Var(name, storage, type, nullptr, {});
97}
98
99ast::Variable* ProgramBuilder::Var(const std::string& name,
100 ast::StorageClass storage,
101 type::Type* type,
102 ast::Expression* constructor,
103 ast::VariableDecorationList decorations) {
104 auto* var = create<ast::Variable>(Symbols().Register(name), storage, type,
105 false, constructor, decorations);
106 OnVariableBuilt(var);
107 return var;
108}
109
110ast::Variable* ProgramBuilder::Var(const Source& source,
111 const std::string& name,
112 ast::StorageClass storage,
113 type::Type* type,
114 ast::Expression* constructor,
115 ast::VariableDecorationList decorations) {
116 auto* var = create<ast::Variable>(source, Symbols().Register(name), storage,
117 type, false, constructor, decorations);
118 OnVariableBuilt(var);
119 return var;
120}
121
122ast::Variable* ProgramBuilder::Const(const std::string& name,
123 ast::StorageClass storage,
124 type::Type* type) {
125 return Const(name, storage, type, nullptr, {});
126}
127
128ast::Variable* ProgramBuilder::Const(const std::string& name,
129 ast::StorageClass storage,
130 type::Type* type,
131 ast::Expression* constructor,
132 ast::VariableDecorationList decorations) {
133 auto* var = create<ast::Variable>(Symbols().Register(name), storage, type,
134 true, constructor, decorations);
135 OnVariableBuilt(var);
136 return var;
137}
138
139ast::Variable* ProgramBuilder::Const(const Source& source,
140 const std::string& name,
141 ast::StorageClass storage,
142 type::Type* type,
143 ast::Expression* constructor,
144 ast::VariableDecorationList decorations) {
145 auto* var = create<ast::Variable>(source, Symbols().Register(name), storage,
146 type, true, constructor, decorations);
147 OnVariableBuilt(var);
148 return var;
149}
150
151} // namespace tint