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