blob: a85ba4dbc7ca667ffe4007961669fc1b756e85a9 [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
Ben Clayton401b96b2021-02-03 17:19:59 +000017#include "src/ast/assignment_statement.h"
Antonio Maioranoe09989a2021-03-31 13:26:43 +000018#include "src/ast/call_statement.h"
Ben Clayton401b96b2021-02-03 17:19:59 +000019#include "src/ast/variable_decl_statement.h"
Ben Clayton90f43cf2021-03-31 20:43:26 +000020#include "src/debug.h"
Ben Clayton708dc2d2021-01-29 11:22:40 +000021#include "src/demangler.h"
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000022#include "src/sem/expression.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000023#include "src/sem/variable.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000024
25namespace tint {
26
Ben Clayton620d77e2021-06-04 19:55:08 +000027ProgramBuilder::VarOptionals::~VarOptionals() = default;
28
Ben Claytona6b9a8e2021-01-26 16:57:10 +000029ProgramBuilder::ProgramBuilder()
Ben Claytone6995de2021-04-13 23:27:27 +000030 : id_(ProgramID::New()),
31 ast_(ast_nodes_.Create<ast::Module>(id_, Source{})) {}
Ben Claytona6b9a8e2021-01-26 16:57:10 +000032
33ProgramBuilder::ProgramBuilder(ProgramBuilder&& rhs)
Ben Claytone6995de2021-04-13 23:27:27 +000034 : id_(std::move(rhs.id_)),
35 types_(std::move(rhs.types_)),
Ben Clayton7fdfff12021-01-29 15:17:30 +000036 ast_nodes_(std::move(rhs.ast_nodes_)),
37 sem_nodes_(std::move(rhs.sem_nodes_)),
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038 ast_(rhs.ast_),
Ben Claytondd1b6fc2021-01-29 10:55:40 +000039 sem_(std::move(rhs.sem_)),
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040 symbols_(std::move(rhs.symbols_)) {
41 rhs.MarkAsMoved();
42}
43
44ProgramBuilder::~ProgramBuilder() = default;
45
46ProgramBuilder& ProgramBuilder::operator=(ProgramBuilder&& rhs) {
47 rhs.MarkAsMoved();
48 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +000049 id_ = std::move(rhs.id_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050 types_ = std::move(rhs.types_);
Ben Clayton7fdfff12021-01-29 15:17:30 +000051 ast_nodes_ = std::move(rhs.ast_nodes_);
52 sem_nodes_ = std::move(rhs.sem_nodes_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053 ast_ = rhs.ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +000054 sem_ = std::move(rhs.sem_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055 symbols_ = std::move(rhs.symbols_);
56 return *this;
57}
58
Ben Claytone43c8302021-01-29 11:59:32 +000059ProgramBuilder ProgramBuilder::Wrap(const Program* program) {
60 ProgramBuilder builder;
Ben Claytone6995de2021-04-13 23:27:27 +000061 builder.id_ = program->ID();
Antonio Maiorano3751fd22021-04-19 22:51:23 +000062 builder.types_ = sem::Manager::Wrap(program->Types());
Ben Claytone43c8302021-01-29 11:59:32 +000063 builder.ast_ = builder.create<ast::Module>(
James Price55838532021-02-09 21:39:10 +000064 program->AST().source(), program->AST().GlobalDeclarations());
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000065 builder.sem_ = sem::Info::Wrap(program->Sem());
Ben Claytone43c8302021-01-29 11:59:32 +000066 builder.symbols_ = program->Symbols();
67 builder.diagnostics_ = program->Diagnostics();
68 return builder;
69}
70
Ben Claytona6b9a8e2021-01-26 16:57:10 +000071bool ProgramBuilder::IsValid() const {
Ben Clayton8454d822021-03-10 11:41:49 +000072 return !diagnostics_.contains_errors();
Ben Claytona6b9a8e2021-01-26 16:57:10 +000073}
74
Ben Clayton708dc2d2021-01-29 11:22:40 +000075std::string ProgramBuilder::str(const ast::Node* node) const {
76 return Demangler().Demangle(Symbols(), node->str(Sem()));
77}
78
Ben Claytona6b9a8e2021-01-26 16:57:10 +000079void ProgramBuilder::MarkAsMoved() {
80 AssertNotMoved();
81 moved_ = true;
82}
83
84void ProgramBuilder::AssertNotMoved() const {
Ben Clayton90f43cf2021-03-31 20:43:26 +000085 if (moved_) {
Ben Claytonba6ab5e2021-05-07 14:49:34 +000086 TINT_ICE(const_cast<ProgramBuilder*>(this)->diagnostics_)
Ben Clayton90f43cf2021-03-31 20:43:26 +000087 << "Attempting to use ProgramBuilder after it has been moved";
88 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +000089}
90
Ben Claytonfbec46f2021-04-30 20:20:19 +000091sem::Type* ProgramBuilder::TypeOf(const ast::Expression* expr) const {
Ben Clayton33352542021-01-29 16:43:41 +000092 auto* sem = Sem().Get(expr);
93 return sem ? sem->Type() : nullptr;
94}
95
Ben Claytonfe0910f2021-05-17 15:51:47 +000096sem::Type* ProgramBuilder::TypeOf(const ast::Variable* var) const {
97 auto* sem = Sem().Get(var);
98 return sem ? sem->Type() : nullptr;
99}
100
Ben Claytonfbec46f2021-04-30 20:20:19 +0000101const sem::Type* ProgramBuilder::TypeOf(const ast::Type* type) const {
102 return Sem().Get(type);
103}
104
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000105ast::ConstructorExpression* ProgramBuilder::ConstructValueFilledWith(
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000106 const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000107 int elem_value) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000108 CloneContext ctx(this);
109
110 if (type->Is<ast::Bool>()) {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000111 return create<ast::ScalarConstructorExpression>(
Ben Clayton109b18f2021-04-28 13:50:43 +0000112 create<ast::BoolLiteral>(elem_value == 0 ? false : true));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000113 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000114 if (type->Is<ast::I32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000115 return create<ast::ScalarConstructorExpression>(
116 create<ast::SintLiteral>(static_cast<i32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000117 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000118 if (type->Is<ast::U32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000119 return create<ast::ScalarConstructorExpression>(
120 create<ast::UintLiteral>(static_cast<u32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000121 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000122 if (type->Is<ast::F32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000123 return create<ast::ScalarConstructorExpression>(
124 create<ast::FloatLiteral>(static_cast<f32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000125 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000126 if (auto* v = type->As<ast::Vector>()) {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000127 ast::ExpressionList el(v->size());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000128 for (size_t i = 0; i < el.size(); i++) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000129 el[i] = ConstructValueFilledWith(ctx.Clone(v->type()), elem_value);
Ben Claytona0cf62f2021-04-17 00:42:41 +0000130 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000131 return create<ast::TypeConstructorExpression>(const_cast<ast::Type*>(type),
132 std::move(el));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000133 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000134 if (auto* m = type->As<ast::Matrix>()) {
135 ast::ExpressionList el(m->columns());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000136 for (size_t i = 0; i < el.size(); i++) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000137 auto* col_vec_type = create<ast::Vector>(ctx.Clone(m->type()), m->rows());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000138 el[i] = ConstructValueFilledWith(col_vec_type, elem_value);
139 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000140 return create<ast::TypeConstructorExpression>(const_cast<ast::Type*>(type),
141 std::move(el));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000142 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000143 if (auto* tn = type->As<ast::TypeName>()) {
144 if (auto* lookup = AST().LookupType(tn->name())) {
145 if (auto* alias = lookup->As<ast::Alias>()) {
146 return ConstructValueFilledWith(ctx.Clone(alias->type()), elem_value);
147 }
148 }
149 TINT_ICE(diagnostics_) << "unable to find NamedType '"
150 << Symbols().NameFor(tn->name()) << "'";
151 return nullptr;
152 }
153
154 TINT_ICE(diagnostics_) << "unhandled type: " << type->TypeInfo().name;
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000155 return nullptr;
156}
157
Ben Clayton19b03192021-05-20 15:04:08 +0000158ast::Type* ProgramBuilder::TypesBuilder::MaybeCreateTypename(
159 ast::Type* type) const {
160 if (auto* nt = As<ast::NamedType>(type)) {
161 return type_name(nt->name());
162 }
163 return type;
164}
165
166const ast::Type* ProgramBuilder::TypesBuilder::MaybeCreateTypename(
167 const ast::Type* type) const {
168 if (auto* nt = As<ast::NamedType>(type)) {
169 return type_name(nt->name());
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000170 }
171 return type;
172}
173
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000174ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {}
175
Ben Clayton169512e2021-04-17 05:52:11 +0000176ast::Statement* ProgramBuilder::WrapInStatement(ast::Literal* lit) {
177 return WrapInStatement(create<ast::ScalarConstructorExpression>(lit));
Ben Clayton401b96b2021-02-03 17:19:59 +0000178}
179
180ast::Statement* ProgramBuilder::WrapInStatement(ast::Expression* expr) {
Ben Claytonfe0910f2021-05-17 15:51:47 +0000181 if (auto* ce = expr->As<ast::CallExpression>()) {
182 return create<ast::CallStatement>(ce);
183 }
Antonio Maioranoe09989a2021-03-31 13:26:43 +0000184 // Create a temporary variable of inferred type from expr.
Ben Claytonfe0910f2021-05-17 15:51:47 +0000185 return Decl(Const(symbols_.New(), nullptr, expr));
Ben Clayton401b96b2021-02-03 17:19:59 +0000186}
187
Ben Clayton169512e2021-04-17 05:52:11 +0000188ast::VariableDeclStatement* ProgramBuilder::WrapInStatement(ast::Variable* v) {
189 return create<ast::VariableDeclStatement>(v);
190}
191
Ben Clayton401b96b2021-02-03 17:19:59 +0000192ast::Statement* ProgramBuilder::WrapInStatement(ast::Statement* stmt) {
193 return stmt;
194}
195
Antonio Maiorano03c01b52021-03-19 14:04:51 +0000196ast::Function* ProgramBuilder::WrapInFunction(ast::StatementList stmts) {
Ben Claytonab26a8f2021-04-07 15:54:11 +0000197 return Func("test_function", {}, ty.void_(), std::move(stmts),
198 {create<ast::StageDecoration>(ast::PipelineStage::kCompute)});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000199}
200
201} // namespace tint