1 // Copyright 2020 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/variable.h"
16
17 #include "src/ast/override_decoration.h"
18 #include "src/program_builder.h"
19 #include "src/sem/variable.h"
20
21 TINT_INSTANTIATE_TYPEINFO(tint::ast::Variable);
22
23 namespace tint {
24 namespace ast {
25
Variable(ProgramID pid,const Source & src,const Symbol & sym,StorageClass dsc,Access da,const ast::Type * ty,bool constant,const Expression * ctor,DecorationList decos)26 Variable::Variable(ProgramID pid,
27 const Source& src,
28 const Symbol& sym,
29 StorageClass dsc,
30 Access da,
31 const ast::Type* ty,
32 bool constant,
33 const Expression* ctor,
34 DecorationList decos)
35 : Base(pid, src),
36 symbol(sym),
37 type(ty),
38 is_const(constant),
39 constructor(ctor),
40 decorations(std::move(decos)),
41 declared_storage_class(dsc),
42 declared_access(da) {
43 TINT_ASSERT(AST, symbol.IsValid());
44 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, symbol, program_id);
45 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, constructor, program_id);
46 }
47
48 Variable::Variable(Variable&&) = default;
49
50 Variable::~Variable() = default;
51
BindingPoint() const52 VariableBindingPoint Variable::BindingPoint() const {
53 const GroupDecoration* group = nullptr;
54 const BindingDecoration* binding = nullptr;
55 for (auto* deco : decorations) {
56 if (auto* g = deco->As<GroupDecoration>()) {
57 group = g;
58 } else if (auto* b = deco->As<BindingDecoration>()) {
59 binding = b;
60 }
61 }
62 return VariableBindingPoint{group, binding};
63 }
64
Clone(CloneContext * ctx) const65 const Variable* Variable::Clone(CloneContext* ctx) const {
66 auto src = ctx->Clone(source);
67 auto sym = ctx->Clone(symbol);
68 auto* ty = ctx->Clone(type);
69 auto* ctor = ctx->Clone(constructor);
70 auto decos = ctx->Clone(decorations);
71 return ctx->dst->create<Variable>(src, sym, declared_storage_class,
72 declared_access, ty, is_const, ctor, decos);
73 }
74
75 } // namespace ast
76 } // namespace tint
77