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 | 9 |
10 namespace SkSL { | 10 namespace SkSL { |
11 | 11 |
12 std::vector<std::shared_ptr<FunctionDeclaration>> SymbolTable::GetFunctions( | 12 SymbolTable::~SymbolTable() { |
13 const std::shar ed_ptr<Symbol>& s) { | 13 for (Symbol* s : fOwnedPointers) { |
14 switch (s->fKind) { | 14 delete s; |
15 } | |
16 } | |
17 | |
18 std::vector<const FunctionDeclaration*> SymbolTable::GetFunctions(const Symbol& s) { | |
19 switch (s.fKind) { | |
15 case Symbol::kFunctionDeclaration_Kind: | 20 case Symbol::kFunctionDeclaration_Kind: |
16 return { std::static_pointer_cast<FunctionDeclaration>(s) }; | 21 return { &((FunctionDeclaration&) s) }; |
17 case Symbol::kUnresolvedFunction_Kind: | 22 case Symbol::kUnresolvedFunction_Kind: |
18 return ((UnresolvedFunction&) *s).fFunctions; | 23 return ((UnresolvedFunction&) s).fFunctions; |
19 default: | 24 default: |
20 return { }; | 25 return { }; |
21 } | 26 } |
22 } | 27 } |
23 | 28 |
24 std::shared_ptr<Symbol> SymbolTable::operator[](const std::string& name) { | 29 const Symbol* SymbolTable::operator[](const std::string& name) { |
25 const auto& entry = fSymbols.find(name); | 30 const auto& entry = fSymbols.find(name); |
26 if (entry == fSymbols.end()) { | 31 if (entry == fSymbols.end()) { |
27 if (fParent) { | 32 if (fParent) { |
28 return (*fParent)[name]; | 33 return (*fParent)[name]; |
29 } | 34 } |
30 return nullptr; | 35 return nullptr; |
31 } | 36 } |
32 if (fParent) { | 37 if (fParent) { |
33 auto functions = GetFunctions(entry->second); | 38 auto functions = GetFunctions(*entry->second); |
34 if (functions.size() > 0) { | 39 if (functions.size() > 0) { |
35 bool modified = false; | 40 bool modified = false; |
36 std::shared_ptr<Symbol> previous = (*fParent)[name]; | 41 const Symbol* previous = (*fParent)[name]; |
37 if (previous) { | 42 if (previous) { |
38 auto previousFunctions = GetFunctions(previous); | 43 auto previousFunctions = GetFunctions(*previous); |
39 for (const std::shared_ptr<FunctionDeclaration>& prev : previous Functions) { | 44 for (const FunctionDeclaration* prev : previousFunctions) { |
40 bool found = false; | 45 bool found = false; |
41 for (const std::shared_ptr<FunctionDeclaration>& current : f unctions) { | 46 for (const FunctionDeclaration* current : functions) { |
42 if (current->matches(*prev)) { | 47 if (current->matches(*prev)) { |
43 found = true; | 48 found = true; |
44 break; | 49 break; |
45 } | 50 } |
46 } | 51 } |
47 if (!found) { | 52 if (!found) { |
48 functions.push_back(prev); | 53 functions.push_back(prev); |
49 modified = true; | 54 modified = true; |
50 } | 55 } |
51 } | 56 } |
52 if (modified) { | 57 if (modified) { |
53 ASSERT(functions.size() > 1); | 58 ASSERT(functions.size() > 1); |
54 return std::shared_ptr<Symbol>(new UnresolvedFunction(functi ons)); | 59 UnresolvedFunction* result = new UnresolvedFunction(function s); |
60 this->takeOwnership(result); | |
dogben
2016/07/08 19:56:40
nit: Maybe takeOwnership could return the raw poin
| |
61 return result; | |
55 } | 62 } |
56 } | 63 } |
57 } | 64 } |
58 } | 65 } |
59 return entry->second; | 66 return entry->second; |
60 } | 67 } |
61 | 68 |
62 void SymbolTable::add(const std::string& name, std::shared_ptr<Symbol> symbol) { | 69 void SymbolTable::takeOwnership(Symbol* s) { |
63 const auto& existing = fSymbols.find(name); | 70 if (fOwner) { |
64 if (existing == fSymbols.end()) { | 71 fOwnedPointers.push_back(s); |
65 fSymbols[name] = symbol; | 72 } else { |
66 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { | 73 ASSERT(fParent); |
67 const std::shared_ptr<Symbol>& oldSymbol = existing->second; | 74 fParent->takeOwnership(s); |
68 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { | 75 } |
69 std::vector<std::shared_ptr<FunctionDeclaration>> functions; | 76 } |
70 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(oldSymbol)); | 77 |
71 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(symbol)); | 78 void SymbolTable::add(const std::string& name, Symbol* symbol) { |
72 fSymbols[name].reset(new UnresolvedFunction(std::move(functions) )); | 79 this->addWithoutOwnership(name, symbol); |
73 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { | 80 this->takeOwnership(symbol); |
74 std::vector<std::shared_ptr<FunctionDeclaration>> functions; | 81 } |
75 for (const auto& f : ((UnresolvedFunction&) *oldSymbol).fFunctio ns) { | 82 |
76 functions.push_back(f); | 83 void SymbolTable::addWithoutOwnership(const std::string& name, const Symbol* sym bol) { |
77 } | 84 const auto& existing = fSymbols.find(name); |
78 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(symbol)); | 85 if (existing == fSymbols.end()) { |
79 fSymbols[name].reset(new UnresolvedFunction(std::move(functions) )); | 86 fSymbols[name] = symbol; |
87 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { | |
88 const Symbol* oldSymbol = existing->second; | |
89 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { | |
90 std::vector<const FunctionDeclaration*> functions; | |
91 functions.push_back((const FunctionDeclaration*) oldSymbol); | |
92 functions.push_back((const FunctionDeclaration*) symbol); | |
93 UnresolvedFunction* u = new UnresolvedFunction(std::move(functions)) ; | |
94 fSymbols[name] = u; | |
95 this->takeOwnership(u); | |
96 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { | |
97 std::vector<const FunctionDeclaration*> functions; | |
98 for (const auto& f : ((UnresolvedFunction&) *oldSymbol).fFunctions) { | |
dogben
2016/07/08 19:56:40
nit: s/const auto&/const auto*/
| |
99 functions.push_back(f); | |
80 } | 100 } |
81 } else { | 101 functions.push_back((const FunctionDeclaration*) symbol); |
82 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was a lready defined"); | 102 UnresolvedFunction* u = new UnresolvedFunction(std::move(functions)) ; |
103 fSymbols[name] = u; | |
104 this->takeOwnership(u); | |
83 } | 105 } |
106 } else { | |
107 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was alrea dy defined"); | |
84 } | 108 } |
109 } | |
110 | |
85 } // namespace | 111 } // namespace |
OLD | NEW |