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); |
Jamie Madill | e033999 | 2018-06-20 11:46:40 -0400 | [diff] [blame^] | 87 | ASSERT(name.empty() || symbolType != SymbolType::Empty); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TStructure::TStructure(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 91 | const ImmutableString &name, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 92 | const TFieldList *fields, |
| 93 | SymbolType symbolType) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame] | 94 | : TSymbol(symbolTable, name, symbolType, SymbolClass::Struct), TFieldListCollection(fields) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 95 | { |
| 96 | } |
| 97 | |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 98 | TStructure::TStructure(const TSymbolUniqueId &id, |
| 99 | const ImmutableString &name, |
| 100 | TExtension extension, |
| 101 | const TFieldList *fields) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame] | 102 | : TSymbol(id, name, SymbolType::BuiltIn, extension, SymbolClass::Struct), |
| 103 | TFieldListCollection(fields) |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 104 | { |
| 105 | } |
| 106 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 107 | void TStructure::createSamplerSymbols(const char *namePrefix, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 108 | const TString &apiNamePrefix, |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 109 | TVector<const TVariable *> *outputSymbols, |
| 110 | TMap<const TVariable *, TString> *outputSymbolsToAPINames, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 111 | TSymbolTable *symbolTable) const |
| 112 | { |
| 113 | ASSERT(containsSamplers()); |
| 114 | for (const auto *field : *mFields) |
| 115 | { |
| 116 | const TType *fieldType = field->type(); |
| 117 | if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers()) |
| 118 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 119 | std::stringstream fieldName; |
| 120 | fieldName << namePrefix << "_" << field->name(); |
| 121 | TString fieldApiName = apiNamePrefix + "."; |
| 122 | fieldApiName += field->name().data(); |
| 123 | fieldType->createSamplerSymbols(ImmutableString(fieldName.str()), fieldApiName, |
| 124 | outputSymbols, outputSymbolsToAPINames, symbolTable); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 129 | void TStructure::setName(const ImmutableString &name) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 130 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 131 | ImmutableString *mutableName = const_cast<ImmutableString *>(&mName); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 132 | *mutableName = name; |
| 133 | } |
| 134 | |
| 135 | TInterfaceBlock::TInterfaceBlock(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 136 | const ImmutableString &name, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 137 | const TFieldList *fields, |
| 138 | const TLayoutQualifier &layoutQualifier, |
| 139 | SymbolType symbolType, |
| 140 | TExtension extension) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame] | 141 | : TSymbol(symbolTable, name, symbolType, SymbolClass::InterfaceBlock, extension), |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 142 | TFieldListCollection(fields), |
| 143 | mBlockStorage(layoutQualifier.blockStorage), |
| 144 | mBinding(layoutQualifier.binding) |
| 145 | { |
| 146 | ASSERT(name != nullptr); |
| 147 | } |
| 148 | |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 149 | TInterfaceBlock::TInterfaceBlock(const TSymbolUniqueId &id, |
| 150 | const ImmutableString &name, |
| 151 | TExtension extension, |
| 152 | const TFieldList *fields) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame] | 153 | : TSymbol(id, name, SymbolType::BuiltIn, extension, SymbolClass::InterfaceBlock), |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 154 | TFieldListCollection(fields), |
| 155 | mBlockStorage(EbsUnspecified), |
| 156 | mBinding(0) |
| 157 | { |
| 158 | } |
| 159 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 160 | TFunction::TFunction(TSymbolTable *symbolTable, |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 161 | const ImmutableString &name, |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 162 | SymbolType symbolType, |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 163 | const TType *retType, |
| 164 | bool knownToNotHaveSideEffects) |
Olli Etuaho | 8073a95 | 2018-05-09 16:41:39 +0300 | [diff] [blame] | 165 | : TSymbol(symbolTable, name, symbolType, SymbolClass::Function, TExtension::UNDEFINED), |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 166 | mParametersVector(new TParamVector()), |
| 167 | mParameters(nullptr), |
| 168 | mParamCount(0u), |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 169 | returnType(retType), |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 170 | mMangledName(""), |
| 171 | mOp(EOpNull), |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 172 | defined(false), |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 173 | mHasPrototypeDeclaration(false), |
| 174 | mKnownToNotHaveSideEffects(knownToNotHaveSideEffects) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 175 | { |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 176 | // Functions with an empty name are not allowed. |
| 177 | ASSERT(symbolType != SymbolType::Empty); |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 178 | ASSERT(name != nullptr || symbolType == SymbolType::AngleInternal); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 181 | void TFunction::addParameter(const TVariable *p) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 182 | { |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 183 | ASSERT(mParametersVector); |
| 184 | mParametersVector->push_back(p); |
| 185 | mParameters = mParametersVector->data(); |
| 186 | mParamCount = mParametersVector->size(); |
| 187 | mMangledName = ImmutableString(""); |
| 188 | } |
| 189 | |
| 190 | void TFunction::shareParameters(const TFunction ¶metersSource) |
| 191 | { |
| 192 | mParametersVector = nullptr; |
| 193 | mParameters = parametersSource.mParameters; |
| 194 | mParamCount = parametersSource.mParamCount; |
| 195 | ASSERT(parametersSource.name() == name()); |
| 196 | mMangledName = parametersSource.mMangledName; |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 199 | ImmutableString TFunction::buildMangledName() const |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 200 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 201 | std::string newName(name().data(), name().length()); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 202 | newName += kFunctionMangledNameSeparator; |
| 203 | |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 204 | for (size_t i = 0u; i < mParamCount; ++i) |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 205 | { |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 206 | newName += mParameters[i]->getType().getMangledName(); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 207 | } |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 208 | return ImmutableString(newName); |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 211 | bool TFunction::isMain() const |
| 212 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 213 | return symbolType() == SymbolType::UserDefined && name() == kMainName; |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | bool TFunction::isImageFunction() const |
| 217 | { |
| 218 | return symbolType() == SymbolType::BuiltIn && |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 219 | (name() == kImageSizeName || name() == kImageLoadName || name() == kImageStoreName); |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 222 | } // namespace sh |