• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SRC_RESOLVER_DEPENDENCY_GRAPH_H_
16 #define SRC_RESOLVER_DEPENDENCY_GRAPH_H_
17 
18 #include <unordered_map>
19 #include <vector>
20 
21 #include "src/ast/module.h"
22 #include "src/diagnostic/diagnostic.h"
23 
24 namespace tint {
25 namespace resolver {
26 
27 /// DependencyGraph holds information about module-scope declaration dependency
28 /// analysis and symbol resolutions.
29 struct DependencyGraph {
30   /// Constructor
31   DependencyGraph();
32   /// Move-constructor
33   DependencyGraph(DependencyGraph&&);
34   /// Destructor
35   ~DependencyGraph();
36 
37   /// Build() performs symbol resolution and dependency analysis on `module`,
38   /// populating `output` with the resulting dependency graph.
39   /// @param module the AST module to analyse
40   /// @param symbols the symbol table
41   /// @param diagnostics the diagnostic list to populate with errors / warnings
42   /// @param output the resulting DependencyGraph
43   /// @param allow_out_of_order_decls if true, then out-of-order declarations
44   /// are not considered an error
45   /// @returns true on success, false on error
46   static bool Build(const ast::Module& module,
47                     const SymbolTable& symbols,
48                     diag::List& diagnostics,
49                     DependencyGraph& output,
50                     bool allow_out_of_order_decls);
51 
52   /// All globals in dependency-sorted order.
53   std::vector<const ast::Node*> ordered_globals;
54 
55   /// Map of ast::IdentifierExpression or ast::TypeName to a type, function, or
56   /// variable that declares the symbol.
57   std::unordered_map<const ast::Node*, const ast::Node*> resolved_symbols;
58 
59   /// Map of ast::Variable to a type, function, or variable that is shadowed by
60   /// the variable key. A declaration (X) shadows another (Y) if X and Y use
61   /// the same symbol, and X is declared in a sub-scope of the scope that
62   /// declares Y.
63   std::unordered_map<const ast::Variable*, const ast::Node*> shadows;
64 };
65 
66 }  // namespace resolver
67 }  // namespace tint
68 
69 #endif  // SRC_RESOLVER_DEPENDENCY_GRAPH_H_
70