blob: ec87e54bbd299e892b778df30d527875e1b7e6d8 [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"
Ben Claytondcdf66e2022-06-17 12:48:51 +000016#include "src/tint/ast/binding_attribute.h"
17#include "src/tint/ast/group_attribute.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000018
19TINT_INSTANTIATE_TYPEINFO(tint::ast::Variable);
20
dan sinclair34323ac2022-04-07 18:39:35 +000021namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000022
23Variable::Variable(ProgramID pid,
Ben Clayton4a92a3c2022-07-18 20:50:02 +000024 NodeID nid,
Ryan Harrisondbc13af2022-02-21 15:19:07 +000025 const Source& src,
26 const Symbol& sym,
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027 const ast::Type* ty,
Ryan Harrisondbc13af2022-02-21 15:19:07 +000028 const Expression* ctor,
Ben Clayton783b1692022-08-02 17:03:35 +000029 utils::VectorRef<const Attribute*> attrs)
Ben Clayton4a92a3c2022-07-18 20:50:02 +000030 : Base(pid, nid, src), symbol(sym), type(ty), constructor(ctor), attributes(std::move(attrs)) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000031 TINT_ASSERT(AST, symbol.IsValid());
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, symbol, program_id);
33 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, constructor, program_id);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034}
35
36Variable::Variable(Variable&&) = default;
37
38Variable::~Variable() = default;
39
40VariableBindingPoint Variable::BindingPoint() const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000041 const GroupAttribute* group = nullptr;
42 const BindingAttribute* binding = nullptr;
43 for (auto* attr : attributes) {
Ben Claytondcdf66e2022-06-17 12:48:51 +000044 Switch(
45 attr, //
46 [&](const GroupAttribute* a) { group = a; },
47 [&](const BindingAttribute* a) { binding = a; });
Ryan Harrisondbc13af2022-02-21 15:19:07 +000048 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000049 return VariableBindingPoint{group, binding};
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050}
51
dan sinclair34323ac2022-04-07 18:39:35 +000052} // namespace tint::ast