Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 1 | // 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 | |
| 23 | namespace 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 sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 31 | /// // a address space. |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 32 | /// var<workgroup> width : i32; // no initializer |
| 33 | /// var<private> height : i32 = 3; // with initializer |
| 34 | /// |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 35 | /// // A variable declared inside a function doesn't take a address space, |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 36 | /// // 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 sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 42 | class Var final : public utils::Castable<Var, Variable> { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 43 | public: |
| 44 | /// Create a 'var' variable |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 45 | /// @param pid the identifier of the program that owns this node |
| 46 | /// @param nid the unique node identifier |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 47 | /// @param source the variable source |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 48 | /// @param name the variable name |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 49 | /// @param type the declared variable type |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 50 | /// @param declared_address_space the declared address space |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 51 | /// @param declared_access the declared access control |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 52 | /// @param initializer the initializer expression |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 53 | /// @param attributes the variable attributes |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 54 | Var(ProgramID pid, |
| 55 | NodeID nid, |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 56 | const Source& source, |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 57 | const Identifier* name, |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 58 | Type type, |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 59 | const Expression* declared_address_space, |
| 60 | const Expression* declared_access, |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 61 | const Expression* initializer, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 62 | utils::VectorRef<const Attribute*> attributes); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 63 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 64 | /// Destructor |
| 65 | ~Var() override; |
| 66 | |
Ben Clayton | e48ef8e | 2022-06-26 10:52:50 +0000 | [diff] [blame] | 67 | /// @returns "var" |
| 68 | const char* Kind() const override; |
| 69 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 70 | /// 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 sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 76 | /// The declared address space |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 77 | const Expression* const declared_address_space = nullptr; |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 78 | |
| 79 | /// The declared access control |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 80 | const Expression* const declared_access = nullptr; |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /// A list of `var` declarations |
| 84 | using VarList = std::vector<const Var*>; |
| 85 | |
| 86 | } // namespace tint::ast |
| 87 | |
| 88 | #endif // SRC_TINT_AST_VAR_H_ |