blob: 7f2edc791cf9a26c38152d09aa5bad5f5ef83882 [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,
36 TExtension extension)
37 : mName(name),
38 mUniqueId(symbolTable->nextUniqueId()),
39 mSymbolType(symbolType),
40 mExtension(extension)
41{
42 ASSERT(mSymbolType == SymbolType::BuiltIn || mExtension == TExtension::UNDEFINED);
Olli Etuahofbb1c792018-01-19 16:26:59 +020043 ASSERT(mName != "" || mSymbolType == SymbolType::AngleInternal ||
Olli Etuaho95ed1942018-02-01 14:01:19 +020044 mSymbolType == SymbolType::Empty);
Olli Etuahod4529f32017-12-12 13:06:40 +020045}
46
Olli Etuahofbb1c792018-01-19 16:26:59 +020047ImmutableString TSymbol::name() const
Olli Etuahod4529f32017-12-12 13:06:40 +020048{
Olli Etuahofbb1c792018-01-19 16:26:59 +020049 if (mName != "")
Olli Etuahod4529f32017-12-12 13:06:40 +020050 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020051 return mName;
Olli Etuahod4529f32017-12-12 13:06:40 +020052 }
53 ASSERT(mSymbolType == SymbolType::AngleInternal);
Olli Etuahofbb1c792018-01-19 16:26:59 +020054 int uniqueId = mUniqueId.get();
55 ImmutableStringBuilder symbolNameOut(sizeof(uniqueId) * 2u + 1u);
56 symbolNameOut << 's';
57 symbolNameOut.appendHex(mUniqueId.get());
58 return symbolNameOut;
Olli Etuahod4529f32017-12-12 13:06:40 +020059}
60
Olli Etuahofbb1c792018-01-19 16:26:59 +020061ImmutableString TSymbol::getMangledName() const
Olli Etuahod4529f32017-12-12 13:06:40 +020062{
63 ASSERT(mSymbolType != SymbolType::Empty);
Olli Etuahobed35d72017-12-20 16:36:26 +020064 return name();
Olli Etuahod4529f32017-12-12 13:06:40 +020065}
66
67TVariable::TVariable(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +020068 const ImmutableString &name,
Olli Etuahob60d30f2018-01-16 12:31:06 +020069 const TType *type,
Olli Etuahod4529f32017-12-12 13:06:40 +020070 SymbolType symbolType,
71 TExtension extension)
Olli Etuahob60d30f2018-01-16 12:31:06 +020072 : TSymbol(symbolTable, name, symbolType, extension), mType(type), unionArray(nullptr)
Olli Etuahod4529f32017-12-12 13:06:40 +020073{
Olli Etuahob60d30f2018-01-16 12:31:06 +020074 ASSERT(mType);
Olli Etuahod4529f32017-12-12 13:06:40 +020075}
76
77TStructure::TStructure(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +020078 const ImmutableString &name,
Olli Etuahod4529f32017-12-12 13:06:40 +020079 const TFieldList *fields,
80 SymbolType symbolType)
81 : TSymbol(symbolTable, name, symbolType), TFieldListCollection(fields)
82{
83}
84
Olli Etuaho391bda22018-02-23 11:43:14 +020085TStructure::TStructure(const TSymbolUniqueId &id,
86 const ImmutableString &name,
87 TExtension extension,
88 const TFieldList *fields)
89 : TSymbol(id, name, SymbolType::BuiltIn, extension), TFieldListCollection(fields)
90{
91}
92
Olli Etuahofbb1c792018-01-19 16:26:59 +020093void TStructure::createSamplerSymbols(const char *namePrefix,
Olli Etuahod4529f32017-12-12 13:06:40 +020094 const TString &apiNamePrefix,
Olli Etuahobbd9d4c2017-12-21 12:02:00 +020095 TVector<const TVariable *> *outputSymbols,
96 TMap<const TVariable *, TString> *outputSymbolsToAPINames,
Olli Etuahod4529f32017-12-12 13:06:40 +020097 TSymbolTable *symbolTable) const
98{
99 ASSERT(containsSamplers());
100 for (const auto *field : *mFields)
101 {
102 const TType *fieldType = field->type();
103 if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
104 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200105 std::stringstream fieldName;
106 fieldName << namePrefix << "_" << field->name();
107 TString fieldApiName = apiNamePrefix + ".";
108 fieldApiName += field->name().data();
109 fieldType->createSamplerSymbols(ImmutableString(fieldName.str()), fieldApiName,
110 outputSymbols, outputSymbolsToAPINames, symbolTable);
Olli Etuahod4529f32017-12-12 13:06:40 +0200111 }
112 }
113}
114
Olli Etuahofbb1c792018-01-19 16:26:59 +0200115void TStructure::setName(const ImmutableString &name)
Olli Etuahod4529f32017-12-12 13:06:40 +0200116{
Olli Etuahofbb1c792018-01-19 16:26:59 +0200117 ImmutableString *mutableName = const_cast<ImmutableString *>(&mName);
Olli Etuahod4529f32017-12-12 13:06:40 +0200118 *mutableName = name;
119}
120
121TInterfaceBlock::TInterfaceBlock(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200122 const ImmutableString &name,
Olli Etuahod4529f32017-12-12 13:06:40 +0200123 const TFieldList *fields,
124 const TLayoutQualifier &layoutQualifier,
125 SymbolType symbolType,
126 TExtension extension)
127 : TSymbol(symbolTable, name, symbolType, extension),
128 TFieldListCollection(fields),
129 mBlockStorage(layoutQualifier.blockStorage),
130 mBinding(layoutQualifier.binding)
131{
132 ASSERT(name != nullptr);
133}
134
Olli Etuaho391bda22018-02-23 11:43:14 +0200135TInterfaceBlock::TInterfaceBlock(const TSymbolUniqueId &id,
136 const ImmutableString &name,
137 TExtension extension,
138 const TFieldList *fields)
139 : TSymbol(id, name, SymbolType::BuiltIn, extension),
140 TFieldListCollection(fields),
141 mBlockStorage(EbsUnspecified),
142 mBinding(0)
143{
144}
145
Olli Etuahod4529f32017-12-12 13:06:40 +0200146TFunction::TFunction(TSymbolTable *symbolTable,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200147 const ImmutableString &name,
Olli Etuahod4529f32017-12-12 13:06:40 +0200148 SymbolType symbolType,
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200149 const TType *retType,
150 bool knownToNotHaveSideEffects)
151 : TSymbol(symbolTable, name, symbolType, TExtension::UNDEFINED),
152 mParametersVector(new TParamVector()),
153 mParameters(nullptr),
154 mParamCount(0u),
Olli Etuahod4529f32017-12-12 13:06:40 +0200155 returnType(retType),
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200156 mMangledName(""),
157 mOp(EOpNull),
Olli Etuahod4529f32017-12-12 13:06:40 +0200158 defined(false),
Olli Etuaho0c371002017-12-13 17:00:25 +0400159 mHasPrototypeDeclaration(false),
160 mKnownToNotHaveSideEffects(knownToNotHaveSideEffects)
Olli Etuahod4529f32017-12-12 13:06:40 +0200161{
Olli Etuaho0c371002017-12-13 17:00:25 +0400162 // Functions with an empty name are not allowed.
163 ASSERT(symbolType != SymbolType::Empty);
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200164 ASSERT(name != nullptr || symbolType == SymbolType::AngleInternal);
Olli Etuahod4529f32017-12-12 13:06:40 +0200165}
166
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200167void TFunction::addParameter(const TConstParameter &p)
Olli Etuahod4529f32017-12-12 13:06:40 +0200168{
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200169 ASSERT(mParametersVector);
170 mParametersVector->push_back(p);
171 mParameters = mParametersVector->data();
172 mParamCount = mParametersVector->size();
173 mMangledName = ImmutableString("");
174}
175
176void TFunction::shareParameters(const TFunction &parametersSource)
177{
178 mParametersVector = nullptr;
179 mParameters = parametersSource.mParameters;
180 mParamCount = parametersSource.mParamCount;
181 ASSERT(parametersSource.name() == name());
182 mMangledName = parametersSource.mMangledName;
Olli Etuahod4529f32017-12-12 13:06:40 +0200183}
184
Olli Etuahofbb1c792018-01-19 16:26:59 +0200185ImmutableString TFunction::buildMangledName() const
Olli Etuahod4529f32017-12-12 13:06:40 +0200186{
Olli Etuahofbb1c792018-01-19 16:26:59 +0200187 std::string newName(name().data(), name().length());
Olli Etuahod4529f32017-12-12 13:06:40 +0200188 newName += kFunctionMangledNameSeparator;
189
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200190 for (size_t i = 0u; i < mParamCount; ++i)
Olli Etuahod4529f32017-12-12 13:06:40 +0200191 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200192 newName += mParameters[i].type->getMangledName();
Olli Etuahod4529f32017-12-12 13:06:40 +0200193 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200194 return ImmutableString(newName);
Olli Etuahod4529f32017-12-12 13:06:40 +0200195}
196
Olli Etuaho1bb85282017-12-14 13:39:53 +0200197bool TFunction::isMain() const
198{
Olli Etuahofbb1c792018-01-19 16:26:59 +0200199 return symbolType() == SymbolType::UserDefined && name() == kMainName;
Olli Etuaho1bb85282017-12-14 13:39:53 +0200200}
201
202bool TFunction::isImageFunction() const
203{
204 return symbolType() == SymbolType::BuiltIn &&
Olli Etuahofbb1c792018-01-19 16:26:59 +0200205 (name() == kImageSizeName || name() == kImageLoadName || name() == kImageStoreName);
Olli Etuaho1bb85282017-12-14 13:39:53 +0200206}
207
Olli Etuahod4529f32017-12-12 13:06:40 +0200208} // namespace sh