Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2021 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 | #include "src/tint/resolver/dependency_graph.h" |
| 16 | |
| 17 | #include <string> |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 18 | #include <utility> |
| 19 | #include <vector> |
| 20 | |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 21 | #include "src/tint/ast/alias.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 22 | #include "src/tint/ast/assignment_statement.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 23 | #include "src/tint/ast/block_statement.h" |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 24 | #include "src/tint/ast/break_if_statement.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 25 | #include "src/tint/ast/break_statement.h" |
| 26 | #include "src/tint/ast/call_statement.h" |
| 27 | #include "src/tint/ast/compound_assignment_statement.h" |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 28 | #include "src/tint/ast/const.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | #include "src/tint/ast/continue_statement.h" |
James Price | 098e3d8 | 2023-01-24 21:01:36 +0000 | [diff] [blame] | 30 | #include "src/tint/ast/diagnostic_attribute.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | #include "src/tint/ast/discard_statement.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 32 | #include "src/tint/ast/for_loop_statement.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 33 | #include "src/tint/ast/id_attribute.h" |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 34 | #include "src/tint/ast/identifier.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 35 | #include "src/tint/ast/if_statement.h" |
| 36 | #include "src/tint/ast/increment_decrement_statement.h" |
| 37 | #include "src/tint/ast/internal_attribute.h" |
| 38 | #include "src/tint/ast/interpolate_attribute.h" |
| 39 | #include "src/tint/ast/invariant_attribute.h" |
Ben Clayton | c0c8abc | 2023-03-02 17:36:37 +0000 | [diff] [blame] | 40 | #include "src/tint/ast/let.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 41 | #include "src/tint/ast/location_attribute.h" |
| 42 | #include "src/tint/ast/loop_statement.h" |
dan sinclair | a79c660 | 2023-02-21 18:49:40 +0000 | [diff] [blame] | 43 | #include "src/tint/ast/must_use_attribute.h" |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 44 | #include "src/tint/ast/override.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 45 | #include "src/tint/ast/return_statement.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 46 | #include "src/tint/ast/stage_attribute.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 47 | #include "src/tint/ast/stride_attribute.h" |
| 48 | #include "src/tint/ast/struct.h" |
| 49 | #include "src/tint/ast/struct_member_align_attribute.h" |
| 50 | #include "src/tint/ast/struct_member_offset_attribute.h" |
| 51 | #include "src/tint/ast/struct_member_size_attribute.h" |
| 52 | #include "src/tint/ast/switch_statement.h" |
Ben Clayton | 2cdf134 | 2023-02-03 13:24:18 +0000 | [diff] [blame] | 53 | #include "src/tint/ast/templated_identifier.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 54 | #include "src/tint/ast/traverse_expressions.h" |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 55 | #include "src/tint/ast/var.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 56 | #include "src/tint/ast/variable_decl_statement.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 57 | #include "src/tint/ast/while_statement.h" |
| 58 | #include "src/tint/ast/workgroup_attribute.h" |
dan sinclair | ef30aa4 | 2023-02-19 04:28:04 +0000 | [diff] [blame] | 59 | #include "src/tint/builtin/builtin.h" |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 60 | #include "src/tint/builtin/builtin_value.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | #include "src/tint/scope_stack.h" |
| 62 | #include "src/tint/sem/builtin.h" |
Ben Clayton | 23946b3 | 2023-03-09 16:50:19 +0000 | [diff] [blame] | 63 | #include "src/tint/switch.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 64 | #include "src/tint/utils/block_allocator.h" |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 65 | #include "src/tint/utils/compiler_macros.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 66 | #include "src/tint/utils/defer.h" |
| 67 | #include "src/tint/utils/map.h" |
| 68 | #include "src/tint/utils/scoped_assignment.h" |
Ben Clayton | ca0b9ef | 2023-05-26 21:20:36 +0000 | [diff] [blame] | 69 | #include "src/tint/utils/string.h" |
dan sinclair | b23cda4 | 2023-02-28 17:31:54 +0000 | [diff] [blame] | 70 | #include "src/tint/utils/string_stream.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 71 | #include "src/tint/utils/unique_vector.h" |
| 72 | |
| 73 | #define TINT_DUMP_DEPENDENCY_GRAPH 0 |
| 74 | |
dan sinclair | d209379 | 2022-04-07 17:45:45 +0000 | [diff] [blame] | 75 | namespace tint::resolver { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 76 | namespace { |
| 77 | |
| 78 | // Forward declaration |
| 79 | struct Global; |
| 80 | |
| 81 | /// Dependency describes how one global depends on another global |
| 82 | struct DependencyInfo { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 83 | /// The source of the symbol that forms the dependency |
| 84 | Source source; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /// DependencyEdge describes the two Globals used to define a dependency |
| 88 | /// relationship. |
| 89 | struct DependencyEdge { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 90 | /// The Global that depends on #to |
| 91 | const Global* from; |
| 92 | /// The Global that is depended on by #from |
| 93 | const Global* to; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /// DependencyEdgeCmp implements the contracts of std::equal_to<DependencyEdge> |
| 97 | /// and std::hash<DependencyEdge>. |
| 98 | struct DependencyEdgeCmp { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 99 | /// Equality operator |
| 100 | bool operator()(const DependencyEdge& lhs, const DependencyEdge& rhs) const { |
| 101 | return lhs.from == rhs.from && lhs.to == rhs.to; |
| 102 | } |
| 103 | /// Hashing operator |
| 104 | inline std::size_t operator()(const DependencyEdge& d) const { |
| 105 | return utils::Hash(d.from, d.to); |
| 106 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /// A map of DependencyEdge to DependencyInfo |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 110 | using DependencyEdges = |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 111 | utils::Hashmap<DependencyEdge, DependencyInfo, 64, DependencyEdgeCmp, DependencyEdgeCmp>; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 112 | |
| 113 | /// Global describes a module-scope variable, type or function. |
| 114 | struct Global { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 115 | explicit Global(const ast::Node* n) : node(n) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 116 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 117 | /// The declaration ast::Node |
| 118 | const ast::Node* node; |
| 119 | /// A list of dependencies that this global depends on |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 120 | utils::Vector<Global*, 8> deps; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /// A map of global name to Global |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 124 | using GlobalMap = utils::Hashmap<Symbol, Global*, 16>; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 125 | |
| 126 | /// Raises an ICE that a global ast::Node type was not handled by this system. |
| 127 | void UnhandledNode(diag::List& diagnostics, const ast::Node* node) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 128 | TINT_ICE(Resolver, diagnostics) << "unhandled node type: " << node->TypeInfo().name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /// Raises an error diagnostic with the given message and source. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 132 | void AddError(diag::List& diagnostics, const std::string& msg, const Source& source) { |
| 133 | diagnostics.add_error(diag::System::Resolver, msg, source); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /// Raises a note diagnostic with the given message and source. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 137 | void AddNote(diag::List& diagnostics, const std::string& msg, const Source& source) { |
| 138 | diagnostics.add_note(diag::System::Resolver, msg, source); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /// DependencyScanner is used to traverse a module to build the list of |
| 142 | /// global-to-global dependencies. |
| 143 | class DependencyScanner { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 144 | public: |
| 145 | /// Constructor |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 146 | /// @param globals_by_name map of global symbol to Global pointer |
| 147 | /// @param diagnostics diagnostic messages, appended with any errors found |
| 148 | /// @param graph the dependency graph to populate with resolved symbols |
| 149 | /// @param edges the map of globals-to-global dependency edges, which will |
| 150 | /// be populated by calls to Scan() |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 151 | DependencyScanner(const GlobalMap& globals_by_name, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 152 | diag::List& diagnostics, |
| 153 | DependencyGraph& graph, |
| 154 | DependencyEdges& edges) |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 155 | : globals_(globals_by_name), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 156 | diagnostics_(diagnostics), |
| 157 | graph_(graph), |
| 158 | dependency_edges_(edges) { |
| 159 | // Register all the globals at global-scope |
| 160 | for (auto it : globals_by_name) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 161 | scope_stack_.Set(it.key, it.value->node); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 162 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 163 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 164 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 165 | /// Walks the global declarations, resolving symbols, and determining the |
| 166 | /// dependencies of each global. |
| 167 | void Scan(Global* global) { |
| 168 | TINT_SCOPED_ASSIGNMENT(current_global_, global); |
| 169 | Switch( |
| 170 | global->node, |
| 171 | [&](const ast::Struct* str) { |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 172 | Declare(str->name->symbol, str); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 173 | for (auto* member : str->members) { |
dan sinclair | cb41b8f | 2022-09-26 15:54:55 +0000 | [diff] [blame] | 174 | TraverseAttributes(member->attributes); |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 175 | TraverseExpression(member->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 176 | } |
| 177 | }, |
| 178 | [&](const ast::Alias* alias) { |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 179 | Declare(alias->name->symbol, alias); |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 180 | TraverseExpression(alias->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 181 | }, |
| 182 | [&](const ast::Function* func) { |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 183 | Declare(func->name->symbol, func); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 184 | TraverseFunction(func); |
| 185 | }, |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 186 | [&](const ast::Variable* v) { |
| 187 | Declare(v->name->symbol, v); |
| 188 | TraverseVariable(v); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 189 | }, |
James Price | 98dc5a8 | 2023-02-04 12:20:55 +0000 | [diff] [blame] | 190 | [&](const ast::DiagnosticDirective*) { |
| 191 | // Diagnostic directives do not affect the dependency graph. |
James Price | 098e3d8 | 2023-01-24 21:01:36 +0000 | [diff] [blame] | 192 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 193 | [&](const ast::Enable*) { |
James Price | 098e3d8 | 2023-01-24 21:01:36 +0000 | [diff] [blame] | 194 | // Enable directives do not affect the dependency graph. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 195 | }, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 196 | [&](const ast::ConstAssert* assertion) { TraverseExpression(assertion->condition); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 197 | [&](Default) { UnhandledNode(diagnostics_, global->node); }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 198 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 199 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 200 | private: |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 201 | /// Traverses the variable, performing symbol resolution. |
| 202 | void TraverseVariable(const ast::Variable* v) { |
| 203 | if (auto* var = v->As<ast::Var>()) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 204 | TraverseExpression(var->declared_address_space); |
| 205 | TraverseExpression(var->declared_access); |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 206 | } |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 207 | TraverseExpression(v->type); |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 208 | TraverseAttributes(v->attributes); |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 209 | TraverseExpression(v->initializer); |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /// Traverses the function, performing symbol resolution and determining global dependencies. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 213 | void TraverseFunction(const ast::Function* func) { |
Ben Clayton | ca98b1b | 2022-11-07 13:15:21 +0000 | [diff] [blame] | 214 | TraverseAttributes(func->attributes); |
| 215 | TraverseAttributes(func->return_type_attributes); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 216 | // Perform symbol resolution on all the parameter types before registering |
| 217 | // the parameters themselves. This allows the case of declaring a parameter |
| 218 | // with the same identifier as its type. |
| 219 | for (auto* param : func->params) { |
Ben Clayton | ca98b1b | 2022-11-07 13:15:21 +0000 | [diff] [blame] | 220 | TraverseAttributes(param->attributes); |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 221 | TraverseExpression(param->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 222 | } |
| 223 | // Resolve the return type |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 224 | TraverseExpression(func->return_type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 225 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 226 | // Push the scope stack for the parameters and function body. |
| 227 | scope_stack_.Push(); |
| 228 | TINT_DEFER(scope_stack_.Pop()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 229 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 230 | for (auto* param : func->params) { |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 231 | if (auto* shadows = scope_stack_.Get(param->name->symbol)) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 232 | graph_.shadows.Add(param, shadows); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 233 | } |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 234 | Declare(param->name->symbol, param); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 235 | } |
| 236 | if (func->body) { |
| 237 | TraverseStatements(func->body->statements); |
| 238 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 239 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 240 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 241 | /// Traverses the statements, performing symbol resolution and determining |
| 242 | /// global dependencies. |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 243 | void TraverseStatements(utils::VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 244 | for (auto* s : stmts) { |
| 245 | TraverseStatement(s); |
| 246 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 247 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 248 | |
| 249 | /// Traverses the statement, performing symbol resolution and determining |
| 250 | /// global dependencies. |
| 251 | void TraverseStatement(const ast::Statement* stmt) { |
| 252 | if (!stmt) { |
| 253 | return; |
| 254 | } |
| 255 | Switch( |
| 256 | stmt, // |
| 257 | [&](const ast::AssignmentStatement* a) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 258 | TraverseExpression(a->lhs); |
| 259 | TraverseExpression(a->rhs); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 260 | }, |
| 261 | [&](const ast::BlockStatement* b) { |
| 262 | scope_stack_.Push(); |
| 263 | TINT_DEFER(scope_stack_.Pop()); |
| 264 | TraverseStatements(b->statements); |
| 265 | }, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 266 | [&](const ast::BreakIfStatement* b) { TraverseExpression(b->condition); }, |
| 267 | [&](const ast::CallStatement* r) { TraverseExpression(r->expr); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 268 | [&](const ast::CompoundAssignmentStatement* a) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 269 | TraverseExpression(a->lhs); |
| 270 | TraverseExpression(a->rhs); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 271 | }, |
| 272 | [&](const ast::ForLoopStatement* l) { |
| 273 | scope_stack_.Push(); |
| 274 | TINT_DEFER(scope_stack_.Pop()); |
| 275 | TraverseStatement(l->initializer); |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 276 | TraverseExpression(l->condition); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 277 | TraverseStatement(l->continuing); |
| 278 | TraverseStatement(l->body); |
| 279 | }, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 280 | [&](const ast::IncrementDecrementStatement* i) { TraverseExpression(i->lhs); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 281 | [&](const ast::LoopStatement* l) { |
| 282 | scope_stack_.Push(); |
| 283 | TINT_DEFER(scope_stack_.Pop()); |
| 284 | TraverseStatements(l->body->statements); |
| 285 | TraverseStatement(l->continuing); |
| 286 | }, |
| 287 | [&](const ast::IfStatement* i) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 288 | TraverseExpression(i->condition); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 289 | TraverseStatement(i->body); |
| 290 | if (i->else_statement) { |
| 291 | TraverseStatement(i->else_statement); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 292 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 293 | }, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 294 | [&](const ast::ReturnStatement* r) { TraverseExpression(r->value); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 295 | [&](const ast::SwitchStatement* s) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 296 | TraverseExpression(s->condition); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 297 | for (auto* c : s->body) { |
| 298 | for (auto* sel : c->selectors) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 299 | TraverseExpression(sel->expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 300 | } |
| 301 | TraverseStatement(c->body); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 302 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 303 | }, |
| 304 | [&](const ast::VariableDeclStatement* v) { |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 305 | if (auto* shadows = scope_stack_.Get(v->variable->name->symbol)) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 306 | graph_.shadows.Add(v->variable, shadows); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 307 | } |
Ben Clayton | 79781f2 | 2023-02-18 17:13:18 +0000 | [diff] [blame] | 308 | TraverseVariable(v->variable); |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 309 | Declare(v->variable->name->symbol, v->variable); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 310 | }, |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 311 | [&](const ast::WhileStatement* w) { |
| 312 | scope_stack_.Push(); |
| 313 | TINT_DEFER(scope_stack_.Pop()); |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 314 | TraverseExpression(w->condition); |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 315 | TraverseStatement(w->body); |
| 316 | }, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 317 | [&](const ast::ConstAssert* assertion) { TraverseExpression(assertion->condition); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 318 | [&](Default) { |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 319 | if (TINT_UNLIKELY((!stmt->IsAnyOf<ast::BreakStatement, ast::ContinueStatement, |
| 320 | ast::DiscardStatement>()))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 321 | UnhandledNode(diagnostics_, stmt); |
| 322 | } |
| 323 | }); |
| 324 | } |
| 325 | |
| 326 | /// Adds the symbol definition to the current scope, raising an error if two |
| 327 | /// symbols collide within the same scope. |
| 328 | void Declare(Symbol symbol, const ast::Node* node) { |
| 329 | auto* old = scope_stack_.Set(symbol, node); |
| 330 | if (old != nullptr && node != old) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 331 | auto name = symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 332 | AddError(diagnostics_, "redeclaration of '" + name + "'", node->source); |
| 333 | AddNote(diagnostics_, "'" + name + "' previously declared here", old->source); |
| 334 | } |
| 335 | } |
| 336 | |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 337 | /// Traverses the expression @p root_expr, performing symbol resolution and determining global |
| 338 | /// dependencies. |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 339 | void TraverseExpression(const ast::Expression* root_expr) { |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 340 | if (!root_expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 341 | return; |
| 342 | } |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 343 | |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 344 | utils::Vector<const ast::Expression*, 8> pending{root_expr}; |
Ben Clayton | 1131d31 | 2023-02-10 19:24:24 +0000 | [diff] [blame] | 345 | while (!pending.IsEmpty()) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 346 | ast::TraverseExpressions(pending.Pop(), diagnostics_, [&](const ast::Expression* expr) { |
Ben Clayton | 1131d31 | 2023-02-10 19:24:24 +0000 | [diff] [blame] | 347 | Switch( |
| 348 | expr, |
| 349 | [&](const ast::IdentifierExpression* e) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 350 | AddDependency(e->identifier, e->identifier->symbol); |
Ben Clayton | 1131d31 | 2023-02-10 19:24:24 +0000 | [diff] [blame] | 351 | if (auto* tmpl_ident = e->identifier->As<ast::TemplatedIdentifier>()) { |
| 352 | for (auto* arg : tmpl_ident->arguments) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 353 | pending.Push(arg); |
Ben Clayton | 1131d31 | 2023-02-10 19:24:24 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | }, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 357 | [&](const ast::CallExpression* call) { TraverseExpression(call->target); }, |
| 358 | [&](const ast::BitcastExpression* cast) { TraverseExpression(cast->type); }); |
Ben Clayton | 1131d31 | 2023-02-10 19:24:24 +0000 | [diff] [blame] | 359 | return ast::TraverseAction::Descend; |
| 360 | }); |
| 361 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 362 | } |
| 363 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 364 | /// Traverses the attribute list, performing symbol resolution and |
| 365 | /// determining global dependencies. |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 366 | void TraverseAttributes(utils::VectorRef<const ast::Attribute*> attrs) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 367 | for (auto* attr : attrs) { |
| 368 | TraverseAttribute(attr); |
| 369 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 370 | } |
| 371 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 372 | /// Traverses the attribute, performing symbol resolution and determining |
| 373 | /// global dependencies. |
| 374 | void TraverseAttribute(const ast::Attribute* attr) { |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 375 | bool handled = Switch( |
| 376 | attr, |
| 377 | [&](const ast::BindingAttribute* binding) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 378 | TraverseExpression(binding->expr); |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 379 | return true; |
| 380 | }, |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 381 | [&](const ast::BuiltinAttribute* builtin) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 382 | TraverseExpression(builtin->builtin); |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 383 | return true; |
| 384 | }, |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 385 | [&](const ast::GroupAttribute* group) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 386 | TraverseExpression(group->expr); |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 387 | return true; |
| 388 | }, |
dan sinclair | 155165c | 2022-10-17 14:35:43 +0000 | [diff] [blame] | 389 | [&](const ast::IdAttribute* id) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 390 | TraverseExpression(id->expr); |
dan sinclair | 155165c | 2022-10-17 14:35:43 +0000 | [diff] [blame] | 391 | return true; |
| 392 | }, |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 393 | [&](const ast::InterpolateAttribute* interpolate) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 394 | TraverseExpression(interpolate->type); |
| 395 | TraverseExpression(interpolate->sampling); |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 396 | return true; |
| 397 | }, |
dan sinclair | 3fd42ae | 2022-10-17 15:21:48 +0000 | [diff] [blame] | 398 | [&](const ast::LocationAttribute* loc) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 399 | TraverseExpression(loc->expr); |
dan sinclair | 3fd42ae | 2022-10-17 15:21:48 +0000 | [diff] [blame] | 400 | return true; |
| 401 | }, |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 402 | [&](const ast::StructMemberAlignAttribute* align) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 403 | TraverseExpression(align->expr); |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 404 | return true; |
| 405 | }, |
| 406 | [&](const ast::StructMemberSizeAttribute* size) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 407 | TraverseExpression(size->expr); |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 408 | return true; |
| 409 | }, |
| 410 | [&](const ast::WorkgroupAttribute* wg) { |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 411 | TraverseExpression(wg->x); |
| 412 | TraverseExpression(wg->y); |
| 413 | TraverseExpression(wg->z); |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 414 | return true; |
Ben Clayton | 63d0fab | 2023-03-06 15:43:16 +0000 | [diff] [blame] | 415 | }, |
| 416 | [&](const ast::InternalAttribute* i) { |
| 417 | for (auto* dep : i->dependencies) { |
| 418 | TraverseExpression(dep); |
| 419 | } |
| 420 | return true; |
dan sinclair | 72ac53e | 2022-10-14 18:39:04 +0000 | [diff] [blame] | 421 | }); |
| 422 | if (handled) { |
dan sinclair | 308c55d | 2022-10-14 18:27:35 +0000 | [diff] [blame] | 423 | return; |
| 424 | } |
dan sinclair | cb41b8f | 2022-09-26 15:54:55 +0000 | [diff] [blame] | 425 | |
Ben Clayton | 63d0fab | 2023-03-06 15:43:16 +0000 | [diff] [blame] | 426 | if (attr->IsAnyOf<ast::BuiltinAttribute, ast::DiagnosticAttribute, |
dan sinclair | a79c660 | 2023-02-21 18:49:40 +0000 | [diff] [blame] | 427 | ast::InterpolateAttribute, ast::InvariantAttribute, ast::MustUseAttribute, |
| 428 | ast::StageAttribute, ast::StrideAttribute, |
| 429 | ast::StructMemberOffsetAttribute>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 430 | return; |
| 431 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 432 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 433 | UnhandledNode(diagnostics_, attr); |
| 434 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 435 | |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 436 | /// Adds the dependency from @p from to @p to, erroring if @p to cannot be resolved. |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 437 | void AddDependency(const ast::Identifier* from, Symbol to) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 438 | auto* resolved = scope_stack_.Get(to); |
| 439 | if (!resolved) { |
dan sinclair | 5a5b7df | 2023-04-21 11:54:23 +0000 | [diff] [blame] | 440 | switch (to.Type()) { |
| 441 | case Symbol::BuiltinType::kNone: |
| 442 | graph_.resolved_identifiers.Add(from, UnresolvedIdentifier{to.Name()}); |
| 443 | break; |
| 444 | case Symbol::BuiltinType::kFunction: |
| 445 | graph_.resolved_identifiers.Add( |
| 446 | from, ResolvedIdentifier(to.BuiltinValue<builtin::Function>())); |
| 447 | break; |
| 448 | case Symbol::BuiltinType::kBuiltin: |
| 449 | graph_.resolved_identifiers.Add( |
| 450 | from, ResolvedIdentifier(to.BuiltinValue<builtin::Builtin>())); |
| 451 | break; |
| 452 | case Symbol::BuiltinType::kBuiltinValue: |
| 453 | graph_.resolved_identifiers.Add( |
| 454 | from, ResolvedIdentifier(to.BuiltinValue<builtin::BuiltinValue>())); |
| 455 | break; |
| 456 | case Symbol::BuiltinType::kAddressSpace: |
| 457 | graph_.resolved_identifiers.Add( |
| 458 | from, ResolvedIdentifier(to.BuiltinValue<builtin::AddressSpace>())); |
| 459 | break; |
| 460 | case Symbol::BuiltinType::kTexelFormat: |
| 461 | graph_.resolved_identifiers.Add( |
| 462 | from, ResolvedIdentifier(to.BuiltinValue<builtin::TexelFormat>())); |
| 463 | break; |
| 464 | case Symbol::BuiltinType::kAccess: |
| 465 | graph_.resolved_identifiers.Add( |
| 466 | from, ResolvedIdentifier(to.BuiltinValue<builtin::Access>())); |
| 467 | break; |
| 468 | case Symbol::BuiltinType::kInterpolationType: |
| 469 | graph_.resolved_identifiers.Add( |
| 470 | from, ResolvedIdentifier(to.BuiltinValue<builtin::InterpolationType>())); |
| 471 | break; |
| 472 | case Symbol::BuiltinType::kInterpolationSampling: |
| 473 | graph_.resolved_identifiers.Add( |
| 474 | from, |
| 475 | ResolvedIdentifier(to.BuiltinValue<builtin::InterpolationSampling>())); |
| 476 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 477 | } |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 478 | return; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 479 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 480 | |
Ben Clayton | 7c6e229 | 2022-11-23 21:04:25 +0000 | [diff] [blame] | 481 | if (auto global = globals_.Find(to); global && (*global)->node == resolved) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 482 | if (dependency_edges_.Add(DependencyEdge{current_global_, *global}, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 483 | DependencyInfo{from->source})) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 484 | current_global_->deps.Push(*global); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 487 | |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 488 | graph_.resolved_identifiers.Add(from, ResolvedIdentifier(resolved)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 491 | using VariableMap = utils::Hashmap<Symbol, const ast::Variable*, 32>; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 492 | const GlobalMap& globals_; |
| 493 | diag::List& diagnostics_; |
| 494 | DependencyGraph& graph_; |
| 495 | DependencyEdges& dependency_edges_; |
| 496 | |
James Price | 2cf32b1 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 497 | ScopeStack<Symbol, const ast::Node*> scope_stack_; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 498 | Global* current_global_ = nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | /// The global dependency analysis system |
| 502 | struct DependencyAnalysis { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 503 | public: |
| 504 | /// Constructor |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 505 | DependencyAnalysis(diag::List& diagnostics, DependencyGraph& graph) |
| 506 | : diagnostics_(diagnostics), graph_(graph) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 507 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 508 | /// Performs global dependency analysis on the module, emitting any errors to |
| 509 | /// #diagnostics. |
| 510 | /// @returns true if analysis found no errors, otherwise false. |
| 511 | bool Run(const ast::Module& module) { |
Ben Clayton | 7d04ced | 2022-08-03 08:43:15 +0000 | [diff] [blame] | 512 | // Reserve container memory |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 513 | graph_.resolved_identifiers.Reserve(module.GlobalDeclarations().Length()); |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 514 | sorted_.Reserve(module.GlobalDeclarations().Length()); |
Ben Clayton | 7d04ced | 2022-08-03 08:43:15 +0000 | [diff] [blame] | 515 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 516 | // Collect all the named globals from the AST module |
| 517 | GatherGlobals(module); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 518 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 519 | // Traverse the named globals to build the dependency graph |
| 520 | DetermineDependencies(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 521 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 522 | // Sort the globals into dependency order |
| 523 | SortGlobals(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 524 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 525 | // Dump the dependency graph if TINT_DUMP_DEPENDENCY_GRAPH is non-zero |
| 526 | DumpDependencyGraph(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 527 | |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 528 | graph_.ordered_globals = sorted_.Release(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 529 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 530 | return !diagnostics_.contains_errors(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 531 | } |
| 532 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 533 | private: |
| 534 | /// @param node the ast::Node of the global declaration |
| 535 | /// @returns the symbol of the global declaration node |
| 536 | /// @note will raise an ICE if the node is not a type, function or variable |
| 537 | /// declaration |
| 538 | Symbol SymbolOf(const ast::Node* node) const { |
| 539 | return Switch( |
| 540 | node, // |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 541 | [&](const ast::TypeDecl* td) { return td->name->symbol; }, |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 542 | [&](const ast::Function* func) { return func->name->symbol; }, |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 543 | [&](const ast::Variable* var) { return var->name->symbol; }, |
James Price | 98dc5a8 | 2023-02-04 12:20:55 +0000 | [diff] [blame] | 544 | [&](const ast::DiagnosticDirective*) { return Symbol(); }, |
Ben Clayton | 02791e9 | 2022-08-02 23:28:28 +0000 | [diff] [blame] | 545 | [&](const ast::Enable*) { return Symbol(); }, |
Ben Clayton | c98d57d | 2023-01-24 14:59:43 +0000 | [diff] [blame] | 546 | [&](const ast::ConstAssert*) { return Symbol(); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 547 | [&](Default) { |
| 548 | UnhandledNode(diagnostics_, node); |
| 549 | return Symbol{}; |
| 550 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 551 | } |
| 552 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 553 | /// @param node the ast::Node of the global declaration |
| 554 | /// @returns the name of the global declaration node |
| 555 | /// @note will raise an ICE if the node is not a type, function or variable |
| 556 | /// declaration |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 557 | std::string NameOf(const ast::Node* node) const { return SymbolOf(node).Name(); } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 558 | |
| 559 | /// @param node the ast::Node of the global declaration |
| 560 | /// @returns a string representation of the global declaration kind |
| 561 | /// @note will raise an ICE if the node is not a type, function or variable |
| 562 | /// declaration |
| 563 | std::string KindOf(const ast::Node* node) { |
| 564 | return Switch( |
Ben Clayton | c98d57d | 2023-01-24 14:59:43 +0000 | [diff] [blame] | 565 | node, // |
| 566 | [&](const ast::Struct*) { return "struct"; }, // |
| 567 | [&](const ast::Alias*) { return "alias"; }, // |
| 568 | [&](const ast::Function*) { return "function"; }, // |
| 569 | [&](const ast::Variable* v) { return v->Kind(); }, // |
| 570 | [&](const ast::ConstAssert*) { return "const_assert"; }, // |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 571 | [&](Default) { |
| 572 | UnhandledNode(diagnostics_, node); |
| 573 | return "<error>"; |
| 574 | }); |
| 575 | } |
| 576 | |
| 577 | /// Traverses `module`, collecting all the global declarations and populating |
| 578 | /// the #globals and #declaration_order fields. |
| 579 | void GatherGlobals(const ast::Module& module) { |
| 580 | for (auto* node : module.GlobalDeclarations()) { |
| 581 | auto* global = allocator_.Create(node); |
Ben Clayton | 02791e9 | 2022-08-02 23:28:28 +0000 | [diff] [blame] | 582 | if (auto symbol = SymbolOf(node); symbol.IsValid()) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 583 | globals_.Add(symbol, global); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 584 | } |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 585 | declaration_order_.Push(global); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | /// Walks the global declarations, determining the dependencies of each global |
| 590 | /// and adding these to each global's Global::deps field. |
| 591 | void DetermineDependencies() { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 592 | DependencyScanner scanner(globals_, diagnostics_, graph_, dependency_edges_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 593 | for (auto* global : declaration_order_) { |
| 594 | scanner.Scan(global); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /// Performs a depth-first traversal of `root`'s dependencies, calling `enter` |
| 599 | /// as the function decends into each dependency and `exit` when bubbling back |
| 600 | /// up towards the root. |
| 601 | /// @param enter is a function with the signature: `bool(Global*)`. The |
| 602 | /// `enter` function returns true if TraverseDependencies() should traverse |
| 603 | /// the dependency, otherwise it will be skipped. |
| 604 | /// @param exit is a function with the signature: `void(Global*)`. The `exit` |
| 605 | /// function is only called if the corresponding `enter` call returned true. |
| 606 | template <typename ENTER, typename EXIT> |
| 607 | void TraverseDependencies(const Global* root, ENTER&& enter, EXIT&& exit) { |
| 608 | // Entry is a single entry in the traversal stack. Entry points to a |
| 609 | // dep_idx'th dependency of Entry::global. |
| 610 | struct Entry { |
| 611 | const Global* global; // The parent global |
| 612 | size_t dep_idx; // The dependency index in `global->deps` |
| 613 | }; |
| 614 | |
| 615 | if (!enter(root)) { |
| 616 | return; |
| 617 | } |
| 618 | |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 619 | utils::Vector<Entry, 16> stack{Entry{root, 0}}; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 620 | while (true) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 621 | auto& entry = stack.Back(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 622 | // Have we exhausted the dependencies of entry.global? |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 623 | if (entry.dep_idx < entry.global->deps.Length()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 624 | // No, there's more dependencies to traverse. |
| 625 | auto& dep = entry.global->deps[entry.dep_idx]; |
| 626 | // Does the caller want to enter this dependency? |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 627 | if (enter(dep)) { // Yes. |
| 628 | stack.Push(Entry{dep, 0}); // Enter the dependency. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 629 | } else { |
| 630 | entry.dep_idx++; // No. Skip this node. |
| 631 | } |
| 632 | } else { |
| 633 | // Yes. Time to back up. |
| 634 | // Exit this global, pop the stack, and if there's another parent node, |
| 635 | // increment its dependency index, and loop again. |
| 636 | exit(entry.global); |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 637 | stack.Pop(); |
| 638 | if (stack.IsEmpty()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 639 | return; // All done. |
| 640 | } |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 641 | stack.Back().dep_idx++; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 642 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 645 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 646 | /// SortGlobals sorts the globals into dependency order, erroring if cyclic |
| 647 | /// dependencies are found. The sorted dependencies are assigned to #sorted. |
| 648 | void SortGlobals() { |
| 649 | if (diagnostics_.contains_errors()) { |
| 650 | return; // This code assumes there are no undeclared identifiers. |
| 651 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 652 | |
James Price | 098e3d8 | 2023-01-24 21:01:36 +0000 | [diff] [blame] | 653 | // Make sure all directives go before any other global declarations. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 654 | for (auto* global : declaration_order_) { |
James Price | 98dc5a8 | 2023-02-04 12:20:55 +0000 | [diff] [blame] | 655 | if (global->node->IsAnyOf<ast::DiagnosticDirective, ast::Enable>()) { |
James Price | 098e3d8 | 2023-01-24 21:01:36 +0000 | [diff] [blame] | 656 | sorted_.Add(global->node); |
Zhaoming Jiang | 418e873 | 2022-06-16 08:30:17 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | for (auto* global : declaration_order_) { |
James Price | 98dc5a8 | 2023-02-04 12:20:55 +0000 | [diff] [blame] | 661 | if (global->node->IsAnyOf<ast::DiagnosticDirective, ast::Enable>()) { |
James Price | 098e3d8 | 2023-01-24 21:01:36 +0000 | [diff] [blame] | 662 | // Skip directives here, as they are already added. |
Zhaoming Jiang | 418e873 | 2022-06-16 08:30:17 +0000 | [diff] [blame] | 663 | continue; |
| 664 | } |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 665 | utils::UniqueVector<const Global*, 8> stack; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 666 | TraverseDependencies( |
| 667 | global, |
| 668 | [&](const Global* g) { // Enter |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 669 | if (!stack.Add(g)) { |
| 670 | CyclicDependencyFound(g, stack.Release()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 673 | if (sorted_.Contains(g->node)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 674 | // Visited this global already. |
| 675 | // stack was pushed, but exit() will not be called when we return |
| 676 | // false, so pop here. |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 677 | stack.Pop(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
| 680 | return true; |
| 681 | }, |
| 682 | [&](const Global* g) { // Exit. Only called if Enter returned true. |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 683 | sorted_.Add(g->node); |
| 684 | stack.Pop(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 685 | }); |
| 686 | |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 687 | sorted_.Add(global->node); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 688 | |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 689 | if (TINT_UNLIKELY(!stack.IsEmpty())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 690 | // Each stack.push() must have a corresponding stack.pop_back(). |
| 691 | TINT_ICE(Resolver, diagnostics_) |
| 692 | << "stack not empty after returning from TraverseDependencies()"; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /// DepInfoFor() looks up the global dependency information for the dependency |
| 698 | /// of global `from` depending on `to`. |
| 699 | /// @note will raise an ICE if the edge is not found. |
| 700 | DependencyInfo DepInfoFor(const Global* from, const Global* to) const { |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 701 | auto info = dependency_edges_.Find(DependencyEdge{from, to}); |
| 702 | if (TINT_LIKELY(info)) { |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 703 | return *info; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 704 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 705 | TINT_ICE(Resolver, diagnostics_) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 706 | << "failed to find dependency info for edge: '" << NameOf(from->node) << "' -> '" |
| 707 | << NameOf(to->node) << "'"; |
| 708 | return {}; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 709 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 710 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 711 | /// CyclicDependencyFound() emits an error diagnostic for a cyclic dependency. |
| 712 | /// @param root is the global that starts the cyclic dependency, which must be |
| 713 | /// found in `stack`. |
| 714 | /// @param stack is the global dependency stack that contains a loop. |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 715 | void CyclicDependencyFound(const Global* root, utils::VectorRef<const Global*> stack) { |
dan sinclair | b23cda4 | 2023-02-28 17:31:54 +0000 | [diff] [blame] | 716 | utils::StringStream msg; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 717 | msg << "cyclic dependency found: "; |
| 718 | constexpr size_t kLoopNotStarted = ~0u; |
| 719 | size_t loop_start = kLoopNotStarted; |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 720 | for (size_t i = 0; i < stack.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 721 | auto* e = stack[i]; |
| 722 | if (loop_start == kLoopNotStarted && e == root) { |
| 723 | loop_start = i; |
| 724 | } |
| 725 | if (loop_start != kLoopNotStarted) { |
| 726 | msg << "'" << NameOf(e->node) << "' -> "; |
| 727 | } |
| 728 | } |
| 729 | msg << "'" << NameOf(root->node) << "'"; |
| 730 | AddError(diagnostics_, msg.str(), root->node->source); |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 731 | for (size_t i = loop_start; i < stack.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 732 | auto* from = stack[i]; |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 733 | auto* to = (i + 1 < stack.Length()) ? stack[i + 1] : stack[loop_start]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 734 | auto info = DepInfoFor(from, to); |
| 735 | AddNote(diagnostics_, |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 736 | KindOf(from->node) + " '" + NameOf(from->node) + "' references " + |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 737 | KindOf(to->node) + " '" + NameOf(to->node) + "' here", |
| 738 | info.source); |
| 739 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 740 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 741 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 742 | void DumpDependencyGraph() { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 743 | #if TINT_DUMP_DEPENDENCY_GRAPH == 0 |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 744 | if ((true)) { |
| 745 | return; |
| 746 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 747 | #endif // TINT_DUMP_DEPENDENCY_GRAPH |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 748 | printf("=========================\n"); |
| 749 | printf("------ declaration ------ \n"); |
| 750 | for (auto* global : declaration_order_) { |
| 751 | printf("%s\n", NameOf(global->node).c_str()); |
| 752 | } |
| 753 | printf("------ dependencies ------ \n"); |
| 754 | for (auto* node : sorted_) { |
| 755 | auto symbol = SymbolOf(node); |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 756 | auto* global = *globals_.Find(symbol); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 757 | printf("%s depends on:\n", symbol.Name().c_str()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 758 | for (auto* dep : global->deps) { |
| 759 | printf(" %s\n", NameOf(dep->node).c_str()); |
| 760 | } |
| 761 | } |
| 762 | printf("=========================\n"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 763 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 764 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 765 | /// Program diagnostics |
| 766 | diag::List& diagnostics_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 767 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 768 | /// The resulting dependency graph |
| 769 | DependencyGraph& graph_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 770 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 771 | /// Allocator of Globals |
| 772 | utils::BlockAllocator<Global> allocator_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 773 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 774 | /// Global map, keyed by name. Populated by GatherGlobals(). |
| 775 | GlobalMap globals_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 776 | |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 777 | /// Map of DependencyEdge to DependencyInfo. Populated by DetermineDependencies(). |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 778 | DependencyEdges dependency_edges_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 779 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 780 | /// Globals in declaration order. Populated by GatherGlobals(). |
Ben Clayton | 9418152 | 2022-11-09 20:55:33 +0000 | [diff] [blame] | 781 | utils::Vector<Global*, 64> declaration_order_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 782 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 783 | /// Globals in sorted dependency order. Populated by SortGlobals(). |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 784 | utils::UniqueVector<const ast::Node*, 64> sorted_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
| 787 | } // namespace |
| 788 | |
| 789 | DependencyGraph::DependencyGraph() = default; |
| 790 | DependencyGraph::DependencyGraph(DependencyGraph&&) = default; |
| 791 | DependencyGraph::~DependencyGraph() = default; |
| 792 | |
| 793 | bool DependencyGraph::Build(const ast::Module& module, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 794 | diag::List& diagnostics, |
| 795 | DependencyGraph& output) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 796 | DependencyAnalysis da{diagnostics, output}; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 797 | return da.Run(module); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 798 | } |
| 799 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 800 | std::string ResolvedIdentifier::String(diag::List& diagnostics) const { |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 801 | if (auto* node = Node()) { |
| 802 | return Switch( |
| 803 | node, |
| 804 | [&](const ast::TypeDecl* n) { // |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 805 | return "type '" + n->name->symbol.Name() + "'"; |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 806 | }, |
| 807 | [&](const ast::Var* n) { // |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 808 | return "var '" + n->name->symbol.Name() + "'"; |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 809 | }, |
Ben Clayton | c0c8abc | 2023-03-02 17:36:37 +0000 | [diff] [blame] | 810 | [&](const ast::Let* n) { // |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 811 | return "let '" + n->name->symbol.Name() + "'"; |
Ben Clayton | c0c8abc | 2023-03-02 17:36:37 +0000 | [diff] [blame] | 812 | }, |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 813 | [&](const ast::Const* n) { // |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 814 | return "const '" + n->name->symbol.Name() + "'"; |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 815 | }, |
| 816 | [&](const ast::Override* n) { // |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 817 | return "override '" + n->name->symbol.Name() + "'"; |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 818 | }, |
| 819 | [&](const ast::Function* n) { // |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 820 | return "function '" + n->name->symbol.Name() + "'"; |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 821 | }, |
Ben Clayton | fdef033 | 2023-02-21 21:07:35 +0000 | [diff] [blame] | 822 | [&](const ast::Parameter* n) { // |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 823 | return "parameter '" + n->name->symbol.Name() + "'"; |
Ben Clayton | fdef033 | 2023-02-21 21:07:35 +0000 | [diff] [blame] | 824 | }, |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 825 | [&](Default) { |
| 826 | TINT_UNREACHABLE(Resolver, diagnostics) |
| 827 | << "unhandled ast::Node: " << node->TypeInfo().name; |
| 828 | return "<unknown>"; |
| 829 | }); |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 830 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 831 | if (auto builtin_fn = BuiltinFunction(); builtin_fn != builtin::Function::kNone) { |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 832 | return "builtin function '" + utils::ToString(builtin_fn) + "'"; |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 833 | } |
dan sinclair | ef30aa4 | 2023-02-19 04:28:04 +0000 | [diff] [blame] | 834 | if (auto builtin_ty = BuiltinType(); builtin_ty != builtin::Builtin::kUndefined) { |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 835 | return "builtin type '" + utils::ToString(builtin_ty) + "'"; |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 836 | } |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 837 | if (auto builtin_val = BuiltinValue(); builtin_val != builtin::BuiltinValue::kUndefined) { |
| 838 | return "builtin value '" + utils::ToString(builtin_val) + "'"; |
| 839 | } |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 840 | if (auto access = Access(); access != builtin::Access::kUndefined) { |
Ben Clayton | 031e2f5 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 841 | return "access '" + utils::ToString(access) + "'"; |
| 842 | } |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 843 | if (auto addr = AddressSpace(); addr != builtin::AddressSpace::kUndefined) { |
Ben Clayton | 031e2f5 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 844 | return "address space '" + utils::ToString(addr) + "'"; |
| 845 | } |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 846 | if (auto type = InterpolationType(); type != builtin::InterpolationType::kUndefined) { |
| 847 | return "interpolation type '" + utils::ToString(type) + "'"; |
| 848 | } |
| 849 | if (auto smpl = InterpolationSampling(); smpl != builtin::InterpolationSampling::kUndefined) { |
| 850 | return "interpolation sampling '" + utils::ToString(smpl) + "'"; |
| 851 | } |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 852 | if (auto fmt = TexelFormat(); fmt != builtin::TexelFormat::kUndefined) { |
Ben Clayton | 031e2f5 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 853 | return "texel format '" + utils::ToString(fmt) + "'"; |
| 854 | } |
Ben Clayton | afc53fa | 2023-02-22 17:15:53 +0000 | [diff] [blame] | 855 | if (auto* unresolved = Unresolved()) { |
| 856 | return "unresolved identifier '" + unresolved->name + "'"; |
| 857 | } |
| 858 | |
Ben Clayton | 0ddddb0 | 2023-02-09 15:35:27 +0000 | [diff] [blame] | 859 | TINT_UNREACHABLE(Resolver, diagnostics) << "unhandled ResolvedIdentifier"; |
| 860 | return "<unknown>"; |
Ben Clayton | cf0e930 | 2023-02-08 15:18:43 +0000 | [diff] [blame] | 861 | } |
| 862 | |
dan sinclair | d209379 | 2022-04-07 17:45:45 +0000 | [diff] [blame] | 863 | } // namespace tint::resolver |