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); |
| 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) { |
| 35 auto entry = fSymbols.find(name); |
| 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); |
| 48 for (std::shared_ptr<FunctionDeclaration> prev : previousFunctio
ns) { |
| 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; |
| 74 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 75 std::shared_ptr<Symbol> oldSymbol = existing->second; |
| 76 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 77 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 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)); |
| 81 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { |
| 82 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 83 for (auto f : std::static_pointer_cast<UnresolvedFunction>(oldSy
mbol)->fFunctions) { |
| 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 } |
| 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 |