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