Ben Clayton | a07396a | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 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/ast/module.h" |
| 16 | |
Ben Clayton | a07396a | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 19 | #include "src/ast/type_decl.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 20 | #include "src/program_builder.h" |
Ben Clayton | a07396a | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 21 | |
Ben Clayton | 7732ca5 | 2021-03-02 20:51:18 +0000 | [diff] [blame] | 22 | TINT_INSTANTIATE_TYPEINFO(tint::ast::Module); |
Ben Clayton | a07396a | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 23 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 24 | namespace tint { |
| 25 | namespace ast { |
| 26 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 27 | Module::Module(ProgramID pid, const Source& src) : Base(pid, src) {} |
Ben Clayton | a07396a | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 28 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 29 | Module::Module(ProgramID pid, |
| 30 | const Source& src, |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 31 | std::vector<const ast::Node*> global_decls) |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 32 | : Base(pid, src), global_declarations_(std::move(global_decls)) { |
James Price | 5583853 | 2021-02-09 21:39:10 +0000 | [diff] [blame] | 33 | for (auto* decl : global_declarations_) { |
| 34 | if (decl == nullptr) { |
| 35 | continue; |
| 36 | } |
| 37 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 38 | if (auto* ty = decl->As<ast::TypeDecl>()) { |
Ben Clayton | 950809f | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 39 | type_decls_.push_back(ty); |
James Price | 5583853 | 2021-02-09 21:39:10 +0000 | [diff] [blame] | 40 | } 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 Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 45 | diag::List diagnostics; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 46 | TINT_ICE(AST, diagnostics) << "Unknown global declaration type"; |
James Price | 5583853 | 2021-02-09 21:39:10 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
Ben Clayton | a07396a | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 50 | |
| 51 | Module::~Module() = default; |
| 52 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 53 | const ast::TypeDecl* Module::LookupType(Symbol name) const { |
Ben Clayton | 950809f | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 54 | for (auto* ty : TypeDecls()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 55 | if (ty->name == name) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 56 | return ty; |
Ben Clayton | 49a545c | 2021-05-04 18:03:11 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 62 | void Module::AddGlobalVariable(const ast::Variable* var) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 63 | TINT_ASSERT(AST, var); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 64 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, var, program_id); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 65 | global_variables_.push_back(var); |
| 66 | global_declarations_.push_back(var); |
| 67 | } |
| 68 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 69 | void Module::AddTypeDecl(const ast::TypeDecl* type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 70 | TINT_ASSERT(AST, type); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 71 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id); |
Ben Clayton | 950809f | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 72 | type_decls_.push_back(type); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 73 | global_declarations_.push_back(type); |
| 74 | } |
| 75 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 76 | void Module::AddFunction(const ast::Function* func) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 77 | TINT_ASSERT(AST, func); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 78 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, func, program_id); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 79 | functions_.push_back(func); |
| 80 | global_declarations_.push_back(func); |
| 81 | } |
| 82 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 83 | const Module* Module::Clone(CloneContext* ctx) const { |
Ben Clayton | 7b6bcb6 | 2021-02-11 20:23:03 +0000 | [diff] [blame] | 84 | auto* out = ctx->dst->create<Module>(); |
| 85 | out->Copy(ctx, this); |
| 86 | return out; |
| 87 | } |
| 88 | |
| 89 | void Module::Copy(CloneContext* ctx, const Module* src) { |
Ben Clayton | cfed1cb | 2021-06-16 09:19:36 +0000 | [diff] [blame] | 90 | ctx->Clone(global_declarations_, src->global_declarations_); |
Ben Clayton | 9efc4fc | 2021-06-18 21:15:25 +0000 | [diff] [blame] | 91 | |
| 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 Clayton | cfed1cb | 2021-06-16 09:19:36 +0000 | [diff] [blame] | 98 | for (auto* decl : global_declarations_) { |
Ben Clayton | 90f43cf | 2021-03-31 20:43:26 +0000 | [diff] [blame] | 99 | if (!decl) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 100 | TINT_ICE(AST, ctx->dst->Diagnostics()) |
| 101 | << "src global declaration was nullptr"; |
Ben Clayton | 90f43cf | 2021-03-31 20:43:26 +0000 | [diff] [blame] | 102 | continue; |
| 103 | } |
Ben Clayton | cfed1cb | 2021-06-16 09:19:36 +0000 | [diff] [blame] | 104 | if (auto* type = decl->As<ast::TypeDecl>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 105 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id); |
Ben Clayton | cfed1cb | 2021-06-16 09:19:36 +0000 | [diff] [blame] | 106 | type_decls_.push_back(type); |
James Price | 5583853 | 2021-02-09 21:39:10 +0000 | [diff] [blame] | 107 | } else if (auto* func = decl->As<Function>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 108 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, func, program_id); |
Ben Clayton | cfed1cb | 2021-06-16 09:19:36 +0000 | [diff] [blame] | 109 | functions_.push_back(func); |
James Price | 5583853 | 2021-02-09 21:39:10 +0000 | [diff] [blame] | 110 | } else if (auto* var = decl->As<Variable>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 111 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, var, program_id); |
Ben Clayton | cfed1cb | 2021-06-16 09:19:36 +0000 | [diff] [blame] | 112 | global_variables_.push_back(var); |
James Price | 5583853 | 2021-02-09 21:39:10 +0000 | [diff] [blame] | 113 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 114 | TINT_ICE(AST, ctx->dst->Diagnostics()) |
| 115 | << "Unknown global declaration type"; |
James Price | 5583853 | 2021-02-09 21:39:10 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
Ben Clayton | a07396a | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 120 | } // namespace ast |
| 121 | } // namespace tint |