Chromium Code Reviews| 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 std::static_pointer_cast<UnresolvedFunction>(s)->fFunctions; | |
|
dogben
2016/06/28 02:14:55
nit: ((UnresolvedFunction*)s.get())->fFunctions av
| |
| 19 default: | |
| 20 return { }; | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 std::shared_ptr<Symbol> SymbolTable::operator[](const std::string& name) { | |
| 25 auto entry = fSymbols.find(name); | |
|
dogben
2016/06/28 02:14:55
nit: const ref
| |
| 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 (std::shared_ptr<FunctionDeclaration> prev : previousFunctio ns) { | |
|
dogben
2016/06/28 02:14:55
nit: const ref
| |
| 40 bool found = false; | |
| 41 for (std::shared_ptr<FunctionDeclaration> current : function s) { | |
|
dogben
2016/06/28 02:14:55
nit: const ref
| |
| 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 auto existing = fSymbols.find(name); | |
|
dogben
2016/06/28 02:14:55
nit: const ref
| |
| 64 if (existing == fSymbols.end()) { | |
| 65 fSymbols[name] = symbol; | |
| 66 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { | |
| 67 std::shared_ptr<Symbol> oldSymbol = existing->second; | |
|
dogben
2016/06/28 02:14:55
nit: const ref
| |
| 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] = std::shared_ptr<Symbol>(new UnresolvedFunction( functions)); | |
|
dogben
2016/06/28 02:14:55
nit: fSymbols[name].reset(new UnresolvedFunction(s
| |
| 73 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { | |
| 74 std::vector<std::shared_ptr<FunctionDeclaration>> functions; | |
| 75 for (auto f : std::static_pointer_cast<UnresolvedFunction>(oldSy mbol)->fFunctions) { | |
|
dogben
2016/06/28 02:14:55
nit: const ref
| |
| 76 functions.push_back(f); | |
| 77 } | |
| 78 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(symbol)); | |
| 79 fSymbols[name] = std::shared_ptr<Symbol>(new UnresolvedFunction( functions)); | |
| 80 } | |
| 81 } else { | |
| 82 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was a lready defined"); | |
| 83 } | |
| 84 } | |
| 85 } // namespace | |
| OLD | NEW |