blob: a3f89e2288fa6386587660fbb09b5c210b595179 [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 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_SYMBOLTABLE
9#define SKSL_SYMBOLTABLE
10
Ethan Nicholasee04df42018-08-02 14:32:22 -040011#include <unordered_map>
ethannicholasb3058bd2016-07-01 08:22:01 -070012#include <memory>
ethannicholasd598f792016-07-25 10:08:54 -070013#include <vector>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/sksl/SkSLErrorReporter.h"
15#include "src/sksl/ir/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:
Ethan Nicholas8feeff92017-03-30 14:11:58 -040027 SymbolTable(ErrorReporter* errorReporter)
28 : fErrorReporter(*errorReporter) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070029
Ethan Nicholas8feeff92017-03-30 14:11:58 -040030 SymbolTable(std::shared_ptr<SymbolTable> parent, ErrorReporter* errorReporter)
ethannicholasb3058bd2016-07-01 08:22:01 -070031 : fParent(parent)
Ethan Nicholas8feeff92017-03-30 14:11:58 -040032 , fErrorReporter(*errorReporter) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070033
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070034 const Symbol* operator[](StringFragment name);
ethannicholasb3058bd2016-07-01 08:22:01 -070035
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070036 void add(StringFragment name, std::unique_ptr<Symbol> symbol);
ethannicholasd598f792016-07-25 10:08:54 -070037
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070038 void addWithoutOwnership(StringFragment name, const Symbol* symbol);
ethannicholasd598f792016-07-25 10:08:54 -070039
Ethan Nicholas91164d12019-05-15 15:29:54 -040040 Symbol* takeOwnership(std::unique_ptr<Symbol> s);
ethannicholasb3058bd2016-07-01 08:22:01 -070041
Ethan Nicholas91164d12019-05-15 15:29:54 -040042 IRNode* takeOwnership(std::unique_ptr<IRNode> n);
Ethan Nicholasaae47c82017-11-10 15:34:03 -050043
Ethan Nicholasb65024b2020-05-19 09:31:38 -040044 String* takeOwnership(std::unique_ptr<String> n);
45
ethannicholasddb37d62016-10-20 09:54:00 -070046 void markAllFunctionsBuiltin();
47
Ethan Nicholasee04df42018-08-02 14:32:22 -040048 std::unordered_map<StringFragment, const Symbol*>::iterator begin();
Ethan Nicholasaae47c82017-11-10 15:34:03 -050049
Ethan Nicholasee04df42018-08-02 14:32:22 -040050 std::unordered_map<StringFragment, const Symbol*>::iterator end();
Ethan Nicholasaae47c82017-11-10 15:34:03 -050051
ethannicholasb3058bd2016-07-01 08:22:01 -070052 const std::shared_ptr<SymbolTable> fParent;
53
54private:
ethannicholasd598f792016-07-25 10:08:54 -070055 static std::vector<const FunctionDeclaration*> GetFunctions(const Symbol& s);
ethannicholasb3058bd2016-07-01 08:22:01 -070056
Ethan Nicholasaae47c82017-11-10 15:34:03 -050057 std::vector<std::unique_ptr<Symbol>> fOwnedSymbols;
58
59 std::vector<std::unique_ptr<IRNode>> fOwnedNodes;
ethannicholasd598f792016-07-25 10:08:54 -070060
Ethan Nicholasb65024b2020-05-19 09:31:38 -040061 std::vector<std::unique_ptr<String>> fOwnedStrings;
62
Ethan Nicholasee04df42018-08-02 14:32:22 -040063 std::unordered_map<StringFragment, const Symbol*> fSymbols;
ethannicholasb3058bd2016-07-01 08:22:01 -070064
65 ErrorReporter& fErrorReporter;
66};
67
68} // namespace
69
70#endif