blob: 0f76194c079dd268a98676e0cf37c0aefb5dd4d4 [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
27ProgramBuilder::ProgramBuilder()
Ben Claytone6995de2021-04-13 23:27:27 +000028 : id_(ProgramID::New()),
29 ast_(ast_nodes_.Create<ast::Module>(id_, Source{})) {}
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030
31ProgramBuilder::ProgramBuilder(ProgramBuilder&& rhs)
Ben Claytone6995de2021-04-13 23:27:27 +000032 : id_(std::move(rhs.id_)),
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();
Ben Claytone6995de2021-04-13 23:27:27 +000047 id_ = std::move(rhs.id_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000048 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;
Ben Claytone6995de2021-04-13 23:27:27 +000059 builder.id_ = program->ID();
Antonio Maiorano3751fd22021-04-19 22:51:23 +000060 builder.types_ = sem::Manager::Wrap(program->Types());
Ben Claytone43c8302021-01-29 11:59:32 +000061 builder.ast_ = builder.create<ast::Module>(
James Price55838532021-02-09 21:39:10 +000062 program->AST().source(), program->AST().GlobalDeclarations());
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000063 builder.sem_ = sem::Info::Wrap(program->Sem());
Ben Claytone43c8302021-01-29 11:59:32 +000064 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 Clayton8454d822021-03-10 11:41:49 +000070 return !diagnostics_.contains_errors();
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 {
Ben Clayton90f43cf2021-03-31 20:43:26 +000083 if (moved_) {
Ben Claytonba6ab5e2021-05-07 14:49:34 +000084 TINT_ICE(const_cast<ProgramBuilder*>(this)->diagnostics_)
Ben Clayton90f43cf2021-03-31 20:43:26 +000085 << "Attempting to use ProgramBuilder after it has been moved";
86 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087}
88
Ben Claytonfbec46f2021-04-30 20:20:19 +000089sem::Type* ProgramBuilder::TypeOf(const ast::Expression* expr) const {
Ben Clayton33352542021-01-29 16:43:41 +000090 auto* sem = Sem().Get(expr);
91 return sem ? sem->Type() : nullptr;
92}
93
Ben Claytonfe0910f2021-05-17 15:51:47 +000094sem::Type* ProgramBuilder::TypeOf(const ast::Variable* var) const {
95 auto* sem = Sem().Get(var);
96 return sem ? sem->Type() : nullptr;
97}
98
Ben Claytonfbec46f2021-04-30 20:20:19 +000099const sem::Type* ProgramBuilder::TypeOf(const ast::Type* type) const {
100 return Sem().Get(type);
101}
102
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000103ast::ConstructorExpression* ProgramBuilder::ConstructValueFilledWith(
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000104 const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000105 int elem_value) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000106 CloneContext ctx(this);
107
108 if (type->Is<ast::Bool>()) {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000109 return create<ast::ScalarConstructorExpression>(
Ben Clayton109b18f2021-04-28 13:50:43 +0000110 create<ast::BoolLiteral>(elem_value == 0 ? false : true));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000111 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000112 if (type->Is<ast::I32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000113 return create<ast::ScalarConstructorExpression>(
114 create<ast::SintLiteral>(static_cast<i32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000115 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000116 if (type->Is<ast::U32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000117 return create<ast::ScalarConstructorExpression>(
118 create<ast::UintLiteral>(static_cast<u32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000119 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000120 if (type->Is<ast::F32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000121 return create<ast::ScalarConstructorExpression>(
122 create<ast::FloatLiteral>(static_cast<f32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000123 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000124 if (auto* v = type->As<ast::Vector>()) {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000125 ast::ExpressionList el(v->size());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000126 for (size_t i = 0; i < el.size(); i++) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000127 el[i] = ConstructValueFilledWith(ctx.Clone(v->type()), elem_value);
Ben Claytona0cf62f2021-04-17 00:42:41 +0000128 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000129 return create<ast::TypeConstructorExpression>(const_cast<ast::Type*>(type),
130 std::move(el));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000131 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000132 if (auto* m = type->As<ast::Matrix>()) {
133 ast::ExpressionList el(m->columns());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000134 for (size_t i = 0; i < el.size(); i++) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000135 auto* col_vec_type = create<ast::Vector>(ctx.Clone(m->type()), m->rows());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000136 el[i] = ConstructValueFilledWith(col_vec_type, elem_value);
137 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000138 return create<ast::TypeConstructorExpression>(const_cast<ast::Type*>(type),
139 std::move(el));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000140 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000141 if (auto* tn = type->As<ast::TypeName>()) {
142 if (auto* lookup = AST().LookupType(tn->name())) {
143 if (auto* alias = lookup->As<ast::Alias>()) {
144 return ConstructValueFilledWith(ctx.Clone(alias->type()), elem_value);
145 }
146 }
147 TINT_ICE(diagnostics_) << "unable to find NamedType '"
148 << Symbols().NameFor(tn->name()) << "'";
149 return nullptr;
150 }
151
152 TINT_ICE(diagnostics_) << "unhandled type: " << type->TypeInfo().name;
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000153 return nullptr;
154}
155
Ben Clayton19b03192021-05-20 15:04:08 +0000156ast::Type* ProgramBuilder::TypesBuilder::MaybeCreateTypename(
157 ast::Type* type) const {
158 if (auto* nt = As<ast::NamedType>(type)) {
159 return type_name(nt->name());
160 }
161 return type;
162}
163
164const ast::Type* ProgramBuilder::TypesBuilder::MaybeCreateTypename(
165 const ast::Type* type) const {
166 if (auto* nt = As<ast::NamedType>(type)) {
167 return type_name(nt->name());
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000168 }
169 return type;
170}
171
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000172ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {}
173
Ben Clayton169512e2021-04-17 05:52:11 +0000174ast::Statement* ProgramBuilder::WrapInStatement(ast::Literal* lit) {
175 return WrapInStatement(create<ast::ScalarConstructorExpression>(lit));
Ben Clayton401b96b2021-02-03 17:19:59 +0000176}
177
178ast::Statement* ProgramBuilder::WrapInStatement(ast::Expression* expr) {
Ben Claytonfe0910f2021-05-17 15:51:47 +0000179 if (auto* ce = expr->As<ast::CallExpression>()) {
180 return create<ast::CallStatement>(ce);
181 }
Antonio Maioranoe09989a2021-03-31 13:26:43 +0000182 // Create a temporary variable of inferred type from expr.
Ben Claytonfe0910f2021-05-17 15:51:47 +0000183 return Decl(Const(symbols_.New(), nullptr, expr));
Ben Clayton401b96b2021-02-03 17:19:59 +0000184}
185
Ben Clayton169512e2021-04-17 05:52:11 +0000186ast::VariableDeclStatement* ProgramBuilder::WrapInStatement(ast::Variable* v) {
187 return create<ast::VariableDeclStatement>(v);
188}
189
Ben Clayton401b96b2021-02-03 17:19:59 +0000190ast::Statement* ProgramBuilder::WrapInStatement(ast::Statement* stmt) {
191 return stmt;
192}
193
Antonio Maiorano03c01b52021-03-19 14:04:51 +0000194ast::Function* ProgramBuilder::WrapInFunction(ast::StatementList stmts) {
Ben Claytonab26a8f2021-04-07 15:54:11 +0000195 return Func("test_function", {}, ty.void_(), std::move(stmts),
196 {create<ast::StageDecoration>(ast::PipelineStage::kCompute)});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000197}
198
199} // namespace tint