blob: 55275cc0173644b31e7b7fa206765a1017093452 [file] [log] [blame]
Ben Claytonb17aea12021-02-03 17:51:09 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0(the "License");
Ben Claytonb17aea12021-02-03 17:51:09 +00004// 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 Maiorano5cd71b82021-04-16 19:07:51 +000015#ifndef SRC_SEM_VARIABLE_H_
16#define SRC_SEM_VARIABLE_H_
Ben Claytonb17aea12021-02-03 17:51:09 +000017
Ben Claytonb17aea12021-02-03 17:51:09 +000018#include <vector>
19
Ben Clayton93e8f522021-06-04 20:41:47 +000020#include "src/ast/access.h"
Ben Claytonb17aea12021-02-03 17:51:09 +000021#include "src/ast/storage_class.h"
James Pricee55e2102021-06-18 09:27:13 +000022#include "src/sem/binding_point.h"
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000023#include "src/sem/expression.h"
Ben Claytonb17aea12021-02-03 17:51:09 +000024
25namespace tint {
26
27// Forward declarations
28namespace ast {
Ben Clayton86c2cbf2021-04-07 08:09:01 +000029class IdentifierExpression;
Ben Claytonb17aea12021-02-03 17:51:09 +000030class Variable;
31} // namespace ast
Ben Claytonb17aea12021-02-03 17:51:09 +000032
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000033namespace sem {
Ben Claytonb17aea12021-02-03 17:51:09 +000034
Antonio Maiorano3751fd22021-04-19 22:51:23 +000035// Forward declarations
36class Type;
Ben Clayton86c2cbf2021-04-07 08:09:01 +000037class VariableUser;
38
Ben Claytonb17aea12021-02-03 17:51:09 +000039/// Variable holds the semantic information for variables.
40class Variable : public Castable<Variable, Node> {
41 public:
James Pricef2f3bfc2021-05-13 20:32:32 +000042 /// Constructor for variables and non-overridable constants
Ben Claytonb17aea12021-02-03 17:51:09 +000043 /// @param declaration the AST declaration node
Antonio Maiorano9ef17472021-03-26 12:47:58 +000044 /// @param type the variable type
Ben Claytonb17aea12021-02-03 17:51:09 +000045 /// @param storage_class the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +000046 /// @param access the variable access control type
James Pricee55e2102021-06-18 09:27:13 +000047 /// @param binding_point the optional resource binding point of the variable
Ben Clayton86c2cbf2021-04-07 08:09:01 +000048 Variable(const ast::Variable* declaration,
Antonio Maiorano26fa9922021-04-22 15:34:13 +000049 const sem::Type* type,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +000050 ast::StorageClass storage_class,
James Pricee55e2102021-06-18 09:27:13 +000051 ast::Access access,
52 sem::BindingPoint binding_point = {});
Ben Claytonb17aea12021-02-03 17:51:09 +000053
James Pricef2f3bfc2021-05-13 20:32:32 +000054 /// 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 Claytonb17aea12021-02-03 17:51:09 +000062 /// Destructor
63 ~Variable() override;
64
65 /// @returns the AST declaration node
Antonio Maiorano9ef17472021-03-26 12:47:58 +000066 const ast::Variable* Declaration() const { return declaration_; }
67
Antonio Maiorano25436862021-04-13 13:32:33 +000068 /// @returns the canonical type for the variable
Antonio Maiorano26fa9922021-04-22 15:34:13 +000069 sem::Type* Type() const { return const_cast<sem::Type*>(type_); }
Ben Claytonb17aea12021-02-03 17:51:09 +000070
71 /// @returns the storage class for the variable
72 ast::StorageClass StorageClass() const { return storage_class_; }
73
Antonio Maioranodc4e6c12021-05-14 17:51:13 +000074 /// @returns the access control for the variable
Ben Clayton93e8f522021-06-04 20:41:47 +000075 ast::Access Access() const { return access_; }
Antonio Maioranodc4e6c12021-05-14 17:51:13 +000076
James Pricee55e2102021-06-18 09:27:13 +000077 /// @returns the resource binding point for the variable
78 sem::BindingPoint BindingPoint() const { return binding_point_; }
79
James Pricec9af5972021-02-16 21:15:01 +000080 /// @returns the expressions that use the variable
Ben Clayton86c2cbf2021-04-07 08:09:01 +000081 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 Pricec9af5972021-02-16 21:15:01 +000085
James Pricef2f3bfc2021-05-13 20:32:32 +000086 /// @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 Price72f6ba42021-05-14 19:46:53 +000090 uint16_t ConstantId() const { return constant_id_; }
James Pricef2f3bfc2021-05-13 20:32:32 +000091
Ben Claytonb17aea12021-02-03 17:51:09 +000092 private:
Antonio Maiorano9ef17472021-03-26 12:47:58 +000093 const ast::Variable* const declaration_;
Antonio Maiorano26fa9922021-04-22 15:34:13 +000094 const sem::Type* const type_;
Ben Claytonb17aea12021-02-03 17:51:09 +000095 ast::StorageClass const storage_class_;
Ben Clayton93e8f522021-06-04 20:41:47 +000096 ast::Access const access_;
James Pricee55e2102021-06-18 09:27:13 +000097 sem::BindingPoint binding_point_;
Ben Clayton86c2cbf2021-04-07 08:09:01 +000098 std::vector<const VariableUser*> users_;
James Pricef2f3bfc2021-05-13 20:32:32 +000099 const bool is_pipeline_constant_;
100 const uint16_t constant_id_ = 0;
Ben Clayton86c2cbf2021-04-07 08:09:01 +0000101};
102
103/// VariableUser holds the semantic information for an identifier expression
104/// node that resolves to a variable.
105class 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 Maiorano26fa9922021-04-22 15:34:13 +0000113 const sem::Type* type,
Ben Clayton86c2cbf2021-04-07 08:09:01 +0000114 Statement* statement,
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000115 sem::Variable* variable);
Ben Clayton86c2cbf2021-04-07 08:09:01 +0000116
117 /// @returns the variable that this expression refers to
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000118 const sem::Variable* Variable() const { return variable_; }
Ben Clayton86c2cbf2021-04-07 08:09:01 +0000119
120 private:
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000121 sem::Variable const* const variable_;
Ben Claytonb17aea12021-02-03 17:51:09 +0000122};
123
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000124} // namespace sem
Ben Claytonb17aea12021-02-03 17:51:09 +0000125} // namespace tint
126
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000127#endif // SRC_SEM_VARIABLE_H_