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 | #ifndef SKSL_SYMBOLTABLE |
9 | #define SKSL_SYMBOLTABLE |
10 | |
11 | #include <unordered_map> |
12 | #include <memory> |
13 | #include <vector> |
14 | #include "src/sksl/SkSLErrorReporter.h" |
15 | #include "src/sksl/ir/SkSLSymbol.h" |
16 | |
17 | namespace SkSL { |
18 | |
19 | struct FunctionDeclaration; |
20 | |
21 | /** |
22 | * Maps identifiers to symbols. Functions, in particular, are mapped to either FunctionDeclaration |
23 | * or UnresolvedFunction depending on whether they are overloaded or not. |
24 | */ |
25 | class SymbolTable { |
26 | public: |
27 | SymbolTable(ErrorReporter* errorReporter) |
28 | : fErrorReporter(*errorReporter) {} |
29 | |
30 | SymbolTable(std::shared_ptr<SymbolTable> parent) |
31 | : fParent(parent) |
32 | , fErrorReporter(parent->fErrorReporter) {} |
33 | |
34 | const Symbol* operator[](StringFragment name); |
35 | |
36 | void addWithoutOwnership(StringFragment name, const Symbol* symbol); |
37 | |
38 | template <typename T> |
39 | const T* add(StringFragment name, std::unique_ptr<T> symbol) { |
40 | const T* ptr = symbol.get(); |
41 | this->addWithoutOwnership(name, ptr); |
42 | this->takeOwnershipOfSymbol(std::move(symbol)); |
43 | return ptr; |
44 | } |
45 | |
46 | template <typename T> |
47 | const T* takeOwnershipOfSymbol(std::unique_ptr<T> symbol) { |
48 | const T* ptr = symbol.get(); |
49 | fOwnedSymbols.push_back(std::move(symbol)); |
50 | return ptr; |
51 | } |
52 | |
53 | template <typename T> |
54 | const T* takeOwnershipOfIRNode(std::unique_ptr<T> node) { |
55 | const T* ptr = node.get(); |
56 | fOwnedNodes.push_back(std::move(node)); |
57 | return ptr; |
58 | } |
59 | |
60 | const String* takeOwnershipOfString(std::unique_ptr<String> n); |
61 | |
62 | std::unordered_map<StringFragment, const Symbol*>::iterator begin(); |
63 | |
64 | std::unordered_map<StringFragment, const Symbol*>::iterator end(); |
65 | |
66 | const std::shared_ptr<SymbolTable> fParent; |
67 | |
68 | std::vector<std::unique_ptr<const Symbol>> fOwnedSymbols; |
69 | |
70 | private: |
71 | static std::vector<const FunctionDeclaration*> GetFunctions(const Symbol& s); |
72 | |
73 | std::vector<std::unique_ptr<IRNode>> fOwnedNodes; |
74 | |
75 | std::vector<std::unique_ptr<String>> fOwnedStrings; |
76 | |
77 | std::unordered_map<StringFragment, const Symbol*> fSymbols; |
78 | |
79 | ErrorReporter& fErrorReporter; |
80 | |
81 | friend class Dehydrator; |
82 | }; |
83 | |
84 | } // namespace SkSL |
85 | |
86 | #endif |
87 |