blob: be2b49c48d9c40f7693879d155678ee519916b41 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 <memory>
12#include <unordered_map>
ethannicholasd598f792016-07-25 10:08:54 -070013#include <vector>
ethannicholasb3058bd2016-07-01 08:22:01 -070014#include "SkSLErrorReporter.h"
15#include "SkSLSymbol.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070016
17namespace SkSL {
18
ethannicholasd598f792016-07-25 10:08:54 -070019struct FunctionDeclaration;
20
ethannicholasb3058bd2016-07-01 08:22:01 -070021/**
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
Greg Daniel792d0f12016-11-20 14:53:35 +000034 const Symbol* operator[](const std::string& name);
ethannicholasb3058bd2016-07-01 08:22:01 -070035
Greg Daniel792d0f12016-11-20 14:53:35 +000036 void add(const std::string& name, std::unique_ptr<Symbol> symbol);
ethannicholasd598f792016-07-25 10:08:54 -070037
Greg Daniel792d0f12016-11-20 14:53:35 +000038 void addWithoutOwnership(const std::string& name, const Symbol* symbol);
ethannicholasd598f792016-07-25 10:08:54 -070039
40 Symbol* takeOwnership(Symbol* s);
ethannicholasb3058bd2016-07-01 08:22:01 -070041
ethannicholasddb37d62016-10-20 09:54:00 -070042 void markAllFunctionsBuiltin();
43
ethannicholasb3058bd2016-07-01 08:22:01 -070044 const std::shared_ptr<SymbolTable> fParent;
45
46private:
ethannicholasd598f792016-07-25 10:08:54 -070047 static std::vector<const FunctionDeclaration*> GetFunctions(const Symbol& s);
ethannicholasb3058bd2016-07-01 08:22:01 -070048
ethannicholasd598f792016-07-25 10:08:54 -070049 std::vector<std::unique_ptr<Symbol>> fOwnedPointers;
50
Greg Daniel792d0f12016-11-20 14:53:35 +000051 std::unordered_map<std::string, const Symbol*> fSymbols;
ethannicholasb3058bd2016-07-01 08:22:01 -070052
53 ErrorReporter& fErrorReporter;
54};
55
56} // namespace
57
58#endif