ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 8 | #include "SkSLSymbolTable.h" |
| 9 | #include "SkSLUnresolvedFunction.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 10 | |
| 11 | namespace SkSL { |
| 12 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 13 | std::vector<const FunctionDeclaration*> SymbolTable::GetFunctions(const Symbol& s) { |
| 14 | switch (s.fKind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 15 | case Symbol::kFunctionDeclaration_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 16 | return { &((FunctionDeclaration&) s) }; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 17 | case Symbol::kUnresolvedFunction_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 18 | return ((UnresolvedFunction&) s).fFunctions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | default: |
ethannicholas | 0730be7 | 2016-09-01 07:59:02 -0700 | [diff] [blame] | 20 | return std::vector<const FunctionDeclaration*>(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 24 | const Symbol* SymbolTable::operator[](const String& name) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 33 | auto functions = GetFunctions(*entry->second); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 34 | if (functions.size() > 0) { |
| 35 | bool modified = false; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 36 | const Symbol* previous = (*fParent)[name]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | if (previous) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 38 | auto previousFunctions = GetFunctions(*previous); |
| 39 | for (const FunctionDeclaration* prev : previousFunctions) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 40 | bool found = false; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 41 | for (const FunctionDeclaration* current : functions) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 54 | return this->takeOwnership(new UnresolvedFunction(functions)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return entry->second; |
| 60 | } |
| 61 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 62 | Symbol* SymbolTable::takeOwnership(Symbol* s) { |
| 63 | fOwnedPointers.push_back(std::unique_ptr<Symbol>(s)); |
| 64 | return s; |
| 65 | } |
| 66 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 67 | void SymbolTable::add(const String& name, std::unique_ptr<Symbol> symbol) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 68 | this->addWithoutOwnership(name, symbol.get()); |
| 69 | fOwnedPointers.push_back(std::move(symbol)); |
| 70 | } |
| 71 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 72 | void SymbolTable::addWithoutOwnership(const String& name, const Symbol* symbol) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 73 | const auto& existing = fSymbols.find(name); |
| 74 | if (existing == fSymbols.end()) { |
| 75 | fSymbols[name] = symbol; |
| 76 | } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 77 | const Symbol* oldSymbol = existing->second; |
| 78 | if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { |
| 79 | std::vector<const FunctionDeclaration*> 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); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 89 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 90 | functions.push_back((const FunctionDeclaration*) symbol); |
| 91 | UnresolvedFunction* u = new UnresolvedFunction(std::move(functions)); |
| 92 | fSymbols[name] = u; |
| 93 | this->takeOwnership(u); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 94 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 95 | } else { |
| 96 | fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was already defined"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 97 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 98 | } |
| 99 | |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 100 | |
| 101 | void SymbolTable::markAllFunctionsBuiltin() { |
| 102 | for (const auto& pair : fSymbols) { |
| 103 | switch (pair.second->fKind) { |
| 104 | case Symbol::kFunctionDeclaration_Kind: |
| 105 | ((FunctionDeclaration&) *pair.second).fBuiltin = true; |
| 106 | break; |
| 107 | case Symbol::kUnresolvedFunction_Kind: |
| 108 | for (auto& f : ((UnresolvedFunction&) *pair.second).fFunctions) { |
| 109 | ((FunctionDeclaration*) f)->fBuiltin = true; |
| 110 | } |
| 111 | break; |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | } // namespace |