blob: a59b03a0b9ed94ba67949bf867497c0063728c69 [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 Claytona6b9a8e2021-01-26 16:57:10 +000023
24namespace tint {
25
26ProgramBuilder::ProgramBuilder()
Ben Claytone6995de2021-04-13 23:27:27 +000027 : id_(ProgramID::New()),
28 ast_(ast_nodes_.Create<ast::Module>(id_, Source{})) {}
Ben Claytona6b9a8e2021-01-26 16:57:10 +000029
30ProgramBuilder::ProgramBuilder(ProgramBuilder&& rhs)
Ben Claytone6995de2021-04-13 23:27:27 +000031 : id_(std::move(rhs.id_)),
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();
Ben Claytone6995de2021-04-13 23:27:27 +000046 id_ = std::move(rhs.id_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047 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;
Ben Claytone6995de2021-04-13 23:27:27 +000058 builder.id_ = program->ID();
Antonio Maiorano3751fd22021-04-19 22:51:23 +000059 builder.types_ = sem::Manager::Wrap(program->Types());
Ben Claytone43c8302021-01-29 11:59:32 +000060 builder.ast_ = builder.create<ast::Module>(
James Price55838532021-02-09 21:39:10 +000061 program->AST().source(), program->AST().GlobalDeclarations());
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000062 builder.sem_ = sem::Info::Wrap(program->Sem());
Ben Claytone43c8302021-01-29 11:59:32 +000063 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 Clayton8454d822021-03-10 11:41:49 +000069 return !diagnostics_.contains_errors();
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 {
Ben Clayton90f43cf2021-03-31 20:43:26 +000082 if (moved_) {
Ben Claytonba6ab5e2021-05-07 14:49:34 +000083 TINT_ICE(const_cast<ProgramBuilder*>(this)->diagnostics_)
Ben Clayton90f43cf2021-03-31 20:43:26 +000084 << "Attempting to use ProgramBuilder after it has been moved";
85 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +000086}
87
Ben Claytonfbec46f2021-04-30 20:20:19 +000088sem::Type* ProgramBuilder::TypeOf(const ast::Expression* expr) const {
Ben Clayton33352542021-01-29 16:43:41 +000089 auto* sem = Sem().Get(expr);
90 return sem ? sem->Type() : nullptr;
91}
92
Ben Claytonfbec46f2021-04-30 20:20:19 +000093const sem::Type* ProgramBuilder::TypeOf(const ast::Type* type) const {
94 return Sem().Get(type);
95}
96
Antonio Maiorano39a65a12021-03-31 12:46:52 +000097ast::ConstructorExpression* ProgramBuilder::ConstructValueFilledWith(
Ben Clayton02ebf0d2021-05-05 09:09:41 +000098 const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +000099 int elem_value) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000100 CloneContext ctx(this);
101
102 if (type->Is<ast::Bool>()) {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000103 return create<ast::ScalarConstructorExpression>(
Ben Clayton109b18f2021-04-28 13:50:43 +0000104 create<ast::BoolLiteral>(elem_value == 0 ? false : true));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000105 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000106 if (type->Is<ast::I32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000107 return create<ast::ScalarConstructorExpression>(
108 create<ast::SintLiteral>(static_cast<i32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000109 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000110 if (type->Is<ast::U32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000111 return create<ast::ScalarConstructorExpression>(
112 create<ast::UintLiteral>(static_cast<u32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000113 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000114 if (type->Is<ast::F32>()) {
Ben Clayton109b18f2021-04-28 13:50:43 +0000115 return create<ast::ScalarConstructorExpression>(
116 create<ast::FloatLiteral>(static_cast<f32>(elem_value)));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000117 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000118 if (auto* v = type->As<ast::Vector>()) {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000119 ast::ExpressionList el(v->size());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000120 for (size_t i = 0; i < el.size(); i++) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000121 el[i] = ConstructValueFilledWith(ctx.Clone(v->type()), elem_value);
Ben Claytona0cf62f2021-04-17 00:42:41 +0000122 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000123 return create<ast::TypeConstructorExpression>(const_cast<ast::Type*>(type),
124 std::move(el));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000125 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000126 if (auto* m = type->As<ast::Matrix>()) {
127 ast::ExpressionList el(m->columns());
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 auto* col_vec_type = create<ast::Vector>(ctx.Clone(m->type()), m->rows());
Ben Claytona0cf62f2021-04-17 00:42:41 +0000130 el[i] = ConstructValueFilledWith(col_vec_type, elem_value);
131 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000132 return create<ast::TypeConstructorExpression>(const_cast<ast::Type*>(type),
133 std::move(el));
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000134 }
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000135 if (auto* tn = type->As<ast::TypeName>()) {
136 if (auto* lookup = AST().LookupType(tn->name())) {
137 if (auto* alias = lookup->As<ast::Alias>()) {
138 return ConstructValueFilledWith(ctx.Clone(alias->type()), elem_value);
139 }
140 }
141 TINT_ICE(diagnostics_) << "unable to find NamedType '"
142 << Symbols().NameFor(tn->name()) << "'";
143 return nullptr;
144 }
145
146 TINT_ICE(diagnostics_) << "unhandled type: " << type->TypeInfo().name;
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000147 return nullptr;
148}
149
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000150typ::Type ProgramBuilder::TypesBuilder::MaybeCreateTypename(
151 typ::Type type) const {
Ben Clayton02ebf0d2021-05-05 09:09:41 +0000152 if (auto* nt = As<ast::NamedType>(type.ast)) {
153 return {type_name(nt->name()), type.sem};
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000154 }
155 return type;
156}
157
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000158ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {}
159
Ben Clayton169512e2021-04-17 05:52:11 +0000160ast::Statement* ProgramBuilder::WrapInStatement(ast::Literal* lit) {
161 return WrapInStatement(create<ast::ScalarConstructorExpression>(lit));
Ben Clayton401b96b2021-02-03 17:19:59 +0000162}
163
164ast::Statement* ProgramBuilder::WrapInStatement(ast::Expression* expr) {
Antonio Maioranoe09989a2021-03-31 13:26:43 +0000165 // Create a temporary variable of inferred type from expr.
166 return Decl(Var(symbols_.New(), nullptr, ast::StorageClass::kFunction, expr));
Ben Clayton401b96b2021-02-03 17:19:59 +0000167}
168
Ben Clayton169512e2021-04-17 05:52:11 +0000169ast::VariableDeclStatement* ProgramBuilder::WrapInStatement(ast::Variable* v) {
170 return create<ast::VariableDeclStatement>(v);
171}
172
Ben Clayton401b96b2021-02-03 17:19:59 +0000173ast::Statement* ProgramBuilder::WrapInStatement(ast::Statement* stmt) {
174 return stmt;
175}
176
Antonio Maiorano03c01b52021-03-19 14:04:51 +0000177ast::Function* ProgramBuilder::WrapInFunction(ast::StatementList stmts) {
Ben Claytonab26a8f2021-04-07 15:54:11 +0000178 return Func("test_function", {}, ty.void_(), std::move(stmts),
179 {create<ast::StageDecoration>(ast::PipelineStage::kCompute)});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000180}
181
182} // namespace tint