Hash :
bed35d76
Author :
Date :
2017-12-20T16:36:26
Don't query names of empty symbols This makes it possible to return a reference from TSymbol::name() instead of a pointer. This is safer since it completely avoids the possibility of a nullptr dereference. An assert is making sure that the function is not being called for empty symbols. BUG=angleproject:2267 TEST=angle_unittests Change-Id: I44279f65989dbb828322843fc0216ba84d91dedf Reviewed-on: https://chromium-review.googlesource.com/836894 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
//
// Copyright (c) 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Symbol.h: Symbols representing variables, functions, structures and interface blocks.
//
#ifndef COMPILER_TRANSLATOR_SYMBOL_H_
#define COMPILER_TRANSLATOR_SYMBOL_H_
#include "common/angleutils.h"
#include "compiler/translator/ExtensionBehavior.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/SymbolUniqueId.h"
namespace sh
{
class TSymbolTable;
enum class SymbolType
{
BuiltIn,
UserDefined,
AngleInternal,
Empty, // Meaning symbol without a name.
NotResolved
};
// Symbol base class. (Can build functions or variables out of these...)
class TSymbol : angle::NonCopyable
{
public:
POOL_ALLOCATOR_NEW_DELETE();
TSymbol(TSymbolTable *symbolTable,
const TString *name,
SymbolType symbolType,
TExtension extension = TExtension::UNDEFINED);
virtual ~TSymbol()
{
// don't delete name, it's from the pool
}
// Don't call name() or getMangledName() for empty symbols (symbolType == SymbolType::Empty).
const TString &name() const;
virtual const TString &getMangledName() const;
virtual bool isFunction() const { return false; }
virtual bool isVariable() const { return false; }
virtual bool isStruct() const { return false; }
const TSymbolUniqueId &uniqueId() const { return mUniqueId; }
SymbolType symbolType() const { return mSymbolType; }
TExtension extension() const { return mExtension; }
protected:
const TString *const mName;
private:
const TSymbolUniqueId mUniqueId;
const SymbolType mSymbolType;
const TExtension mExtension;
};
// Variable.
// May store the value of a constant variable of any type (float, int, bool or struct).
class TVariable : public TSymbol
{
public:
TVariable(TSymbolTable *symbolTable,
const TString *name,
const TType &t,
SymbolType symbolType,
TExtension ext = TExtension::UNDEFINED);
~TVariable() override {}
bool isVariable() const override { return true; }
TType &getType() { return type; }
const TType &getType() const { return type; }
void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
const TConstantUnion *getConstPointer() const { return unionArray; }
void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
private:
TType type;
const TConstantUnion *unionArray;
};
// Struct type.
class TStructure : public TSymbol, public TFieldListCollection
{
public:
TStructure(TSymbolTable *symbolTable,
const TString *name,
const TFieldList *fields,
SymbolType symbolType);
bool isStruct() const override { return true; }
void createSamplerSymbols(const TString &namePrefix,
const TString &apiNamePrefix,
TVector<TIntermSymbol *> *outputSymbols,
TMap<TIntermSymbol *, TString> *outputSymbolsToAPINames,
TSymbolTable *symbolTable) const;
void setAtGlobalScope(bool atGlobalScope) { mAtGlobalScope = atGlobalScope; }
bool atGlobalScope() const { return mAtGlobalScope; }
private:
// TODO(zmo): Find a way to get rid of the const_cast in function
// setName(). At the moment keep this function private so only
// friend class RegenerateStructNames may call it.
friend class RegenerateStructNames;
void setName(const TString &name);
bool mAtGlobalScope;
};
// Interface block. Note that this contains the block name, not the instance name. Interface block
// instances are stored as TVariable.
class TInterfaceBlock : public TSymbol, public TFieldListCollection
{
public:
TInterfaceBlock(TSymbolTable *symbolTable,
const TString *name,
const TFieldList *fields,
const TLayoutQualifier &layoutQualifier,
SymbolType symbolType,
TExtension extension = TExtension::UNDEFINED);
TLayoutBlockStorage blockStorage() const { return mBlockStorage; }
int blockBinding() const { return mBinding; }
private:
TLayoutBlockStorage mBlockStorage;
int mBinding;
// Note that we only record matrix packing on a per-field granularity.
};
// Immutable version of TParameter.
struct TConstParameter
{
TConstParameter() : name(nullptr), type(nullptr) {}
explicit TConstParameter(const TString *n) : name(n), type(nullptr) {}
explicit TConstParameter(const TType *t) : name(nullptr), type(t) {}
TConstParameter(const TString *n, const TType *t) : name(n), type(t) {}
// Both constructor arguments must be const.
TConstParameter(TString *n, TType *t) = delete;
TConstParameter(const TString *n, TType *t) = delete;
TConstParameter(TString *n, const TType *t) = delete;
const TString *const name;
const TType *const type;
};
// The function sub-class of symbols and the parser will need to
// share this definition of a function parameter.
struct TParameter
{
// Destructively converts to TConstParameter.
// This method resets name and type to nullptrs to make sure
// their content cannot be modified after the call.
TConstParameter turnToConst()
{
const TString *constName = name;
const TType *constType = type;
name = nullptr;
type = nullptr;
return TConstParameter(constName, constType);
}
const TString *name;
TType *type;
};
// The function sub-class of a symbol.
class TFunction : public TSymbol
{
public:
TFunction(TSymbolTable *symbolTable,
const TString *name,
const TType *retType,
SymbolType symbolType,
bool knownToNotHaveSideEffects,
TOperator tOp = EOpNull,
TExtension extension = TExtension::UNDEFINED);
~TFunction() override;
bool isFunction() const override { return true; }
void addParameter(const TConstParameter &p)
{
parameters.push_back(p);
mangledName = nullptr;
}
void swapParameters(const TFunction ¶metersSource);
const TString &getMangledName() const override
{
if (mangledName == nullptr)
{
mangledName = buildMangledName();
}
return *mangledName;
}
static const TString &GetMangledNameFromCall(const TString &functionName,
const TIntermSequence &arguments);
const TType &getReturnType() const { return *returnType; }
TOperator getBuiltInOp() const { return op; }
void setDefined() { defined = true; }
bool isDefined() { return defined; }
void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
size_t getParamCount() const { return parameters.size(); }
const TConstParameter &getParam(size_t i) const { return parameters[i]; }
bool isKnownToNotHaveSideEffects() const { return mKnownToNotHaveSideEffects; }
bool isMain() const;
bool isImageFunction() const;
private:
void clearParameters();
const TString *buildMangledName() const;
typedef TVector<TConstParameter> TParamList;
TParamList parameters;
const TType *returnType;
mutable const TString *mangledName;
// TODO(oetuaho): Remove op from TFunction once TFunction is not used for looking up builtins or
// constructors.
TOperator op;
bool defined;
bool mHasPrototypeDeclaration;
bool mKnownToNotHaveSideEffects;
};
} // namespace sh
#endif // COMPILER_TRANSLATOR_SYMBOL_H_