Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 1 | // |
| 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 | |
| 17 | namespace sh |
| 18 | { |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | static const char kFunctionMangledNameSeparator = '('; |
| 24 | |
| 25 | } // anonymous namespace |
| 26 | |
| 27 | TSymbol::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); |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 39 | ASSERT(mName == nullptr || *mName != ""); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 42 | const TString &TSymbol::name() const |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 43 | { |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 44 | if (mName != nullptr) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 45 | { |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 46 | return *mName; |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 47 | } |
| 48 | ASSERT(mSymbolType == SymbolType::AngleInternal); |
| 49 | TInfoSinkBase symbolNameOut; |
| 50 | symbolNameOut << "s" << mUniqueId.get(); |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 51 | return *NewPoolTString(symbolNameOut.c_str()); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | const TString &TSymbol::getMangledName() const |
| 55 | { |
| 56 | ASSERT(mSymbolType != SymbolType::Empty); |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 57 | return name(); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TVariable::TVariable(TSymbolTable *symbolTable, |
| 61 | const TString *name, |
| 62 | const TType &t, |
| 63 | SymbolType symbolType, |
| 64 | TExtension extension) |
| 65 | : TSymbol(symbolTable, name, symbolType, extension), type(t), unionArray(nullptr) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | TStructure::TStructure(TSymbolTable *symbolTable, |
| 70 | const TString *name, |
| 71 | const TFieldList *fields, |
| 72 | SymbolType symbolType) |
| 73 | : TSymbol(symbolTable, name, symbolType), TFieldListCollection(fields) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | void TStructure::createSamplerSymbols(const TString &namePrefix, |
| 78 | const TString &apiNamePrefix, |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame^] | 79 | TVector<const TVariable *> *outputSymbols, |
| 80 | TMap<const TVariable *, TString> *outputSymbolsToAPINames, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 81 | TSymbolTable *symbolTable) const |
| 82 | { |
| 83 | ASSERT(containsSamplers()); |
| 84 | for (const auto *field : *mFields) |
| 85 | { |
| 86 | const TType *fieldType = field->type(); |
| 87 | if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers()) |
| 88 | { |
| 89 | TString fieldName = namePrefix + "_" + field->name(); |
| 90 | TString fieldApiName = apiNamePrefix + "." + field->name(); |
| 91 | fieldType->createSamplerSymbols(fieldName, fieldApiName, outputSymbols, |
| 92 | outputSymbolsToAPINames, symbolTable); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void TStructure::setName(const TString &name) |
| 98 | { |
| 99 | TString *mutableName = const_cast<TString *>(mName); |
| 100 | *mutableName = name; |
| 101 | } |
| 102 | |
| 103 | TInterfaceBlock::TInterfaceBlock(TSymbolTable *symbolTable, |
| 104 | const TString *name, |
| 105 | const TFieldList *fields, |
| 106 | const TLayoutQualifier &layoutQualifier, |
| 107 | SymbolType symbolType, |
| 108 | TExtension extension) |
| 109 | : TSymbol(symbolTable, name, symbolType, extension), |
| 110 | TFieldListCollection(fields), |
| 111 | mBlockStorage(layoutQualifier.blockStorage), |
| 112 | mBinding(layoutQualifier.binding) |
| 113 | { |
| 114 | ASSERT(name != nullptr); |
| 115 | } |
| 116 | |
| 117 | TFunction::TFunction(TSymbolTable *symbolTable, |
| 118 | const TString *name, |
| 119 | const TType *retType, |
| 120 | SymbolType symbolType, |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 121 | bool knownToNotHaveSideEffects, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 122 | TOperator tOp, |
| 123 | TExtension extension) |
| 124 | : TSymbol(symbolTable, name, symbolType, extension), |
| 125 | returnType(retType), |
| 126 | mangledName(nullptr), |
| 127 | op(tOp), |
| 128 | defined(false), |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 129 | mHasPrototypeDeclaration(false), |
| 130 | mKnownToNotHaveSideEffects(knownToNotHaveSideEffects) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 131 | { |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 132 | // Functions with an empty name are not allowed. |
| 133 | ASSERT(symbolType != SymbolType::Empty); |
| 134 | ASSERT(name != nullptr || symbolType == SymbolType::AngleInternal || tOp != EOpNull); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // |
| 138 | // Functions have buried pointers to delete. |
| 139 | // |
| 140 | TFunction::~TFunction() |
| 141 | { |
| 142 | clearParameters(); |
| 143 | } |
| 144 | |
| 145 | void TFunction::clearParameters() |
| 146 | { |
| 147 | for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i) |
| 148 | delete (*i).type; |
| 149 | parameters.clear(); |
| 150 | mangledName = nullptr; |
| 151 | } |
| 152 | |
| 153 | void TFunction::swapParameters(const TFunction ¶metersSource) |
| 154 | { |
| 155 | clearParameters(); |
| 156 | for (auto parameter : parametersSource.parameters) |
| 157 | { |
| 158 | addParameter(parameter); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | const TString *TFunction::buildMangledName() const |
| 163 | { |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 164 | std::string newName = name().c_str(); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 165 | newName += kFunctionMangledNameSeparator; |
| 166 | |
| 167 | for (const auto &p : parameters) |
| 168 | { |
| 169 | newName += p.type->getMangledName(); |
| 170 | } |
| 171 | return NewPoolTString(newName.c_str()); |
| 172 | } |
| 173 | |
| 174 | const TString &TFunction::GetMangledNameFromCall(const TString &functionName, |
| 175 | const TIntermSequence &arguments) |
| 176 | { |
| 177 | std::string newName = functionName.c_str(); |
| 178 | newName += kFunctionMangledNameSeparator; |
| 179 | |
| 180 | for (TIntermNode *argument : arguments) |
| 181 | { |
| 182 | newName += argument->getAsTyped()->getType().getMangledName(); |
| 183 | } |
| 184 | return *NewPoolTString(newName.c_str()); |
| 185 | } |
| 186 | |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 187 | bool TFunction::isMain() const |
| 188 | { |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 189 | return symbolType() == SymbolType::UserDefined && name() == "main"; |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | bool TFunction::isImageFunction() const |
| 193 | { |
| 194 | return symbolType() == SymbolType::BuiltIn && |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 195 | (name() == "imageSize" || name() == "imageLoad" || name() == "imageStore"); |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 198 | } // namespace sh |