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 || |
Olli Etuaho | 95ed194 | 2018-02-01 14:01:19 +0200 | [diff] [blame] | 38 | 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, |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 62 | const TType *type, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 63 | SymbolType symbolType, |
| 64 | TExtension extension) |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 65 | : TSymbol(symbolTable, name, symbolType, extension), mType(type), unionArray(nullptr) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 66 | { |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 67 | ASSERT(mType); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TStructure::TStructure(TSymbolTable *symbolTable, |
| 71 | const TString *name, |
| 72 | const TFieldList *fields, |
| 73 | SymbolType symbolType) |
| 74 | : TSymbol(symbolTable, name, symbolType), TFieldListCollection(fields) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void TStructure::createSamplerSymbols(const TString &namePrefix, |
| 79 | const TString &apiNamePrefix, |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 80 | TVector<const TVariable *> *outputSymbols, |
| 81 | TMap<const TVariable *, TString> *outputSymbolsToAPINames, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 82 | TSymbolTable *symbolTable) const |
| 83 | { |
| 84 | ASSERT(containsSamplers()); |
| 85 | for (const auto *field : *mFields) |
| 86 | { |
| 87 | const TType *fieldType = field->type(); |
| 88 | if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers()) |
| 89 | { |
| 90 | TString fieldName = namePrefix + "_" + field->name(); |
| 91 | TString fieldApiName = apiNamePrefix + "." + field->name(); |
| 92 | fieldType->createSamplerSymbols(fieldName, fieldApiName, outputSymbols, |
| 93 | outputSymbolsToAPINames, symbolTable); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void TStructure::setName(const TString &name) |
| 99 | { |
| 100 | TString *mutableName = const_cast<TString *>(mName); |
| 101 | *mutableName = name; |
| 102 | } |
| 103 | |
| 104 | TInterfaceBlock::TInterfaceBlock(TSymbolTable *symbolTable, |
| 105 | const TString *name, |
| 106 | const TFieldList *fields, |
| 107 | const TLayoutQualifier &layoutQualifier, |
| 108 | SymbolType symbolType, |
| 109 | TExtension extension) |
| 110 | : TSymbol(symbolTable, name, symbolType, extension), |
| 111 | TFieldListCollection(fields), |
| 112 | mBlockStorage(layoutQualifier.blockStorage), |
| 113 | mBinding(layoutQualifier.binding) |
| 114 | { |
| 115 | ASSERT(name != nullptr); |
| 116 | } |
| 117 | |
| 118 | TFunction::TFunction(TSymbolTable *symbolTable, |
| 119 | const TString *name, |
| 120 | const TType *retType, |
| 121 | SymbolType symbolType, |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 122 | bool knownToNotHaveSideEffects, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 123 | TOperator tOp, |
| 124 | TExtension extension) |
| 125 | : TSymbol(symbolTable, name, symbolType, extension), |
| 126 | returnType(retType), |
| 127 | mangledName(nullptr), |
| 128 | op(tOp), |
| 129 | defined(false), |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 130 | mHasPrototypeDeclaration(false), |
| 131 | mKnownToNotHaveSideEffects(knownToNotHaveSideEffects) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 132 | { |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 133 | // Functions with an empty name are not allowed. |
| 134 | ASSERT(symbolType != SymbolType::Empty); |
| 135 | ASSERT(name != nullptr || symbolType == SymbolType::AngleInternal || tOp != EOpNull); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 138 | TFunction::~TFunction() |
| 139 | { |
Olli Etuaho | 68981eb | 2018-01-23 17:46:12 +0200 | [diff] [blame] | 140 | // Just here to discourage the compiler from inlining it. |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void TFunction::clearParameters() |
| 144 | { |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 145 | parameters.clear(); |
| 146 | mangledName = nullptr; |
| 147 | } |
| 148 | |
| 149 | void TFunction::swapParameters(const TFunction ¶metersSource) |
| 150 | { |
| 151 | clearParameters(); |
| 152 | for (auto parameter : parametersSource.parameters) |
| 153 | { |
| 154 | addParameter(parameter); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | const TString *TFunction::buildMangledName() const |
| 159 | { |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 160 | std::string newName = name().c_str(); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 161 | newName += kFunctionMangledNameSeparator; |
| 162 | |
| 163 | for (const auto &p : parameters) |
| 164 | { |
| 165 | newName += p.type->getMangledName(); |
| 166 | } |
| 167 | return NewPoolTString(newName.c_str()); |
| 168 | } |
| 169 | |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 170 | bool TFunction::isMain() const |
| 171 | { |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 172 | return symbolType() == SymbolType::UserDefined && name() == "main"; |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | bool TFunction::isImageFunction() const |
| 176 | { |
| 177 | return symbolType() == SymbolType::BuiltIn && |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 178 | (name() == "imageSize" || name() == "imageLoad" || name() == "imageStore"); |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 181 | } // namespace sh |