ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_SYMBOLTABLE |
| 9 | #define SKSL_SYMBOLTABLE |
| 10 | |
Ethan Nicholas | ee04df4 | 2018-08-02 14:32:22 -0400 | [diff] [blame] | 11 | #include <unordered_map> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 12 | #include <memory> |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 13 | #include <vector> |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/sksl/SkSLErrorReporter.h" |
| 15 | #include "src/sksl/ir/SkSLSymbol.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 16 | |
| 17 | namespace SkSL { |
| 18 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 19 | struct FunctionDeclaration; |
| 20 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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: |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 27 | SymbolTable(ErrorReporter* errorReporter) |
| 28 | : fErrorReporter(*errorReporter) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 30 | SymbolTable(std::shared_ptr<SymbolTable> parent, ErrorReporter* errorReporter) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | : fParent(parent) |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 32 | , fErrorReporter(*errorReporter) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 33 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 34 | const Symbol* operator[](StringFragment name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 36 | void add(StringFragment name, std::unique_ptr<Symbol> symbol); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 37 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 38 | void addWithoutOwnership(StringFragment name, const Symbol* symbol); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 39 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 40 | Symbol* takeOwnership(std::unique_ptr<Symbol> s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 41 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 42 | IRNode* takeOwnership(std::unique_ptr<IRNode> n); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 43 | |
Ethan Nicholas | b65024b | 2020-05-19 09:31:38 -0400 | [diff] [blame^] | 44 | String* takeOwnership(std::unique_ptr<String> n); |
| 45 | |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 46 | void markAllFunctionsBuiltin(); |
| 47 | |
Ethan Nicholas | ee04df4 | 2018-08-02 14:32:22 -0400 | [diff] [blame] | 48 | std::unordered_map<StringFragment, const Symbol*>::iterator begin(); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 49 | |
Ethan Nicholas | ee04df4 | 2018-08-02 14:32:22 -0400 | [diff] [blame] | 50 | std::unordered_map<StringFragment, const Symbol*>::iterator end(); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 51 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 52 | const std::shared_ptr<SymbolTable> fParent; |
| 53 | |
| 54 | private: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 55 | static std::vector<const FunctionDeclaration*> GetFunctions(const Symbol& s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 56 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 57 | std::vector<std::unique_ptr<Symbol>> fOwnedSymbols; |
| 58 | |
| 59 | std::vector<std::unique_ptr<IRNode>> fOwnedNodes; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 60 | |
Ethan Nicholas | b65024b | 2020-05-19 09:31:38 -0400 | [diff] [blame^] | 61 | std::vector<std::unique_ptr<String>> fOwnedStrings; |
| 62 | |
Ethan Nicholas | ee04df4 | 2018-08-02 14:32:22 -0400 | [diff] [blame] | 63 | std::unordered_map<StringFragment, const Symbol*> fSymbols; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 64 | |
| 65 | ErrorReporter& fErrorReporter; |
| 66 | }; |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | #endif |