blob: 24999d2f1df53784108f82100ba4e2f6d2d6fff1 [file] [log] [blame]
Ben Claytona07396a2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
Dan Sinclair6e581892020-03-02 15:47:43 -05002//
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/ast/module.h"
16
Ben Claytona07396a2021-01-26 16:57:10 +000017#include <utility>
18
Ben Clayton8758f102021-06-09 14:32:14 +000019#include "src/ast/type_decl.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000020#include "src/program_builder.h"
Ben Claytona07396a2021-01-26 16:57:10 +000021
Ben Clayton7732ca52021-03-02 20:51:18 +000022TINT_INSTANTIATE_TYPEINFO(tint::ast::Module);
Ben Claytona07396a2021-01-26 16:57:10 +000023
Dan Sinclair6e581892020-03-02 15:47:43 -050024namespace tint {
25namespace ast {
26
Ben Clayton4f3ff572021-10-15 17:33:10 +000027Module::Module(ProgramID pid, const Source& src) : Base(pid, src) {}
Ben Claytona07396a2021-01-26 16:57:10 +000028
Ben Clayton4f3ff572021-10-15 17:33:10 +000029Module::Module(ProgramID pid,
30 const Source& src,
Ben Clayton86481202021-10-19 18:38:54 +000031 std::vector<const ast::Node*> global_decls)
Ben Clayton4f3ff572021-10-15 17:33:10 +000032 : Base(pid, src), global_declarations_(std::move(global_decls)) {
James Price55838532021-02-09 21:39:10 +000033 for (auto* decl : global_declarations_) {
34 if (decl == nullptr) {
35 continue;
36 }
37
Ben Clayton8758f102021-06-09 14:32:14 +000038 if (auto* ty = decl->As<ast::TypeDecl>()) {
Ben Clayton950809f2021-06-09 14:32:14 +000039 type_decls_.push_back(ty);
James Price55838532021-02-09 21:39:10 +000040 } else if (auto* func = decl->As<Function>()) {
41 functions_.push_back(func);
42 } else if (auto* var = decl->As<Variable>()) {
43 global_variables_.push_back(var);
44 } else {
Ben Clayton6b4924f2021-02-17 20:13:34 +000045 diag::List diagnostics;
Ben Claytonffd28e22021-06-24 11:27:36 +000046 TINT_ICE(AST, diagnostics) << "Unknown global declaration type";
James Price55838532021-02-09 21:39:10 +000047 }
48 }
49}
Ben Claytona07396a2021-01-26 16:57:10 +000050
51Module::~Module() = default;
52
Ben Clayton8758f102021-06-09 14:32:14 +000053const ast::TypeDecl* Module::LookupType(Symbol name) const {
Ben Clayton950809f2021-06-09 14:32:14 +000054 for (auto* ty : TypeDecls()) {
Ben Clayton4f3ff572021-10-15 17:33:10 +000055 if (ty->name == name) {
Ben Clayton02ebf0d2021-05-05 09:09:41 +000056 return ty;
Ben Clayton49a545c2021-05-04 18:03:11 +000057 }
58 }
59 return nullptr;
60}
61
Ben Clayton86481202021-10-19 18:38:54 +000062void Module::AddGlobalVariable(const ast::Variable* var) {
Ben Claytonffd28e22021-06-24 11:27:36 +000063 TINT_ASSERT(AST, var);
Ben Clayton4f3ff572021-10-15 17:33:10 +000064 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, var, program_id);
Ben Clayton02ebf0d2021-05-05 09:09:41 +000065 global_variables_.push_back(var);
66 global_declarations_.push_back(var);
67}
68
Ben Clayton86481202021-10-19 18:38:54 +000069void Module::AddTypeDecl(const ast::TypeDecl* type) {
Ben Claytonffd28e22021-06-24 11:27:36 +000070 TINT_ASSERT(AST, type);
Ben Clayton4f3ff572021-10-15 17:33:10 +000071 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id);
Ben Clayton950809f2021-06-09 14:32:14 +000072 type_decls_.push_back(type);
Ben Clayton02ebf0d2021-05-05 09:09:41 +000073 global_declarations_.push_back(type);
74}
75
Ben Clayton86481202021-10-19 18:38:54 +000076void Module::AddFunction(const ast::Function* func) {
Ben Claytonffd28e22021-06-24 11:27:36 +000077 TINT_ASSERT(AST, func);
Ben Clayton4f3ff572021-10-15 17:33:10 +000078 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, func, program_id);
Ben Clayton02ebf0d2021-05-05 09:09:41 +000079 functions_.push_back(func);
80 global_declarations_.push_back(func);
81}
82
Ben Clayton86481202021-10-19 18:38:54 +000083const Module* Module::Clone(CloneContext* ctx) const {
Ben Clayton7b6bcb62021-02-11 20:23:03 +000084 auto* out = ctx->dst->create<Module>();
85 out->Copy(ctx, this);
86 return out;
87}
88
89void Module::Copy(CloneContext* ctx, const Module* src) {
Ben Claytoncfed1cb2021-06-16 09:19:36 +000090 ctx->Clone(global_declarations_, src->global_declarations_);
Ben Clayton9efc4fc2021-06-18 21:15:25 +000091
92 // During the clone, declarations may have been placed into the module.
93 // Clear everything out, as we're about to re-bin the declarations.
94 type_decls_.clear();
95 functions_.clear();
96 global_variables_.clear();
97
Ben Claytoncfed1cb2021-06-16 09:19:36 +000098 for (auto* decl : global_declarations_) {
Ben Clayton90f43cf2021-03-31 20:43:26 +000099 if (!decl) {
Ben Claytonffd28e22021-06-24 11:27:36 +0000100 TINT_ICE(AST, ctx->dst->Diagnostics())
101 << "src global declaration was nullptr";
Ben Clayton90f43cf2021-03-31 20:43:26 +0000102 continue;
103 }
Ben Claytoncfed1cb2021-06-16 09:19:36 +0000104 if (auto* type = decl->As<ast::TypeDecl>()) {
Ben Clayton4f3ff572021-10-15 17:33:10 +0000105 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id);
Ben Claytoncfed1cb2021-06-16 09:19:36 +0000106 type_decls_.push_back(type);
James Price55838532021-02-09 21:39:10 +0000107 } else if (auto* func = decl->As<Function>()) {
Ben Clayton4f3ff572021-10-15 17:33:10 +0000108 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, func, program_id);
Ben Claytoncfed1cb2021-06-16 09:19:36 +0000109 functions_.push_back(func);
James Price55838532021-02-09 21:39:10 +0000110 } else if (auto* var = decl->As<Variable>()) {
Ben Clayton4f3ff572021-10-15 17:33:10 +0000111 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, var, program_id);
Ben Claytoncfed1cb2021-06-16 09:19:36 +0000112 global_variables_.push_back(var);
James Price55838532021-02-09 21:39:10 +0000113 } else {
Ben Claytonffd28e22021-06-24 11:27:36 +0000114 TINT_ICE(AST, ctx->dst->Diagnostics())
115 << "Unknown global declaration type";
James Price55838532021-02-09 21:39:10 +0000116 }
117 }
Ben Claytona07396a2021-01-26 16:57:10 +0000118}
119
Dan Sinclair6e581892020-03-02 15:47:43 -0500120} // namespace ast
121} // namespace tint