Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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/tint/ast/variable.h" |
| 16 | |
| 17 | #include "src/tint/program_builder.h" |
| 18 | #include "src/tint/sem/variable.h" |
| 19 | |
| 20 | TINT_INSTANTIATE_TYPEINFO(tint::ast::Variable); |
| 21 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 22 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | |
| 24 | Variable::Variable(ProgramID pid, |
| 25 | const Source& src, |
| 26 | const Symbol& sym, |
| 27 | StorageClass dsc, |
| 28 | Access da, |
| 29 | const ast::Type* ty, |
| 30 | bool constant, |
| 31 | bool overridable, |
| 32 | const Expression* ctor, |
| 33 | AttributeList attrs) |
| 34 | : Base(pid, src), |
| 35 | symbol(sym), |
| 36 | type(ty), |
| 37 | is_const(constant), |
| 38 | is_overridable(overridable), |
| 39 | constructor(ctor), |
| 40 | attributes(std::move(attrs)), |
| 41 | declared_storage_class(dsc), |
| 42 | declared_access(da) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 43 | TINT_ASSERT(AST, symbol.IsValid()); |
| 44 | TINT_ASSERT(AST, is_overridable ? is_const : true); |
| 45 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, symbol, program_id); |
| 46 | TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, constructor, program_id); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | Variable::Variable(Variable&&) = default; |
| 50 | |
| 51 | Variable::~Variable() = default; |
| 52 | |
| 53 | VariableBindingPoint Variable::BindingPoint() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | const GroupAttribute* group = nullptr; |
| 55 | const BindingAttribute* binding = nullptr; |
| 56 | for (auto* attr : attributes) { |
| 57 | if (auto* g = attr->As<GroupAttribute>()) { |
| 58 | group = g; |
| 59 | } else if (auto* b = attr->As<BindingAttribute>()) { |
| 60 | binding = b; |
| 61 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | return VariableBindingPoint{group, binding}; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | const Variable* Variable::Clone(CloneContext* ctx) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 67 | auto src = ctx->Clone(source); |
| 68 | auto sym = ctx->Clone(symbol); |
| 69 | auto* ty = ctx->Clone(type); |
| 70 | auto* ctor = ctx->Clone(constructor); |
| 71 | auto attrs = ctx->Clone(attributes); |
| 72 | return ctx->dst->create<Variable>(src, sym, declared_storage_class, declared_access, ty, |
| 73 | is_const, is_overridable, ctor, attrs); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 76 | } // namespace tint::ast |