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 | */ |
| 7 | |
| 8 | #ifndef SKSL_SYMBOLTABLE |
| 9 | #define SKSL_SYMBOLTABLE |
| 10 | |
| 11 | #include <memory> |
| 12 | #include <unordered_map> |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 13 | #include <vector> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | #include "SkSLErrorReporter.h" |
| 15 | #include "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: |
| 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 Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 34 | const Symbol* operator[](const std::string& name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 36 | void add(const std::string& name, std::unique_ptr<Symbol> symbol); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 37 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 38 | void addWithoutOwnership(const std::string& name, const Symbol* symbol); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 39 | |
| 40 | Symbol* takeOwnership(Symbol* s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 41 | |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 42 | void markAllFunctionsBuiltin(); |
| 43 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 44 | const std::shared_ptr<SymbolTable> fParent; |
| 45 | |
| 46 | private: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 47 | static std::vector<const FunctionDeclaration*> GetFunctions(const Symbol& s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 49 | std::vector<std::unique_ptr<Symbol>> fOwnedPointers; |
| 50 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 51 | std::unordered_map<std::string, const Symbol*> fSymbols; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 52 | |
| 53 | ErrorReporter& fErrorReporter; |
| 54 | }; |
| 55 | |
| 56 | } // namespace |
| 57 | |
| 58 | #endif |