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
17namespace SkSL {
18
19struct 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 */
25class SymbolTable {
26public:
27 SymbolTable(ErrorReporter* errorReporter)
28 : fErrorReporter(*errorReporter) {}
29
30 SymbolTable(std::shared_ptr<SymbolTable> parent, ErrorReporter* errorReporter)
31 : fParent(parent)
32 , fErrorReporter(*errorReporter) {}
33
34 const Symbol* operator[](StringFragment name);
35
36 void add(StringFragment name, std::unique_ptr<Symbol> symbol);
37
38 void addWithoutOwnership(StringFragment name, const Symbol* symbol);
39
40 Symbol* takeOwnership(std::unique_ptr<Symbol> s);
41
42 IRNode* takeOwnership(std::unique_ptr<IRNode> n);
43
44 void markAllFunctionsBuiltin();
45
46 std::unordered_map<StringFragment, const Symbol*>::iterator begin();
47
48 std::unordered_map<StringFragment, const Symbol*>::iterator end();
49
50 const std::shared_ptr<SymbolTable> fParent;
51
52private:
53 static std::vector<const FunctionDeclaration*> GetFunctions(const Symbol& s);
54
55 std::vector<std::unique_ptr<Symbol>> fOwnedSymbols;
56
57 std::vector<std::unique_ptr<IRNode>> fOwnedNodes;
58
59 std::unordered_map<StringFragment, const Symbol*> fSymbols;
60
61 ErrorReporter& fErrorReporter;
62};
63
64} // namespace
65
66#endif
67