blob: d4d08fe5ca8c0d47775241b1883d83bc88a739d1 [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()
28 : ty(this), ast_(nodes_.Create<ast::Module>(Source{})) {}
29
30ProgramBuilder::ProgramBuilder(ProgramBuilder&& rhs)
31 : ty(std::move(rhs.ty)),
32 types_(std::move(rhs.types_)),
33 nodes_(std::move(rhs.nodes_)),
34 ast_(rhs.ast_),
Ben Claytondd1b6fc2021-01-29 10:55:40 +000035 sem_(std::move(rhs.sem_)),
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036 symbols_(std::move(rhs.symbols_)) {
37 rhs.MarkAsMoved();
38}
39
40ProgramBuilder::~ProgramBuilder() = default;
41
42ProgramBuilder& ProgramBuilder::operator=(ProgramBuilder&& rhs) {
43 rhs.MarkAsMoved();
44 AssertNotMoved();
45 ty = std::move(rhs.ty);
46 types_ = std::move(rhs.types_);
47 nodes_ = std::move(rhs.nodes_);
48 ast_ = rhs.ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +000049 sem_ = std::move(rhs.sem_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050 symbols_ = std::move(rhs.symbols_);
51 return *this;
52}
53
54bool ProgramBuilder::IsValid() const {
Ben Clayton844217f2021-01-27 18:49:05 +000055 return !diagnostics_.contains_errors() && ast_->IsValid();
Ben Claytona6b9a8e2021-01-26 16:57:10 +000056}
57
Ben Clayton708dc2d2021-01-29 11:22:40 +000058std::string ProgramBuilder::str(const ast::Node* node) const {
59 return Demangler().Demangle(Symbols(), node->str(Sem()));
60}
61
Ben Claytona6b9a8e2021-01-26 16:57:10 +000062void ProgramBuilder::MarkAsMoved() {
63 AssertNotMoved();
64 moved_ = true;
65}
66
67void ProgramBuilder::AssertNotMoved() const {
68 assert(!moved_);
69}
70
71ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {}
72
73ast::Variable* ProgramBuilder::Var(const std::string& name,
74 ast::StorageClass storage,
75 type::Type* type) {
76 return Var(name, storage, type, nullptr, {});
77}
78
79ast::Variable* ProgramBuilder::Var(const std::string& name,
80 ast::StorageClass storage,
81 type::Type* type,
82 ast::Expression* constructor,
83 ast::VariableDecorationList decorations) {
84 auto* var = create<ast::Variable>(Symbols().Register(name), storage, type,
85 false, constructor, decorations);
86 OnVariableBuilt(var);
87 return var;
88}
89
90ast::Variable* ProgramBuilder::Var(const Source& source,
91 const std::string& name,
92 ast::StorageClass storage,
93 type::Type* type,
94 ast::Expression* constructor,
95 ast::VariableDecorationList decorations) {
96 auto* var = create<ast::Variable>(source, Symbols().Register(name), storage,
97 type, false, constructor, decorations);
98 OnVariableBuilt(var);
99 return var;
100}
101
102ast::Variable* ProgramBuilder::Const(const std::string& name,
103 ast::StorageClass storage,
104 type::Type* type) {
105 return Const(name, storage, type, nullptr, {});
106}
107
108ast::Variable* ProgramBuilder::Const(const std::string& name,
109 ast::StorageClass storage,
110 type::Type* type,
111 ast::Expression* constructor,
112 ast::VariableDecorationList decorations) {
113 auto* var = create<ast::Variable>(Symbols().Register(name), storage, type,
114 true, constructor, decorations);
115 OnVariableBuilt(var);
116 return var;
117}
118
119ast::Variable* ProgramBuilder::Const(const Source& source,
120 const std::string& name,
121 ast::StorageClass storage,
122 type::Type* type,
123 ast::Expression* constructor,
124 ast::VariableDecorationList decorations) {
125 auto* var = create<ast::Variable>(source, Symbols().Register(name), storage,
126 type, true, constructor, decorations);
127 OnVariableBuilt(var);
128 return var;
129}
130
131} // namespace tint