OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #ifndef SKSL_SYMBOLTABLE | |
9 #define SKSL_SYMBOLTABLE | |
10 | |
11 #include <memory> | |
12 #include <unordered_map> | |
13 #include "SkSLErrorReporter.h" | |
14 #include "SkSLSymbol.h" | |
15 #include "SkSLUnresolvedFunction.h" | |
16 | |
17 namespace SkSL { | |
18 | |
19 std::vector<std::shared_ptr<FunctionDeclaration>> get_functions(std::shared_ptr< Symbol> s); | |
dogben
2016/06/21 21:52:50
nit: make this a static method of SymbolTable?
| |
20 | |
21 /** | |
22 * Maps identifiers to symbols. Functions, in particular, are mapped to either F unctionDeclaration | |
23 * or UnresolvedFunction depending on whether they are overloaded or not. | |
24 */ | |
25 class SymbolTable { | |
26 public: | |
27 SymbolTable(ErrorReporter& errorReporter) | |
28 : fErrorReporter(errorReporter) {} | |
29 | |
30 SymbolTable(std::shared_ptr<SymbolTable> parent, ErrorReporter& errorReporte r) | |
31 : fParent(parent) | |
32 , fErrorReporter(errorReporter) {} | |
33 | |
34 std::shared_ptr<Symbol> operator[](std::string name) { | |
dogben
2016/06/21 21:52:51
Can this return const ref?
IIUC, every creation a
| |
35 auto entry = fSymbols.find(name); | |
dogben
2016/06/21 21:52:50
nit: const ref
| |
36 if (entry == fSymbols.end()) { | |
37 if (fParent != nullptr) { | |
38 return (*fParent)[name]; | |
39 } | |
40 return nullptr; | |
41 } | |
42 if (fParent != nullptr) { | |
43 auto functions = get_functions(entry->second); | |
44 if (functions.size() > 0) { | |
45 bool modified = false; | |
46 std::shared_ptr<Symbol> previous = (*fParent)[name]; | |
47 auto previousFunctions = get_functions(entry->second); | |
dogben
2016/06/21 21:52:50
Should this be get_functions(previous)?
| |
48 for (std::shared_ptr<FunctionDeclaration> prev : previousFunctio ns) { | |
dogben
2016/06/21 21:52:50
nit: const ref
Next loop also.
| |
49 bool found = false; | |
50 for (std::shared_ptr<FunctionDeclaration> current : function s) { | |
51 if (current->matches(prev)) { | |
52 found = true; | |
53 break; | |
54 } | |
55 } | |
56 if (!found) { | |
57 functions.push_back(prev); | |
58 modified = true; | |
59 } | |
60 } | |
61 if (modified) { | |
62 ASSERT(functions.count() > 1); | |
63 return std::shared_ptr<Symbol>(new UnresolvedFunction(functi ons)); | |
64 } | |
65 } | |
66 } | |
67 return entry->second; | |
68 } | |
69 | |
70 void add(std::string name, std::shared_ptr<Symbol> symbol) { | |
71 auto existing = fSymbols.find(name); | |
72 if (existing == fSymbols.end()) { | |
73 fSymbols[name] = symbol; | |
dogben
2016/06/21 21:52:51
nit: std::move(symbol), everywhere in this functio
| |
74 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { | |
75 std::shared_ptr<Symbol> oldSymbol = existing->second; | |
dogben
2016/06/21 21:52:51
nit: const ref?
Or regular ref and use .reset bel
| |
76 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { | |
77 std::vector<std::shared_ptr<FunctionDeclaration>> functions; | |
dogben
2016/06/21 21:52:50
nit: Construct with capacity 2?
Or use {} initial
| |
78 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(oldSymbol)); | |
79 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(symbol)); | |
80 fSymbols[name] = std::shared_ptr<Symbol>(new UnresolvedFunction( functions)); | |
dogben
2016/06/21 21:52:51
nit: std::move, two places
| |
81 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { | |
82 std::vector<std::shared_ptr<FunctionDeclaration>> functions; | |
dogben
2016/06/21 21:52:50
nit: Construct with capacity std::static_pointer_c
| |
83 for (auto f : std::static_pointer_cast<UnresolvedFunction>(oldSy mbol)->fFunctions) { | |
dogben
2016/06/21 21:52:51
nit: const auto&
Or:
const auto& oldFunctions = s
| |
84 functions.push_back(f); | |
85 } | |
86 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(symbol)); | |
87 fSymbols[name] = std::shared_ptr<Symbol>(new UnresolvedFunction( functions)); | |
88 } | |
dogben
2016/06/21 21:52:50
else error?
| |
89 } else { | |
90 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was a lready defined"); | |
91 } | |
92 } | |
93 | |
94 const std::shared_ptr<SymbolTable> fParent; | |
95 | |
96 private: | |
97 std::unordered_map<std::string, std::shared_ptr<Symbol>> fSymbols; | |
98 | |
99 ErrorReporter& fErrorReporter; | |
100 }; | |
101 | |
102 } // namespace | |
103 | |
104 #endif | |
OLD | NEW |