blob: 9b60d1061a2a9a420717b00f40b0627d637370b2 [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
Olli Etuahofbb1c792018-01-19 16:26:59 +020015#include "compiler/translator/ImmutableStringBuilder.h"
Olli Etuahod4529f32017-12-12 13:06:40 +020016#include "compiler/translator/SymbolTable.h"
17
18namespace sh
19{
20
21namespace
22{
23
Olli Etuahofbb1c792018-01-19 16:26:59 +020024constexpr const ImmutableString kMainName("main");
25constexpr const ImmutableString kImageLoadName("imageLoad");
26constexpr const ImmutableString kImageStoreName("imageStore");
27constexpr const ImmutableString kImageSizeName("imageSize");
28
Olli Etuahod4529f32017-12-12 13:06:40 +020029static const char kFunctionMangledNameSeparator = '(';
30
31} // anonymous namespace
32
33TSymbol::TSymbol(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +020034 const ImmutableString &name,
Olli Etuahod4529f32017-12-12 13:06:40 +020035 SymbolType symbolType,
Olli Etuaho8073a952018-05-09 16:41:39 +030036 SymbolClass symbolClass,
Olli Etuahod4529f32017-12-12 13:06:40 +020037 TExtension extension)
38 : mName(name),
39 mUniqueId(symbolTable->nextUniqueId()),
40 mSymbolType(symbolType),
Olli Etuaho8073a952018-05-09 16:41:39 +030041 mExtension(extension),
42 mSymbolClass(symbolClass)
Olli Etuahod4529f32017-12-12 13:06:40 +020043{
44 ASSERT(mSymbolType == SymbolType::BuiltIn || mExtension == TExtension::UNDEFINED);
Olli Etuahofbb1c792018-01-19 16:26:59 +020045 ASSERT(mName != "" || mSymbolType == SymbolType::AngleInternal ||
Olli Etuaho95ed1942018-02-01 14:01:19 +020046 mSymbolType == SymbolType::Empty);
Olli Etuahod4529f32017-12-12 13:06:40 +020047}
48
Olli Etuahofbb1c792018-01-19 16:26:59 +020049ImmutableString TSymbol::name() const
Olli Etuahod4529f32017-12-12 13:06:40 +020050{
Olli Etuaho40b1c772018-03-19 10:29:37 +020051 if (!mName.empty())
Olli Etuahod4529f32017-12-12 13:06:40 +020052 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020053 return mName;
Olli Etuahod4529f32017-12-12 13:06:40 +020054 }
Olli Etuaho40b1c772018-03-19 10:29:37 +020055 // This can be called for nameless function parameters in HLSL.
56 ASSERT(mSymbolType == SymbolType::AngleInternal ||
57 (mSymbolType == SymbolType::Empty && isVariable()));
Olli Etuahofbb1c792018-01-19 16:26:59 +020058 int uniqueId = mUniqueId.get();
59 ImmutableStringBuilder symbolNameOut(sizeof(uniqueId) * 2u + 1u);
60 symbolNameOut << 's';
61 symbolNameOut.appendHex(mUniqueId.get());
62 return symbolNameOut;
Olli Etuahod4529f32017-12-12 13:06:40 +020063}
64
Olli Etuahofbb1c792018-01-19 16:26:59 +020065ImmutableString TSymbol::getMangledName() const
Olli Etuahod4529f32017-12-12 13:06:40 +020066{
Olli Etuaho8073a952018-05-09 16:41:39 +030067 if (mSymbolClass == SymbolClass::Function)
68 {
69 // We do this instead of using proper virtual functions so that we can better support
70 // constexpr symbols.
71 return static_cast<const TFunction *>(this)->getFunctionMangledName();
72 }
Olli Etuahod4529f32017-12-12 13:06:40 +020073 ASSERT(mSymbolType != SymbolType::Empty);
Olli Etuahobed35d72017-12-20 16:36:26 +020074 return name();
Olli Etuahod4529f32017-12-12 13:06:40 +020075}
76
77TVariable::TVariable(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +020078 const ImmutableString &name,
Olli Etuahob60d30f2018-01-16 12:31:06 +020079 const TType *type,
Olli Etuahod4529f32017-12-12 13:06:40 +020080 SymbolType symbolType,
81 TExtension extension)
Olli Etuaho8073a952018-05-09 16:41:39 +030082 : TSymbol(symbolTable, name, symbolType, SymbolClass::Variable, extension),
83 mType(type),
84 unionArray(nullptr)
Olli Etuahod4529f32017-12-12 13:06:40 +020085{
Olli Etuahob60d30f2018-01-16 12:31:06 +020086 ASSERT(mType);
Olli Etuahod4529f32017-12-12 13:06:40 +020087}
88
89TStructure::TStructure(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +020090 const ImmutableString &name,
Olli Etuahod4529f32017-12-12 13:06:40 +020091 const TFieldList *fields,
92 SymbolType symbolType)
Olli Etuaho8073a952018-05-09 16:41:39 +030093 : TSymbol(symbolTable, name, symbolType, SymbolClass::Struct), TFieldListCollection(fields)
Olli Etuahod4529f32017-12-12 13:06:40 +020094{
95}
96
Olli Etuaho391bda22018-02-23 11:43:14 +020097TStructure::TStructure(const TSymbolUniqueId &id,
98 const ImmutableString &name,
99 TExtension extension,
100 const TFieldList *fields)
Olli Etuaho8073a952018-05-09 16:41:39 +0300101 : TSymbol(id, name, SymbolType::BuiltIn, extension, SymbolClass::Struct),
102 TFieldListCollection(fields)
Olli Etuaho391bda22018-02-23 11:43:14 +0200103{
104}
105
Olli Etuahofbb1c792018-01-19 16:26:59 +0200106void TStructure::createSamplerSymbols(const char *namePrefix,
Olli Etuahod4529f32017-12-12 13:06:40 +0200107 const TString &apiNamePrefix,
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200108 TVector<const TVariable *> *outputSymbols,
109 TMap<const TVariable *, TString> *outputSymbolsToAPINames,
Olli Etuahod4529f32017-12-12 13:06:40 +0200110 TSymbolTable *symbolTable) const
111{
112 ASSERT(containsSamplers());
113 for (const auto *field : *mFields)
114 {
115 const TType *fieldType = field->type();
116 if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
117 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200118 std::stringstream fieldName;
119 fieldName << namePrefix << "_" << field->name();
120 TString fieldApiName = apiNamePrefix + ".";
121 fieldApiName += field->name().data();
122 fieldType->createSamplerSymbols(ImmutableString(fieldName.str()), fieldApiName,
123 outputSymbols, outputSymbolsToAPINames, symbolTable);
Olli Etuahod4529f32017-12-12 13:06:40 +0200124 }
125 }
126}
127
Olli Etuahofbb1c792018-01-19 16:26:59 +0200128void TStructure::setName(const ImmutableString &name)
Olli Etuahod4529f32017-12-12 13:06:40 +0200129{
Olli Etuahofbb1c792018-01-19 16:26:59 +0200130 ImmutableString *mutableName = const_cast<ImmutableString *>(&mName);
Olli Etuahod4529f32017-12-12 13:06:40 +0200131 *mutableName = name;
132}
133
134TInterfaceBlock::TInterfaceBlock(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200135 const ImmutableString &name,
Olli Etuahod4529f32017-12-12 13:06:40 +0200136 const TFieldList *fields,
137 const TLayoutQualifier &layoutQualifier,
138 SymbolType symbolType,
139 TExtension extension)
Olli Etuaho8073a952018-05-09 16:41:39 +0300140 : TSymbol(symbolTable, name, symbolType, SymbolClass::InterfaceBlock, extension),
Olli Etuahod4529f32017-12-12 13:06:40 +0200141 TFieldListCollection(fields),
142 mBlockStorage(layoutQualifier.blockStorage),
143 mBinding(layoutQualifier.binding)
144{
145 ASSERT(name != nullptr);
146}
147
Olli Etuaho391bda22018-02-23 11:43:14 +0200148TInterfaceBlock::TInterfaceBlock(const TSymbolUniqueId &id,
149 const ImmutableString &name,
150 TExtension extension,
151 const TFieldList *fields)
Olli Etuaho8073a952018-05-09 16:41:39 +0300152 : TSymbol(id, name, SymbolType::BuiltIn, extension, SymbolClass::InterfaceBlock),
Olli Etuaho391bda22018-02-23 11:43:14 +0200153 TFieldListCollection(fields),
154 mBlockStorage(EbsUnspecified),
155 mBinding(0)
156{
157}
158
Olli Etuahod4529f32017-12-12 13:06:40 +0200159TFunction::TFunction(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200160 const ImmutableString &name,
Olli Etuahod4529f32017-12-12 13:06:40 +0200161 SymbolType symbolType,
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200162 const TType *retType,
163 bool knownToNotHaveSideEffects)
Olli Etuaho8073a952018-05-09 16:41:39 +0300164 : TSymbol(symbolTable, name, symbolType, SymbolClass::Function, TExtension::UNDEFINED),
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200165 mParametersVector(new TParamVector()),
166 mParameters(nullptr),
167 mParamCount(0u),
Olli Etuahod4529f32017-12-12 13:06:40 +0200168 returnType(retType),
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200169 mMangledName(""),
170 mOp(EOpNull),
Olli Etuahod4529f32017-12-12 13:06:40 +0200171 defined(false),
Olli Etuaho0c371002017-12-13 17:00:25 +0400172 mHasPrototypeDeclaration(false),
173 mKnownToNotHaveSideEffects(knownToNotHaveSideEffects)
Olli Etuahod4529f32017-12-12 13:06:40 +0200174{
Olli Etuaho0c371002017-12-13 17:00:25 +0400175 // Functions with an empty name are not allowed.
176 ASSERT(symbolType != SymbolType::Empty);
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200177 ASSERT(name != nullptr || symbolType == SymbolType::AngleInternal);
Olli Etuahod4529f32017-12-12 13:06:40 +0200178}
179
Olli Etuahod4bd9632018-03-08 16:32:44 +0200180void TFunction::addParameter(const TVariable *p)
Olli Etuahod4529f32017-12-12 13:06:40 +0200181{
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200182 ASSERT(mParametersVector);
183 mParametersVector->push_back(p);
184 mParameters = mParametersVector->data();
185 mParamCount = mParametersVector->size();
186 mMangledName = ImmutableString("");
187}
188
189void TFunction::shareParameters(const TFunction &parametersSource)
190{
191 mParametersVector = nullptr;
192 mParameters = parametersSource.mParameters;
193 mParamCount = parametersSource.mParamCount;
194 ASSERT(parametersSource.name() == name());
195 mMangledName = parametersSource.mMangledName;
Olli Etuahod4529f32017-12-12 13:06:40 +0200196}
197
Olli Etuahofbb1c792018-01-19 16:26:59 +0200198ImmutableString TFunction::buildMangledName() const
Olli Etuahod4529f32017-12-12 13:06:40 +0200199{
Olli Etuahofbb1c792018-01-19 16:26:59 +0200200 std::string newName(name().data(), name().length());
Olli Etuahod4529f32017-12-12 13:06:40 +0200201 newName += kFunctionMangledNameSeparator;
202
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200203 for (size_t i = 0u; i < mParamCount; ++i)
Olli Etuahod4529f32017-12-12 13:06:40 +0200204 {
Olli Etuahod4bd9632018-03-08 16:32:44 +0200205 newName += mParameters[i]->getType().getMangledName();
Olli Etuahod4529f32017-12-12 13:06:40 +0200206 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200207 return ImmutableString(newName);
Olli Etuahod4529f32017-12-12 13:06:40 +0200208}
209
Olli Etuaho1bb85282017-12-14 13:39:53 +0200210bool TFunction::isMain() const
211{
Olli Etuahofbb1c792018-01-19 16:26:59 +0200212 return symbolType() == SymbolType::UserDefined && name() == kMainName;
Olli Etuaho1bb85282017-12-14 13:39:53 +0200213}
214
215bool TFunction::isImageFunction() const
216{
217 return symbolType() == SymbolType::BuiltIn &&
Olli Etuahofbb1c792018-01-19 16:26:59 +0200218 (name() == kImageSizeName || name() == kImageLoadName || name() == kImageStoreName);
Olli Etuaho1bb85282017-12-14 13:39:53 +0200219}
220
Olli Etuahod4529f32017-12-12 13:06:40 +0200221} // namespace sh