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 "SkSLSymbol.h" |
| 14 #include "SkSLUnresolvedFunction.h" |
| 15 |
| 16 namespace SkSL { |
| 17 |
| 18 std::vector<std::shared_ptr<FunctionDeclaration>> get_functions(std::shared_ptr<
Symbol> s); |
| 19 |
| 20 /** |
| 21 * Maps identifiers to symbols. Functions, in particular, are mapped to either F
unctionDeclaration |
| 22 * or UnresolvedFunction depending on whether they are overloaded or not. |
| 23 */ |
| 24 class SymbolTable { |
| 25 public: |
| 26 SymbolTable() {} |
| 27 |
| 28 SymbolTable(std::shared_ptr<SymbolTable> parent) |
| 29 : fParent(parent) {} |
| 30 |
| 31 std::shared_ptr<Symbol> operator[](std::string name) { |
| 32 auto entry = fSymbols.find(name); |
| 33 if (entry == fSymbols.end()) { |
| 34 if (fParent != nullptr) { |
| 35 return (*fParent)[name]; |
| 36 } |
| 37 return nullptr; |
| 38 } |
| 39 if (fParent != nullptr) { |
| 40 auto functions = get_functions(entry->second); |
| 41 if (functions.size() > 0) { |
| 42 bool modified = false; |
| 43 std::shared_ptr<Symbol> previous = (*fParent)[name]; |
| 44 auto previousFunctions = get_functions(entry->second); |
| 45 for (std::shared_ptr<FunctionDeclaration> prev : previousFunctio
ns) { |
| 46 bool found = false; |
| 47 for (std::shared_ptr<FunctionDeclaration> current : function
s) { |
| 48 if (current->matches(prev)) { |
| 49 found = true; |
| 50 break; |
| 51 } |
| 52 } |
| 53 if (!found) { |
| 54 functions.push_back(prev); |
| 55 modified = true; |
| 56 } |
| 57 } |
| 58 if (modified) { |
| 59 ASSERT(functions.count() > 1); |
| 60 return std::shared_ptr<Symbol>(new UnresolvedFunction(functi
ons)); |
| 61 } |
| 62 } |
| 63 } |
| 64 return entry->second; |
| 65 } |
| 66 |
| 67 void add(std::string name, std::shared_ptr<Symbol> symbol) { |
| 68 // FIXME need to report duplicate symbols |
| 69 auto existing = fSymbols.find(name); |
| 70 if (existing == fSymbols.end()) { |
| 71 fSymbols[name] = symbol; |
| 72 } else { |
| 73 ASSERT(symbol->fKind == Symbol::kFunctionDeclaration_Kind); |
| 74 std::shared_ptr<Symbol> oldSymbol = existing->second; |
| 75 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 76 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 77 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(oldSymbol)); |
| 78 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(symbol)); |
| 79 fSymbols[name] = std::shared_ptr<Symbol>(new UnresolvedFunction(
functions)); |
| 80 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { |
| 81 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 82 for (auto f : std::static_pointer_cast<UnresolvedFunction>(oldSy
mbol)->fFunctions) { |
| 83 functions.push_back(f); |
| 84 } |
| 85 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(symbol)); |
| 86 fSymbols[name] = std::shared_ptr<Symbol>(new UnresolvedFunction(
functions)); |
| 87 } |
| 88 } |
| 89 } |
| 90 |
| 91 const std::shared_ptr<SymbolTable> fParent; |
| 92 |
| 93 private: |
| 94 std::unordered_map<std::string, std::shared_ptr<Symbol>> fSymbols; |
| 95 }; |
| 96 |
| 97 } // namespace |
| 98 |
| 99 #endif |
OLD | NEW |