blob: 38a42b4c0992f194c72868dd031e0d2bd636cc31 [file] [log] [blame]
Olli Etuahod4529f32017-12-12 13:06:40 +02001//
2// Copyright (c) 2017 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// Symbol.cpp: Symbols representing variables, functions, structures and interface blocks.
7//
8
9#if defined(_MSC_VER)
10#pragma warning(disable : 4718)
11#endif
12
13#include "compiler/translator/Symbol.h"
14
15#include "compiler/translator/SymbolTable.h"
16
17namespace sh
18{
19
20namespace
21{
22
23static const char kFunctionMangledNameSeparator = '(';
24
25} // anonymous namespace
26
27TSymbol::TSymbol(TSymbolTable *symbolTable,
28 const TString *name,
29 SymbolType symbolType,
30 TExtension extension)
31 : mName(name),
32 mUniqueId(symbolTable->nextUniqueId()),
33 mSymbolType(symbolType),
34 mExtension(extension)
35{
36 ASSERT(mSymbolType == SymbolType::BuiltIn || mExtension == TExtension::UNDEFINED);
37 ASSERT(mName != nullptr || mSymbolType == SymbolType::AngleInternal ||
38 mSymbolType == SymbolType::NotResolved || mSymbolType == SymbolType::Empty);
39}
40
41const TString *TSymbol::name() const
42{
43 if (mName != nullptr || mSymbolType == SymbolType::Empty)
44 {
45 return mName;
46 }
47 ASSERT(mSymbolType == SymbolType::AngleInternal);
48 TInfoSinkBase symbolNameOut;
49 symbolNameOut << "s" << mUniqueId.get();
50 return NewPoolTString(symbolNameOut.c_str());
51}
52
53const TString &TSymbol::getMangledName() const
54{
55 ASSERT(mSymbolType != SymbolType::Empty);
56 return *name();
57}
58
59TVariable::TVariable(TSymbolTable *symbolTable,
60 const TString *name,
61 const TType &t,
62 SymbolType symbolType,
63 TExtension extension)
64 : TSymbol(symbolTable, name, symbolType, extension), type(t), unionArray(nullptr)
65{
66}
67
68TStructure::TStructure(TSymbolTable *symbolTable,
69 const TString *name,
70 const TFieldList *fields,
71 SymbolType symbolType)
72 : TSymbol(symbolTable, name, symbolType), TFieldListCollection(fields)
73{
74}
75
76void TStructure::createSamplerSymbols(const TString &namePrefix,
77 const TString &apiNamePrefix,
78 TVector<TIntermSymbol *> *outputSymbols,
79 TMap<TIntermSymbol *, TString> *outputSymbolsToAPINames,
80 TSymbolTable *symbolTable) const
81{
82 ASSERT(containsSamplers());
83 for (const auto *field : *mFields)
84 {
85 const TType *fieldType = field->type();
86 if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
87 {
88 TString fieldName = namePrefix + "_" + field->name();
89 TString fieldApiName = apiNamePrefix + "." + field->name();
90 fieldType->createSamplerSymbols(fieldName, fieldApiName, outputSymbols,
91 outputSymbolsToAPINames, symbolTable);
92 }
93 }
94}
95
96void TStructure::setName(const TString &name)
97{
98 TString *mutableName = const_cast<TString *>(mName);
99 *mutableName = name;
100}
101
102TInterfaceBlock::TInterfaceBlock(TSymbolTable *symbolTable,
103 const TString *name,
104 const TFieldList *fields,
105 const TLayoutQualifier &layoutQualifier,
106 SymbolType symbolType,
107 TExtension extension)
108 : TSymbol(symbolTable, name, symbolType, extension),
109 TFieldListCollection(fields),
110 mBlockStorage(layoutQualifier.blockStorage),
111 mBinding(layoutQualifier.binding)
112{
113 ASSERT(name != nullptr);
114}
115
116TFunction::TFunction(TSymbolTable *symbolTable,
117 const TString *name,
118 const TType *retType,
119 SymbolType symbolType,
120 TOperator tOp,
121 TExtension extension)
122 : TSymbol(symbolTable, name, symbolType, extension),
123 returnType(retType),
124 mangledName(nullptr),
125 op(tOp),
126 defined(false),
127 mHasPrototypeDeclaration(false)
128{
129}
130
131//
132// Functions have buried pointers to delete.
133//
134TFunction::~TFunction()
135{
136 clearParameters();
137}
138
139void TFunction::clearParameters()
140{
141 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
142 delete (*i).type;
143 parameters.clear();
144 mangledName = nullptr;
145}
146
147void TFunction::swapParameters(const TFunction &parametersSource)
148{
149 clearParameters();
150 for (auto parameter : parametersSource.parameters)
151 {
152 addParameter(parameter);
153 }
154}
155
156const TString *TFunction::buildMangledName() const
157{
158 std::string newName = name()->c_str();
159 newName += kFunctionMangledNameSeparator;
160
161 for (const auto &p : parameters)
162 {
163 newName += p.type->getMangledName();
164 }
165 return NewPoolTString(newName.c_str());
166}
167
168const TString &TFunction::GetMangledNameFromCall(const TString &functionName,
169 const TIntermSequence &arguments)
170{
171 std::string newName = functionName.c_str();
172 newName += kFunctionMangledNameSeparator;
173
174 for (TIntermNode *argument : arguments)
175 {
176 newName += argument->getAsTyped()->getType().getMangledName();
177 }
178 return *NewPoolTString(newName.c_str());
179}
180
181} // namespace sh