3 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
8 This file contains the Yacc grammar for GLSL ES.
9 Based on ANSI C Yacc grammar:
10 http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
12 IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_glslang_parser.sh,
13 WHICH GENERATES THE GLSL ES PARSER (glslang_tab.cpp AND glslang_tab.h).
18 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
19 // Use of this source code is governed by a BSD-style license that can be
20 // found in the LICENSE file.
23 // This file is auto-generated by generate_glslang_parser.sh. DO NOT EDIT!
25 #include "compiler/SymbolTable.h"
26 #include "compiler/ParseHelper.h"
27 #include "GLSLANG/ShaderLang.h"
29 #define YYLEX_PARAM context->scanner
32 %expect 1 /* One shift reduce conflict because of if | else */
34 %parse-param {TParseContext* context}
51 TIntermNode* intermNode;
52 TIntermNodePair nodePair;
53 TIntermTyped* intermTypedNode;
54 TIntermAggregate* intermAggregate;
69 extern int yylex(YYSTYPE* yylval_param, void* yyscanner);
70 extern void yyerror(TParseContext* context, const char* reason);
72 #define FRAG_VERT_ONLY(S, L) { \
73 if (context->shaderType != SH_FRAGMENT_SHADER && \
74 context->shaderType != SH_VERTEX_SHADER) { \
75 context->error(L, " supported in vertex/fragment shaders only ", S, "", ""); \
80 #define VERTEX_ONLY(S, L) { \
81 if (context->shaderType != SH_VERTEX_SHADER) { \
82 context->error(L, " supported in vertex shaders only ", S, "", ""); \
87 #define FRAG_ONLY(S, L) { \
88 if (context->shaderType != SH_FRAGMENT_SHADER) { \
89 context->error(L, " supported in fragment shaders only ", S, "", ""); \
95 %token <lex> INVARIANT HIGH_PRECISION MEDIUM_PRECISION LOW_PRECISION PRECISION
96 %token <lex> ATTRIBUTE CONST_QUAL BOOL_TYPE FLOAT_TYPE INT_TYPE
97 %token <lex> BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN
98 %token <lex> BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 VEC2 VEC3 VEC4
99 %token <lex> MATRIX2 MATRIX3 MATRIX4 IN_QUAL OUT_QUAL INOUT_QUAL UNIFORM VARYING
100 %token <lex> STRUCT VOID_TYPE WHILE
101 %token <lex> SAMPLER2D SAMPLERCUBE
103 %token <lex> IDENTIFIER TYPE_NAME FLOATCONSTANT INTCONSTANT BOOLCONSTANT
104 %token <lex> FIELD_SELECTION
105 %token <lex> LEFT_OP RIGHT_OP
106 %token <lex> INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
107 %token <lex> AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
108 %token <lex> MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
109 %token <lex> SUB_ASSIGN
111 %token <lex> LEFT_PAREN RIGHT_PAREN LEFT_BRACKET RIGHT_BRACKET LEFT_BRACE RIGHT_BRACE DOT
112 %token <lex> COMMA COLON EQUAL SEMICOLON BANG DASH TILDE PLUS STAR SLASH PERCENT
113 %token <lex> LEFT_ANGLE RIGHT_ANGLE VERTICAL_BAR CARET AMPERSAND QUESTION
115 %type <interm> assignment_operator unary_operator
116 %type <interm.intermTypedNode> variable_identifier primary_expression postfix_expression
117 %type <interm.intermTypedNode> expression integer_expression assignment_expression
118 %type <interm.intermTypedNode> unary_expression multiplicative_expression additive_expression
119 %type <interm.intermTypedNode> relational_expression equality_expression
120 %type <interm.intermTypedNode> conditional_expression constant_expression
121 %type <interm.intermTypedNode> logical_or_expression logical_xor_expression logical_and_expression
122 %type <interm.intermTypedNode> shift_expression and_expression exclusive_or_expression inclusive_or_expression
123 %type <interm.intermTypedNode> function_call initializer condition conditionopt
125 %type <interm.intermNode> translation_unit function_definition
126 %type <interm.intermNode> statement simple_statement
127 %type <interm.intermAggregate> statement_list compound_statement
128 %type <interm.intermNode> declaration_statement selection_statement expression_statement
129 %type <interm.intermNode> declaration external_declaration
130 %type <interm.intermNode> for_init_statement compound_statement_no_new_scope
131 %type <interm.nodePair> selection_rest_statement for_rest_statement
132 %type <interm.intermNode> iteration_statement jump_statement statement_no_new_scope
133 %type <interm> single_declaration init_declarator_list
135 %type <interm> parameter_declaration parameter_declarator parameter_type_specifier
136 %type <interm.qualifier> parameter_qualifier
138 %type <interm.precision> precision_qualifier
139 %type <interm.type> type_qualifier fully_specified_type type_specifier
140 %type <interm.type> type_specifier_no_prec type_specifier_nonarray
141 %type <interm.type> struct_specifier
142 %type <interm.typeLine> struct_declarator
143 %type <interm.typeList> struct_declarator_list struct_declaration struct_declaration_list
144 %type <interm.function> function_header function_declarator function_identifier
145 %type <interm.function> function_header_with_parameters function_call_header
146 %type <interm> function_call_header_with_parameters function_call_header_no_parameters function_call_generic function_prototype
147 %type <interm> function_call_or_method
149 %start translation_unit
154 // The symbol table search was done in the lexical phase
155 const TSymbol* symbol = $1.symbol;
156 const TVariable* variable;
158 context->error($1.line, "undeclared identifier", $1.string->c_str(), "");
160 TType type(EbtFloat, EbpUndefined);
161 TVariable* fakeVariable = new TVariable($1.string, type);
162 context->symbolTable.insert(*fakeVariable);
163 variable = fakeVariable;
165 // This identifier can only be a variable type symbol
166 if (! symbol->isVariable()) {
167 context->error($1.line, "variable expected", $1.string->c_str(), "");
170 variable = static_cast<const TVariable*>(symbol);
173 // don't delete $1.string, it's used by error recovery, and the pool
174 // pop will reclaim the memory
176 if (variable->getType().getQualifier() == EvqConst ) {
177 ConstantUnion* constArray = variable->getConstPointer();
178 TType t(variable->getType());
179 $$ = context->intermediate.addConstantUnion(constArray, t, $1.line);
181 $$ = context->intermediate.addSymbol(variable->getUniqueId(),
183 variable->getType(), $1.line);
188 : variable_identifier {
193 // INT_TYPE is only 16-bit plus sign bit for vertex/fragment shaders,
194 // check for overflow for constants
196 if (abs($1.i) >= (1 << 16)) {
197 context->error($1.line, " integer constant overflow", "", "");
200 ConstantUnion *unionArray = new ConstantUnion[1];
201 unionArray->setIConst($1.i);
202 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $1.line);
205 ConstantUnion *unionArray = new ConstantUnion[1];
206 unionArray->setFConst($1.f);
207 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), $1.line);
210 ConstantUnion *unionArray = new ConstantUnion[1];
211 unionArray->setBConst($1.b);
212 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $1.line);
214 | LEFT_PAREN expression RIGHT_PAREN {
220 : primary_expression {
223 | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET {
224 if (!$1->isArray() && !$1->isMatrix() && !$1->isVector()) {
225 if ($1->getAsSymbolNode())
226 context->error($2.line, " left of '[' is not of type array, matrix, or vector ", $1->getAsSymbolNode()->getSymbol().c_str(), "");
228 context->error($2.line, " left of '[' is not of type array, matrix, or vector ", "expression", "");
231 if ($1->getType().getQualifier() == EvqConst && $3->getQualifier() == EvqConst) {
232 if ($1->isArray()) { // constant folding for arrays
233 $$ = context->addConstArrayNode($3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), $1, $2.line);
234 } else if ($1->isVector()) { // constant folding for vectors
235 TVectorFields fields;
237 fields.offsets[0] = $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(); // need to do it this way because v.xy sends fields integer array
238 $$ = context->addConstVectorNode(fields, $1, $2.line);
239 } else if ($1->isMatrix()) { // constant folding for matrices
240 $$ = context->addConstMatrixNode($3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), $1, $2.line);
243 if ($3->getQualifier() == EvqConst) {
244 if (($1->isVector() || $1->isMatrix()) && $1->getType().getNominalSize() <= $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst() && !$1->isArray() ) {
245 context->error($2.line, "", "[", "field selection out of range '%d'", $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst());
249 if ($1->getType().getArraySize() == 0) {
250 if ($1->getType().getMaxArraySize() <= $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst()) {
251 if (context->arraySetMaxSize($1->getAsSymbolNode(), $1->getTypePointer(), $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), true, $2.line))
254 if (context->arraySetMaxSize($1->getAsSymbolNode(), $1->getTypePointer(), 0, false, $2.line))
257 } else if ( $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst() >= $1->getType().getArraySize()) {
258 context->error($2.line, "", "[", "array index out of range '%d'", $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst());
262 $$ = context->intermediate.addIndex(EOpIndexDirect, $1, $3, $2.line);
265 if ($1->isArray() && $1->getType().getArraySize() == 0) {
266 context->error($2.line, "", "[", "array must be redeclared with a size before being indexed with a variable");
270 $$ = context->intermediate.addIndex(EOpIndexIndirect, $1, $3, $2.line);
274 ConstantUnion *unionArray = new ConstantUnion[1];
275 unionArray->setFConst(0.0f);
276 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), $2.line);
277 } else if ($1->isArray()) {
278 if ($1->getType().getStruct())
279 $$->setType(TType($1->getType().getStruct(), $1->getType().getTypeName()));
281 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary, $1->getNominalSize(), $1->isMatrix()));
283 if ($1->getType().getQualifier() == EvqConst)
284 $$->getTypePointer()->setQualifier(EvqConst);
285 } else if ($1->isMatrix() && $1->getType().getQualifier() == EvqConst)
286 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqConst, $1->getNominalSize()));
287 else if ($1->isMatrix())
288 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary, $1->getNominalSize()));
289 else if ($1->isVector() && $1->getType().getQualifier() == EvqConst)
290 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqConst));
291 else if ($1->isVector())
292 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary));
294 $$->setType($1->getType());
299 | postfix_expression DOT FIELD_SELECTION {
301 context->error($3.line, "cannot apply dot operator to an array", ".", "");
305 if ($1->isVector()) {
306 TVectorFields fields;
307 if (! context->parseVectorFields(*$3.string, $1->getNominalSize(), fields, $3.line)) {
309 fields.offsets[0] = 0;
313 if ($1->getType().getQualifier() == EvqConst) { // constant folding for vector fields
314 $$ = context->addConstVectorNode(fields, $1, $3.line);
320 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqConst, (int) (*$3.string).size()));
322 if (fields.num == 1) {
323 ConstantUnion *unionArray = new ConstantUnion[1];
324 unionArray->setIConst(fields.offsets[0]);
325 TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $3.line);
326 $$ = context->intermediate.addIndex(EOpIndexDirect, $1, index, $2.line);
327 $$->setType(TType($1->getBasicType(), $1->getPrecision()));
329 TString vectorString = *$3.string;
330 TIntermTyped* index = context->intermediate.addSwizzle(fields, $3.line);
331 $$ = context->intermediate.addIndex(EOpVectorSwizzle, $1, index, $2.line);
332 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary, (int) vectorString.size()));
335 } else if ($1->isMatrix()) {
336 TMatrixFields fields;
337 if (! context->parseMatrixFields(*$3.string, $1->getNominalSize(), fields, $3.line)) {
338 fields.wholeRow = false;
339 fields.wholeCol = false;
345 if (fields.wholeRow || fields.wholeCol) {
346 context->error($2.line, " non-scalar fields not implemented yet", ".", "");
348 ConstantUnion *unionArray = new ConstantUnion[1];
349 unionArray->setIConst(0);
350 TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $3.line);
351 $$ = context->intermediate.addIndex(EOpIndexDirect, $1, index, $2.line);
352 $$->setType(TType($1->getBasicType(), $1->getPrecision(),EvqTemporary, $1->getNominalSize()));
354 ConstantUnion *unionArray = new ConstantUnion[1];
355 unionArray->setIConst(fields.col * $1->getNominalSize() + fields.row);
356 TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $3.line);
357 $$ = context->intermediate.addIndex(EOpIndexDirect, $1, index, $2.line);
358 $$->setType(TType($1->getBasicType(), $1->getPrecision()));
360 } else if ($1->getBasicType() == EbtStruct) {
361 bool fieldFound = false;
362 const TTypeList* fields = $1->getType().getStruct();
364 context->error($2.line, "structure has no fields", "Internal Error", "");
369 for (i = 0; i < fields->size(); ++i) {
370 if ((*fields)[i].type->getFieldName() == *$3.string) {
376 if ($1->getType().getQualifier() == EvqConst) {
377 $$ = context->addConstStruct(*$3.string, $1, $2.line);
383 $$->setType(*(*fields)[i].type);
384 // change the qualifier of the return type, not of the structure field
385 // as the structure definition is shared between various structures.
386 $$->getTypePointer()->setQualifier(EvqConst);
389 ConstantUnion *unionArray = new ConstantUnion[1];
390 unionArray->setIConst(i);
391 TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, *(*fields)[i].type, $3.line);
392 $$ = context->intermediate.addIndex(EOpIndexDirectStruct, $1, index, $2.line);
393 $$->setType(*(*fields)[i].type);
396 context->error($2.line, " no such field in structure", $3.string->c_str(), "");
402 context->error($2.line, " field selection requires structure, vector, or matrix on left hand side", $3.string->c_str(), "");
406 // don't delete $3.string, it's from the pool
408 | postfix_expression INC_OP {
409 if (context->lValueErrorCheck($2.line, "++", $1))
411 $$ = context->intermediate.addUnaryMath(EOpPostIncrement, $1, $2.line, context->symbolTable);
413 context->unaryOpError($2.line, "++", $1->getCompleteString());
418 | postfix_expression DEC_OP {
419 if (context->lValueErrorCheck($2.line, "--", $1))
421 $$ = context->intermediate.addUnaryMath(EOpPostDecrement, $1, $2.line, context->symbolTable);
423 context->unaryOpError($2.line, "--", $1->getCompleteString());
432 if (context->integerErrorCheck($1, "[]"))
439 : function_call_or_method {
440 TFunction* fnCall = $1.function;
441 TOperator op = fnCall->getBuiltInOp();
446 // Then this should be a constructor.
447 // Don't go through the symbol table for constructors.
448 // Their parameters will be verified algorithmically.
450 TType type(EbtVoid, EbpUndefined); // use this to get the type back
451 if (context->constructorErrorCheck($1.line, $1.intermNode, *fnCall, op, &type)) {
455 // It's a constructor, of type 'type'.
457 $$ = context->addConstructor($1.intermNode, &type, op, fnCall, $1.line);
462 $$ = context->intermediate.setAggregateOperator(0, op, $1.line);
467 // Not a constructor. Find it in the symbol table.
469 const TFunction* fnCandidate;
471 fnCandidate = context->findFunction($1.line, fnCall, &builtIn);
474 // A declared function.
476 if (builtIn && !fnCandidate->getExtension().empty() &&
477 context->extensionErrorCheck($1.line, fnCandidate->getExtension())) {
480 op = fnCandidate->getBuiltInOp();
481 if (builtIn && op != EOpNull) {
483 // A function call mapped to a built-in operation.
485 if (fnCandidate->getParamCount() == 1) {
487 // Treat it like a built-in unary operator.
489 $$ = context->intermediate.addUnaryMath(op, $1.intermNode, 0, context->symbolTable);
491 context->error($1.intermNode->getLine(), " wrong operand type", "Internal Error",
492 "built in unary operator function. Type: %s",
493 static_cast<TIntermTyped*>($1.intermNode)->getCompleteString().c_str());
497 $$ = context->intermediate.setAggregateOperator($1.intermAggregate, op, $1.line);
500 // This is a real function call
502 $$ = context->intermediate.setAggregateOperator($1.intermAggregate, EOpFunctionCall, $1.line);
503 $$->setType(fnCandidate->getReturnType());
505 // this is how we know whether the given function is a builtIn function or a user defined function
506 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
507 // if builtIn == true, it's definitely a builtIn function with EOpNull
509 $$->getAsAggregate()->setUserDefined();
510 $$->getAsAggregate()->setName(fnCandidate->getMangledName());
513 for (int i = 0; i < fnCandidate->getParamCount(); ++i) {
514 qual = fnCandidate->getParam(i).type->getQualifier();
515 if (qual == EvqOut || qual == EvqInOut) {
516 if (context->lValueErrorCheck($$->getLine(), "assign", $$->getAsAggregate()->getSequence()[i]->getAsTyped())) {
517 context->error($1.intermNode->getLine(), "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error", "");
523 $$->setType(fnCandidate->getReturnType());
525 // error message was put out by PaFindFunction()
526 // Put on a dummy node for error recovery
527 ConstantUnion *unionArray = new ConstantUnion[1];
528 unionArray->setFConst(0.0f);
529 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), $1.line);
537 function_call_or_method
538 : function_call_generic {
541 | postfix_expression DOT function_call_generic {
542 context->error($3.line, "methods are not supported", "", "");
548 function_call_generic
549 : function_call_header_with_parameters RIGHT_PAREN {
553 | function_call_header_no_parameters RIGHT_PAREN {
559 function_call_header_no_parameters
560 : function_call_header VOID_TYPE {
564 | function_call_header {
570 function_call_header_with_parameters
571 : function_call_header assignment_expression {
572 TParameter param = { 0, new TType($2->getType()) };
573 $1->addParameter(param);
577 | function_call_header_with_parameters COMMA assignment_expression {
578 TParameter param = { 0, new TType($3->getType()) };
579 $1.function->addParameter(param);
580 $$.function = $1.function;
581 $$.intermNode = context->intermediate.growAggregate($1.intermNode, $3, $2.line);
586 : function_identifier LEFT_PAREN {
591 // Grammar Note: Constructors look like functions, but are recognized as types.
594 : type_specifier_nonarray {
598 TOperator op = EOpNull;
600 op = EOpConstructStruct;
606 case 2: op = EOpConstructMat2; break;
607 case 3: op = EOpConstructMat3; break;
608 case 4: op = EOpConstructMat4; break;
612 case 1: op = EOpConstructFloat; break;
613 case 2: op = EOpConstructVec2; break;
614 case 3: op = EOpConstructVec3; break;
615 case 4: op = EOpConstructVec4; break;
621 case 1: op = EOpConstructInt; break;
622 case 2: FRAG_VERT_ONLY("ivec2", $1.line); op = EOpConstructIVec2; break;
623 case 3: FRAG_VERT_ONLY("ivec3", $1.line); op = EOpConstructIVec3; break;
624 case 4: FRAG_VERT_ONLY("ivec4", $1.line); op = EOpConstructIVec4; break;
629 case 1: op = EOpConstructBool; break;
630 case 2: FRAG_VERT_ONLY("bvec2", $1.line); op = EOpConstructBVec2; break;
631 case 3: FRAG_VERT_ONLY("bvec3", $1.line); op = EOpConstructBVec3; break;
632 case 4: FRAG_VERT_ONLY("bvec4", $1.line); op = EOpConstructBVec4; break;
638 context->error($1.line, "cannot construct this type", getBasicString($1.type), "");
641 op = EOpConstructFloat;
646 TFunction *function = new TFunction(&tempString, type, op);
650 if (context->reservedErrorCheck($1.line, *$1.string))
652 TType type(EbtVoid, EbpUndefined);
653 TFunction *function = new TFunction($1.string, type);
657 if (context->reservedErrorCheck($1.line, *$1.string))
659 TType type(EbtVoid, EbpUndefined);
660 TFunction *function = new TFunction($1.string, type);
666 : postfix_expression {
669 | INC_OP unary_expression {
670 if (context->lValueErrorCheck($1.line, "++", $2))
672 $$ = context->intermediate.addUnaryMath(EOpPreIncrement, $2, $1.line, context->symbolTable);
674 context->unaryOpError($1.line, "++", $2->getCompleteString());
679 | DEC_OP unary_expression {
680 if (context->lValueErrorCheck($1.line, "--", $2))
682 $$ = context->intermediate.addUnaryMath(EOpPreDecrement, $2, $1.line, context->symbolTable);
684 context->unaryOpError($1.line, "--", $2->getCompleteString());
689 | unary_operator unary_expression {
690 if ($1.op != EOpNull) {
691 $$ = context->intermediate.addUnaryMath($1.op, $2, $1.line, context->symbolTable);
693 const char* errorOp = "";
695 case EOpNegative: errorOp = "-"; break;
696 case EOpLogicalNot: errorOp = "!"; break;
699 context->unaryOpError($1.line, errorOp, $2->getCompleteString());
707 // Grammar Note: No traditional style type casts.
710 : PLUS { $$.line = $1.line; $$.op = EOpNull; }
711 | DASH { $$.line = $1.line; $$.op = EOpNegative; }
712 | BANG { $$.line = $1.line; $$.op = EOpLogicalNot; }
714 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
716 multiplicative_expression
717 : unary_expression { $$ = $1; }
718 | multiplicative_expression STAR unary_expression {
719 FRAG_VERT_ONLY("*", $2.line);
720 $$ = context->intermediate.addBinaryMath(EOpMul, $1, $3, $2.line, context->symbolTable);
722 context->binaryOpError($2.line, "*", $1->getCompleteString(), $3->getCompleteString());
727 | multiplicative_expression SLASH unary_expression {
728 FRAG_VERT_ONLY("/", $2.line);
729 $$ = context->intermediate.addBinaryMath(EOpDiv, $1, $3, $2.line, context->symbolTable);
731 context->binaryOpError($2.line, "/", $1->getCompleteString(), $3->getCompleteString());
739 : multiplicative_expression { $$ = $1; }
740 | additive_expression PLUS multiplicative_expression {
741 $$ = context->intermediate.addBinaryMath(EOpAdd, $1, $3, $2.line, context->symbolTable);
743 context->binaryOpError($2.line, "+", $1->getCompleteString(), $3->getCompleteString());
748 | additive_expression DASH multiplicative_expression {
749 $$ = context->intermediate.addBinaryMath(EOpSub, $1, $3, $2.line, context->symbolTable);
751 context->binaryOpError($2.line, "-", $1->getCompleteString(), $3->getCompleteString());
759 : additive_expression { $$ = $1; }
762 relational_expression
763 : shift_expression { $$ = $1; }
764 | relational_expression LEFT_ANGLE shift_expression {
765 $$ = context->intermediate.addBinaryMath(EOpLessThan, $1, $3, $2.line, context->symbolTable);
767 context->binaryOpError($2.line, "<", $1->getCompleteString(), $3->getCompleteString());
769 ConstantUnion *unionArray = new ConstantUnion[1];
770 unionArray->setBConst(false);
771 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
774 | relational_expression RIGHT_ANGLE shift_expression {
775 $$ = context->intermediate.addBinaryMath(EOpGreaterThan, $1, $3, $2.line, context->symbolTable);
777 context->binaryOpError($2.line, ">", $1->getCompleteString(), $3->getCompleteString());
779 ConstantUnion *unionArray = new ConstantUnion[1];
780 unionArray->setBConst(false);
781 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
784 | relational_expression LE_OP shift_expression {
785 $$ = context->intermediate.addBinaryMath(EOpLessThanEqual, $1, $3, $2.line, context->symbolTable);
787 context->binaryOpError($2.line, "<=", $1->getCompleteString(), $3->getCompleteString());
789 ConstantUnion *unionArray = new ConstantUnion[1];
790 unionArray->setBConst(false);
791 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
794 | relational_expression GE_OP shift_expression {
795 $$ = context->intermediate.addBinaryMath(EOpGreaterThanEqual, $1, $3, $2.line, context->symbolTable);
797 context->binaryOpError($2.line, ">=", $1->getCompleteString(), $3->getCompleteString());
799 ConstantUnion *unionArray = new ConstantUnion[1];
800 unionArray->setBConst(false);
801 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
807 : relational_expression { $$ = $1; }
808 | equality_expression EQ_OP relational_expression {
809 $$ = context->intermediate.addBinaryMath(EOpEqual, $1, $3, $2.line, context->symbolTable);
811 context->binaryOpError($2.line, "==", $1->getCompleteString(), $3->getCompleteString());
813 ConstantUnion *unionArray = new ConstantUnion[1];
814 unionArray->setBConst(false);
815 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
818 | equality_expression NE_OP relational_expression {
819 $$ = context->intermediate.addBinaryMath(EOpNotEqual, $1, $3, $2.line, context->symbolTable);
821 context->binaryOpError($2.line, "!=", $1->getCompleteString(), $3->getCompleteString());
823 ConstantUnion *unionArray = new ConstantUnion[1];
824 unionArray->setBConst(false);
825 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
831 : equality_expression { $$ = $1; }
834 exclusive_or_expression
835 : and_expression { $$ = $1; }
838 inclusive_or_expression
839 : exclusive_or_expression { $$ = $1; }
842 logical_and_expression
843 : inclusive_or_expression { $$ = $1; }
844 | logical_and_expression AND_OP inclusive_or_expression {
845 $$ = context->intermediate.addBinaryMath(EOpLogicalAnd, $1, $3, $2.line, context->symbolTable);
847 context->binaryOpError($2.line, "&&", $1->getCompleteString(), $3->getCompleteString());
849 ConstantUnion *unionArray = new ConstantUnion[1];
850 unionArray->setBConst(false);
851 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
856 logical_xor_expression
857 : logical_and_expression { $$ = $1; }
858 | logical_xor_expression XOR_OP logical_and_expression {
859 $$ = context->intermediate.addBinaryMath(EOpLogicalXor, $1, $3, $2.line, context->symbolTable);
861 context->binaryOpError($2.line, "^^", $1->getCompleteString(), $3->getCompleteString());
863 ConstantUnion *unionArray = new ConstantUnion[1];
864 unionArray->setBConst(false);
865 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
870 logical_or_expression
871 : logical_xor_expression { $$ = $1; }
872 | logical_or_expression OR_OP logical_xor_expression {
873 $$ = context->intermediate.addBinaryMath(EOpLogicalOr, $1, $3, $2.line, context->symbolTable);
875 context->binaryOpError($2.line, "||", $1->getCompleteString(), $3->getCompleteString());
877 ConstantUnion *unionArray = new ConstantUnion[1];
878 unionArray->setBConst(false);
879 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
884 conditional_expression
885 : logical_or_expression { $$ = $1; }
886 | logical_or_expression QUESTION expression COLON assignment_expression {
887 if (context->boolErrorCheck($2.line, $1))
890 $$ = context->intermediate.addSelection($1, $3, $5, $2.line);
891 if ($3->getType() != $5->getType())
895 context->binaryOpError($2.line, ":", $3->getCompleteString(), $5->getCompleteString());
902 assignment_expression
903 : conditional_expression { $$ = $1; }
904 | unary_expression assignment_operator assignment_expression {
905 if (context->lValueErrorCheck($2.line, "assign", $1))
907 $$ = context->intermediate.addAssign($2.op, $1, $3, $2.line);
909 context->assignError($2.line, "assign", $1->getCompleteString(), $3->getCompleteString());
917 : EQUAL { $$.line = $1.line; $$.op = EOpAssign; }
918 | MUL_ASSIGN { FRAG_VERT_ONLY("*=", $1.line); $$.line = $1.line; $$.op = EOpMulAssign; }
919 | DIV_ASSIGN { FRAG_VERT_ONLY("/=", $1.line); $$.line = $1.line; $$.op = EOpDivAssign; }
920 | ADD_ASSIGN { $$.line = $1.line; $$.op = EOpAddAssign; }
921 | SUB_ASSIGN { $$.line = $1.line; $$.op = EOpSubAssign; }
925 : assignment_expression {
928 | expression COMMA assignment_expression {
929 $$ = context->intermediate.addComma($1, $3, $2.line);
931 context->binaryOpError($2.line, ",", $1->getCompleteString(), $3->getCompleteString());
939 : conditional_expression {
940 if (context->constErrorCheck($1))
947 : function_prototype SEMICOLON {
948 TFunction &function = *($1.function);
950 TIntermAggregate *prototype = new TIntermAggregate;
951 prototype->setType(function.getReturnType());
952 prototype->setName(function.getName());
954 for (int i = 0; i < function.getParamCount(); i++)
956 const TParameter ¶m = function.getParam(i);
959 TVariable *variable = new TVariable(param.name, *param.type);
961 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), $1.line), $1.line);
965 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(0, "", *param.type, $1.line), $1.line);
969 prototype->setOp(EOpPrototype);
972 | init_declarator_list SEMICOLON {
973 if ($1.intermAggregate)
974 $1.intermAggregate->setOp(EOpDeclaration);
975 $$ = $1.intermAggregate;
977 | PRECISION precision_qualifier type_specifier_no_prec SEMICOLON {
978 context->symbolTable.setDefaultPrecision( $3.type, $2 );
984 : function_declarator RIGHT_PAREN {
986 // Multiple declarations of the same function are allowed.
988 // If this is a definition, the definition production code will check for redefinitions
989 // (we don't know at this point if it's a definition or not).
991 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
993 TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find($1->getMangledName()));
995 if (prevDec->getReturnType() != $1->getReturnType()) {
996 context->error($2.line, "overloaded functions must have the same return type", $1->getReturnType().getBasicString(), "");
999 for (int i = 0; i < prevDec->getParamCount(); ++i) {
1000 if (prevDec->getParam(i).type->getQualifier() != $1->getParam(i).type->getQualifier()) {
1001 context->error($2.line, "overloaded functions must have the same parameter qualifiers", $1->getParam(i).type->getQualifierString(), "");
1008 // If this is a redeclaration, it could also be a definition,
1009 // in which case, we want to use the variable names from this one, and not the one that's
1010 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
1015 context->symbolTable.insert(*$$.function);
1023 | function_header_with_parameters {
1029 function_header_with_parameters
1030 : function_header parameter_declaration {
1031 // Add the parameter
1033 if ($2.param.type->getBasicType() != EbtVoid)
1034 $1->addParameter($2.param);
1036 delete $2.param.type;
1038 | function_header_with_parameters COMMA parameter_declaration {
1040 // Only first parameter of one-parameter functions can be void
1041 // The check for named parameters not being void is done in parameter_declarator
1043 if ($3.param.type->getBasicType() == EbtVoid) {
1045 // This parameter > first is void
1047 context->error($2.line, "cannot be an argument type except for '(void)'", "void", "");
1049 delete $3.param.type;
1051 // Add the parameter
1053 $1->addParameter($3.param);
1059 : fully_specified_type IDENTIFIER LEFT_PAREN {
1060 if ($1.qualifier != EvqGlobal && $1.qualifier != EvqTemporary) {
1061 context->error($2.line, "no qualifiers allowed for function return", getQualifierString($1.qualifier), "");
1064 // make sure a sampler is not involved as well...
1065 if (context->structQualifierErrorCheck($2.line, $1))
1068 // Add the function as a prototype after parsing it (we do not support recursion)
1069 TFunction *function;
1071 function = new TFunction($2.string, type);
1076 parameter_declarator
1078 : type_specifier IDENTIFIER {
1079 if ($1.type == EbtVoid) {
1080 context->error($2.line, "illegal use of type 'void'", $2.string->c_str(), "");
1083 if (context->reservedErrorCheck($2.line, *$2.string))
1085 TParameter param = {$2.string, new TType($1)};
1089 | type_specifier IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1090 // Check that we can make an array out of this type
1091 if (context->arrayTypeErrorCheck($3.line, $1))
1094 if (context->reservedErrorCheck($2.line, *$2.string))
1098 if (context->arraySizeErrorCheck($3.line, $4, size))
1100 $1.setArray(true, size);
1102 TType* type = new TType($1);
1103 TParameter param = { $2.string, type };
1109 parameter_declaration
1111 // The only parameter qualifier a parameter can have are
1112 // IN_QUAL, OUT_QUAL, INOUT_QUAL, or CONST.
1118 : type_qualifier parameter_qualifier parameter_declarator {
1120 if (context->paramErrorCheck($3.line, $1.qualifier, $2, $$.param.type))
1123 | parameter_qualifier parameter_declarator {
1125 if (context->parameterSamplerErrorCheck($2.line, $1, *$2.param.type))
1127 if (context->paramErrorCheck($2.line, EvqTemporary, $1, $$.param.type))
1133 | type_qualifier parameter_qualifier parameter_type_specifier {
1135 if (context->paramErrorCheck($3.line, $1.qualifier, $2, $$.param.type))
1138 | parameter_qualifier parameter_type_specifier {
1140 if (context->parameterSamplerErrorCheck($2.line, $1, *$2.param.type))
1142 if (context->paramErrorCheck($2.line, EvqTemporary, $1, $$.param.type))
1162 parameter_type_specifier
1164 TParameter param = { 0, new TType($1) };
1169 init_declarator_list
1170 : single_declaration {
1173 | init_declarator_list COMMA IDENTIFIER {
1174 TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$3.string, TType($1.type), $3.line);
1175 $$.intermAggregate = context->intermediate.growAggregate($1.intermNode, symbol, $3.line);
1177 if (context->structQualifierErrorCheck($3.line, $$.type))
1180 if (context->nonInitConstErrorCheck($3.line, *$3.string, $$.type))
1183 TVariable* variable = 0;
1184 if (context->nonInitErrorCheck($3.line, *$3.string, $$.type, variable))
1186 if (symbol && variable)
1187 symbol->setId(variable->getUniqueId());
1189 | init_declarator_list COMMA IDENTIFIER LEFT_BRACKET RIGHT_BRACKET {
1190 if (context->structQualifierErrorCheck($3.line, $1.type))
1193 if (context->nonInitConstErrorCheck($3.line, *$3.string, $1.type))
1198 if (context->arrayTypeErrorCheck($4.line, $1.type) || context->arrayQualifierErrorCheck($4.line, $1.type))
1201 $1.type.setArray(true);
1202 TVariable* variable;
1203 if (context->arrayErrorCheck($4.line, *$3.string, $1.type, variable))
1207 | init_declarator_list COMMA IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1208 if (context->structQualifierErrorCheck($3.line, $1.type))
1211 if (context->nonInitConstErrorCheck($3.line, *$3.string, $1.type))
1216 if (context->arrayTypeErrorCheck($4.line, $1.type) || context->arrayQualifierErrorCheck($4.line, $1.type))
1220 if (context->arraySizeErrorCheck($4.line, $5, size))
1222 $1.type.setArray(true, size);
1223 TVariable* variable = 0;
1224 if (context->arrayErrorCheck($4.line, *$3.string, $1.type, variable))
1226 TType type = TType($1.type);
1227 type.setArraySize(size);
1228 $$.intermAggregate = context->intermediate.growAggregate($1.intermNode, context->intermediate.addSymbol(variable ? variable->getUniqueId() : 0, *$3.string, type, $3.line), $3.line);
1231 | init_declarator_list COMMA IDENTIFIER EQUAL initializer {
1232 if (context->structQualifierErrorCheck($3.line, $1.type))
1237 TIntermNode* intermNode;
1238 if (!context->executeInitializer($3.line, *$3.string, $1.type, $5, intermNode)) {
1240 // build the intermediate representation
1243 $$.intermAggregate = context->intermediate.growAggregate($1.intermNode, intermNode, $4.line);
1245 $$.intermAggregate = $1.intermAggregate;
1248 $$.intermAggregate = 0;
1254 : fully_specified_type {
1256 $$.intermAggregate = context->intermediate.makeAggregate(context->intermediate.addSymbol(0, "", TType($1), $1.line), $1.line);
1258 | fully_specified_type IDENTIFIER {
1259 TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$2.string, TType($1), $2.line);
1260 $$.intermAggregate = context->intermediate.makeAggregate(symbol, $2.line);
1262 if (context->structQualifierErrorCheck($2.line, $$.type))
1265 if (context->nonInitConstErrorCheck($2.line, *$2.string, $$.type))
1270 TVariable* variable = 0;
1271 if (context->nonInitErrorCheck($2.line, *$2.string, $$.type, variable))
1273 if (variable && symbol)
1274 symbol->setId(variable->getUniqueId());
1276 | fully_specified_type IDENTIFIER LEFT_BRACKET RIGHT_BRACKET {
1277 context->error($2.line, "unsized array declarations not supported", $2.string->c_str(), "");
1280 TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$2.string, TType($1), $2.line);
1281 $$.intermAggregate = context->intermediate.makeAggregate(symbol, $2.line);
1284 | fully_specified_type IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1285 TType type = TType($1);
1287 if (context->arraySizeErrorCheck($2.line, $4, size))
1289 type.setArraySize(size);
1290 TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$2.string, type, $2.line);
1291 $$.intermAggregate = context->intermediate.makeAggregate(symbol, $2.line);
1293 if (context->structQualifierErrorCheck($2.line, $1))
1296 if (context->nonInitConstErrorCheck($2.line, *$2.string, $1))
1301 if (context->arrayTypeErrorCheck($3.line, $1) || context->arrayQualifierErrorCheck($3.line, $1))
1305 if (context->arraySizeErrorCheck($3.line, $4, size))
1308 $1.setArray(true, size);
1309 TVariable* variable = 0;
1310 if (context->arrayErrorCheck($3.line, *$2.string, $1, variable))
1312 if (variable && symbol)
1313 symbol->setId(variable->getUniqueId());
1316 | fully_specified_type IDENTIFIER EQUAL initializer {
1317 if (context->structQualifierErrorCheck($2.line, $1))
1322 TIntermNode* intermNode;
1323 if (!context->executeInitializer($2.line, *$2.string, $1, $4, intermNode)) {
1325 // Build intermediate representation
1328 $$.intermAggregate = context->intermediate.makeAggregate(intermNode, $3.line);
1330 $$.intermAggregate = 0;
1333 $$.intermAggregate = 0;
1336 | INVARIANT IDENTIFIER {
1337 VERTEX_ONLY("invariant declaration", $1.line);
1338 $$.qualifier = EvqInvariantVaryingOut;
1339 $$.intermAggregate = 0;
1343 // Place holder for the pack/unpack languages.
1345 // | buffer_specifier {
1346 // $$.intermAggregate = 0;
1350 // Grammar Note: No 'enum', or 'typedef'.
1353 // Place holder for the pack/unpack languages.
1355 //%type <interm> buffer_declaration
1356 //%type <interm.type> buffer_specifier input_or_output buffer_declaration_list
1358 // : input_or_output LEFT_BRACE buffer_declaration_list RIGHT_BRACE {
1364 // if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "input"))
1365 // context->recover();
1366 // UNPACK_ONLY("input", $1.line);
1367 // $$.qualifier = EvqInput;
1370 // if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "output"))
1371 // context->recover();
1372 // PACK_ONLY("output", $1.line);
1373 // $$.qualifier = EvqOutput;
1378 // Place holder for the pack/unpack languages.
1380 //buffer_declaration_list
1381 // : buffer_declaration {
1383 // | buffer_declaration_list buffer_declaration {
1388 // Input/output semantics:
1389 // float must be 16 or 32 bits
1390 // float alignment restrictions?
1391 // check for only one input and only one output
1392 // sum of bitfields has to be multiple of 32
1396 // Place holder for the pack/unpack languages.
1398 //buffer_declaration
1399 // : type_specifier IDENTIFIER COLON constant_expression SEMICOLON {
1400 // if (context->reservedErrorCheck($2.line, *$2.string, context))
1401 // context->recover();
1402 // $$.variable = new TVariable($2.string, $1);
1403 // if (! context->symbolTable.insert(*$$.variable)) {
1404 // context->error($2.line, "redefinition", $$.variable->getName().c_str(), "");
1405 // context->recover();
1406 // // don't have to delete $$.variable, the pool pop will take care of it
1411 fully_specified_type
1416 context->error($1.line, "not supported", "first-class array", "");
1421 | type_qualifier type_specifier {
1423 context->error($2.line, "not supported", "first-class array", "");
1428 if ($1.qualifier == EvqAttribute &&
1429 ($2.type == EbtBool || $2.type == EbtInt)) {
1430 context->error($2.line, "cannot be bool or int", getQualifierString($1.qualifier), "");
1433 if (($1.qualifier == EvqVaryingIn || $1.qualifier == EvqVaryingOut) &&
1434 ($2.type == EbtBool || $2.type == EbtInt)) {
1435 context->error($2.line, "cannot be bool or int", getQualifierString($1.qualifier), "");
1439 $$.qualifier = $1.qualifier;
1445 $$.setBasic(EbtVoid, EvqConst, $1.line);
1448 VERTEX_ONLY("attribute", $1.line);
1449 if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "attribute"))
1451 $$.setBasic(EbtVoid, EvqAttribute, $1.line);
1454 if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "varying"))
1456 if (context->shaderType == SH_VERTEX_SHADER)
1457 $$.setBasic(EbtVoid, EvqVaryingOut, $1.line);
1459 $$.setBasic(EbtVoid, EvqVaryingIn, $1.line);
1461 | INVARIANT VARYING {
1462 if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "invariant varying"))
1464 if (context->shaderType == SH_VERTEX_SHADER)
1465 $$.setBasic(EbtVoid, EvqInvariantVaryingOut, $1.line);
1467 $$.setBasic(EbtVoid, EvqInvariantVaryingIn, $1.line);
1470 if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "uniform"))
1472 $$.setBasic(EbtVoid, EvqUniform, $1.line);
1477 : type_specifier_no_prec {
1480 if ($$.precision == EbpUndefined) {
1481 $$.precision = context->symbolTable.getDefaultPrecision($1.type);
1482 if (context->precisionErrorCheck($1.line, $$.precision, $1.type)) {
1487 | precision_qualifier type_specifier_no_prec {
1497 | MEDIUM_PRECISION {
1505 type_specifier_no_prec
1506 : type_specifier_nonarray {
1509 | type_specifier_nonarray LEFT_BRACKET constant_expression RIGHT_BRACKET {
1512 if (context->arrayTypeErrorCheck($2.line, $1))
1516 if (context->arraySizeErrorCheck($2.line, $3, size))
1518 $$.setArray(true, size);
1523 type_specifier_nonarray
1525 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1526 $$.setBasic(EbtVoid, qual, $1.line);
1529 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1530 $$.setBasic(EbtFloat, qual, $1.line);
1533 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1534 $$.setBasic(EbtInt, qual, $1.line);
1537 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1538 $$.setBasic(EbtBool, qual, $1.line);
1540 // | UNSIGNED INT_TYPE {
1541 // PACK_UNPACK_ONLY("unsigned", $1.line);
1542 // TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1543 // $$.setBasic(EbtInt, qual, $1.line);
1546 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1547 $$.setBasic(EbtFloat, qual, $1.line);
1551 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1552 $$.setBasic(EbtFloat, qual, $1.line);
1556 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1557 $$.setBasic(EbtFloat, qual, $1.line);
1561 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1562 $$.setBasic(EbtBool, qual, $1.line);
1566 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1567 $$.setBasic(EbtBool, qual, $1.line);
1571 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1572 $$.setBasic(EbtBool, qual, $1.line);
1576 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1577 $$.setBasic(EbtInt, qual, $1.line);
1581 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1582 $$.setBasic(EbtInt, qual, $1.line);
1586 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1587 $$.setBasic(EbtInt, qual, $1.line);
1591 FRAG_VERT_ONLY("mat2", $1.line);
1592 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1593 $$.setBasic(EbtFloat, qual, $1.line);
1594 $$.setAggregate(2, true);
1597 FRAG_VERT_ONLY("mat3", $1.line);
1598 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1599 $$.setBasic(EbtFloat, qual, $1.line);
1600 $$.setAggregate(3, true);
1603 FRAG_VERT_ONLY("mat4", $1.line);
1604 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1605 $$.setBasic(EbtFloat, qual, $1.line);
1606 $$.setAggregate(4, true);
1609 FRAG_VERT_ONLY("sampler2D", $1.line);
1610 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1611 $$.setBasic(EbtSampler2D, qual, $1.line);
1614 FRAG_VERT_ONLY("samplerCube", $1.line);
1615 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1616 $$.setBasic(EbtSamplerCube, qual, $1.line);
1618 | struct_specifier {
1619 FRAG_VERT_ONLY("struct", $1.line);
1621 $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1625 // This is for user defined type names. The lexical phase looked up the
1628 TType& structure = static_cast<TVariable*>($1.symbol)->getType();
1629 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1630 $$.setBasic(EbtStruct, qual, $1.line);
1631 $$.userDef = &structure;
1636 : STRUCT IDENTIFIER LEFT_BRACE struct_declaration_list RIGHT_BRACE {
1637 if (context->reservedErrorCheck($2.line, *$2.string))
1640 TType* structure = new TType($4, *$2.string);
1641 TVariable* userTypeDef = new TVariable($2.string, *structure, true);
1642 if (! context->symbolTable.insert(*userTypeDef)) {
1643 context->error($2.line, "redefinition", $2.string->c_str(), "struct");
1646 $$.setBasic(EbtStruct, EvqTemporary, $1.line);
1647 $$.userDef = structure;
1649 | STRUCT LEFT_BRACE struct_declaration_list RIGHT_BRACE {
1650 TType* structure = new TType($3, TString(""));
1651 $$.setBasic(EbtStruct, EvqTemporary, $1.line);
1652 $$.userDef = structure;
1656 struct_declaration_list
1657 : struct_declaration {
1660 | struct_declaration_list struct_declaration {
1662 for (unsigned int i = 0; i < $2->size(); ++i) {
1663 for (unsigned int j = 0; j < $$->size(); ++j) {
1664 if ((*$$)[j].type->getFieldName() == (*$2)[i].type->getFieldName()) {
1665 context->error((*$2)[i].line, "duplicate field name in structure:", "struct", (*$2)[i].type->getFieldName().c_str());
1669 $$->push_back((*$2)[i]);
1675 : type_specifier struct_declarator_list SEMICOLON {
1678 if (context->voidErrorCheck($1.line, (*$2)[0].type->getFieldName(), $1)) {
1681 for (unsigned int i = 0; i < $$->size(); ++i) {
1683 // Careful not to replace already known aspects of type, like array-ness
1685 TType* type = (*$$)[i].type;
1686 type->setBasicType($1.type);
1687 type->setNominalSize($1.size);
1688 type->setMatrix($1.matrix);
1689 type->setPrecision($1.precision);
1691 // don't allow arrays of arrays
1692 if (type->isArray()) {
1693 if (context->arrayTypeErrorCheck($1.line, $1))
1697 type->setArraySize($1.arraySize);
1699 type->setStruct($1.userDef->getStruct());
1700 type->setTypeName($1.userDef->getTypeName());
1706 struct_declarator_list
1707 : struct_declarator {
1708 $$ = NewPoolTTypeList();
1711 | struct_declarator_list COMMA struct_declarator {
1718 if (context->reservedErrorCheck($1.line, *$1.string))
1721 $$.type = new TType(EbtVoid, EbpUndefined);
1723 $$.type->setFieldName(*$1.string);
1725 | IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1726 if (context->reservedErrorCheck($1.line, *$1.string))
1729 $$.type = new TType(EbtVoid, EbpUndefined);
1731 $$.type->setFieldName(*$1.string);
1734 if (context->arraySizeErrorCheck($2.line, $3, size))
1736 $$.type->setArraySize(size);
1741 : assignment_expression { $$ = $1; }
1744 declaration_statement
1745 : declaration { $$ = $1; }
1749 : compound_statement { $$ = $1; }
1750 | simple_statement { $$ = $1; }
1753 // Grammar Note: No labeled statements; 'goto' is not supported.
1756 : declaration_statement { $$ = $1; }
1757 | expression_statement { $$ = $1; }
1758 | selection_statement { $$ = $1; }
1759 | iteration_statement { $$ = $1; }
1760 | jump_statement { $$ = $1; }
1764 : LEFT_BRACE RIGHT_BRACE { $$ = 0; }
1765 | LEFT_BRACE { context->symbolTable.push(); } statement_list { context->symbolTable.pop(); } RIGHT_BRACE {
1767 $3->setOp(EOpSequence);
1768 $3->setEndLine($5.line);
1774 statement_no_new_scope
1775 : compound_statement_no_new_scope { $$ = $1; }
1776 | simple_statement { $$ = $1; }
1779 compound_statement_no_new_scope
1780 // Statement that doesn't create a new scope, for selection_statement, iteration_statement
1781 : LEFT_BRACE RIGHT_BRACE {
1784 | LEFT_BRACE statement_list RIGHT_BRACE {
1786 $2->setOp(EOpSequence);
1787 $2->setEndLine($3.line);
1795 $$ = context->intermediate.makeAggregate($1, 0);
1797 | statement_list statement {
1798 $$ = context->intermediate.growAggregate($1, $2, 0);
1802 expression_statement
1803 : SEMICOLON { $$ = 0; }
1804 | expression SEMICOLON { $$ = static_cast<TIntermNode*>($1); }
1808 : IF LEFT_PAREN expression RIGHT_PAREN selection_rest_statement {
1809 if (context->boolErrorCheck($1.line, $3))
1811 $$ = context->intermediate.addSelection($3, $5, $1.line);
1815 selection_rest_statement
1816 : statement ELSE statement {
1826 // Grammar Note: No 'switch'. Switch statements not supported.
1829 // In 1996 c++ draft, conditions can include single declarations
1832 if (context->boolErrorCheck($1->getLine(), $1))
1835 | fully_specified_type IDENTIFIER EQUAL initializer {
1836 TIntermNode* intermNode;
1837 if (context->structQualifierErrorCheck($2.line, $1))
1839 if (context->boolErrorCheck($2.line, $1))
1842 if (!context->executeInitializer($2.line, *$2.string, $1, $4, intermNode))
1852 : WHILE LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } condition RIGHT_PAREN statement_no_new_scope {
1853 context->symbolTable.pop();
1854 $$ = context->intermediate.addLoop(ELoopWhile, 0, $4, 0, $6, $1.line);
1855 --context->loopNestingLevel;
1857 | DO { ++context->loopNestingLevel; } statement WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON {
1858 if (context->boolErrorCheck($8.line, $6))
1861 $$ = context->intermediate.addLoop(ELoopDoWhile, 0, $6, 0, $3, $4.line);
1862 --context->loopNestingLevel;
1864 | FOR LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope {
1865 context->symbolTable.pop();
1866 $$ = context->intermediate.addLoop(ELoopFor, $4, reinterpret_cast<TIntermTyped*>($5.node1), reinterpret_cast<TIntermTyped*>($5.node2), $7, $1.line);
1867 --context->loopNestingLevel;
1872 : expression_statement {
1875 | declaration_statement {
1884 | /* May be null */ {
1890 : conditionopt SEMICOLON {
1894 | conditionopt SEMICOLON expression {
1901 : CONTINUE SEMICOLON {
1902 if (context->loopNestingLevel <= 0) {
1903 context->error($1.line, "continue statement only allowed in loops", "", "");
1906 $$ = context->intermediate.addBranch(EOpContinue, $1.line);
1909 if (context->loopNestingLevel <= 0) {
1910 context->error($1.line, "break statement only allowed in loops", "", "");
1913 $$ = context->intermediate.addBranch(EOpBreak, $1.line);
1915 | RETURN SEMICOLON {
1916 $$ = context->intermediate.addBranch(EOpReturn, $1.line);
1917 if (context->currentFunctionType->getBasicType() != EbtVoid) {
1918 context->error($1.line, "non-void function must return a value", "return", "");
1922 | RETURN expression SEMICOLON {
1923 $$ = context->intermediate.addBranch(EOpReturn, $2, $1.line);
1924 context->functionReturnsValue = true;
1925 if (context->currentFunctionType->getBasicType() == EbtVoid) {
1926 context->error($1.line, "void function cannot return a value", "return", "");
1928 } else if (*(context->currentFunctionType) != $2->getType()) {
1929 context->error($1.line, "function return is not matching type:", "return", "");
1933 | DISCARD SEMICOLON {
1934 FRAG_ONLY("discard", $1.line);
1935 $$ = context->intermediate.addBranch(EOpKill, $1.line);
1939 // Grammar Note: No 'goto'. Gotos are not supported.
1942 : external_declaration {
1944 context->treeRoot = $$;
1946 | translation_unit external_declaration {
1947 $$ = context->intermediate.growAggregate($1, $2, 0);
1948 context->treeRoot = $$;
1952 external_declaration
1953 : function_definition {
1962 : function_prototype {
1963 TFunction* function = $1.function;
1964 TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find(function->getMangledName()));
1966 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1967 // as it would have just been put in the symbol table. Otherwise, we're looking up
1968 // an earlier occurance.
1970 if (prevDec->isDefined()) {
1972 // Then this function already has a body.
1974 context->error($1.line, "function already has a body", function->getName().c_str(), "");
1977 prevDec->setDefined();
1980 // Raise error message if main function takes any parameters or return anything other than void
1982 if (function->getName() == "main") {
1983 if (function->getParamCount() > 0) {
1984 context->error($1.line, "function cannot take any parameter(s)", function->getName().c_str(), "");
1987 if (function->getReturnType().getBasicType() != EbtVoid) {
1988 context->error($1.line, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1994 // New symbol table scope for body of function plus its arguments
1996 context->symbolTable.push();
1999 // Remember the return type for later checking for RETURN statements.
2001 context->currentFunctionType = &(prevDec->getReturnType());
2002 context->functionReturnsValue = false;
2005 // Insert parameters into the symbol table.
2006 // If the parameter has no name, it's not an error, just don't insert it
2007 // (could be used for unused args).
2009 // Also, accumulate the list of parameters into the HIL, so lower level code
2010 // knows where to find parameters.
2012 TIntermAggregate* paramNodes = new TIntermAggregate;
2013 for (int i = 0; i < function->getParamCount(); i++) {
2014 const TParameter& param = function->getParam(i);
2015 if (param.name != 0) {
2016 TVariable *variable = new TVariable(param.name, *param.type);
2018 // Insert the parameters with name in the symbol table.
2020 if (! context->symbolTable.insert(*variable)) {
2021 context->error($1.line, "redefinition", variable->getName().c_str(), "");
2027 // Add the parameter to the HIL
2029 paramNodes = context->intermediate.growAggregate(
2031 context->intermediate.addSymbol(variable->getUniqueId(),
2032 variable->getName(),
2033 variable->getType(), $1.line),
2036 paramNodes = context->intermediate.growAggregate(paramNodes, context->intermediate.addSymbol(0, "", *param.type, $1.line), $1.line);
2039 context->intermediate.setAggregateOperator(paramNodes, EOpParameters, $1.line);
2040 $1.intermAggregate = paramNodes;
2041 context->loopNestingLevel = 0;
2043 compound_statement_no_new_scope {
2044 //?? Check that all paths return a value if return type != void ?
2045 // May be best done as post process phase on intermediate code
2046 if (context->currentFunctionType->getBasicType() != EbtVoid && ! context->functionReturnsValue) {
2047 context->error($1.line, "function does not return a value:", "", $1.function->getName().c_str());
2050 context->symbolTable.pop();
2051 $$ = context->intermediate.growAggregate($1.intermAggregate, $3, 0);
2052 context->intermediate.setAggregateOperator($$, EOpFunction, $1.line);
2053 $$->getAsAggregate()->setName($1.function->getMangledName().c_str());
2054 $$->getAsAggregate()->setType($1.function->getReturnType());
2056 // store the pragma information for debug and optimize and other vendor specific
2057 // information. This information can be queried from the parse tree
2058 $$->getAsAggregate()->setOptimize(context->contextPragma.optimize);
2059 $$->getAsAggregate()->setDebug(context->contextPragma.debug);
2060 $$->getAsAggregate()->addToPragmaTable(context->contextPragma.pragmaTable);
2062 if ($3 && $3->getAsAggregate())
2063 $$->getAsAggregate()->setEndLine($3->getAsAggregate()->getEndLine());
2069 int glslang_parse(TParseContext* context) {
2070 return yyparse(context);