blob: 9dc200f762e822ac7e10f9adced9f56422153310 [file] [log] [blame]
Ben Clayton87c78dd2021-02-03 16:43:20 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0(the "License");
Ben Clayton87c78dd2021-02-03 16:43:20 +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
15#ifndef SRC_SEMANTIC_FUNCTION_H_
16#define SRC_SEMANTIC_FUNCTION_H_
17
18#include <utility>
19#include <vector>
20
Ben Clayton2ddb1782021-02-08 19:13:19 +000021#include "src/semantic/call_target.h"
Ben Clayton87c78dd2021-02-03 16:43:20 +000022#include "src/type/sampler_type.h"
23
24namespace tint {
25
26// Forward declarations
27namespace ast {
28class BindingDecoration;
Ben Clayton2ddb1782021-02-08 19:13:19 +000029class BuiltinDecoration;
30class Function;
Ben Clayton87c78dd2021-02-03 16:43:20 +000031class GroupDecoration;
Ben Clayton87c78dd2021-02-03 16:43:20 +000032class LocationDecoration;
Ben Clayton87c78dd2021-02-03 16:43:20 +000033} // namespace ast
Ben Clayton87c78dd2021-02-03 16:43:20 +000034
35namespace semantic {
36
Ben Claytonb17aea12021-02-03 17:51:09 +000037class Variable;
38
Ben Clayton87c78dd2021-02-03 16:43:20 +000039/// Function holds the semantic information for function nodes.
Ben Clayton2ddb1782021-02-08 19:13:19 +000040class Function : public Castable<Function, CallTarget> {
Ben Clayton87c78dd2021-02-03 16:43:20 +000041 public:
42 /// Information about a binding
43 struct BindingInfo {
44 /// The binding decoration
45 ast::BindingDecoration* binding = nullptr;
46 /// The group decoration
47 ast::GroupDecoration* group = nullptr;
48 };
49
50 /// Constructor
Ben Clayton2ddb1782021-02-08 19:13:19 +000051 /// @param ast the ast::Function
Ben Clayton87c78dd2021-02-03 16:43:20 +000052 /// @param referenced_module_vars the referenced module variables
53 /// @param local_referenced_module_vars the locally referenced module
54 /// variables
55 /// @param ancestor_entry_points the ancestor entry points
Ben Clayton2ddb1782021-02-08 19:13:19 +000056 Function(ast::Function* ast,
57 std::vector<const Variable*> referenced_module_vars,
58 std::vector<const Variable*> local_referenced_module_vars,
59 std::vector<Symbol> ancestor_entry_points);
Ben Clayton87c78dd2021-02-03 16:43:20 +000060
61 /// Destructor
62 ~Function() override;
63
64 /// Note: If this function calls other functions, the return will also include
65 /// all of the referenced variables from the callees.
66 /// @returns the referenced module variables
Ben Claytonb17aea12021-02-03 17:51:09 +000067 const std::vector<const Variable*>& ReferencedModuleVariables() const {
Ben Clayton87c78dd2021-02-03 16:43:20 +000068 return referenced_module_vars_;
69 }
70 /// @returns the locally referenced module variables
Ben Claytonb17aea12021-02-03 17:51:09 +000071 const std::vector<const Variable*>& LocalReferencedModuleVariables() const {
Ben Clayton87c78dd2021-02-03 16:43:20 +000072 return local_referenced_module_vars_;
73 }
74 /// @returns the ancestor entry points
75 const std::vector<Symbol>& AncestorEntryPoints() const {
76 return ancestor_entry_points_;
77 }
78 /// Retrieves any referenced location variables
79 /// @returns the <variable, decoration> pair.
Ben Claytonb17aea12021-02-03 17:51:09 +000080 const std::vector<std::pair<const Variable*, ast::LocationDecoration*>>
Ben Clayton87c78dd2021-02-03 16:43:20 +000081 ReferencedLocationVariables() const;
82
83 /// Retrieves any referenced builtin variables
84 /// @returns the <variable, decoration> pair.
Ben Claytonb17aea12021-02-03 17:51:09 +000085 const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
Ben Clayton87c78dd2021-02-03 16:43:20 +000086 ReferencedBuiltinVariables() const;
87
88 /// Retrieves any referenced uniform variables. Note, the variables must be
89 /// decorated with both binding and group decorations.
90 /// @returns the referenced uniforms
Ben Claytonb17aea12021-02-03 17:51:09 +000091 const std::vector<std::pair<const Variable*, BindingInfo>>
Ben Clayton87c78dd2021-02-03 16:43:20 +000092 ReferencedUniformVariables() const;
93
94 /// Retrieves any referenced storagebuffer variables. Note, the variables
95 /// must be decorated with both binding and group decorations.
96 /// @returns the referenced storagebuffers
Ben Claytonb17aea12021-02-03 17:51:09 +000097 const std::vector<std::pair<const Variable*, BindingInfo>>
Ryan Harrisond086c142021-02-16 15:10:23 +000098 ReferencedStorageBufferVariables() const;
Ben Clayton87c78dd2021-02-03 16:43:20 +000099
100 /// Retrieves any referenced regular Sampler variables. Note, the
101 /// variables must be decorated with both binding and group decorations.
102 /// @returns the referenced storagebuffers
Ben Claytonb17aea12021-02-03 17:51:09 +0000103 const std::vector<std::pair<const Variable*, BindingInfo>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000104 ReferencedSamplerVariables() const;
105
106 /// Retrieves any referenced comparison Sampler variables. Note, the
107 /// variables must be decorated with both binding and group decorations.
108 /// @returns the referenced storagebuffers
Ben Claytonb17aea12021-02-03 17:51:09 +0000109 const std::vector<std::pair<const Variable*, BindingInfo>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000110 ReferencedComparisonSamplerVariables() const;
111
112 /// Retrieves any referenced sampled textures variables. Note, the
113 /// variables must be decorated with both binding and group decorations.
114 /// @returns the referenced sampled textures
Ben Claytonb17aea12021-02-03 17:51:09 +0000115 const std::vector<std::pair<const Variable*, BindingInfo>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000116 ReferencedSampledTextureVariables() const;
117
118 /// Retrieves any referenced multisampled textures variables. Note, the
119 /// variables must be decorated with both binding and group decorations.
120 /// @returns the referenced sampled textures
Ben Claytonb17aea12021-02-03 17:51:09 +0000121 const std::vector<std::pair<const Variable*, BindingInfo>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000122 ReferencedMultisampledTextureVariables() const;
123
Ryan Harrisond086c142021-02-16 15:10:23 +0000124 /// Retrieves any referenced storage texture variables. Note, the variables
125 /// must be decorated with both binding and group decorations.
126 /// @returns the referenced storage textures
127 const std::vector<std::pair<const Variable*, BindingInfo>>
128 ReferencedStorageTextureVariables() const;
129
Ben Clayton87c78dd2021-02-03 16:43:20 +0000130 /// Retrieves any locally referenced builtin variables
131 /// @returns the <variable, decoration> pairs.
Ben Claytonb17aea12021-02-03 17:51:09 +0000132 const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000133 LocalReferencedBuiltinVariables() const;
134
135 /// Checks if the given entry point is an ancestor
136 /// @param sym the entry point symbol
137 /// @returns true if `sym` is an ancestor entry point of this function
138 bool HasAncestorEntryPoint(Symbol sym) const;
139
140 private:
Ben Claytonb17aea12021-02-03 17:51:09 +0000141 const std::vector<std::pair<const Variable*, BindingInfo>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000142 ReferencedSamplerVariablesImpl(type::SamplerKind kind) const;
Ben Claytonb17aea12021-02-03 17:51:09 +0000143 const std::vector<std::pair<const Variable*, BindingInfo>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000144 ReferencedSampledTextureVariablesImpl(bool multisampled) const;
145
Ben Claytonb17aea12021-02-03 17:51:09 +0000146 std::vector<const Variable*> const referenced_module_vars_;
147 std::vector<const Variable*> const local_referenced_module_vars_;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000148 std::vector<Symbol> const ancestor_entry_points_;
149};
150
151} // namespace semantic
152} // namespace tint
153
154#endif // SRC_SEMANTIC_FUNCTION_H_