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 #include "SkSLSymbolTable.h" |
| 9 |
| 10 namespace SkSL { |
| 11 |
| 12 std::vector<std::shared_ptr<FunctionDeclaration>> SymbolTable::GetFunctions( |
| 13 const std::shar
ed_ptr<Symbol>& s) { |
| 14 switch (s->fKind) { |
| 15 case Symbol::kFunctionDeclaration_Kind: |
| 16 return { std::static_pointer_cast<FunctionDeclaration>(s) }; |
| 17 case Symbol::kUnresolvedFunction_Kind: |
| 18 return ((UnresolvedFunction&) *s).fFunctions; |
| 19 default: |
| 20 return { }; |
| 21 } |
| 22 } |
| 23 |
| 24 std::shared_ptr<Symbol> SymbolTable::operator[](const std::string& name) { |
| 25 const auto& entry = fSymbols.find(name); |
| 26 if (entry == fSymbols.end()) { |
| 27 if (fParent) { |
| 28 return (*fParent)[name]; |
| 29 } |
| 30 return nullptr; |
| 31 } |
| 32 if (fParent) { |
| 33 auto functions = GetFunctions(entry->second); |
| 34 if (functions.size() > 0) { |
| 35 bool modified = false; |
| 36 std::shared_ptr<Symbol> previous = (*fParent)[name]; |
| 37 if (previous) { |
| 38 auto previousFunctions = GetFunctions(previous); |
| 39 for (const std::shared_ptr<FunctionDeclaration>& prev : previous
Functions) { |
| 40 bool found = false; |
| 41 for (const std::shared_ptr<FunctionDeclaration>& current : f
unctions) { |
| 42 if (current->matches(*prev)) { |
| 43 found = true; |
| 44 break; |
| 45 } |
| 46 } |
| 47 if (!found) { |
| 48 functions.push_back(prev); |
| 49 modified = true; |
| 50 } |
| 51 } |
| 52 if (modified) { |
| 53 ASSERT(functions.size() > 1); |
| 54 return std::shared_ptr<Symbol>(new UnresolvedFunction(functi
ons)); |
| 55 } |
| 56 } |
| 57 } |
| 58 } |
| 59 return entry->second; |
| 60 } |
| 61 |
| 62 void SymbolTable::add(const std::string& name, std::shared_ptr<Symbol> symbol) { |
| 63 const auto& existing = fSymbols.find(name); |
| 64 if (existing == fSymbols.end()) { |
| 65 fSymbols[name] = symbol; |
| 66 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 67 const std::shared_ptr<Symbol>& oldSymbol = existing->second; |
| 68 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 69 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 70 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(oldSymbol)); |
| 71 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(symbol)); |
| 72 fSymbols[name].reset(new UnresolvedFunction(std::move(functions)
)); |
| 73 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { |
| 74 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 75 for (const auto& f : ((UnresolvedFunction&) *oldSymbol).fFunctio
ns) { |
| 76 functions.push_back(f); |
| 77 } |
| 78 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(symbol)); |
| 79 fSymbols[name].reset(new UnresolvedFunction(std::move(functions)
)); |
| 80 } |
| 81 } else { |
| 82 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was a
lready defined"); |
| 83 } |
| 84 } |
| 85 } // namespace |
OLD | NEW |