Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0(the "License"); |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 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 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 15 | #ifndef SRC_SEM_VARIABLE_H_ |
| 16 | #define SRC_SEM_VARIABLE_H_ |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 17 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 20 | #include "src/ast/access.h" |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 21 | #include "src/ast/storage_class.h" |
James Price | e55e210 | 2021-06-18 09:27:13 +0000 | [diff] [blame] | 22 | #include "src/sem/binding_point.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 23 | #include "src/sem/expression.h" |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 24 | |
| 25 | namespace tint { |
| 26 | |
| 27 | // Forward declarations |
| 28 | namespace ast { |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 29 | class IdentifierExpression; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 30 | class Variable; |
| 31 | } // namespace ast |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 32 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 33 | namespace sem { |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 34 | |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 35 | // Forward declarations |
| 36 | class Type; |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 37 | class VariableUser; |
| 38 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 39 | /// Variable holds the semantic information for variables. |
| 40 | class Variable : public Castable<Variable, Node> { |
| 41 | public: |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 42 | /// Constructor for variables and non-overridable constants |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 43 | /// @param declaration the AST declaration node |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 44 | /// @param type the variable type |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 45 | /// @param storage_class the variable storage class |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 46 | /// @param access the variable access control type |
James Price | e55e210 | 2021-06-18 09:27:13 +0000 | [diff] [blame] | 47 | /// @param binding_point the optional resource binding point of the variable |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 48 | Variable(const ast::Variable* declaration, |
Antonio Maiorano | 26fa992 | 2021-04-22 15:34:13 +0000 | [diff] [blame] | 49 | const sem::Type* type, |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 50 | ast::StorageClass storage_class, |
James Price | e55e210 | 2021-06-18 09:27:13 +0000 | [diff] [blame] | 51 | ast::Access access, |
| 52 | sem::BindingPoint binding_point = {}); |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 53 | |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 54 | /// Constructor for overridable pipeline constants |
| 55 | /// @param declaration the AST declaration node |
| 56 | /// @param type the variable type |
| 57 | /// @param constant_id the pipeline constant ID |
| 58 | Variable(const ast::Variable* declaration, |
| 59 | const sem::Type* type, |
| 60 | uint16_t constant_id); |
| 61 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 62 | /// Destructor |
| 63 | ~Variable() override; |
| 64 | |
| 65 | /// @returns the AST declaration node |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 66 | const ast::Variable* Declaration() const { return declaration_; } |
| 67 | |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 68 | /// @returns the canonical type for the variable |
Antonio Maiorano | 26fa992 | 2021-04-22 15:34:13 +0000 | [diff] [blame] | 69 | sem::Type* Type() const { return const_cast<sem::Type*>(type_); } |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 70 | |
| 71 | /// @returns the storage class for the variable |
| 72 | ast::StorageClass StorageClass() const { return storage_class_; } |
| 73 | |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 74 | /// @returns the access control for the variable |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 75 | ast::Access Access() const { return access_; } |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 76 | |
James Price | e55e210 | 2021-06-18 09:27:13 +0000 | [diff] [blame] | 77 | /// @returns the resource binding point for the variable |
| 78 | sem::BindingPoint BindingPoint() const { return binding_point_; } |
| 79 | |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 80 | /// @returns the expressions that use the variable |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 81 | const std::vector<const VariableUser*>& Users() const { return users_; } |
| 82 | |
| 83 | /// @param user the user to add |
| 84 | void AddUser(const VariableUser* user) { users_.emplace_back(user); } |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 85 | |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 86 | /// @returns true if this variable is an overridable pipeline constant |
| 87 | bool IsPipelineConstant() const { return is_pipeline_constant_; } |
| 88 | |
| 89 | /// @returns the pipeline constant ID associated with the variable |
James Price | 72f6ba4 | 2021-05-14 19:46:53 +0000 | [diff] [blame] | 90 | uint16_t ConstantId() const { return constant_id_; } |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 91 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 92 | private: |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 93 | const ast::Variable* const declaration_; |
Antonio Maiorano | 26fa992 | 2021-04-22 15:34:13 +0000 | [diff] [blame] | 94 | const sem::Type* const type_; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 95 | ast::StorageClass const storage_class_; |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 96 | ast::Access const access_; |
James Price | e55e210 | 2021-06-18 09:27:13 +0000 | [diff] [blame] | 97 | sem::BindingPoint binding_point_; |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 98 | std::vector<const VariableUser*> users_; |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 99 | const bool is_pipeline_constant_; |
| 100 | const uint16_t constant_id_ = 0; |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /// VariableUser holds the semantic information for an identifier expression |
| 104 | /// node that resolves to a variable. |
| 105 | class VariableUser : public Castable<VariableUser, Expression> { |
| 106 | public: |
| 107 | /// Constructor |
| 108 | /// @param declaration the AST identifier node |
| 109 | /// @param type the resolved type of the expression |
| 110 | /// @param statement the statement that owns this expression |
| 111 | /// @param variable the semantic variable |
| 112 | VariableUser(ast::IdentifierExpression* declaration, |
Antonio Maiorano | 26fa992 | 2021-04-22 15:34:13 +0000 | [diff] [blame] | 113 | const sem::Type* type, |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 114 | Statement* statement, |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 115 | sem::Variable* variable); |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 116 | |
| 117 | /// @returns the variable that this expression refers to |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 118 | const sem::Variable* Variable() const { return variable_; } |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 119 | |
| 120 | private: |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 121 | sem::Variable const* const variable_; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 124 | } // namespace sem |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 125 | } // namespace tint |
| 126 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 127 | #endif // SRC_SEM_VARIABLE_H_ |