Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0(the "License"); |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +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 | |
| 15 | #ifndef SRC_SEMANTIC_FUNCTION_H_ |
| 16 | #define SRC_SEMANTIC_FUNCTION_H_ |
| 17 | |
| 18 | #include <utility> |
| 19 | #include <vector> |
| 20 | |
Ben Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 21 | #include "src/semantic/call_target.h" |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 22 | #include "src/type/sampler_type.h" |
| 23 | |
| 24 | namespace tint { |
| 25 | |
| 26 | // Forward declarations |
| 27 | namespace ast { |
| 28 | class BindingDecoration; |
Ben Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 29 | class BuiltinDecoration; |
| 30 | class Function; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 31 | class GroupDecoration; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 32 | class LocationDecoration; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 33 | } // namespace ast |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 34 | |
| 35 | namespace semantic { |
| 36 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 37 | class Variable; |
| 38 | |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 39 | /// Function holds the semantic information for function nodes. |
Ben Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 40 | class Function : public Castable<Function, CallTarget> { |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 41 | 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 Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 51 | /// @param ast the ast::Function |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 52 | /// @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 Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 56 | 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 Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 60 | |
| 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 67 | const std::vector<const Variable*>& ReferencedModuleVariables() const { |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 68 | return referenced_module_vars_; |
| 69 | } |
| 70 | /// @returns the locally referenced module variables |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 71 | const std::vector<const Variable*>& LocalReferencedModuleVariables() const { |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 72 | 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 80 | const std::vector<std::pair<const Variable*, ast::LocationDecoration*>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 81 | ReferencedLocationVariables() const; |
| 82 | |
| 83 | /// Retrieves any referenced builtin variables |
| 84 | /// @returns the <variable, decoration> pair. |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 85 | const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 86 | 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 91 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 92 | 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 97 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ryan Harrison | d086c14 | 2021-02-16 15:10:23 +0000 | [diff] [blame^] | 98 | ReferencedStorageBufferVariables() const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 99 | |
| 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 103 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 104 | 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 109 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 110 | 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 115 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 116 | 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 121 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 122 | ReferencedMultisampledTextureVariables() const; |
| 123 | |
Ryan Harrison | d086c14 | 2021-02-16 15:10:23 +0000 | [diff] [blame^] | 124 | /// 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 Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 130 | /// Retrieves any locally referenced builtin variables |
| 131 | /// @returns the <variable, decoration> pairs. |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 132 | const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 133 | 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 Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 141 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 142 | ReferencedSamplerVariablesImpl(type::SamplerKind kind) const; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 143 | const std::vector<std::pair<const Variable*, BindingInfo>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 144 | ReferencedSampledTextureVariablesImpl(bool multisampled) const; |
| 145 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 146 | std::vector<const Variable*> const referenced_module_vars_; |
| 147 | std::vector<const Variable*> const local_referenced_module_vars_; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 148 | std::vector<Symbol> const ancestor_entry_points_; |
| 149 | }; |
| 150 | |
| 151 | } // namespace semantic |
| 152 | } // namespace tint |
| 153 | |
| 154 | #endif // SRC_SEMANTIC_FUNCTION_H_ |