• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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/ast/module.h"
16 
17 #include <utility>
18 
19 #include "src/ast/type_decl.h"
20 #include "src/program_builder.h"
21 
22 TINT_INSTANTIATE_TYPEINFO(tint::ast::Module);
23 
24 namespace tint {
25 namespace ast {
26 
Module(ProgramID pid,const Source & src)27 Module::Module(ProgramID pid, const Source& src) : Base(pid, src) {}
28 
Module(ProgramID pid,const Source & src,std::vector<const ast::Node * > global_decls)29 Module::Module(ProgramID pid,
30                const Source& src,
31                std::vector<const ast::Node*> global_decls)
32     : Base(pid, src), global_declarations_(std::move(global_decls)) {
33   for (auto* decl : global_declarations_) {
34     if (decl == nullptr) {
35       continue;
36     }
37 
38     if (auto* ty = decl->As<ast::TypeDecl>()) {
39       type_decls_.push_back(ty);
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 {
45       diag::List diagnostics;
46       TINT_ICE(AST, diagnostics) << "Unknown global declaration type";
47     }
48   }
49 }
50 
51 Module::~Module() = default;
52 
LookupType(Symbol name) const53 const ast::TypeDecl* Module::LookupType(Symbol name) const {
54   for (auto* ty : TypeDecls()) {
55     if (ty->name == name) {
56       return ty;
57     }
58   }
59   return nullptr;
60 }
61 
AddGlobalVariable(const ast::Variable * var)62 void Module::AddGlobalVariable(const ast::Variable* var) {
63   TINT_ASSERT(AST, var);
64   TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, var, program_id);
65   global_variables_.push_back(var);
66   global_declarations_.push_back(var);
67 }
68 
AddTypeDecl(const ast::TypeDecl * type)69 void Module::AddTypeDecl(const ast::TypeDecl* type) {
70   TINT_ASSERT(AST, type);
71   TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id);
72   type_decls_.push_back(type);
73   global_declarations_.push_back(type);
74 }
75 
AddFunction(const ast::Function * func)76 void Module::AddFunction(const ast::Function* func) {
77   TINT_ASSERT(AST, func);
78   TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, func, program_id);
79   functions_.push_back(func);
80   global_declarations_.push_back(func);
81 }
82 
Clone(CloneContext * ctx) const83 const Module* Module::Clone(CloneContext* ctx) const {
84   auto* out = ctx->dst->create<Module>();
85   out->Copy(ctx, this);
86   return out;
87 }
88 
Copy(CloneContext * ctx,const Module * src)89 void Module::Copy(CloneContext* ctx, const Module* src) {
90   ctx->Clone(global_declarations_, src->global_declarations_);
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 
98   for (auto* decl : global_declarations_) {
99     if (!decl) {
100       TINT_ICE(AST, ctx->dst->Diagnostics())
101           << "src global declaration was nullptr";
102       continue;
103     }
104     if (auto* type = decl->As<ast::TypeDecl>()) {
105       TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id);
106       type_decls_.push_back(type);
107     } else if (auto* func = decl->As<Function>()) {
108       TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, func, program_id);
109       functions_.push_back(func);
110     } else if (auto* var = decl->As<Variable>()) {
111       TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, var, program_id);
112       global_variables_.push_back(var);
113     } else {
114       TINT_ICE(AST, ctx->dst->Diagnostics())
115           << "Unknown global declaration type";
116     }
117   }
118 }
119 
120 }  // namespace ast
121 }  // namespace tint
122