blob: f3484949154c9cc5fe1291f9debb6d022220ec63 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2020 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
dan sinclair99181d82023-07-20 01:14:15 +000028#ifndef SRC_TINT_LANG_WGSL_AST_VARIABLE_H_
29#define SRC_TINT_LANG_WGSL_AST_VARIABLE_H_
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
31#include <utility>
32#include <vector>
33
Ben Claytoncd52f382023-08-07 13:11:08 +000034#include "src/tint/lang/core/access.h"
35#include "src/tint/lang/core/address_space.h"
dan sinclair99181d82023-07-20 01:14:15 +000036#include "src/tint/lang/wgsl/ast/attribute.h"
37#include "src/tint/lang/wgsl/ast/binding_attribute.h"
38#include "src/tint/lang/wgsl/ast/expression.h"
39#include "src/tint/lang/wgsl/ast/group_attribute.h"
Le Hoang Quyenc2241312024-05-28 18:29:28 +000040#include "src/tint/lang/wgsl/ast/input_attachment_index_attribute.h"
Ben Claytonae18c412023-07-29 13:00:40 +000041#include "src/tint/lang/wgsl/ast/node.h"
dan sinclair99181d82023-07-20 01:14:15 +000042#include "src/tint/lang/wgsl/ast/type.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043
Ryan Harrisondbc13af2022-02-21 15:19:07 +000044// Forward declarations
Ben Claytona7230f02022-04-11 14:37:21 +000045namespace tint::ast {
Ben Clayton651d9e22023-02-09 10:34:14 +000046class Identifier;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047class LocationAttribute;
Ben Claytona7230f02022-04-11 14:37:21 +000048} // namespace tint::ast
49
50namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000051
Ben Claytondcdf66e2022-06-17 12:48:51 +000052/// Variable is the base class for Var, Let, Const, Override and Parameter.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000053///
Ben Claytondcdf66e2022-06-17 12:48:51 +000054/// An instance of this class represents one of five constructs in WGSL: "var" declaration, "let"
55/// declaration, "override" declaration, "const" declaration, or formal parameter to a function.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056///
Ben Claytondcdf66e2022-06-17 12:48:51 +000057/// @see https://www.w3.org/TR/WGSL/#value-decls
dan sinclairbae54e72023-07-28 15:01:54 +000058class Variable : public Castable<Variable, Node> {
dan sinclair41e4d9a2022-05-01 14:40:55 +000059 public:
Ben Claytondcdf66e2022-06-17 12:48:51 +000060 /// Constructor
Ben Clayton4a92a3c2022-07-18 20:50:02 +000061 /// @param pid the identifier of the program that owns this node
62 /// @param nid the unique node identifier
Ben Clayton651d9e22023-02-09 10:34:14 +000063 /// @param src the variable source
64 /// @param name The struct member name
dan sinclair41e4d9a2022-05-01 14:40:55 +000065 /// @param type the declared variable type
dan sinclair6e77b472022-10-20 13:38:28 +000066 /// @param initializer the initializer expression
dan sinclair41e4d9a2022-05-01 14:40:55 +000067 /// @param attributes the variable attributes
dan sinclair637a2fe2023-07-24 21:11:41 +000068 Variable(GenerationID pid,
Ben Clayton4a92a3c2022-07-18 20:50:02 +000069 NodeID nid,
Ben Clayton651d9e22023-02-09 10:34:14 +000070 const Source& src,
71 const Identifier* name,
Ben Clayton971318f2023-02-14 13:52:43 +000072 Type type,
dan sinclair6e77b472022-10-20 13:38:28 +000073 const Expression* initializer,
dan sinclairbae54e72023-07-28 15:01:54 +000074 VectorRef<const Attribute*> attributes);
Ben Claytondcdf66e2022-06-17 12:48:51 +000075
Ben Claytondcdf66e2022-06-17 12:48:51 +000076 /// Destructor
dan sinclair41e4d9a2022-05-01 14:40:55 +000077 ~Variable() override;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000078
dan sinclairacdf6e12022-08-24 15:47:25 +000079 /// @returns true if the variable has both group and binding attributes
80 bool HasBindingPoint() const {
Ben Clayton5662f792023-02-22 00:08:55 +000081 return HasAttribute<BindingAttribute>(attributes) &&
82 HasAttribute<GroupAttribute>(attributes);
dan sinclairacdf6e12022-08-24 15:47:25 +000083 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000084
Le Hoang Quyenc2241312024-05-28 18:29:28 +000085 /// @returns true if the variable has an input_attachment_index attribute
86 bool HasInputAttachmentIndex() const {
87 return HasAttribute<InputAttachmentIndexAttribute>(attributes);
88 }
89
Ben Claytone48ef8e2022-06-26 10:52:50 +000090 /// @returns the kind of the variable, which can be used in diagnostics
91 /// e.g. "var", "let", "const", etc
92 virtual const char* Kind() const = 0;
93
Ben Clayton651d9e22023-02-09 10:34:14 +000094 /// The variable name
95 const Identifier* const name;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000096
dan sinclair41e4d9a2022-05-01 14:40:55 +000097 /// The declared variable type. This is null if the type is inferred, e.g.:
98 /// let f = 1.0;
99 /// var i = 1;
Ben Clayton971318f2023-02-14 13:52:43 +0000100 const Type type;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000101
dan sinclair6e77b472022-10-20 13:38:28 +0000102 /// The initializer expression or nullptr if none set
103 const Expression* const initializer;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000104
dan sinclair41e4d9a2022-05-01 14:40:55 +0000105 /// The attributes attached to this variable
dan sinclairbae54e72023-07-28 15:01:54 +0000106 const tint::Vector<const Attribute*, 2> attributes;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000107};
108
dan sinclair34323ac2022-04-07 18:39:35 +0000109} // namespace tint::ast
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000110
dan sinclair99181d82023-07-20 01:14:15 +0000111#endif // SRC_TINT_LANG_WGSL_AST_VARIABLE_H_