blob: 2b8e1ff844ea0d629b29291a3a6e7c455521bfc0 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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.orge057c5d2012-01-26 19:18:24 +000012#if defined(_MSC_VER)
13#pragma warning(disable: 4718)
14#endif
15
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/SymbolTable.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000018#include <stdio.h>
kbr@chromium.org476541f2011-10-27 21:14:51 +000019#include <algorithm>
20
Nicolas Capensbd10cf52013-06-20 09:51:51 -040021int TSymbolTableLevel::uniqueId = 0;
22
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024// Functions have buried pointers to delete.
25//
26TFunction::~TFunction()
27{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000028 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
29 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030}
31
32//
33// Symbol table levels are a map of pointers to symbols that have to be deleted.
34//
35TSymbolTableLevel::~TSymbolTableLevel()
36{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000037 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
38 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039}
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//
47void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
48{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000049 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.com4f39fd92010-03-08 20:26:45 +000057}
58
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000059//
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//
65void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
66{
67 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
Jamie Madill2aeb26a2013-07-08 14:02:55 -040068 TSymbol* symbol = it->second;
69 if (symbol->getName() == name) {
70 symbol->relateToExtension(ext);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000071 }
72 }
73}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074
75TSymbol::TSymbol(const TSymbol& copyOf)
76{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000077 name = NewPoolTString(copyOf.name->c_str());
78 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079}
80
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000081TSymbol *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
103TSymbol *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 Priyadarshibc3f1ac2013-09-23 14:57:02 -0400118
119TSymbolTable::~TSymbolTable()
120{
121 while (table.size() > 0)
122 pop();
123}