daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // |
| 8 | // Symbol table for parsing. Most functionaliy and main ideas |
| 9 | // are documented in the header file. |
| 10 | // |
| 11 | |
apatrick@chromium.org | e057c5d | 2012-01-26 19:18:24 +0000 | [diff] [blame] | 12 | #if defined(_MSC_VER) |
| 13 | #pragma warning(disable: 4718) |
| 14 | #endif |
| 15 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 16 | #include "compiler/translator/SymbolTable.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
apatrick@chromium.org | 8187fa8 | 2010-06-15 22:09:28 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 21 | int TSymbolTableLevel::uniqueId = 0; |
| 22 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 23 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 24 | // Functions have buried pointers to delete. |
| 25 | // |
| 26 | TFunction::~TFunction() |
| 27 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 28 | for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i) |
| 29 | delete (*i).type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // |
| 33 | // Symbol table levels are a map of pointers to symbols that have to be deleted. |
| 34 | // |
| 35 | TSymbolTableLevel::~TSymbolTableLevel() |
| 36 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 37 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) |
| 38 | delete (*it).second; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // |
| 42 | // Change all function entries in the table with the non-mangled name |
| 43 | // to be related to the provided built-in operation. This is a low |
| 44 | // performance operation, and only intended for symbol tables that |
| 45 | // live across a large number of compiles. |
| 46 | // |
| 47 | void TSymbolTableLevel::relateToOperator(const char* name, TOperator op) |
| 48 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 49 | tLevel::iterator it; |
| 50 | for (it = level.begin(); it != level.end(); ++it) { |
| 51 | if ((*it).second->isFunction()) { |
| 52 | TFunction* function = static_cast<TFunction*>((*it).second); |
| 53 | if (function->getName() == name) |
| 54 | function->relateToOperator(op); |
| 55 | } |
| 56 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 57 | } |
| 58 | |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 59 | // |
| 60 | // Change all function entries in the table with the non-mangled name |
| 61 | // to be related to the provided built-in extension. This is a low |
| 62 | // performance operation, and only intended for symbol tables that |
| 63 | // live across a large number of compiles. |
| 64 | // |
| 65 | void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext) |
| 66 | { |
| 67 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) { |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 68 | TSymbol* symbol = it->second; |
| 69 | if (symbol->getName() == name) { |
| 70 | symbol->relateToExtension(ext); |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 74 | |
| 75 | TSymbol::TSymbol(const TSymbol& copyOf) |
| 76 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 77 | name = NewPoolTString(copyOf.name->c_str()); |
| 78 | uniqueId = copyOf.uniqueId; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 81 | TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope) |
| 82 | { |
| 83 | int level = currentLevel(); |
| 84 | TSymbol *symbol; |
| 85 | |
| 86 | do |
| 87 | { |
| 88 | if (level == ESSL3_BUILTINS && shaderVersion != 300) level--; |
| 89 | if (level == ESSL1_BUILTINS && shaderVersion != 100) level--; |
| 90 | |
| 91 | symbol = table[level]->find(name); |
| 92 | } |
| 93 | while (symbol == 0 && --level >= 0); |
| 94 | |
| 95 | if (builtIn) |
| 96 | *builtIn = (level <= LAST_BUILTIN_LEVEL); |
| 97 | if (sameScope) |
| 98 | *sameScope = (level == currentLevel()); |
| 99 | |
| 100 | return symbol; |
| 101 | } |
| 102 | |
| 103 | TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) |
| 104 | { |
| 105 | for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--) |
| 106 | { |
| 107 | if (level == ESSL3_BUILTINS && shaderVersion != 300) level--; |
| 108 | if (level == ESSL1_BUILTINS && shaderVersion != 100) level--; |
| 109 | |
| 110 | TSymbol *symbol = table[level]->find(name); |
| 111 | |
| 112 | if (symbol) |
| 113 | return symbol; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 118 | |
| 119 | TSymbolTable::~TSymbolTable() |
| 120 | { |
| 121 | while (table.size() > 0) |
| 122 | pop(); |
| 123 | } |