Move the complex pieces of C++ code from glslang.y to ParseHelper.cpp. Updated some tests.

git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@22846 e7fa87d3-cd2b-0410-9028-fcbf551c1848
This commit is contained in:
John Kessenich 2013-08-27 05:57:15 +00:00
parent d46b31fdc5
commit 23bdb29eac
6 changed files with 557 additions and 464 deletions

View file

@ -260,157 +260,13 @@ postfix_expression
$$ = $1;
}
| postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET {
parseContext.variableCheck($1);
if (!$1->isArray() && !$1->isMatrix() && !$1->isVector()) {
if ($1->getAsSymbolNode())
parseContext.error($2.loc, " left of '[' is not of type array, matrix, or vector ", $1->getAsSymbolNode()->getName().c_str(), "");
else
parseContext.error($2.loc, " left of '[' is not of type array, matrix, or vector ", "expression", "");
}
if ($1->getType().getQualifier().storage == EvqConst && $3->getQualifier().storage == EvqConst) {
if ($1->isArray()) { // constant folding for arrays
$$ = parseContext.addConstArrayNode($3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), $1, $2.loc);
} else if ($1->isVector()) { // constant folding for vectors
TVectorFields fields;
fields.num = 1;
fields.offsets[0] = $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(); // need to do it this way because v.xy sends fields integer array
$$ = parseContext.addConstVectorNode(fields, $1, $2.loc);
} else if ($1->isMatrix()) { // constant folding for matrices
$$ = parseContext.addConstMatrixNode($3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), $1, $2.loc);
}
} else {
if ($3->getQualifier().storage == EvqConst) {
int index = $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
if (! $1->isArray() && ($1->isVector() && $1->getType().getVectorSize() <= index ||
$1->isMatrix() && $1->getType().getMatrixCols() <= index))
parseContext.error($2.loc, "", "[", "index out of range '%d'", $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst());
else {
if ($1->isArray()) {
if ($1->getType().getArraySize() == 0) {
if ($1->getType().getMaxArraySize() <= $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst())
parseContext.arraySetMaxSize($1->getAsSymbolNode(), $1->getTypePointer(), $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), true, $2.loc);
else
parseContext.arraySetMaxSize($1->getAsSymbolNode(), $1->getTypePointer(), 0, false, $2.loc);
} else if ( $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst() >= $1->getType().getArraySize() ||
$3->getAsConstantUnion()->getUnionArrayPointer()->getIConst() < 0)
parseContext.error($2.loc, "", "[", "array index out of range '%d'", $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst());
}
$$ = parseContext.intermediate.addIndex(EOpIndexDirect, $1, $3, $2.loc);
}
} else {
if ($1->isArray() && $1->getType().getArraySize() == 0)
parseContext.error($2.loc, "", "[", "array must be redeclared with a size before being indexed with a variable");
if ($1->getBasicType() == EbtBlock)
parseContext.requireProfile($1->getLoc(), static_cast<EProfileMask>(~EEsProfileMask), "variable indexing block array");
if ($1->getBasicType() == EbtSampler) {
parseContext.requireProfile($1->getLoc(), static_cast<EProfileMask>(ECoreProfileMask | ECompatibilityProfileMask), "variable indexing sampler array");
parseContext.profileRequires($1->getLoc(), ECoreProfile, 400, 0, "variable indexing sampler array");
}
$$ = parseContext.intermediate.addIndex(EOpIndexIndirect, $1, $3, $2.loc);
}
}
if ($$ == 0) {
constUnion *unionArray = new constUnion[1];
unionArray->setDConst(0.0);
$$ = parseContext.intermediate.addConstantUnion(unionArray, TType(EbtFloat, EvqConst), $2.loc);
} else {
TType newType($1->getType());
newType.dereference();
$$->setType(newType);
// TODO: functionality: does this drop const qualification for const[const] ?
}
$$ = parseContext.handleBracketDereference($2.loc, $1, $3);
}
| function_call {
$$ = $1;
}
| postfix_expression DOT FIELD_SELECTION {
parseContext.variableCheck($1);
if ($1->isArray()) {
//
// It can only be a method (e.g., length), which can't be resolved until
// we later see the function calling syntax. Save away the name for now.
//
if (*$3.string == "length") {
parseContext.profileRequires($3.loc, ENoProfile, 120, "GL_3DL_array_objects", ".length");
$$ = parseContext.intermediate.addMethod($1, TType(EbtInt), $3.string, $2.loc);
} else {
parseContext.error($3.loc, "only the length method is supported for array", $3.string->c_str(), "");
$$ = $1;
}
} else if ($1->isVector()) {
TVectorFields fields;
if (! parseContext.parseVectorFields($3.loc, *$3.string, $1->getVectorSize(), fields)) {
fields.num = 1;
fields.offsets[0] = 0;
}
if ($1->getType().getQualifier().storage == EvqConst) { // constant folding for vector fields
$$ = parseContext.addConstVectorNode(fields, $1, $3.loc);
if ($$ == 0)
$$ = $1;
else
$$->setType(TType($1->getBasicType(), EvqConst, (int) (*$3.string).size()));
} else {
if (fields.num == 1) {
constUnion *unionArray = new constUnion[1];
unionArray->setIConst(fields.offsets[0]);
TIntermTyped* index = parseContext.intermediate.addConstantUnion(unionArray, TType(EbtInt, EvqConst), $3.loc);
$$ = parseContext.intermediate.addIndex(EOpIndexDirect, $1, index, $2.loc);
$$->setType(TType($1->getBasicType(), EvqTemporary, $1->getType().getQualifier().precision));
} else {
TString vectorString = *$3.string;
TIntermTyped* index = parseContext.intermediate.addSwizzle(fields, $3.loc);
$$ = parseContext.intermediate.addIndex(EOpVectorSwizzle, $1, index, $2.loc);
$$->setType(TType($1->getBasicType(), EvqTemporary, $1->getType().getQualifier().precision, (int) vectorString.size()));
}
}
} else if ($1->isMatrix())
parseContext.error($2.loc, "field selection not allowed on matrix", ".", "");
else if ($1->getBasicType() == EbtStruct || $1->getBasicType() == EbtBlock) {
bool fieldFound = false;
TTypeList* fields = $1->getType().getStruct();
if (fields == 0) {
parseContext.error($2.loc, "structure has no fields", "Internal Error", "");
$$ = $1;
} else {
unsigned int i;
for (i = 0; i < fields->size(); ++i) {
if ((*fields)[i].type->getFieldName() == *$3.string) {
fieldFound = true;
break;
}
}
if (fieldFound) {
if ($1->getType().getQualifier().storage == EvqConst) {
$$ = parseContext.addConstStruct(*$3.string, $1, $2.loc);
if ($$ == 0)
$$ = $1;
else {
$$->setType(*(*fields)[i].type);
// change the qualifier of the return type, not of the structure field
// as the structure definition is shared between various structures.
$$->getTypePointer()->getQualifier().storage = EvqConst;
}
} else {
constUnion *unionArray = new constUnion[1];
unionArray->setIConst(i);
TIntermTyped* index = parseContext.intermediate.addConstantUnion(unionArray, TType(EbtInt, EvqConst), $3.loc);
$$ = parseContext.intermediate.addIndex(EOpIndexDirectStruct, $1, index, $2.loc);
$$->setType(*(*fields)[i].type);
}
} else {
parseContext.error($2.loc, " no such field in structure", $3.string->c_str(), "");
$$ = $1;
}
}
} else {
parseContext.error($2.loc, " dot operator requires structure, array, vector, or matrix on left hand side", $3.string->c_str(), "");
$$ = $1;
}
// don't delete $3.string, it's from the pool
$$ = parseContext.handleDotDereference($3.loc, $1, *$3.string);
}
| postfix_expression INC_OP {
parseContext.variableCheck($1);
@ -441,99 +297,10 @@ integer_expression
function_call
: function_call_or_method {
TFunction* fnCall = $1.function;
TOperator op = fnCall->getBuiltInOp();
if (op == EOpArrayLength) {
if (fnCall->getParamCount() > 0)
parseContext.error($1.loc, "method does not accept any arguments", fnCall->getName().c_str(), "");
int length;
if ($1.intermNode->getAsTyped() == 0 || ! $1.intermNode->getAsTyped()->getType().isArray() || $1.intermNode->getAsTyped()->getType().getArraySize() == 0) {
parseContext.error($1.loc, "", fnCall->getName().c_str(), "array must be declared with a size before using this method");
length = 1;
} else
length = $1.intermNode->getAsTyped()->getType().getArraySize();
constUnion *unionArray = new constUnion[1];
unionArray->setIConst(length);
$$ = parseContext.intermediate.addConstantUnion(unionArray, TType(EbtInt, EvqConst), $1.loc);
} else if (op != EOpNull) {
//
// Then this should be a constructor.
// Don't go through the symbol table for constructors.
// Their parameters will be verified algorithmically.
//
TType type(EbtVoid); // use this to get the type back
if (parseContext.constructorError($1.loc, $1.intermNode, *fnCall, op, type)) {
$$ = 0;
} else {
//
// It's a constructor, of type 'type'.
//
$$ = parseContext.addConstructor($1.intermNode, type, op, fnCall, $1.loc);
if ($$ == 0)
parseContext.error($1.loc, "cannot construct with these arguments", type.getCompleteString().c_str(), "");
}
if ($$ == 0)
$$ = parseContext.intermediate.setAggregateOperator(0, op, type, $1.loc);
} else {
//
// Not a constructor. Find it in the symbol table.
//
const TFunction* fnCandidate;
bool builtIn;
fnCandidate = parseContext.findFunction($1.loc, fnCall, &builtIn);
if (fnCandidate) {
//
// A declared function. But, it might still map to a built-in
// operation.
//
op = fnCandidate->getBuiltInOp();
if (builtIn && op != EOpNull) {
// A function call mapped to a built-in operation.
$$ = parseContext.intermediate.addBuiltInFunctionCall($1.loc, op, fnCandidate->getParamCount() == 1, $1.intermNode, fnCandidate->getReturnType());
if ($$ == 0) {
parseContext.error($1.intermNode->getLoc(), " wrong operand type", "Internal Error",
"built in unary operator function. Type: %s",
static_cast<TIntermTyped*>($1.intermNode)->getCompleteString().c_str());
YYERROR;
}
} else {
// This is a real function call
$$ = parseContext.intermediate.setAggregateOperator($1.intermAggregate, EOpFunctionCall, fnCandidate->getReturnType(), $1.loc);
// this is how we know whether the given function is a builtIn function or a user defined function
// if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
// if builtIn == true, it's definitely a builtIn function with EOpNull
if (!builtIn)
$$->getAsAggregate()->setUserDefined();
$$->getAsAggregate()->setName(fnCandidate->getMangledName());
TStorageQualifier qual;
TQualifierList& qualifierList = $$->getAsAggregate()->getQualifierList();
for (int i = 0; i < fnCandidate->getParamCount(); ++i) {
qual = (*fnCandidate)[i].type->getQualifier().storage;
if (qual == EvqOut || qual == EvqInOut) {
if (parseContext.lValueErrorCheck($$->getLoc(), "assign", $$->getAsAggregate()->getSequence()[i]->getAsTyped()))
parseContext.error($1.intermNode->getLoc(), "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error", "");
}
qualifierList.push_back(qual);
}
// built-in texturing functions get their return value precision from the precision of the sampler
if (builtIn && fnCandidate->getReturnType().getQualifier().precision == EpqNone &&
fnCandidate->getParamCount() > 0 && (*fnCandidate)[0].type->getBasicType() == EbtSampler)
$$->getQualifier().precision = $$->getAsAggregate()->getSequence()[0]->getAsTyped()->getQualifier().precision;
}
} else {
// error message was put out by PaFindFunction()
// Put on a dummy node for error recovery
constUnion *unionArray = new constUnion[1];
unionArray->setDConst(0.0);
$$ = parseContext.intermediate.addConstantUnion(unionArray, TType(EbtFloat, EvqConst), $1.loc);
}
}
delete fnCall;
$$ = parseContext.handleFunctionCall($1.loc, $1.function, $1.intermNode, $1.intermAggregate);
if ($$ == 0)
YYERROR;
delete $1.function;
}
;
@ -588,142 +355,9 @@ function_call_header
function_identifier
: type_specifier {
//
// Constructor
//
$$.function = 0;
$$.intermNode = 0;
if ($1.arraySizes) {
parseContext.profileRequires($1.loc, ENoProfile, 120, "GL_3DL_array_objects", "arrayed constructor");
parseContext.profileRequires($1.loc, EEsProfile, 300, "GL_3DL_array_objects", "arrayed constructor");
}
$1.qualifier.precision = EpqNone;
if ($1.userDef) {
TString tempString = "";
TType type($1);
TFunction *function = new TFunction(&tempString, type, EOpConstructStruct);
$$.function = function;
} else {
TOperator op = EOpNull;
switch ($1.basicType) {
case EbtFloat:
if ($1.matrixCols) {
switch ($1.matrixCols) {
case 2:
switch ($1.matrixRows) {
case 2: op = EOpConstructMat2x2; break;
case 3: op = EOpConstructMat2x3; break;
case 4: op = EOpConstructMat2x4; break;
default: break; // some compilers want this
}
break;
case 3:
switch ($1.matrixRows) {
case 2: op = EOpConstructMat3x2; break;
case 3: op = EOpConstructMat3x3; break;
case 4: op = EOpConstructMat3x4; break;
default: break; // some compilers want this
}
break;
case 4:
switch ($1.matrixRows) {
case 2: op = EOpConstructMat4x2; break;
case 3: op = EOpConstructMat4x3; break;
case 4: op = EOpConstructMat4x4; break;
default: break; // some compilers want this
}
break;
default: break; // some compilers want this
}
} else {
switch($1.vectorSize) {
case 1: op = EOpConstructFloat; break;
case 2: op = EOpConstructVec2; break;
case 3: op = EOpConstructVec3; break;
case 4: op = EOpConstructVec4; break;
default: break; // some compilers want this
}
}
break;
case EbtDouble:
if ($1.matrixCols) {
switch ($1.matrixCols) {
case 2:
switch ($1.matrixRows) {
case 2: op = EOpConstructDMat2x2; break;
case 3: op = EOpConstructDMat2x3; break;
case 4: op = EOpConstructDMat2x4; break;
default: break; // some compilers want this
}
break;
case 3:
switch ($1.matrixRows) {
case 2: op = EOpConstructDMat3x2; break;
case 3: op = EOpConstructDMat3x3; break;
case 4: op = EOpConstructDMat3x4; break;
default: break; // some compilers want this
}
break;
case 4:
switch ($1.matrixRows) {
case 2: op = EOpConstructDMat4x2; break;
case 3: op = EOpConstructDMat4x3; break;
case 4: op = EOpConstructDMat4x4; break;
default: break; // some compilers want this
}
break;
}
} else {
switch($1.vectorSize) {
case 1: op = EOpConstructDouble; break;
case 2: op = EOpConstructDVec2; break;
case 3: op = EOpConstructDVec3; break;
case 4: op = EOpConstructDVec4; break;
default: break; // some compilers want this
}
}
break;
case EbtInt:
switch($1.vectorSize) {
case 1: op = EOpConstructInt; break;
case 2: op = EOpConstructIVec2; break;
case 3: op = EOpConstructIVec3; break;
case 4: op = EOpConstructIVec4; break;
default: break; // some compilers want this
}
break;
case EbtUint:
switch($1.vectorSize) {
case 1: op = EOpConstructUint; break;
case 2: op = EOpConstructUVec2; break;
case 3: op = EOpConstructUVec3; break;
case 4: op = EOpConstructUVec4; break;
default: break; // some compilers want this
}
break;
case EbtBool:
switch($1.vectorSize) {
case 1: op = EOpConstructBool; break;
case 2: op = EOpConstructBVec2; break;
case 3: op = EOpConstructBVec3; break;
case 4: op = EOpConstructBVec4; break;
default: break; // some compilers want this
}
break;
default: break; // some compilers want this
}
if (op == EOpNull) {
parseContext.error($1.loc, "cannot construct this type", TType::getBasicString($1.basicType), "");
$1.basicType = EbtFloat;
op = EOpConstructFloat;
}
TString tempString = "";
TType type($1);
TFunction *function = new TFunction(&tempString, type, op);
$$.function = function;
}
$$.function = parseContext.handleConstructorCall($1.loc, $1);
}
| postfix_expression {
//
@ -2821,90 +2455,7 @@ external_declaration
function_definition
: function_prototype {
TFunction& function = *($1.function);
TSymbol* symbol = parseContext.symbolTable.find(function.getMangledName());
TFunction* prevDec = symbol ? symbol->getAsFunction() : 0;
if (! prevDec)
parseContext.error($1.loc, "can't find function name", function.getName().c_str(), "");
//
// Note: 'prevDec' could be 'function' if this is the first time we've seen function
// as it would have just been put in the symbol table. Otherwise, we're looking up
// an earlier occurance.
//
if (prevDec && prevDec->isDefined()) {
//
// Then this function already has a body.
//
parseContext.error($1.loc, "function already has a body", function.getName().c_str(), "");
}
if (prevDec) {
prevDec->setDefined();
//
// Remember the return type for later checking for RETURN statements.
//
parseContext.currentFunctionType = &(prevDec->getReturnType());
} else
parseContext.currentFunctionType = new TType(EbtVoid);
parseContext.functionReturnsValue = false;
//
// Raise error message if main function takes any parameters or return anything other than void
//
if (function.getName() == "main") {
if (function.getParamCount() > 0)
parseContext.error($1.loc, "function cannot take any parameter(s)", function.getName().c_str(), "");
if (function.getReturnType().getBasicType() != EbtVoid)
parseContext.error($1.loc, "", function.getReturnType().getCompleteTypeString().c_str(), "main function cannot return a value");
}
//
// New symbol table scope for body of function plus its arguments
//
parseContext.symbolTable.push();
//
// Insert parameters into the symbol table.
// If the parameter has no name, it's not an error, just don't insert it
// (could be used for unused args).
//
// Also, accumulate the list of parameters into the HIL, so lower level code
// knows where to find parameters.
//
TIntermAggregate* paramNodes = new TIntermAggregate;
for (int i = 0; i < function.getParamCount(); i++) {
TParameter& param = function[i];
if (param.name != 0) {
TVariable *variable = new TVariable(param.name, *param.type);
//
// Insert the parameters with name in the symbol table.
//
if (! parseContext.symbolTable.insert(*variable)) {
parseContext.error($1.loc, "redefinition", variable->getName().c_str(), "");
delete variable;
}
//
// Transfer ownership of name pointer to symbol table.
//
param.name = 0;
//
// Add the parameter to the HIL
//
paramNodes = parseContext.intermediate.growAggregate(
paramNodes,
parseContext.intermediate.addSymbol(variable->getUniqueId(),
variable->getName(),
variable->getType(), $1.loc),
$1.loc);
} else {
paramNodes = parseContext.intermediate.growAggregate(paramNodes, parseContext.intermediate.addSymbol(0, "", *param.type, $1.loc), $1.loc);
}
}
parseContext.intermediate.setAggregateOperator(paramNodes, EOpParameters, TType(EbtVoid), $1.loc);
$1.intermAggregate = paramNodes;
parseContext.loopNestingLevel = 0;
$1.intermAggregate = parseContext.handleFunctionPrototype($1.loc, *$1.function);
}
compound_statement_no_new_scope {
// May be best done as post process phase on intermediate code