blob: 26991f252a2faee85d1859a13330159636661815 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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
20TINT_INSTANTIATE_TYPEINFO(tint::ast::Variable);
21
dan sinclair34323ac2022-04-07 18:39:35 +000022namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000023
24Variable::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 sinclair41e4d9a2022-05-01 14:40:55 +000043 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 Harrisondbc13af2022-02-21 15:19:07 +000047}
48
49Variable::Variable(Variable&&) = default;
50
51Variable::~Variable() = default;
52
53VariableBindingPoint Variable::BindingPoint() const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 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 Harrisondbc13af2022-02-21 15:19:07 +000062 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 return VariableBindingPoint{group, binding};
Ryan Harrisondbc13af2022-02-21 15:19:07 +000064}
65
66const Variable* Variable::Clone(CloneContext* ctx) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000067 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 Harrisondbc13af2022-02-21 15:19:07 +000074}
75
dan sinclair34323ac2022-04-07 18:39:35 +000076} // namespace tint::ast