blob: e3cfde0984cb223965acb1a9ce1a91eadf2ee618 [file] [log] [blame]
Ben Claytondcdf66e2022-06-17 12:48:51 +00001// Copyright 2022 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#ifndef SRC_TINT_AST_VAR_H_
16#define SRC_TINT_AST_VAR_H_
17
18#include <utility>
19#include <vector>
20
21#include "src/tint/ast/variable.h"
22
23namespace tint::ast {
24
25/// A "var" declaration is a name for typed storage.
26///
27/// Examples:
28///
29/// ```
30/// // Declared outside a function, i.e. at module scope, requires
dan sinclairff7cf212022-10-03 14:05:23 +000031/// // a address space.
Ben Claytondcdf66e2022-06-17 12:48:51 +000032/// var<workgroup> width : i32; // no initializer
33/// var<private> height : i32 = 3; // with initializer
34///
dan sinclairff7cf212022-10-03 14:05:23 +000035/// // A variable declared inside a function doesn't take a address space,
Ben Claytondcdf66e2022-06-17 12:48:51 +000036/// // and maps to SPIR-V Function storage.
37/// var computed_depth : i32;
38/// var area : i32 = compute_area(width, height);
39/// ```
40///
41/// @see https://www.w3.org/TR/WGSL/#var-decls
dan sinclair12fa3032023-04-19 23:52:33 +000042class Var final : public utils::Castable<Var, Variable> {
Ben Claytondcdf66e2022-06-17 12:48:51 +000043 public:
44 /// Create a 'var' variable
Ben Clayton4a92a3c2022-07-18 20:50:02 +000045 /// @param pid the identifier of the program that owns this node
46 /// @param nid the unique node identifier
Ben Claytondcdf66e2022-06-17 12:48:51 +000047 /// @param source the variable source
Ben Clayton651d9e22023-02-09 10:34:14 +000048 /// @param name the variable name
Ben Claytondcdf66e2022-06-17 12:48:51 +000049 /// @param type the declared variable type
dan sinclairff7cf212022-10-03 14:05:23 +000050 /// @param declared_address_space the declared address space
Ben Claytondcdf66e2022-06-17 12:48:51 +000051 /// @param declared_access the declared access control
dan sinclair6e77b472022-10-20 13:38:28 +000052 /// @param initializer the initializer expression
Ben Claytondcdf66e2022-06-17 12:48:51 +000053 /// @param attributes the variable attributes
Ben Clayton4a92a3c2022-07-18 20:50:02 +000054 Var(ProgramID pid,
55 NodeID nid,
Ben Claytondcdf66e2022-06-17 12:48:51 +000056 const Source& source,
Ben Clayton651d9e22023-02-09 10:34:14 +000057 const Identifier* name,
Ben Clayton971318f2023-02-14 13:52:43 +000058 Type type,
Ben Clayton79781f22023-02-18 17:13:18 +000059 const Expression* declared_address_space,
60 const Expression* declared_access,
dan sinclair6e77b472022-10-20 13:38:28 +000061 const Expression* initializer,
Ben Clayton783b1692022-08-02 17:03:35 +000062 utils::VectorRef<const Attribute*> attributes);
Ben Claytondcdf66e2022-06-17 12:48:51 +000063
Ben Claytondcdf66e2022-06-17 12:48:51 +000064 /// Destructor
65 ~Var() override;
66
Ben Claytone48ef8e2022-06-26 10:52:50 +000067 /// @returns "var"
68 const char* Kind() const override;
69
Ben Claytondcdf66e2022-06-17 12:48:51 +000070 /// Clones this node and all transitive child nodes using the `CloneContext`
71 /// `ctx`.
72 /// @param ctx the clone context
73 /// @return the newly cloned node
74 const Var* Clone(CloneContext* ctx) const override;
75
dan sinclairff7cf212022-10-03 14:05:23 +000076 /// The declared address space
Ben Clayton79781f22023-02-18 17:13:18 +000077 const Expression* const declared_address_space = nullptr;
Ben Claytondcdf66e2022-06-17 12:48:51 +000078
79 /// The declared access control
Ben Clayton79781f22023-02-18 17:13:18 +000080 const Expression* const declared_access = nullptr;
Ben Claytondcdf66e2022-06-17 12:48:51 +000081};
82
83/// A list of `var` declarations
84using VarList = std::vector<const Var*>;
85
86} // namespace tint::ast
87
88#endif // SRC_TINT_AST_VAR_H_