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