| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSLSymbolTable.h" | 8 #include "SkSLSymbolTable.h" |
| 9 #include "SkSLUnresolvedFunction.h" | |
| 10 | 9 |
| 11 namespace SkSL { | 10 namespace SkSL { |
| 12 | 11 |
| 13 std::vector<const FunctionDeclaration*> SymbolTable::GetFunctions(const Symbol&
s) { | 12 std::vector<std::shared_ptr<FunctionDeclaration>> SymbolTable::GetFunctions( |
| 14 switch (s.fKind) { | 13 const std::shar
ed_ptr<Symbol>& s) { |
| 14 switch (s->fKind) { |
| 15 case Symbol::kFunctionDeclaration_Kind: | 15 case Symbol::kFunctionDeclaration_Kind: |
| 16 return { &((FunctionDeclaration&) s) }; | 16 return { std::static_pointer_cast<FunctionDeclaration>(s) }; |
| 17 case Symbol::kUnresolvedFunction_Kind: | 17 case Symbol::kUnresolvedFunction_Kind: |
| 18 return ((UnresolvedFunction&) s).fFunctions; | 18 return ((UnresolvedFunction&) *s).fFunctions; |
| 19 default: | 19 default: |
| 20 return { }; | 20 return { }; |
| 21 } | 21 } |
| 22 } | 22 } |
| 23 | 23 |
| 24 const Symbol* SymbolTable::operator[](const std::string& name) { | 24 std::shared_ptr<Symbol> SymbolTable::operator[](const std::string& name) { |
| 25 const auto& entry = fSymbols.find(name); | 25 const auto& entry = fSymbols.find(name); |
| 26 if (entry == fSymbols.end()) { | 26 if (entry == fSymbols.end()) { |
| 27 if (fParent) { | 27 if (fParent) { |
| 28 return (*fParent)[name]; | 28 return (*fParent)[name]; |
| 29 } | 29 } |
| 30 return nullptr; | 30 return nullptr; |
| 31 } | 31 } |
| 32 if (fParent) { | 32 if (fParent) { |
| 33 auto functions = GetFunctions(*entry->second); | 33 auto functions = GetFunctions(entry->second); |
| 34 if (functions.size() > 0) { | 34 if (functions.size() > 0) { |
| 35 bool modified = false; | 35 bool modified = false; |
| 36 const Symbol* previous = (*fParent)[name]; | 36 std::shared_ptr<Symbol> previous = (*fParent)[name]; |
| 37 if (previous) { | 37 if (previous) { |
| 38 auto previousFunctions = GetFunctions(*previous); | 38 auto previousFunctions = GetFunctions(previous); |
| 39 for (const FunctionDeclaration* prev : previousFunctions) { | 39 for (const std::shared_ptr<FunctionDeclaration>& prev : previous
Functions) { |
| 40 bool found = false; | 40 bool found = false; |
| 41 for (const FunctionDeclaration* current : functions) { | 41 for (const std::shared_ptr<FunctionDeclaration>& current : f
unctions) { |
| 42 if (current->matches(*prev)) { | 42 if (current->matches(*prev)) { |
| 43 found = true; | 43 found = true; |
| 44 break; | 44 break; |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 if (!found) { | 47 if (!found) { |
| 48 functions.push_back(prev); | 48 functions.push_back(prev); |
| 49 modified = true; | 49 modified = true; |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 if (modified) { | 52 if (modified) { |
| 53 ASSERT(functions.size() > 1); | 53 ASSERT(functions.size() > 1); |
| 54 return this->takeOwnership(new UnresolvedFunction(functions)
); | 54 return std::shared_ptr<Symbol>(new UnresolvedFunction(functi
ons)); |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 return entry->second; | 59 return entry->second; |
| 60 } | 60 } |
| 61 | 61 |
| 62 Symbol* SymbolTable::takeOwnership(Symbol* s) { | 62 void SymbolTable::add(const std::string& name, std::shared_ptr<Symbol> symbol) { |
| 63 fOwnedPointers.push_back(std::unique_ptr<Symbol>(s)); | 63 const auto& existing = fSymbols.find(name); |
| 64 return s; | 64 if (existing == fSymbols.end()) { |
| 65 } | 65 fSymbols[name] = symbol; |
| 66 | 66 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 67 void SymbolTable::add(const std::string& name, std::unique_ptr<Symbol> symbol) { | 67 const std::shared_ptr<Symbol>& oldSymbol = existing->second; |
| 68 this->addWithoutOwnership(name, symbol.get()); | 68 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 69 fOwnedPointers.push_back(std::move(symbol)); | 69 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 70 } | 70 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(oldSymbol)); |
| 71 | 71 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(symbol)); |
| 72 void SymbolTable::addWithoutOwnership(const std::string& name, const Symbol* sym
bol) { | 72 fSymbols[name].reset(new UnresolvedFunction(std::move(functions)
)); |
| 73 const auto& existing = fSymbols.find(name); | 73 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { |
| 74 if (existing == fSymbols.end()) { | 74 std::vector<std::shared_ptr<FunctionDeclaration>> functions; |
| 75 fSymbols[name] = symbol; | 75 for (const auto& f : ((UnresolvedFunction&) *oldSymbol).fFunctio
ns) { |
| 76 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { | 76 functions.push_back(f); |
| 77 const Symbol* oldSymbol = existing->second; | 77 } |
| 78 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { | 78 functions.push_back(std::static_pointer_cast<FunctionDeclaration
>(symbol)); |
| 79 std::vector<const FunctionDeclaration*> functions; | 79 fSymbols[name].reset(new UnresolvedFunction(std::move(functions)
)); |
| 80 functions.push_back((const FunctionDeclaration*) oldSymbol); | |
| 81 functions.push_back((const FunctionDeclaration*) symbol); | |
| 82 UnresolvedFunction* u = new UnresolvedFunction(std::move(functions))
; | |
| 83 fSymbols[name] = u; | |
| 84 this->takeOwnership(u); | |
| 85 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { | |
| 86 std::vector<const FunctionDeclaration*> functions; | |
| 87 for (const auto* f : ((UnresolvedFunction&) *oldSymbol).fFunctions)
{ | |
| 88 functions.push_back(f); | |
| 89 } | 80 } |
| 90 functions.push_back((const FunctionDeclaration*) symbol); | 81 } else { |
| 91 UnresolvedFunction* u = new UnresolvedFunction(std::move(functions))
; | 82 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was a
lready defined"); |
| 92 fSymbols[name] = u; | |
| 93 this->takeOwnership(u); | |
| 94 } | 83 } |
| 95 } else { | |
| 96 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was alrea
dy defined"); | |
| 97 } | 84 } |
| 98 } | |
| 99 | |
| 100 } // namespace | 85 } // namespace |
| OLD | NEW |