Types: Fix #1290: Rationalize and correct "mixed" style array dimensioning.

There a couple functional problems, which when reduced down also led to
some good simplifications and rationalization.  So, this commit:
 - corrects "mixed" functionality: int[A] f[B] -> f[B][A]
 - correct multi-identifier decls: int[A] f[B], g[C] -> f and g are independently sized.
 - increases symmetry between different places in the code that do this
 - makes fewer ways to do the same thing; several methods are just gone now
 - makes more clear when something is copied or shared
This commit is contained in:
John Kessenich 2018-03-26 00:38:53 -06:00
parent 1c3ab274b1
commit 859b0342b8
17 changed files with 1323 additions and 1289 deletions

View file

@ -1315,31 +1315,9 @@ public:
void makeVector() { vector1 = true; }
// Merge type from parent, where a parentType is at the beginning of a declaration,
// establishing some characteristics for all subsequent names, while this type
// is on the individual names.
void mergeType(const TPublicType& parentType)
{
// arrayness is currently the only child aspect that has to be preserved
basicType = parentType.basicType;
vectorSize = parentType.vectorSize;
matrixCols = parentType.matrixCols;
matrixRows = parentType.matrixRows;
vector1 = false; // TPublicType is only GLSL which so far has no vec1
qualifier = parentType.qualifier;
sampler = parentType.sampler;
if (parentType.arraySizes)
newArraySizes(*parentType.arraySizes);
if (parentType.userDef) {
structure = parentType.userDef->getWritableStruct();
setTypeName(parentType.userDef->getTypeName());
}
}
virtual void hideMember() { basicType = EbtVoid; vectorSize = 1; }
virtual bool hiddenMember() const { return basicType == EbtVoid; }
virtual void setTypeName(const TString& n) { typeName = NewPoolTString(n.c_str()); }
virtual void setFieldName(const TString& n) { fieldName = NewPoolTString(n.c_str()); }
virtual const TString& getTypeName() const
{
@ -1369,7 +1347,7 @@ public:
virtual bool isArrayOfArrays() const { return arraySizes != nullptr && arraySizes->getNumDims() > 1; }
virtual int getImplicitArraySize() const { return arraySizes->getImplicitSize(); }
virtual const TArraySizes* getArraySizes() const { return arraySizes; }
virtual TArraySizes& getArraySizes() { assert(arraySizes != nullptr); return *arraySizes; }
virtual TArraySizes* getArraySizes() { return arraySizes; }
virtual bool isScalar() const { return ! isVector() && ! isMatrix() && ! isStruct() && ! isArray(); }
virtual bool isScalarOrVec1() const { return isScalar() || vector1; }
@ -1503,22 +1481,33 @@ public:
assert(type.arraySizes != nullptr);
*arraySizes = *type.arraySizes;
}
void newArraySizes(const TArraySizes& s)
void copyArraySizes(const TArraySizes& s)
{
// For setting a fresh new set of array sizes, not yet worrying about sharing.
arraySizes = new TArraySizes;
*arraySizes = s;
}
void transferArraySizes(TArraySizes* s)
{
// For setting an already allocated set of sizes that this type can use
// (no copy made).
arraySizes = s;
}
void clearArraySizes()
{
arraySizes = 0;
arraySizes = nullptr;
}
void addArrayOuterSizes(const TArraySizes& s)
// Add inner array sizes, to any existing sizes, via copy; the
// sizes passed in can still be reused for other purposes.
void copyArrayInnerSizes(const TArraySizes* s)
{
if (arraySizes == nullptr)
newArraySizes(s);
else
arraySizes->addOuterSizes(s);
if (s != nullptr) {
if (arraySizes == nullptr)
copyArraySizes(*s);
else
arraySizes->addInnerSizes(*s);
}
}
void changeOuterArraySize(int s) { arraySizes->changeOuterSize(s); }
void setImplicitArraySize(int s) { arraySizes->setImplicitSize(s); }

View file

@ -130,10 +130,10 @@ struct TSmallArrayVector {
sizes->push_back(pair);
}
void push_front(const TSmallArrayVector& newDims)
void push_back(const TSmallArrayVector& newDims)
{
alloc();
sizes->insert(sizes->begin(), newDims.sizes->begin(), newDims.sizes->end());
sizes->insert(sizes->end(), newDims.sizes->begin(), newDims.sizes->end());
}
void pop_front()
@ -252,6 +252,7 @@ struct TArraySizes {
void addInnerSize(int s) { addInnerSize((unsigned)s, nullptr); }
void addInnerSize(int s, TIntermTyped* n) { sizes.push_back((unsigned)s, n); }
void addInnerSize(TArraySize pair) { sizes.push_back(pair.size, pair.node); }
void addInnerSizes(const TArraySizes& s) { sizes.push_back(s.sizes); }
void changeOuterSize(int s) { sizes.changeFront((unsigned)s); }
int getImplicitSize() const { return (int)implicitArraySize; }
void setImplicitSize(int s) { implicitArraySize = s; }
@ -288,7 +289,6 @@ struct TArraySizes {
}
bool isImplicit() const { return getOuterSize() == UnsizedArraySize || isInnerImplicit(); }
void addOuterSizes(const TArraySizes& s) { sizes.push_front(s.sizes); }
void dereference() { sizes.pop_front(); }
void copyDereferenced(const TArraySizes& rhs)
{

View file

@ -7981,9 +7981,9 @@ void TBuiltIns::identifyBuiltIns(int version, EProfile profile, const SpvVersion
if (version == 100 || IncludeLegacy(version, profile, spvVersion) || (! ForwardCompatibility && profile != EEsProfile && version < 420)) {
TPrecisionQualifier pq = profile == EEsProfile ? EpqMedium : EpqNone;
TType fragData(EbtFloat, EvqFragColor, pq, 4);
TArraySizes& arraySizes = *new TArraySizes;
arraySizes.addInnerSize(resources.maxDrawBuffers);
fragData.newArraySizes(arraySizes);
TArraySizes* arraySizes = new TArraySizes;
arraySizes->addInnerSize(resources.maxDrawBuffers);
fragData.transferArraySizes(arraySizes);
symbolTable.insert(*new TVariable(NewPoolTString("gl_FragData"), fragData));
SpecialQualifier("gl_FragData", EvqFragColor, EbvFragData, symbolTable);
}

View file

@ -2393,10 +2393,11 @@ bool TParseContext::constructorError(const TSourceLoc& loc, TIntermNode* node, T
// Types have to match, but we're still making the type.
// Finish making the type, and the comparison is done later
// when checking for conversion.
TArraySizes& arraySizes = type.getArraySizes();
TArraySizes& arraySizes = *type.getArraySizes();
// At least the dimensionalities have to match.
if (! function[0].type->isArray() || arraySizes.getNumDims() != function[0].type->getArraySizes().getNumDims() + 1) {
if (! function[0].type->isArray() ||
arraySizes.getNumDims() != function[0].type->getArraySizes()->getNumDims() + 1) {
error(loc, "array constructor argument not correct type to construct array element", "constructor", "");
return true;
}
@ -2406,7 +2407,7 @@ bool TParseContext::constructorError(const TSourceLoc& loc, TIntermNode* node, T
// That means we need to adopt (from the first argument) the other array sizes into the type.
for (int d = 1; d < arraySizes.getNumDims(); ++d) {
if (arraySizes.getDimSize(d) == UnsizedArraySize) {
arraySizes.setDimSize(d, function[0].type->getArraySizes().getDimSize(d - 1));
arraySizes.setDimSize(d, function[0].type->getArraySizes()->getDimSize(d - 1));
}
}
}
@ -3155,8 +3156,11 @@ void TParseContext::arraySizesCheck(const TSourceLoc& loc, const TQualifier& qua
arraySizeRequiredCheck(loc, *arraySizes);
}
void TParseContext::arrayOfArrayVersionCheck(const TSourceLoc& loc)
void TParseContext::arrayOfArrayVersionCheck(const TSourceLoc& loc, const TArraySizes* sizes)
{
if (sizes == nullptr || sizes->getNumDims() == 1)
return;
const char* feature = "arrays of arrays";
requireProfile(loc, EEsProfile | ECoreProfile | ECompatibilityProfile, feature);
@ -3164,36 +3168,6 @@ void TParseContext::arrayOfArrayVersionCheck(const TSourceLoc& loc)
profileRequires(loc, ECoreProfile | ECompatibilityProfile, 430, nullptr, feature);
}
void TParseContext::arrayDimCheck(const TSourceLoc& loc, const TArraySizes* sizes1, const TArraySizes* sizes2)
{
if ((sizes1 && sizes2) ||
(sizes1 && sizes1->getNumDims() > 1) ||
(sizes2 && sizes2->getNumDims() > 1))
arrayOfArrayVersionCheck(loc);
}
void TParseContext::arrayDimCheck(const TSourceLoc& loc, const TType* type, const TArraySizes* sizes2)
{
// skip checking for multiple dimensions on the type; it was caught earlier
if ((type && type->isArray() && sizes2) ||
(sizes2 && sizes2->getNumDims() > 1))
arrayOfArrayVersionCheck(loc);
}
// Merge array dimensions listed in 'sizes' onto the type's array dimensions.
//
// From the spec: "vec4[2] a[3]; // size-3 array of size-2 array of vec4"
//
// That means, the 'sizes' go in front of the 'type' as outermost sizes.
// 'type' is the type part of the declaration (to the left)
// 'sizes' is the arrayness tagged on the identifier (to the right)
//
void TParseContext::arrayDimMerge(TType& type, const TArraySizes* sizes)
{
if (sizes != nullptr)
type.addArrayOuterSizes(*sizes);
}
//
// Do all the semantic checking for declaring or redeclaring an array, with and
// without a size, and make the right changes to the symbol table.
@ -3680,7 +3654,7 @@ void TParseContext::redeclareBuiltinBlock(const TSourceLoc& loc, TTypeList& newT
else if (type.isArray()) {
if (type.isExplicitlySizedArray() && arraySizes->getOuterSize() == UnsizedArraySize)
error(loc, "block already declared with size, can't redeclare as implicitly-sized", blockName.c_str(), "");
else if (type.isExplicitlySizedArray() && type.getArraySizes() != *arraySizes)
else if (type.isExplicitlySizedArray() && *type.getArraySizes() != *arraySizes)
error(loc, "cannot change array size of redeclared block", blockName.c_str(), "");
else if (type.isImplicitlySizedArray() && arraySizes->getOuterSize() != UnsizedArraySize)
type.changeOuterArraySize(arraySizes->getOuterSize());
@ -4621,7 +4595,8 @@ void TParseContext::layoutObjectCheck(const TSourceLoc& loc, const TSymbol& symb
// they are not allowed on block members. For arrayed interfaces (those generally having an
// extra level of arrayness due to interface expansion), the outer array is stripped before
// applying this rule."
void TParseContext::layoutMemberLocationArrayCheck(const TSourceLoc& loc, bool memberWithLocation, TArraySizes* arraySizes)
void TParseContext::layoutMemberLocationArrayCheck(const TSourceLoc& loc, bool memberWithLocation,
TArraySizes* arraySizes)
{
if (memberWithLocation && arraySizes != nullptr) {
if (arraySizes->getNumDims() > (currentBlockQualifier.isArrayedIo(language) ? 1 : 0))
@ -5325,15 +5300,15 @@ void TParseContext::declareTypeDefaults(const TSourceLoc& loc, const TPublicType
// 'publicType' is the type part of the declaration (to the left)
// 'arraySizes' is the arrayness tagged on the identifier (to the right)
//
TIntermNode* TParseContext::declareVariable(const TSourceLoc& loc, TString& identifier, const TPublicType& publicType, TArraySizes* arraySizes, TIntermTyped* initializer)
TIntermNode* TParseContext::declareVariable(const TSourceLoc& loc, TString& identifier, const TPublicType& publicType,
TArraySizes* arraySizes, TIntermTyped* initializer)
{
TType type(publicType); // shallow copy; 'type' shares the arrayness and structure definition with 'publicType'
if (type.isImplicitlySizedArray()) {
// Because "int[] a = int[2](...), b = int[3](...)" makes two arrays a and b
// of different sizes, for this case sharing the shallow copy of arrayness
// with the publicType oversubscribes it, so get a deep copy of the arrayness.
type.newArraySizes(*publicType.arraySizes);
}
// Make a fresh type that combines the characteristics from the individual
// identifier syntax and the declaration-type syntax.
TType type(publicType);
type.transferArraySizes(arraySizes);
type.copyArrayInnerSizes(publicType.arraySizes);
arrayOfArrayVersionCheck(loc, type.getArraySizes());
if (voidErrorCheck(loc, identifier, type.getBasicType()))
return nullptr;
@ -5360,15 +5335,9 @@ TIntermNode* TParseContext::declareVariable(const TSourceLoc& loc, TString& iden
inheritGlobalDefaults(type.getQualifier());
// Declare the variable
if (arraySizes || type.isArray()) {
// Arrayness is potentially coming both from the type and from the
// variable: "int[] a[];" or just one or the other.
// Merge it all to the type, so all arrayness is part of the type.
arrayDimCheck(loc, &type, arraySizes);
arrayDimMerge(type, arraySizes);
if (type.isArray()) {
// Check that implicit sizing is only where allowed.
arraySizesCheck(loc, type.getQualifier(), &type.getArraySizes(), initializer != nullptr, false);
arraySizesCheck(loc, type.getQualifier(), type.getArraySizes(), initializer != nullptr, false);
if (! arrayQualifierError(loc, type.getQualifier()) && ! arrayError(loc, type))
declareArray(loc, identifier, type, symbol);
@ -5509,8 +5478,10 @@ TIntermNode* TParseContext::executeInitializer(const TSourceLoc& loc, TIntermTyp
variable->getType().getArraySizes()->getNumDims()) {
// adopt unsized sizes from the initializer's sizes
for (int d = 1; d < variable->getType().getArraySizes()->getNumDims(); ++d) {
if (variable->getType().getArraySizes()->getDimSize(d) == UnsizedArraySize)
variable->getWritableType().getArraySizes().setDimSize(d, initializer->getType().getArraySizes()->getDimSize(d));
if (variable->getType().getArraySizes()->getDimSize(d) == UnsizedArraySize) {
variable->getWritableType().getArraySizes()->setDimSize(d,
initializer->getType().getArraySizes()->getDimSize(d));
}
}
}
@ -5621,16 +5592,16 @@ TIntermTyped* TParseContext::convertInitializerList(const TSourceLoc& loc, const
// Later on, initializer execution code will deal with array size logic.
TType arrayType;
arrayType.shallowCopy(type); // sharing struct stuff is fine
arrayType.newArraySizes(*type.getArraySizes()); // but get a fresh copy of the array information, to edit below
arrayType.copyArraySizes(*type.getArraySizes()); // but get a fresh copy of the array information, to edit below
// edit array sizes to fill in unsized dimensions
arrayType.changeOuterArraySize((int)initList->getSequence().size());
TIntermTyped* firstInit = initList->getSequence()[0]->getAsTyped();
if (arrayType.isArrayOfArrays() && firstInit->getType().isArray() &&
arrayType.getArraySizes().getNumDims() == firstInit->getType().getArraySizes()->getNumDims() + 1) {
for (int d = 1; d < arrayType.getArraySizes().getNumDims(); ++d) {
if (arrayType.getArraySizes().getDimSize(d) == UnsizedArraySize)
arrayType.getArraySizes().setDimSize(d, firstInit->getType().getArraySizes()->getDimSize(d - 1));
arrayType.getArraySizes()->getNumDims() == firstInit->getType().getArraySizes()->getNumDims() + 1) {
for (int d = 1; d < arrayType.getArraySizes()->getNumDims(); ++d) {
if (arrayType.getArraySizes()->getDimSize(d) == UnsizedArraySize)
arrayType.getArraySizes()->setDimSize(d, firstInit->getType().getArraySizes()->getDimSize(d - 1));
}
}
@ -5944,13 +5915,14 @@ TIntermTyped* TParseContext::constructAggregate(TIntermNode* node, const TType&
//
// Do everything needed to add an interface block.
//
void TParseContext::declareBlock(const TSourceLoc& loc, TTypeList& typeList, const TString* instanceName, TArraySizes* arraySizes)
void TParseContext::declareBlock(const TSourceLoc& loc, TTypeList& typeList, const TString* instanceName,
TArraySizes* arraySizes)
{
blockStageIoCheck(loc, currentBlockQualifier);
blockQualifierCheck(loc, currentBlockQualifier, instanceName != nullptr);
if (arraySizes) {
if (arraySizes != nullptr) {
arraySizesCheck(loc, currentBlockQualifier, arraySizes, false, false);
arrayDimCheck(loc, arraySizes, 0);
arrayOfArrayVersionCheck(loc, arraySizes);
if (arraySizes->getNumDims() > 1)
requireProfile(loc, ~EEsProfile, "array-of-array of block");
}
@ -5967,7 +5939,7 @@ void TParseContext::declareBlock(const TSourceLoc& loc, TTypeList& typeList, con
if ((currentBlockQualifier.storage == EvqUniform || currentBlockQualifier.storage == EvqBuffer) && (memberQualifier.isInterpolation() || memberQualifier.isAuxiliary()))
error(memberLoc, "member of uniform or buffer block cannot have an auxiliary or interpolation qualifier", memberType.getFieldName().c_str(), "");
if (memberType.isArray())
arraySizesCheck(memberLoc, currentBlockQualifier, &memberType.getArraySizes(), false, member == typeList.size() - 1);
arraySizesCheck(memberLoc, currentBlockQualifier, memberType.getArraySizes(), false, member == typeList.size() - 1);
if (memberQualifier.hasOffset()) {
if (spvVersion.spv == 0) {
requireProfile(memberLoc, ~EEsProfile, "offset on block member");
@ -6089,8 +6061,8 @@ void TParseContext::declareBlock(const TSourceLoc& loc, TTypeList& typeList, con
//
TType blockType(&typeList, *blockName, currentBlockQualifier);
if (arraySizes)
blockType.newArraySizes(*arraySizes);
if (arraySizes != nullptr)
blockType.transferArraySizes(arraySizes);
else
ioArrayCheck(loc, blockType, instanceName ? *instanceName : *blockName);

View file

@ -341,10 +341,7 @@ public:
void arraySizeRequiredCheck(const TSourceLoc&, const TArraySizes&);
void structArrayCheck(const TSourceLoc&, const TType& structure);
void arraySizesCheck(const TSourceLoc&, const TQualifier&, TArraySizes*, bool initializer, bool lastMember);
void arrayOfArrayVersionCheck(const TSourceLoc&);
void arrayDimCheck(const TSourceLoc&, const TArraySizes* sizes1, const TArraySizes* sizes2);
void arrayDimCheck(const TSourceLoc&, const TType*, const TArraySizes*);
void arrayDimMerge(TType& type, const TArraySizes* sizes);
void arrayOfArrayVersionCheck(const TSourceLoc&, const TArraySizes*);
bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType);
void boolCheck(const TSourceLoc&, const TIntermTyped*);
void boolCheck(const TSourceLoc&, const TPublicType&);

View file

@ -934,13 +934,15 @@ parameter_declarator
parseContext.profileRequires($1.loc, EEsProfile, 300, 0, "arrayed type");
parseContext.arraySizeRequiredCheck($1.loc, *$1.arraySizes);
}
parseContext.arrayDimCheck($2.loc, $1.arraySizes, $3.arraySizes);
TType* type = new TType($1);
type->transferArraySizes($3.arraySizes);
type->copyArrayInnerSizes($1.arraySizes);
parseContext.arrayOfArrayVersionCheck($2.loc, type->getArraySizes());
parseContext.arraySizeRequiredCheck($3.loc, *$3.arraySizes);
parseContext.reservedErrorCheck($2.loc, *$2.string);
TParameter param = { $2.string, new TType($1)};
parseContext.arrayDimMerge(*param.type, $3.arraySizes);
TParameter param = { $2.string, type };
$$.loc = $2.loc;
$$.param = param;
@ -1075,7 +1077,7 @@ fully_specified_type
}
if ($2.arraySizes && parseContext.arrayQualifierError($2.loc, $1.qualifier))
$2.arraySizes = 0;
$2.arraySizes = nullptr;
parseContext.checkNoShaderLayouts($2.loc, $1.shaderQualifiers);
$2.shaderQualifiers.merge($1.shaderQualifiers);
@ -1352,7 +1354,7 @@ type_specifier
$$.qualifier.precision = parseContext.getDefaultPrecision($$);
}
| type_specifier_nonarray array_specifier {
parseContext.arrayDimCheck($2.loc, $2.arraySizes, 0);
parseContext.arrayOfArrayVersionCheck($2.loc, $2.arraySizes);
$$ = $1;
$$.qualifier.precision = parseContext.getDefaultPrecision($$);
$$.arraySizes = $2.arraySizes;
@ -3110,8 +3112,12 @@ struct_declaration
parseContext.precisionQualifierCheck($1.loc, $1.basicType, $1.qualifier);
for (unsigned int i = 0; i < $$->size(); ++i) {
parseContext.arrayDimCheck($1.loc, (*$$)[i].type, $1.arraySizes);
(*$$)[i].type->mergeType($1);
TType type($1);
type.setFieldName((*$$)[i].type->getFieldName());
type.transferArraySizes((*$$)[i].type->getArraySizes());
type.copyArrayInnerSizes($1.arraySizes);
parseContext.arrayOfArrayVersionCheck((*$$)[i].loc, type.getArraySizes());
(*$$)[i].type->shallowCopy(type);
}
}
| type_qualifier type_specifier struct_declarator_list SEMICOLON {
@ -3131,8 +3137,12 @@ struct_declaration
parseContext.precisionQualifierCheck($2.loc, $2.basicType, $2.qualifier);
for (unsigned int i = 0; i < $$->size(); ++i) {
parseContext.arrayDimCheck($1.loc, (*$$)[i].type, $2.arraySizes);
(*$$)[i].type->mergeType($2);
TType type($2);
type.setFieldName((*$$)[i].type->getFieldName());
type.transferArraySizes((*$$)[i].type->getArraySizes());
type.copyArrayInnerSizes($2.arraySizes);
parseContext.arrayOfArrayVersionCheck((*$$)[i].loc, type.getArraySizes());
(*$$)[i].type->shallowCopy(type);
}
}
;
@ -3154,12 +3164,12 @@ struct_declarator
$$.type->setFieldName(*$1.string);
}
| IDENTIFIER array_specifier {
parseContext.arrayDimCheck($1.loc, $2.arraySizes, 0);
parseContext.arrayOfArrayVersionCheck($1.loc, $2.arraySizes);
$$.type = new TType(EbtVoid);
$$.loc = $1.loc;
$$.type->setFieldName(*$1.string);
$$.type->newArraySizes(*$2.arraySizes);
$$.type->transferArraySizes($2.arraySizes);
}
;

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,8 @@
/* A Bison parser, made by GNU Bison 3.0.4. */
/* A Bison parser, made by GNU Bison 3.0. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -437,7 +437,7 @@ extern int yydebug;
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE YYSTYPE;
union YYSTYPE
{
#line 70 "MachineIndependent/glslang.y" /* yacc.c:1909 */
@ -477,8 +477,6 @@ union YYSTYPE
#line 479 "MachineIndependent/glslang_tab.cpp.h" /* yacc.c:1909 */
};
typedef union YYSTYPE YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1
#endif