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 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 15 | #include "compiler/translator/ImmutableStringBuilder.h" |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 16 | #include "compiler/translator/SymbolTable.h" |
| 17 | |
| 18 | namespace sh |
| 19 | { |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 24 | constexpr const ImmutableString kMainName("main"); |
| 25 | constexpr const ImmutableString kImageLoadName("imageLoad"); |
| 26 | constexpr const ImmutableString kImageStoreName("imageStore"); |
| 27 | constexpr const ImmutableString kImageSizeName("imageSize"); |
| 28 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 29 | static const char kFunctionMangledNameSeparator = '('; |
| 30 | |
| 31 | } // anonymous namespace |
| 32 | |
| 33 | TSymbol::TSymbol(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 34 | const ImmutableString &name, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 35 | SymbolType symbolType, |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 36 | SymbolClass symbolClass, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 37 | TExtension extension) |
| 38 | : mName(name), |
| 39 | mUniqueId(symbolTable->nextUniqueId()), |
| 40 | mSymbolType(symbolType), |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 41 | mExtension(extension), |
| 42 | mSymbolClass(symbolClass) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 43 | { |
| 44 | ASSERT(mSymbolType == SymbolType::BuiltIn || mExtension == TExtension::UNDEFINED); |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 45 | ASSERT(mName != "" || mSymbolType == SymbolType::AngleInternal || |
Olli Etuaho | 95ed194 | 2018-02-01 14:01:19 +0200 | [diff] [blame] | 46 | mSymbolType == SymbolType::Empty); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 49 | ImmutableString TSymbol::name() const |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 50 | { |
Olli Etuaho | 40b1c77 | 2018-03-19 10:29:37 +0200 | [diff] [blame] | 51 | if (!mName.empty()) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 52 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 53 | return mName; |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 54 | } |
Olli Etuaho | 40b1c77 | 2018-03-19 10:29:37 +0200 | [diff] [blame] | 55 | // This can be called for nameless function parameters in HLSL. |
| 56 | ASSERT(mSymbolType == SymbolType::AngleInternal || |
| 57 | (mSymbolType == SymbolType::Empty && isVariable())); |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 58 | int uniqueId = mUniqueId.get(); |
| 59 | ImmutableStringBuilder symbolNameOut(sizeof(uniqueId) * 2u + 1u); |
| 60 | symbolNameOut << 's'; |
| 61 | symbolNameOut.appendHex(mUniqueId.get()); |
| 62 | return symbolNameOut; |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 65 | ImmutableString TSymbol::getMangledName() const |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 66 | { |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 67 | 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 Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 73 | ASSERT(mSymbolType != SymbolType::Empty); |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 74 | return name(); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TVariable::TVariable(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 78 | const ImmutableString &name, |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 79 | const TType *type, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 80 | SymbolType symbolType, |
| 81 | TExtension extension) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 82 | : TSymbol(symbolTable, name, symbolType, SymbolClass::Variable, extension), |
| 83 | mType(type), |
| 84 | unionArray(nullptr) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 85 | { |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 86 | ASSERT(mType); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TStructure::TStructure(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 90 | const ImmutableString &name, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 91 | const TFieldList *fields, |
| 92 | SymbolType symbolType) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 93 | : TSymbol(symbolTable, name, symbolType, SymbolClass::Struct), TFieldListCollection(fields) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 94 | { |
| 95 | } |
| 96 | |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 97 | TStructure::TStructure(const TSymbolUniqueId &id, |
| 98 | const ImmutableString &name, |
| 99 | TExtension extension, |
| 100 | const TFieldList *fields) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 101 | : TSymbol(id, name, SymbolType::BuiltIn, extension, SymbolClass::Struct), |
| 102 | TFieldListCollection(fields) |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 103 | { |
| 104 | } |
| 105 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 106 | void TStructure::createSamplerSymbols(const char *namePrefix, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 107 | const TString &apiNamePrefix, |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 108 | TVector<const TVariable *> *outputSymbols, |
| 109 | TMap<const TVariable *, TString> *outputSymbolsToAPINames, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 110 | 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 Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 118 | 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 Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 128 | void TStructure::setName(const ImmutableString &name) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 129 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 130 | ImmutableString *mutableName = const_cast<ImmutableString *>(&mName); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 131 | *mutableName = name; |
| 132 | } |
| 133 | |
| 134 | TInterfaceBlock::TInterfaceBlock(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 135 | const ImmutableString &name, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 136 | const TFieldList *fields, |
| 137 | const TLayoutQualifier &layoutQualifier, |
| 138 | SymbolType symbolType, |
| 139 | TExtension extension) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 140 | : TSymbol(symbolTable, name, symbolType, SymbolClass::InterfaceBlock, extension), |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 141 | TFieldListCollection(fields), |
| 142 | mBlockStorage(layoutQualifier.blockStorage), |
| 143 | mBinding(layoutQualifier.binding) |
| 144 | { |
| 145 | ASSERT(name != nullptr); |
| 146 | } |
| 147 | |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 148 | TInterfaceBlock::TInterfaceBlock(const TSymbolUniqueId &id, |
| 149 | const ImmutableString &name, |
| 150 | TExtension extension, |
| 151 | const TFieldList *fields) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 152 | : TSymbol(id, name, SymbolType::BuiltIn, extension, SymbolClass::InterfaceBlock), |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 153 | TFieldListCollection(fields), |
| 154 | mBlockStorage(EbsUnspecified), |
| 155 | mBinding(0) |
| 156 | { |
| 157 | } |
| 158 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 159 | TFunction::TFunction(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 160 | const ImmutableString &name, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 161 | SymbolType symbolType, |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 162 | const TType *retType, |
| 163 | bool knownToNotHaveSideEffects) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame^] | 164 | : TSymbol(symbolTable, name, symbolType, SymbolClass::Function, TExtension::UNDEFINED), |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 165 | mParametersVector(new TParamVector()), |
| 166 | mParameters(nullptr), |
| 167 | mParamCount(0u), |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 168 | returnType(retType), |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 169 | mMangledName(""), |
| 170 | mOp(EOpNull), |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 171 | defined(false), |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 172 | mHasPrototypeDeclaration(false), |
| 173 | mKnownToNotHaveSideEffects(knownToNotHaveSideEffects) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 174 | { |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 175 | // Functions with an empty name are not allowed. |
| 176 | ASSERT(symbolType != SymbolType::Empty); |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 177 | ASSERT(name != nullptr || symbolType == SymbolType::AngleInternal); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 180 | void TFunction::addParameter(const TVariable *p) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 181 | { |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 182 | ASSERT(mParametersVector); |
| 183 | mParametersVector->push_back(p); |
| 184 | mParameters = mParametersVector->data(); |
| 185 | mParamCount = mParametersVector->size(); |
| 186 | mMangledName = ImmutableString(""); |
| 187 | } |
| 188 | |
| 189 | void TFunction::shareParameters(const TFunction ¶metersSource) |
| 190 | { |
| 191 | mParametersVector = nullptr; |
| 192 | mParameters = parametersSource.mParameters; |
| 193 | mParamCount = parametersSource.mParamCount; |
| 194 | ASSERT(parametersSource.name() == name()); |
| 195 | mMangledName = parametersSource.mMangledName; |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 198 | ImmutableString TFunction::buildMangledName() const |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 199 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 200 | std::string newName(name().data(), name().length()); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 201 | newName += kFunctionMangledNameSeparator; |
| 202 | |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 203 | for (size_t i = 0u; i < mParamCount; ++i) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 204 | { |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 205 | newName += mParameters[i]->getType().getMangledName(); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 206 | } |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 207 | return ImmutableString(newName); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 210 | bool TFunction::isMain() const |
| 211 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 212 | return symbolType() == SymbolType::UserDefined && name() == kMainName; |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | bool TFunction::isImageFunction() const |
| 216 | { |
| 217 | return symbolType() == SymbolType::BuiltIn && |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 218 | (name() == kImageSizeName || name() == kImageLoadName || name() == kImageStoreName); |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 221 | } // namespace sh |