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