blob: c45cd8be86798848d1e0672283f5ac6dbd9ed028 [file] [log] [blame]
John Kesseniche01a9bc2016-03-12 20:11:22 -07001//
2//Copyright (C) 2016 Google, Inc.
LoopDawg6daaa4f2016-06-23 19:13:48 -06003//Copyright (C) 2016 LunarG, Inc.
John Kesseniche01a9bc2016-03-12 20:11:22 -07004//
5//All rights reserved.
6//
7//Redistribution and use in source and binary forms, with or without
8//modification, are permitted provided that the following conditions
9//are met:
10//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of Google, Inc., nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
23//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34//POSSIBILITY OF SUCH DAMAGE.
35//
36
John Kessenichd016be12016-03-13 11:24:20 -060037//
38// This is a set of mutually recursive methods implementing the HLSL grammar.
39// Generally, each returns
40// - through an argument: a type specifically appropriate to which rule it
41// recognized
42// - through the return value: true/false to indicate whether or not it
43// recognized its rule
44//
45// As much as possible, only grammar recognition should happen in this file,
John Kessenich078d7f22016-03-14 10:02:11 -060046// with all other work being farmed out to hlslParseHelper.cpp, which in turn
John Kessenichd016be12016-03-13 11:24:20 -060047// will build the AST.
48//
49// The next token, yet to be "accepted" is always sitting in 'token'.
50// When a method says it accepts a rule, that means all tokens involved
51// in the rule will have been consumed, and none left in 'token'.
52//
53
John Kesseniche01a9bc2016-03-12 20:11:22 -070054#include "hlslTokens.h"
55#include "hlslGrammar.h"
56
57namespace glslang {
58
59// Root entry point to this recursive decent parser.
60// Return true if compilation unit was successfully accepted.
61bool HlslGrammar::parse()
62{
63 advanceToken();
64 return acceptCompilationUnit();
65}
66
67void HlslGrammar::expected(const char* syntax)
68{
69 parseContext.error(token.loc, "Expected", syntax, "");
70}
71
LoopDawg4886f692016-06-29 10:58:58 -060072void HlslGrammar::unimplemented(const char* error)
73{
74 parseContext.error(token.loc, "Unimplemented", error, "");
75}
76
John Kessenichaecd4972016-03-14 10:46:34 -060077// Only process the next token if it is an identifier.
78// Return true if it was an identifier.
79bool HlslGrammar::acceptIdentifier(HlslToken& idToken)
80{
81 if (peekTokenClass(EHTokIdentifier)) {
82 idToken = token;
83 advanceToken();
84 return true;
85 }
86
87 return false;
88}
89
John Kesseniche01a9bc2016-03-12 20:11:22 -070090// compilationUnit
91// : list of externalDeclaration
steve-lunargcb88de52016-08-03 07:04:18 -060092// | SEMICOLONS
John Kesseniche01a9bc2016-03-12 20:11:22 -070093//
94bool HlslGrammar::acceptCompilationUnit()
95{
John Kessenichd016be12016-03-13 11:24:20 -060096 TIntermNode* unitNode = nullptr;
97
John Kessenich9c86c6a2016-05-03 22:49:24 -060098 while (! peekTokenClass(EHTokNone)) {
steve-lunargcb88de52016-08-03 07:04:18 -060099 // HLSL allows semicolons between global declarations, e.g, between functions.
100 if (acceptTokenClass(EHTokSemicolon))
101 continue;
102
John Kessenichd016be12016-03-13 11:24:20 -0600103 // externalDeclaration
104 TIntermNode* declarationNode;
105 if (! acceptDeclaration(declarationNode))
John Kesseniche01a9bc2016-03-12 20:11:22 -0700106 return false;
John Kessenichd016be12016-03-13 11:24:20 -0600107
108 // hook it up
John Kessenich078d7f22016-03-14 10:02:11 -0600109 unitNode = intermediate.growAggregate(unitNode, declarationNode);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700110 }
111
John Kessenichd016be12016-03-13 11:24:20 -0600112 // set root of AST
John Kessenich078d7f22016-03-14 10:02:11 -0600113 intermediate.setTreeRoot(unitNode);
John Kessenichd016be12016-03-13 11:24:20 -0600114
John Kesseniche01a9bc2016-03-12 20:11:22 -0700115 return true;
116}
117
LoopDawg4886f692016-06-29 10:58:58 -0600118// sampler_state
119// : LEFT_BRACE [sampler_state_assignment ... ] RIGHT_BRACE
120//
121// sampler_state_assignment
122// : sampler_state_identifier EQUAL value SEMICOLON
123//
124// sampler_state_identifier
125// : ADDRESSU
126// | ADDRESSV
127// | ADDRESSW
128// | BORDERCOLOR
129// | FILTER
130// | MAXANISOTROPY
131// | MAXLOD
132// | MINLOD
133// | MIPLODBIAS
134//
135bool HlslGrammar::acceptSamplerState()
136{
137 // TODO: this should be genericized to accept a list of valid tokens and
138 // return token/value pairs. Presently it is specific to texture values.
139
140 if (! acceptTokenClass(EHTokLeftBrace))
141 return true;
142
143 parseContext.warn(token.loc, "unimplemented", "immediate sampler state", "");
144
145 do {
146 // read state name
147 HlslToken state;
148 if (! acceptIdentifier(state))
149 break; // end of list
150
151 // FXC accepts any case
152 TString stateName = *state.string;
153 std::transform(stateName.begin(), stateName.end(), stateName.begin(), ::tolower);
154
155 if (! acceptTokenClass(EHTokAssign)) {
156 expected("assign");
157 return false;
158 }
159
160 if (stateName == "minlod" || stateName == "maxlod") {
161 if (! peekTokenClass(EHTokIntConstant)) {
162 expected("integer");
163 return false;
164 }
165
166 TIntermTyped* lod = nullptr;
167 if (! acceptLiteral(lod)) // should never fail, since we just looked for an integer
168 return false;
169 } else if (stateName == "maxanisotropy") {
170 if (! peekTokenClass(EHTokIntConstant)) {
171 expected("integer");
172 return false;
173 }
174
175 TIntermTyped* maxAnisotropy = nullptr;
176 if (! acceptLiteral(maxAnisotropy)) // should never fail, since we just looked for an integer
177 return false;
178 } else if (stateName == "filter") {
179 HlslToken filterMode;
180 if (! acceptIdentifier(filterMode)) {
181 expected("filter mode");
182 return false;
183 }
184 } else if (stateName == "addressu" || stateName == "addressv" || stateName == "addressw") {
185 HlslToken addrMode;
186 if (! acceptIdentifier(addrMode)) {
187 expected("texture address mode");
188 return false;
189 }
190 } else if (stateName == "miplodbias") {
191 TIntermTyped* lodBias = nullptr;
192 if (! acceptLiteral(lodBias)) {
193 expected("lod bias");
194 return false;
195 }
196 } else if (stateName == "bordercolor") {
197 return false;
198 } else {
199 expected("texture state");
200 return false;
201 }
202
203 // SEMICOLON
204 if (! acceptTokenClass(EHTokSemicolon)) {
205 expected("semicolon");
206 return false;
207 }
208 } while (true);
209
210 if (! acceptTokenClass(EHTokRightBrace))
211 return false;
212
213 return true;
214}
215
216// sampler_declaration_dx9
217// : SAMPLER identifier EQUAL sampler_type sampler_state
218//
John Kesseniche4821e42016-07-16 10:19:43 -0600219bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/)
LoopDawg4886f692016-06-29 10:58:58 -0600220{
221 if (! acceptTokenClass(EHTokSampler))
222 return false;
223
224 // TODO: remove this when DX9 style declarations are implemented.
225 unimplemented("Direct3D 9 sampler declaration");
226
227 // read sampler name
228 HlslToken name;
229 if (! acceptIdentifier(name)) {
230 expected("sampler name");
231 return false;
232 }
233
234 if (! acceptTokenClass(EHTokAssign)) {
235 expected("=");
236 return false;
237 }
238
239 return false;
240}
241
242
John Kesseniche01a9bc2016-03-12 20:11:22 -0700243// declaration
LoopDawg4886f692016-06-29 10:58:58 -0600244// : sampler_declaration_dx9 post_decls SEMICOLON
245// | fully_specified_type declarator_list SEMICOLON
John Kessenich630dd7d2016-06-12 23:52:12 -0600246// | fully_specified_type identifier function_parameters post_decls compound_statement // function definition
LoopDawg4886f692016-06-29 10:58:58 -0600247// | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition
John Kessenich5e69ec62016-07-05 00:02:40 -0600248// | typedef declaration
John Kessenich87142c72016-03-12 20:24:24 -0700249//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600250// declarator_list
251// : declarator COMMA declarator COMMA declarator... // zero or more declarators
John Kessenich532543c2016-07-01 19:06:44 -0600252//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600253// declarator
John Kessenich532543c2016-07-01 19:06:44 -0600254// : identifier array_specifier post_decls
255// | identifier array_specifier post_decls EQUAL assignment_expression
John Kessenichd5ed0b62016-07-04 17:32:45 -0600256// | identifier function_parameters post_decls // function prototype
John Kessenich532543c2016-07-01 19:06:44 -0600257//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600258// Parsing has to go pretty far in to know whether it's a variable, prototype, or
259// function definition, so the implementation below doesn't perfectly divide up the grammar
John Kessenich532543c2016-07-01 19:06:44 -0600260// as above. (The 'identifier' in the first item in init_declarator list is the
261// same as 'identifier' for function declarations.)
262//
263// 'node' could get populated if the declaration creates code, like an initializer
John Kessenichd016be12016-03-13 11:24:20 -0600264// or a function body.
265//
266bool HlslGrammar::acceptDeclaration(TIntermNode*& node)
John Kesseniche01a9bc2016-03-12 20:11:22 -0700267{
John Kessenichd016be12016-03-13 11:24:20 -0600268 node = nullptr;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600269 bool list = false;
John Kessenichd016be12016-03-13 11:24:20 -0600270
John Kessenich5e69ec62016-07-05 00:02:40 -0600271 // typedef
272 bool typedefDecl = acceptTokenClass(EHTokTypedef);
273
John Kessenich87142c72016-03-12 20:24:24 -0700274 TType type;
LoopDawg4886f692016-06-29 10:58:58 -0600275
276 // DX9 sampler declaration use a different syntax
John Kessenich267590d2016-08-05 17:34:34 -0600277 // DX9 shaders need to run through HLSL compiler (fxc) via a back compat mode, it isn't going to
278 // be possible to simultaneously compile D3D10+ style shaders and DX9 shaders. If we want to compile DX9
279 // HLSL shaders, this will have to be a master level switch
280 // As such, the sampler keyword in D3D10+ turns into an automatic sampler type, and is commonly used
281 // For that reason, this line is commented out
Dan Bakerc7e50162016-08-05 14:52:38 -0400282
283 // if (acceptSamplerDeclarationDX9(type))
284 // return true;
LoopDawg4886f692016-06-29 10:58:58 -0600285
286 // fully_specified_type
John Kessenich87142c72016-03-12 20:24:24 -0700287 if (! acceptFullySpecifiedType(type))
288 return false;
LoopDawg4886f692016-06-29 10:58:58 -0600289
290 if (type.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel()) {
291 if (type.getBasicType() == EbtSampler) {
292 // Sampler/textures are uniform by default (if no explicit qualifier is present) in
293 // HLSL. This line silently converts samplers *explicitly* declared static to uniform,
294 // which is incorrect but harmless.
295 type.getQualifier().storage = EvqUniform;
296 } else {
297 type.getQualifier().storage = EvqGlobal;
298 }
299 }
John Kessenich87142c72016-03-12 20:24:24 -0700300
301 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -0600302 HlslToken idToken;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600303 while (acceptIdentifier(idToken)) {
John Kessenich5f934b02016-03-13 17:58:25 -0600304 // function_parameters
John Kessenich9e079532016-09-02 20:05:19 -0600305 TFunction& function = *new TFunction(idToken.string, type);
306 if (acceptFunctionParameters(function)) {
John Kessenich630dd7d2016-06-12 23:52:12 -0600307 // post_decls
John Kessenich9e079532016-09-02 20:05:19 -0600308 acceptPostDecls(function.getWritableType());
John Kessenich078d7f22016-03-14 10:02:11 -0600309
John Kessenichd5ed0b62016-07-04 17:32:45 -0600310 // compound_statement (function body definition) or just a prototype?
311 if (peekTokenClass(EHTokLeftBrace)) {
312 if (list)
313 parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", "");
John Kessenich5e69ec62016-07-05 00:02:40 -0600314 if (typedefDecl)
315 parseContext.error(idToken.loc, "function body can't be in a typedef", "{", "");
John Kessenich9e079532016-09-02 20:05:19 -0600316 return acceptFunctionDefinition(function, node);
John Kessenich5e69ec62016-07-05 00:02:40 -0600317 } else {
318 if (typedefDecl)
319 parseContext.error(idToken.loc, "function typedefs not implemented", "{", "");
John Kessenich9e079532016-09-02 20:05:19 -0600320 parseContext.handleFunctionDeclarator(idToken.loc, function, true);
John Kessenich5e69ec62016-07-05 00:02:40 -0600321 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600322 } else {
323 // a variable declaration
John Kessenich5f934b02016-03-13 17:58:25 -0600324
John Kessenichd5ed0b62016-07-04 17:32:45 -0600325 // array_specifier
326 TArraySizes* arraySizes = nullptr;
327 acceptArraySpecifier(arraySizes);
John Kessenich5f934b02016-03-13 17:58:25 -0600328
LoopDawg4886f692016-06-29 10:58:58 -0600329 // samplers accept immediate sampler state
330 if (type.getBasicType() == EbtSampler) {
331 if (! acceptSamplerState())
332 return false;
333 }
334
John Kessenichd5ed0b62016-07-04 17:32:45 -0600335 // post_decls
336 acceptPostDecls(type);
337
338 // EQUAL assignment_expression
339 TIntermTyped* expressionNode = nullptr;
340 if (acceptTokenClass(EHTokAssign)) {
John Kessenich5e69ec62016-07-05 00:02:40 -0600341 if (typedefDecl)
342 parseContext.error(idToken.loc, "can't have an initializer", "typedef", "");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600343 if (! acceptAssignmentExpression(expressionNode)) {
344 expected("initializer");
345 return false;
346 }
347 }
348
John Kessenich5e69ec62016-07-05 00:02:40 -0600349 if (typedefDecl)
350 parseContext.declareTypedef(idToken.loc, *idToken.string, type, arraySizes);
John Kessenich3d157c52016-07-25 16:05:33 -0600351 else if (type.getBasicType() == EbtBlock)
352 parseContext.declareBlock(idToken.loc, type, idToken.string);
John Kessenich5e69ec62016-07-05 00:02:40 -0600353 else {
354 // Declare the variable and add any initializer code to the AST.
355 // The top-level node is always made into an aggregate, as that's
356 // historically how the AST has been.
357 node = intermediate.growAggregate(node,
358 parseContext.declareVariable(idToken.loc, *idToken.string, type,
359 arraySizes, expressionNode),
360 idToken.loc);
361 }
John Kessenich5f934b02016-03-13 17:58:25 -0600362 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600363
364 if (acceptTokenClass(EHTokComma)) {
365 list = true;
366 continue;
367 }
368 };
369
370 // The top-level node is a sequence.
371 if (node != nullptr)
372 node->getAsAggregate()->setOperator(EOpSequence);
John Kessenich87142c72016-03-12 20:24:24 -0700373
John Kessenich078d7f22016-03-14 10:02:11 -0600374 // SEMICOLON
John Kessenichd5ed0b62016-07-04 17:32:45 -0600375 if (! acceptTokenClass(EHTokSemicolon)) {
376 expected(";");
377 return false;
378 }
379
John Kesseniche01a9bc2016-03-12 20:11:22 -0700380 return true;
381}
382
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600383// control_declaration
384// : fully_specified_type identifier EQUAL expression
385//
386bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node)
387{
388 node = nullptr;
389
390 // fully_specified_type
391 TType type;
392 if (! acceptFullySpecifiedType(type))
393 return false;
394
395 // identifier
396 HlslToken idToken;
397 if (! acceptIdentifier(idToken)) {
398 expected("identifier");
399 return false;
400 }
401
402 // EQUAL
403 TIntermTyped* expressionNode = nullptr;
404 if (! acceptTokenClass(EHTokAssign)) {
405 expected("=");
406 return false;
407 }
408
409 // expression
410 if (! acceptExpression(expressionNode)) {
411 expected("initializer");
412 return false;
413 }
414
415 node = parseContext.declareVariable(idToken.loc, *idToken.string, type, 0, expressionNode);
416
417 return true;
418}
419
John Kessenich87142c72016-03-12 20:24:24 -0700420// fully_specified_type
421// : type_specifier
422// | type_qualifier type_specifier
423//
424bool HlslGrammar::acceptFullySpecifiedType(TType& type)
425{
426 // type_qualifier
427 TQualifier qualifier;
428 qualifier.clear();
John Kessenichb9e39122016-08-17 10:22:08 -0600429 if (! acceptQualifier(qualifier))
430 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600431 TSourceLoc loc = token.loc;
John Kessenich87142c72016-03-12 20:24:24 -0700432
433 // type_specifier
434 if (! acceptType(type))
435 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600436 if (type.getBasicType() == EbtBlock) {
437 // the type was a block, which set some parts of the qualifier
438 parseContext.mergeQualifiers(loc, type.getQualifier(), qualifier, true);
439 // further, it can create an anonymous instance of the block
440 if (peekTokenClass(EHTokSemicolon))
441 parseContext.declareBlock(loc, type);
442 } else
443 type.getQualifier() = qualifier;
John Kessenich87142c72016-03-12 20:24:24 -0700444
445 return true;
446}
447
John Kessenich630dd7d2016-06-12 23:52:12 -0600448// type_qualifier
449// : qualifier qualifier ...
450//
451// Zero or more of these, so this can't return false.
452//
John Kessenichb9e39122016-08-17 10:22:08 -0600453bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
John Kessenich87142c72016-03-12 20:24:24 -0700454{
John Kessenich630dd7d2016-06-12 23:52:12 -0600455 do {
456 switch (peek()) {
457 case EHTokStatic:
458 // normal glslang default
459 break;
460 case EHTokExtern:
461 // TODO: no meaning in glslang?
462 break;
463 case EHTokShared:
464 // TODO: hint
465 break;
466 case EHTokGroupShared:
467 qualifier.storage = EvqShared;
468 break;
469 case EHTokUniform:
470 qualifier.storage = EvqUniform;
471 break;
472 case EHTokConst:
473 qualifier.storage = EvqConst;
474 break;
475 case EHTokVolatile:
476 qualifier.volatil = true;
477 break;
478 case EHTokLinear:
479 qualifier.storage = EvqVaryingIn;
480 qualifier.smooth = true;
481 break;
482 case EHTokCentroid:
483 qualifier.centroid = true;
484 break;
485 case EHTokNointerpolation:
486 qualifier.flat = true;
487 break;
488 case EHTokNoperspective:
489 qualifier.nopersp = true;
490 break;
491 case EHTokSample:
492 qualifier.sample = true;
493 break;
494 case EHTokRowMajor:
495 qualifier.layoutMatrix = ElmRowMajor;
496 break;
497 case EHTokColumnMajor:
498 qualifier.layoutMatrix = ElmColumnMajor;
499 break;
500 case EHTokPrecise:
501 qualifier.noContraction = true;
502 break;
LoopDawg9249c702016-07-12 20:44:32 -0600503 case EHTokIn:
504 qualifier.storage = EvqIn;
505 break;
506 case EHTokOut:
507 qualifier.storage = EvqOut;
508 break;
509 case EHTokInOut:
510 qualifier.storage = EvqInOut;
511 break;
John Kessenichb9e39122016-08-17 10:22:08 -0600512 case EHTokLayout:
513 if (! acceptLayoutQualifierList(qualifier))
514 return false;
515 continue;
John Kessenich630dd7d2016-06-12 23:52:12 -0600516 default:
John Kessenichb9e39122016-08-17 10:22:08 -0600517 return true;
John Kessenich630dd7d2016-06-12 23:52:12 -0600518 }
519 advanceToken();
520 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -0700521}
522
John Kessenichb9e39122016-08-17 10:22:08 -0600523// layout_qualifier_list
524// : LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
525//
526// layout_qualifier
527// : identifier
John Kessenich841db352016-09-02 21:12:23 -0600528// | identifier EQUAL expression
John Kessenichb9e39122016-08-17 10:22:08 -0600529//
530// Zero or more of these, so this can't return false.
531//
532bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
533{
534 if (! acceptTokenClass(EHTokLayout))
535 return false;
536
537 // LEFT_PAREN
538 if (! acceptTokenClass(EHTokLeftParen))
539 return false;
540
541 do {
542 // identifier
543 HlslToken idToken;
544 if (! acceptIdentifier(idToken))
545 break;
546
547 // EQUAL expression
548 if (acceptTokenClass(EHTokAssign)) {
549 TIntermTyped* expr;
550 if (! acceptConditionalExpression(expr)) {
551 expected("expression");
552 return false;
553 }
554 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
555 } else
556 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
557
558 // COMMA
559 if (! acceptTokenClass(EHTokComma))
560 break;
561 } while (true);
562
563 // RIGHT_PAREN
564 if (! acceptTokenClass(EHTokRightParen)) {
565 expected(")");
566 return false;
567 }
568
569 return true;
570}
571
LoopDawg6daaa4f2016-06-23 19:13:48 -0600572// template_type
573// : FLOAT
574// | DOUBLE
575// | INT
576// | DWORD
577// | UINT
578// | BOOL
579//
580bool HlslGrammar::acceptTemplateType(TBasicType& basicType)
581{
582 switch (peek()) {
583 case EHTokFloat:
584 basicType = EbtFloat;
585 break;
586 case EHTokDouble:
587 basicType = EbtDouble;
588 break;
589 case EHTokInt:
590 case EHTokDword:
591 basicType = EbtInt;
592 break;
593 case EHTokUint:
594 basicType = EbtUint;
595 break;
596 case EHTokBool:
597 basicType = EbtBool;
598 break;
599 default:
600 return false;
601 }
602
603 advanceToken();
604
605 return true;
606}
607
608// vector_template_type
609// : VECTOR
610// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
611//
612bool HlslGrammar::acceptVectorTemplateType(TType& type)
613{
614 if (! acceptTokenClass(EHTokVector))
615 return false;
616
617 if (! acceptTokenClass(EHTokLeftAngle)) {
618 // in HLSL, 'vector' alone means float4.
619 new(&type) TType(EbtFloat, EvqTemporary, 4);
620 return true;
621 }
622
623 TBasicType basicType;
624 if (! acceptTemplateType(basicType)) {
625 expected("scalar type");
626 return false;
627 }
628
629 // COMMA
630 if (! acceptTokenClass(EHTokComma)) {
631 expected(",");
632 return false;
633 }
634
635 // integer
636 if (! peekTokenClass(EHTokIntConstant)) {
637 expected("literal integer");
638 return false;
639 }
640
641 TIntermTyped* vecSize;
642 if (! acceptLiteral(vecSize))
643 return false;
644
645 const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
646
647 new(&type) TType(basicType, EvqTemporary, vecSizeI);
648
649 if (vecSizeI == 1)
650 type.makeVector();
651
652 if (!acceptTokenClass(EHTokRightAngle)) {
653 expected("right angle bracket");
654 return false;
655 }
656
657 return true;
658}
659
660// matrix_template_type
661// : MATRIX
662// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
663//
664bool HlslGrammar::acceptMatrixTemplateType(TType& type)
665{
666 if (! acceptTokenClass(EHTokMatrix))
667 return false;
668
669 if (! acceptTokenClass(EHTokLeftAngle)) {
670 // in HLSL, 'matrix' alone means float4x4.
671 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
672 return true;
673 }
674
675 TBasicType basicType;
676 if (! acceptTemplateType(basicType)) {
677 expected("scalar type");
678 return false;
679 }
680
681 // COMMA
682 if (! acceptTokenClass(EHTokComma)) {
683 expected(",");
684 return false;
685 }
686
687 // integer rows
688 if (! peekTokenClass(EHTokIntConstant)) {
689 expected("literal integer");
690 return false;
691 }
692
693 TIntermTyped* rows;
694 if (! acceptLiteral(rows))
695 return false;
696
697 // COMMA
698 if (! acceptTokenClass(EHTokComma)) {
699 expected(",");
700 return false;
701 }
702
703 // integer cols
704 if (! peekTokenClass(EHTokIntConstant)) {
705 expected("literal integer");
706 return false;
707 }
708
709 TIntermTyped* cols;
710 if (! acceptLiteral(cols))
711 return false;
712
713 new(&type) TType(basicType, EvqTemporary, 0,
714 cols->getAsConstantUnion()->getConstArray()[0].getIConst(),
715 rows->getAsConstantUnion()->getConstArray()[0].getIConst());
716
717 if (!acceptTokenClass(EHTokRightAngle)) {
718 expected("right angle bracket");
719 return false;
720 }
721
722 return true;
723}
724
725
LoopDawg4886f692016-06-29 10:58:58 -0600726// sampler_type
727// : SAMPLER
728// | SAMPLER1D
729// | SAMPLER2D
730// | SAMPLER3D
731// | SAMPLERCUBE
732// | SAMPLERSTATE
733// | SAMPLERCOMPARISONSTATE
734bool HlslGrammar::acceptSamplerType(TType& type)
735{
736 // read sampler type
737 const EHlslTokenClass samplerType = peek();
738
LoopDawga78b0292016-07-19 14:28:05 -0600739 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -0600740 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -0600741
LoopDawga78b0292016-07-19 14:28:05 -0600742 bool isShadow = false;
743
LoopDawg4886f692016-06-29 10:58:58 -0600744 switch (samplerType) {
745 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -0600746 case EHTokSampler1d: /*dim = Esd1D*/; break;
747 case EHTokSampler2d: /*dim = Esd2D*/; break;
748 case EHTokSampler3d: /*dim = Esd3D*/; break;
749 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -0600750 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -0600751 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -0600752 default:
753 return false; // not a sampler declaration
754 }
755
756 advanceToken(); // consume the sampler type keyword
757
758 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -0600759
760 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -0600761 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -0600762
763 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
764
765 return true;
766}
767
768// texture_type
769// | BUFFER
770// | TEXTURE1D
771// | TEXTURE1DARRAY
772// | TEXTURE2D
773// | TEXTURE2DARRAY
774// | TEXTURE3D
775// | TEXTURECUBE
776// | TEXTURECUBEARRAY
777// | TEXTURE2DMS
778// | TEXTURE2DMSARRAY
779bool HlslGrammar::acceptTextureType(TType& type)
780{
781 const EHlslTokenClass textureType = peek();
782
783 TSamplerDim dim = EsdNone;
784 bool array = false;
785 bool ms = false;
786
787 switch (textureType) {
788 case EHTokBuffer: dim = EsdBuffer; break;
789 case EHTokTexture1d: dim = Esd1D; break;
790 case EHTokTexture1darray: dim = Esd1D; array = true; break;
791 case EHTokTexture2d: dim = Esd2D; break;
792 case EHTokTexture2darray: dim = Esd2D; array = true; break;
793 case EHTokTexture3d: dim = Esd3D; break;
794 case EHTokTextureCube: dim = EsdCube; break;
795 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
796 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
797 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
798 default:
799 return false; // not a texture declaration
800 }
801
802 advanceToken(); // consume the texture object keyword
803
804 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
805
806 TIntermTyped* msCount = nullptr;
807
808 // texture type: required for multisample types!
809 if (acceptTokenClass(EHTokLeftAngle)) {
810 if (! acceptType(txType)) {
811 expected("scalar or vector type");
812 return false;
813 }
814
815 const TBasicType basicRetType = txType.getBasicType() ;
816
817 if (basicRetType != EbtFloat && basicRetType != EbtUint && basicRetType != EbtInt) {
818 unimplemented("basic type in texture");
819 return false;
820 }
821
steve-lunargd53f7172016-07-27 15:46:48 -0600822 // Buffers can handle small mats if they fit in 4 components
823 if (dim == EsdBuffer && txType.isMatrix()) {
824 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
825 expected("components < 4 in matrix buffer type");
826 return false;
827 }
828
829 // TODO: except we don't handle it yet...
830 unimplemented("matrix type in buffer");
831 return false;
832 }
833
LoopDawg4886f692016-06-29 10:58:58 -0600834 if (!txType.isScalar() && !txType.isVector()) {
835 expected("scalar or vector type");
836 return false;
837 }
838
839 if (txType.getVectorSize() != 1 && txType.getVectorSize() != 4) {
840 // TODO: handle vec2/3 types
841 expected("vector size not yet supported in texture type");
842 return false;
843 }
844
845 if (ms && acceptTokenClass(EHTokComma)) {
846 // read sample count for multisample types, if given
847 if (! peekTokenClass(EHTokIntConstant)) {
848 expected("multisample count");
849 return false;
850 }
851
852 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
853 return false;
854 }
855
856 if (! acceptTokenClass(EHTokRightAngle)) {
857 expected("right angle bracket");
858 return false;
859 }
860 } else if (ms) {
861 expected("texture type for multisample");
862 return false;
863 }
864
865 TArraySizes* arraySizes = nullptr;
866 const bool shadow = txType.isScalar() || (txType.isVector() && txType.getVectorSize() == 1);
867
868 TSampler sampler;
steve-lunargd53f7172016-07-27 15:46:48 -0600869
870 // Buffers are combined.
871 if (dim == EsdBuffer) {
872 sampler.set(txType.getBasicType(), dim, array);
873 } else {
874 // DX10 textures are separated. TODO: DX9.
875 sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms);
876 }
LoopDawg4886f692016-06-29 10:58:58 -0600877
878 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
879
880 return true;
881}
882
883
John Kessenich87142c72016-03-12 20:24:24 -0700884// If token is for a type, update 'type' with the type information,
885// and return true and advance.
886// Otherwise, return false, and don't advance
887bool HlslGrammar::acceptType(TType& type)
888{
John Kessenich9c86c6a2016-05-03 22:49:24 -0600889 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600890 case EHTokVector:
891 return acceptVectorTemplateType(type);
892 break;
893
894 case EHTokMatrix:
895 return acceptMatrixTemplateType(type);
896 break;
897
LoopDawg4886f692016-06-29 10:58:58 -0600898 case EHTokSampler: // fall through
899 case EHTokSampler1d: // ...
900 case EHTokSampler2d: // ...
901 case EHTokSampler3d: // ...
902 case EHTokSamplerCube: // ...
903 case EHTokSamplerState: // ...
904 case EHTokSamplerComparisonState: // ...
905 return acceptSamplerType(type);
906 break;
907
908 case EHTokBuffer: // fall through
909 case EHTokTexture1d: // ...
910 case EHTokTexture1darray: // ...
911 case EHTokTexture2d: // ...
912 case EHTokTexture2darray: // ...
913 case EHTokTexture3d: // ...
914 case EHTokTextureCube: // ...
915 case EHTokTextureCubearray: // ...
916 case EHTokTexture2DMS: // ...
917 case EHTokTexture2DMSarray: // ...
918 return acceptTextureType(type);
919 break;
920
John Kesseniche6e74942016-06-11 16:43:14 -0600921 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -0600922 case EHTokCBuffer:
923 case EHTokTBuffer:
John Kesseniche6e74942016-06-11 16:43:14 -0600924 return acceptStruct(type);
925 break;
926
927 case EHTokIdentifier:
928 // An identifier could be for a user-defined type.
929 // Note we cache the symbol table lookup, to save for a later rule
930 // when this is not a type.
931 token.symbol = parseContext.symbolTable.find(*token.string);
932 if (token.symbol && token.symbol->getAsVariable() && token.symbol->getAsVariable()->isUserType()) {
933 type.shallowCopy(token.symbol->getType());
934 advanceToken();
935 return true;
936 } else
937 return false;
938
John Kessenich71351de2016-06-08 12:50:56 -0600939 case EHTokVoid:
940 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -0700941 break;
John Kessenich71351de2016-06-08 12:50:56 -0600942
John Kessenich87142c72016-03-12 20:24:24 -0700943 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -0600944 new(&type) TType(EbtFloat);
945 break;
John Kessenich87142c72016-03-12 20:24:24 -0700946 case EHTokFloat1:
947 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -0600948 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -0700949 break;
John Kessenich87142c72016-03-12 20:24:24 -0700950 case EHTokFloat2:
951 new(&type) TType(EbtFloat, EvqTemporary, 2);
952 break;
953 case EHTokFloat3:
954 new(&type) TType(EbtFloat, EvqTemporary, 3);
955 break;
956 case EHTokFloat4:
957 new(&type) TType(EbtFloat, EvqTemporary, 4);
958 break;
959
John Kessenich71351de2016-06-08 12:50:56 -0600960 case EHTokDouble:
961 new(&type) TType(EbtDouble);
962 break;
963 case EHTokDouble1:
964 new(&type) TType(EbtDouble);
965 type.makeVector();
966 break;
967 case EHTokDouble2:
968 new(&type) TType(EbtDouble, EvqTemporary, 2);
969 break;
970 case EHTokDouble3:
971 new(&type) TType(EbtDouble, EvqTemporary, 3);
972 break;
973 case EHTokDouble4:
974 new(&type) TType(EbtDouble, EvqTemporary, 4);
975 break;
976
977 case EHTokInt:
978 case EHTokDword:
979 new(&type) TType(EbtInt);
980 break;
981 case EHTokInt1:
982 new(&type) TType(EbtInt);
983 type.makeVector();
984 break;
John Kessenich87142c72016-03-12 20:24:24 -0700985 case EHTokInt2:
986 new(&type) TType(EbtInt, EvqTemporary, 2);
987 break;
988 case EHTokInt3:
989 new(&type) TType(EbtInt, EvqTemporary, 3);
990 break;
991 case EHTokInt4:
992 new(&type) TType(EbtInt, EvqTemporary, 4);
993 break;
994
John Kessenich71351de2016-06-08 12:50:56 -0600995 case EHTokUint:
996 new(&type) TType(EbtUint);
997 break;
998 case EHTokUint1:
999 new(&type) TType(EbtUint);
1000 type.makeVector();
1001 break;
1002 case EHTokUint2:
1003 new(&type) TType(EbtUint, EvqTemporary, 2);
1004 break;
1005 case EHTokUint3:
1006 new(&type) TType(EbtUint, EvqTemporary, 3);
1007 break;
1008 case EHTokUint4:
1009 new(&type) TType(EbtUint, EvqTemporary, 4);
1010 break;
1011
LoopDawg6daaa4f2016-06-23 19:13:48 -06001012
John Kessenich71351de2016-06-08 12:50:56 -06001013 case EHTokBool:
1014 new(&type) TType(EbtBool);
1015 break;
1016 case EHTokBool1:
1017 new(&type) TType(EbtBool);
1018 type.makeVector();
1019 break;
John Kessenich87142c72016-03-12 20:24:24 -07001020 case EHTokBool2:
1021 new(&type) TType(EbtBool, EvqTemporary, 2);
1022 break;
1023 case EHTokBool3:
1024 new(&type) TType(EbtBool, EvqTemporary, 3);
1025 break;
1026 case EHTokBool4:
1027 new(&type) TType(EbtBool, EvqTemporary, 4);
1028 break;
1029
John Kessenich0133c122016-05-20 12:17:26 -06001030 case EHTokInt1x1:
1031 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1032 break;
1033 case EHTokInt1x2:
1034 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
1035 break;
1036 case EHTokInt1x3:
1037 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
1038 break;
1039 case EHTokInt1x4:
1040 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
1041 break;
1042 case EHTokInt2x1:
1043 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
1044 break;
1045 case EHTokInt2x2:
1046 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1047 break;
1048 case EHTokInt2x3:
1049 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
1050 break;
1051 case EHTokInt2x4:
1052 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
1053 break;
1054 case EHTokInt3x1:
1055 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
1056 break;
1057 case EHTokInt3x2:
1058 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
1059 break;
1060 case EHTokInt3x3:
1061 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1062 break;
1063 case EHTokInt3x4:
1064 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
1065 break;
1066 case EHTokInt4x1:
1067 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
1068 break;
1069 case EHTokInt4x2:
1070 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
1071 break;
1072 case EHTokInt4x3:
1073 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
1074 break;
1075 case EHTokInt4x4:
1076 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1077 break;
1078
John Kessenich71351de2016-06-08 12:50:56 -06001079 case EHTokUint1x1:
1080 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1081 break;
1082 case EHTokUint1x2:
1083 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
1084 break;
1085 case EHTokUint1x3:
1086 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
1087 break;
1088 case EHTokUint1x4:
1089 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
1090 break;
1091 case EHTokUint2x1:
1092 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
1093 break;
1094 case EHTokUint2x2:
1095 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1096 break;
1097 case EHTokUint2x3:
1098 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
1099 break;
1100 case EHTokUint2x4:
1101 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
1102 break;
1103 case EHTokUint3x1:
1104 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
1105 break;
1106 case EHTokUint3x2:
1107 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
1108 break;
1109 case EHTokUint3x3:
1110 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1111 break;
1112 case EHTokUint3x4:
1113 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
1114 break;
1115 case EHTokUint4x1:
1116 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
1117 break;
1118 case EHTokUint4x2:
1119 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
1120 break;
1121 case EHTokUint4x3:
1122 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
1123 break;
1124 case EHTokUint4x4:
1125 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1126 break;
1127
1128 case EHTokBool1x1:
1129 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1130 break;
1131 case EHTokBool1x2:
1132 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
1133 break;
1134 case EHTokBool1x3:
1135 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
1136 break;
1137 case EHTokBool1x4:
1138 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
1139 break;
1140 case EHTokBool2x1:
1141 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
1142 break;
1143 case EHTokBool2x2:
1144 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1145 break;
1146 case EHTokBool2x3:
1147 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
1148 break;
1149 case EHTokBool2x4:
1150 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
1151 break;
1152 case EHTokBool3x1:
1153 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
1154 break;
1155 case EHTokBool3x2:
1156 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
1157 break;
1158 case EHTokBool3x3:
1159 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1160 break;
1161 case EHTokBool3x4:
1162 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
1163 break;
1164 case EHTokBool4x1:
1165 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
1166 break;
1167 case EHTokBool4x2:
1168 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
1169 break;
1170 case EHTokBool4x3:
1171 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
1172 break;
1173 case EHTokBool4x4:
1174 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1175 break;
1176
John Kessenich0133c122016-05-20 12:17:26 -06001177 case EHTokFloat1x1:
1178 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1179 break;
1180 case EHTokFloat1x2:
1181 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
1182 break;
1183 case EHTokFloat1x3:
1184 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
1185 break;
1186 case EHTokFloat1x4:
1187 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
1188 break;
1189 case EHTokFloat2x1:
1190 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
1191 break;
John Kessenich87142c72016-03-12 20:24:24 -07001192 case EHTokFloat2x2:
1193 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1194 break;
1195 case EHTokFloat2x3:
1196 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
1197 break;
1198 case EHTokFloat2x4:
1199 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
1200 break;
John Kessenich0133c122016-05-20 12:17:26 -06001201 case EHTokFloat3x1:
1202 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
1203 break;
John Kessenich87142c72016-03-12 20:24:24 -07001204 case EHTokFloat3x2:
1205 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
1206 break;
1207 case EHTokFloat3x3:
1208 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1209 break;
1210 case EHTokFloat3x4:
1211 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
1212 break;
John Kessenich0133c122016-05-20 12:17:26 -06001213 case EHTokFloat4x1:
1214 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
1215 break;
John Kessenich87142c72016-03-12 20:24:24 -07001216 case EHTokFloat4x2:
1217 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
1218 break;
1219 case EHTokFloat4x3:
1220 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
1221 break;
1222 case EHTokFloat4x4:
1223 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1224 break;
1225
John Kessenich0133c122016-05-20 12:17:26 -06001226 case EHTokDouble1x1:
1227 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1228 break;
1229 case EHTokDouble1x2:
1230 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
1231 break;
1232 case EHTokDouble1x3:
1233 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
1234 break;
1235 case EHTokDouble1x4:
1236 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
1237 break;
1238 case EHTokDouble2x1:
1239 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
1240 break;
1241 case EHTokDouble2x2:
1242 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1243 break;
1244 case EHTokDouble2x3:
1245 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
1246 break;
1247 case EHTokDouble2x4:
1248 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
1249 break;
1250 case EHTokDouble3x1:
1251 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
1252 break;
1253 case EHTokDouble3x2:
1254 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
1255 break;
1256 case EHTokDouble3x3:
1257 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1258 break;
1259 case EHTokDouble3x4:
1260 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
1261 break;
1262 case EHTokDouble4x1:
1263 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
1264 break;
1265 case EHTokDouble4x2:
1266 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
1267 break;
1268 case EHTokDouble4x3:
1269 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
1270 break;
1271 case EHTokDouble4x4:
1272 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1273 break;
1274
John Kessenich87142c72016-03-12 20:24:24 -07001275 default:
1276 return false;
1277 }
1278
1279 advanceToken();
1280
1281 return true;
1282}
1283
John Kesseniche6e74942016-06-11 16:43:14 -06001284// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001285// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1286// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1287//
1288// struct_type
1289// : STRUCT
1290// | CBUFFER
1291// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001292//
1293bool HlslGrammar::acceptStruct(TType& type)
1294{
John Kessenichb804de62016-09-05 12:19:18 -06001295 // This storage qualifier will tell us whether it's an AST
1296 // block type or just a generic structure type.
1297 TStorageQualifier storageQualifier = EvqTemporary;
John Kessenich3d157c52016-07-25 16:05:33 -06001298
1299 // CBUFFER
1300 if (acceptTokenClass(EHTokCBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001301 storageQualifier = EvqUniform;
John Kessenich3d157c52016-07-25 16:05:33 -06001302 // TBUFFER
1303 else if (acceptTokenClass(EHTokTBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001304 storageQualifier = EvqBuffer;
John Kesseniche6e74942016-06-11 16:43:14 -06001305 // STRUCT
John Kessenich3d157c52016-07-25 16:05:33 -06001306 else if (! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001307 return false;
1308
1309 // IDENTIFIER
1310 TString structName = "";
1311 if (peekTokenClass(EHTokIdentifier)) {
1312 structName = *token.string;
1313 advanceToken();
1314 }
1315
John Kessenich3d157c52016-07-25 16:05:33 -06001316 // post_decls
1317 acceptPostDecls(type);
1318
John Kesseniche6e74942016-06-11 16:43:14 -06001319 // LEFT_BRACE
1320 if (! acceptTokenClass(EHTokLeftBrace)) {
1321 expected("{");
1322 return false;
1323 }
1324
1325 // struct_declaration_list
1326 TTypeList* typeList;
1327 if (! acceptStructDeclarationList(typeList)) {
1328 expected("struct member declarations");
1329 return false;
1330 }
1331
1332 // RIGHT_BRACE
1333 if (! acceptTokenClass(EHTokRightBrace)) {
1334 expected("}");
1335 return false;
1336 }
1337
1338 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001339 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001340 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001341 else {
1342 TQualifier qualifier = type.getQualifier();
1343 qualifier.storage = storageQualifier;
John Kessenich3d157c52016-07-25 16:05:33 -06001344 new(&type) TType(typeList, structName, qualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001345 }
John Kesseniche6e74942016-06-11 16:43:14 -06001346
John Kessenich3d157c52016-07-25 16:05:33 -06001347 // If it was named, which means the type can be reused later, add
1348 // it to the symbol table. (Unless it's a block, in which
1349 // case the name is not a type.)
1350 if (type.getBasicType() != EbtBlock && structName.size() > 0) {
John Kesseniche6e74942016-06-11 16:43:14 -06001351 TVariable* userTypeDef = new TVariable(&structName, type, true);
1352 if (! parseContext.symbolTable.insert(*userTypeDef))
1353 parseContext.error(token.loc, "redefinition", structName.c_str(), "struct");
1354 }
1355
1356 return true;
1357}
1358
1359// struct_declaration_list
1360// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
1361//
1362// struct_declaration
1363// : fully_specified_type struct_declarator COMMA struct_declarator ...
1364//
1365// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06001366// : IDENTIFIER post_decls
1367// | IDENTIFIER array_specifier post_decls
John Kesseniche6e74942016-06-11 16:43:14 -06001368//
1369bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList)
1370{
1371 typeList = new TTypeList();
1372
1373 do {
1374 // success on seeing the RIGHT_BRACE coming up
1375 if (peekTokenClass(EHTokRightBrace))
1376 return true;
1377
1378 // struct_declaration
1379
1380 // fully_specified_type
1381 TType memberType;
1382 if (! acceptFullySpecifiedType(memberType)) {
1383 expected("member type");
1384 return false;
1385 }
1386
1387 // struct_declarator COMMA struct_declarator ...
1388 do {
1389 // peek IDENTIFIER
1390 if (! peekTokenClass(EHTokIdentifier)) {
1391 expected("member name");
1392 return false;
1393 }
1394
1395 // add it to the list of members
1396 TTypeLoc member = { new TType(EbtVoid), token.loc };
1397 member.type->shallowCopy(memberType);
1398 member.type->setFieldName(*token.string);
1399 typeList->push_back(member);
1400
1401 // accept IDENTIFIER
1402 advanceToken();
1403
1404 // array_specifier
John Kessenich19b92ff2016-06-19 11:50:34 -06001405 TArraySizes* arraySizes = nullptr;
1406 acceptArraySpecifier(arraySizes);
1407 if (arraySizes)
1408 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06001409
John Kessenich630dd7d2016-06-12 23:52:12 -06001410 acceptPostDecls(*member.type);
1411
John Kesseniche6e74942016-06-11 16:43:14 -06001412 // success on seeing the SEMICOLON coming up
1413 if (peekTokenClass(EHTokSemicolon))
1414 break;
1415
1416 // COMMA
1417 if (! acceptTokenClass(EHTokComma)) {
1418 expected(",");
1419 return false;
1420 }
1421
1422 } while (true);
1423
1424 // SEMI_COLON
1425 if (! acceptTokenClass(EHTokSemicolon)) {
1426 expected(";");
1427 return false;
1428 }
1429
1430 } while (true);
1431}
1432
John Kessenich5f934b02016-03-13 17:58:25 -06001433// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06001434// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06001435// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001436//
1437bool HlslGrammar::acceptFunctionParameters(TFunction& function)
1438{
John Kessenich078d7f22016-03-14 10:02:11 -06001439 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001440 if (! acceptTokenClass(EHTokLeftParen))
1441 return false;
1442
John Kessenich71351de2016-06-08 12:50:56 -06001443 // VOID RIGHT_PAREN
1444 if (! acceptTokenClass(EHTokVoid)) {
1445 do {
1446 // parameter_declaration
1447 if (! acceptParameterDeclaration(function))
1448 break;
John Kessenich5f934b02016-03-13 17:58:25 -06001449
John Kessenich71351de2016-06-08 12:50:56 -06001450 // COMMA
1451 if (! acceptTokenClass(EHTokComma))
1452 break;
1453 } while (true);
1454 }
John Kessenich5f934b02016-03-13 17:58:25 -06001455
John Kessenich078d7f22016-03-14 10:02:11 -06001456 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001457 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001458 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06001459 return false;
1460 }
1461
1462 return true;
1463}
1464
1465// parameter_declaration
John Kessenichc3387d32016-06-17 14:21:02 -06001466// : fully_specified_type post_decls
John Kessenich19b92ff2016-06-19 11:50:34 -06001467// | fully_specified_type identifier array_specifier post_decls
John Kessenich5f934b02016-03-13 17:58:25 -06001468//
1469bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
1470{
1471 // fully_specified_type
1472 TType* type = new TType;
1473 if (! acceptFullySpecifiedType(*type))
1474 return false;
1475
1476 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06001477 HlslToken idToken;
1478 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06001479
John Kessenich19b92ff2016-06-19 11:50:34 -06001480 // array_specifier
1481 TArraySizes* arraySizes = nullptr;
1482 acceptArraySpecifier(arraySizes);
1483 if (arraySizes)
1484 type->newArraySizes(*arraySizes);
1485
1486 // post_decls
John Kessenichc3387d32016-06-17 14:21:02 -06001487 acceptPostDecls(*type);
1488
John Kessenich5aa59e22016-06-17 15:50:47 -06001489 parseContext.paramFix(*type);
1490
John Kessenichaecd4972016-03-14 10:46:34 -06001491 TParameter param = { idToken.string, type };
John Kessenich5f934b02016-03-13 17:58:25 -06001492 function.addParameter(param);
1493
1494 return true;
1495}
1496
1497// Do the work to create the function definition in addition to
1498// parsing the body (compound_statement).
1499bool HlslGrammar::acceptFunctionDefinition(TFunction& function, TIntermNode*& node)
1500{
John Kessenicha3051662016-09-02 19:13:36 -06001501 TFunction& functionDeclarator = parseContext.handleFunctionDeclarator(token.loc, function, false /* not prototype */);
John Kessenich1a4b7752016-09-02 19:05:24 -06001502 TSourceLoc loc = token.loc;
John Kessenich5f934b02016-03-13 17:58:25 -06001503
John Kessenich077e0522016-06-09 02:02:17 -06001504 // This does a pushScope()
John Kessenicha3051662016-09-02 19:13:36 -06001505 node = parseContext.handleFunctionDefinition(loc, functionDeclarator);
John Kessenich5f934b02016-03-13 17:58:25 -06001506
1507 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06001508 TIntermNode* functionBody = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06001509 if (acceptCompoundStatement(functionBody)) {
John Kessenicha3051662016-09-02 19:13:36 -06001510 parseContext.handleFunctionBody(loc, functionDeclarator, functionBody, node);
John Kessenich5f934b02016-03-13 17:58:25 -06001511 return true;
1512 }
1513
1514 return false;
1515}
1516
John Kessenich0d2b6de2016-06-05 11:23:11 -06001517// Accept an expression with parenthesis around it, where
1518// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001519// syntactically required ones like in "if ( expression )".
1520//
1521// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06001522//
1523// Note this one is not set up to be speculative; as it gives
1524// errors if not found.
1525//
1526bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
1527{
1528 // LEFT_PAREN
1529 if (! acceptTokenClass(EHTokLeftParen))
1530 expected("(");
1531
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001532 bool decl = false;
1533 TIntermNode* declNode = nullptr;
1534 decl = acceptControlDeclaration(declNode);
1535 if (decl) {
1536 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
1537 expected("initialized declaration");
1538 return false;
1539 } else
1540 expression = declNode->getAsTyped();
1541 } else {
1542 // no declaration
1543 if (! acceptExpression(expression)) {
1544 expected("expression");
1545 return false;
1546 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06001547 }
1548
1549 // RIGHT_PAREN
1550 if (! acceptTokenClass(EHTokRightParen))
1551 expected(")");
1552
1553 return true;
1554}
1555
John Kessenich34fb0362016-05-03 23:17:20 -06001556// The top-level full expression recognizer.
1557//
John Kessenich87142c72016-03-12 20:24:24 -07001558// expression
John Kessenich34fb0362016-05-03 23:17:20 -06001559// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07001560//
1561bool HlslGrammar::acceptExpression(TIntermTyped*& node)
1562{
LoopDawgef764a22016-06-03 09:17:51 -06001563 node = nullptr;
1564
John Kessenich34fb0362016-05-03 23:17:20 -06001565 // assignment_expression
1566 if (! acceptAssignmentExpression(node))
1567 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001568
John Kessenich34fb0362016-05-03 23:17:20 -06001569 if (! peekTokenClass(EHTokComma))
1570 return true;
1571
1572 do {
1573 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06001574 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06001575 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06001576
John Kessenich34fb0362016-05-03 23:17:20 -06001577 // ... assignment_expression
1578 TIntermTyped* rightNode = nullptr;
1579 if (! acceptAssignmentExpression(rightNode)) {
1580 expected("assignment expression");
1581 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001582 }
1583
John Kessenich34fb0362016-05-03 23:17:20 -06001584 node = intermediate.addComma(node, rightNode, loc);
1585
1586 if (! peekTokenClass(EHTokComma))
1587 return true;
1588 } while (true);
1589}
1590
John Kessenich07354242016-07-01 19:58:06 -06001591// initializer
1592// : LEFT_BRACE initializer_list RIGHT_BRACE
1593//
1594// initializer_list
1595// : assignment_expression COMMA assignment_expression COMMA ...
1596//
1597bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
1598{
1599 // LEFT_BRACE
1600 if (! acceptTokenClass(EHTokLeftBrace))
1601 return false;
1602
1603 // initializer_list
1604 TSourceLoc loc = token.loc;
1605 node = nullptr;
1606 do {
1607 // assignment_expression
1608 TIntermTyped* expr;
1609 if (! acceptAssignmentExpression(expr)) {
1610 expected("assignment expression in initializer list");
1611 return false;
1612 }
1613 node = intermediate.growAggregate(node, expr, loc);
1614
1615 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001616 if (acceptTokenClass(EHTokComma)) {
1617 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
1618 return true;
John Kessenich07354242016-07-01 19:58:06 -06001619 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001620 }
John Kessenich07354242016-07-01 19:58:06 -06001621
1622 // RIGHT_BRACE
1623 if (acceptTokenClass(EHTokRightBrace))
1624 return true;
1625
1626 expected(", or }");
1627 return false;
1628 } while (true);
1629}
1630
John Kessenich34fb0362016-05-03 23:17:20 -06001631// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06001632// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06001633//
1634// a op (b op (c op d))
1635//
1636// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06001637// : initializer
1638// | conditional_expression
1639// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06001640//
1641bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
1642{
John Kessenich07354242016-07-01 19:58:06 -06001643 // initializer
1644 if (peekTokenClass(EHTokLeftBrace)) {
1645 if (acceptInitializer(node))
1646 return true;
1647
1648 expected("initializer");
1649 return false;
1650 }
1651
John Kessenich00957f82016-07-27 10:39:57 -06001652 // conditional_expression
1653 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06001654 return false;
1655
John Kessenich07354242016-07-01 19:58:06 -06001656 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06001657 TOperator assignOp = HlslOpMap::assignment(peek());
1658 if (assignOp == EOpNull)
1659 return true;
1660
John Kessenich00957f82016-07-27 10:39:57 -06001661 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06001662 TSourceLoc loc = token.loc;
1663 advanceToken();
1664
John Kessenich00957f82016-07-27 10:39:57 -06001665 // conditional_expression assign_op conditional_expression ...
1666 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06001667 // gets the right-to-left associativity.
1668 TIntermTyped* rightNode = nullptr;
1669 if (! acceptAssignmentExpression(rightNode)) {
1670 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06001671 return false;
John Kessenich87142c72016-03-12 20:24:24 -07001672 }
1673
John Kessenich34fb0362016-05-03 23:17:20 -06001674 node = intermediate.addAssign(assignOp, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06001675 if (node == nullptr) {
1676 parseContext.error(loc, "could not create assignment", "", "");
1677 return false;
1678 }
John Kessenich34fb0362016-05-03 23:17:20 -06001679
1680 if (! peekTokenClass(EHTokComma))
1681 return true;
1682
1683 return true;
1684}
1685
John Kessenich00957f82016-07-27 10:39:57 -06001686// Accept a conditional expression, which associates right-to-left,
1687// accomplished by the "true" expression calling down to lower
1688// precedence levels than this level.
1689//
1690// conditional_expression
1691// : binary_expression
1692// | binary_expression QUESTION expression COLON assignment_expression
1693//
1694bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
1695{
1696 // binary_expression
1697 if (! acceptBinaryExpression(node, PlLogicalOr))
1698 return false;
1699
1700 if (! acceptTokenClass(EHTokQuestion))
1701 return true;
1702
1703 TIntermTyped* trueNode = nullptr;
1704 if (! acceptExpression(trueNode)) {
1705 expected("expression after ?");
1706 return false;
1707 }
1708 TSourceLoc loc = token.loc;
1709
1710 if (! acceptTokenClass(EHTokColon)) {
1711 expected(":");
1712 return false;
1713 }
1714
1715 TIntermTyped* falseNode = nullptr;
1716 if (! acceptAssignmentExpression(falseNode)) {
1717 expected("expression after :");
1718 return false;
1719 }
1720
1721 node = intermediate.addSelection(node, trueNode, falseNode, loc);
1722
1723 return true;
1724}
1725
John Kessenich34fb0362016-05-03 23:17:20 -06001726// Accept a binary expression, for binary operations that
1727// associate left-to-right. This is, it is implicit, for example
1728//
1729// ((a op b) op c) op d
1730//
1731// binary_expression
1732// : expression op expression op expression ...
1733//
1734// where 'expression' is the next higher level in precedence.
1735//
1736bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
1737{
1738 if (precedenceLevel > PlMul)
1739 return acceptUnaryExpression(node);
1740
1741 // assignment_expression
1742 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
1743 return false;
1744
John Kessenich34fb0362016-05-03 23:17:20 -06001745 do {
John Kessenich64076ed2016-07-28 21:43:17 -06001746 TOperator op = HlslOpMap::binary(peek());
1747 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
1748 if (tokenLevel < precedenceLevel)
1749 return true;
1750
John Kessenich34fb0362016-05-03 23:17:20 -06001751 // ... op
1752 TSourceLoc loc = token.loc;
1753 advanceToken();
1754
1755 // ... expression
1756 TIntermTyped* rightNode = nullptr;
1757 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
1758 expected("expression");
1759 return false;
1760 }
1761
1762 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06001763 if (node == nullptr) {
1764 parseContext.error(loc, "Could not perform requested binary operation", "", "");
1765 return false;
1766 }
John Kessenich34fb0362016-05-03 23:17:20 -06001767 } while (true);
1768}
1769
1770// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06001771// : (type) unary_expression
1772// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06001773// | - unary_expression
1774// | ! unary_expression
1775// | ~ unary_expression
1776// | ++ unary_expression
1777// | -- unary_expression
1778// | postfix_expression
1779//
1780bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
1781{
John Kessenich1cc1a282016-06-03 16:55:49 -06001782 // (type) unary_expression
1783 // Have to look two steps ahead, because this could be, e.g., a
1784 // postfix_expression instead, since that also starts with at "(".
1785 if (acceptTokenClass(EHTokLeftParen)) {
1786 TType castType;
1787 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06001788 if (acceptTokenClass(EHTokRightParen)) {
1789 // We've matched "(type)" now, get the expression to cast
1790 TSourceLoc loc = token.loc;
1791 if (! acceptUnaryExpression(node))
1792 return false;
1793
1794 // Hook it up like a constructor
1795 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
1796 if (constructorFunction == nullptr) {
1797 expected("type that can be constructed");
1798 return false;
1799 }
1800 TIntermTyped* arguments = nullptr;
1801 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
1802 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
1803
1804 return true;
1805 } else {
1806 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
1807 // the '(int' part. We must back up twice.
1808 recedeToken();
1809 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06001810 }
John Kessenich1cc1a282016-06-03 16:55:49 -06001811 } else {
1812 // This isn't a type cast, but it still started "(", so if it is a
1813 // unary expression, it can only be a postfix_expression, so try that.
1814 // Back it up first.
1815 recedeToken();
1816 return acceptPostfixExpression(node);
1817 }
1818 }
1819
1820 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06001821 TOperator unaryOp = HlslOpMap::preUnary(peek());
1822
John Kessenich1cc1a282016-06-03 16:55:49 -06001823 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06001824 if (unaryOp == EOpNull)
1825 return acceptPostfixExpression(node);
1826
1827 // op unary_expression
1828 TSourceLoc loc = token.loc;
1829 advanceToken();
1830 if (! acceptUnaryExpression(node))
1831 return false;
1832
1833 // + is a no-op
1834 if (unaryOp == EOpAdd)
1835 return true;
1836
1837 node = intermediate.addUnaryMath(unaryOp, node, loc);
1838
1839 return node != nullptr;
1840}
1841
1842// postfix_expression
1843// : LEFT_PAREN expression RIGHT_PAREN
1844// | literal
1845// | constructor
1846// | identifier
1847// | function_call
1848// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
1849// | postfix_expression DOT IDENTIFIER
1850// | postfix_expression INC_OP
1851// | postfix_expression DEC_OP
1852//
1853bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
1854{
1855 // Not implemented as self-recursive:
1856 // The logical "right recursion" is done with an loop at the end
1857
1858 // idToken will pick up either a variable or a function name in a function call
1859 HlslToken idToken;
1860
John Kessenich21472ae2016-06-04 11:46:33 -06001861 // Find something before the postfix operations, as they can't operate
1862 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07001863 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06001864 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07001865 if (! acceptExpression(node)) {
1866 expected("expression");
1867 return false;
1868 }
1869 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001870 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07001871 return false;
1872 }
John Kessenich34fb0362016-05-03 23:17:20 -06001873 } else if (acceptLiteral(node)) {
1874 // literal (nothing else to do yet), go on to the
1875 } else if (acceptConstructor(node)) {
1876 // constructor (nothing else to do yet)
1877 } else if (acceptIdentifier(idToken)) {
1878 // identifier or function_call name
1879 if (! peekTokenClass(EHTokLeftParen)) {
John Kesseniche6e74942016-06-11 16:43:14 -06001880 node = parseContext.handleVariable(idToken.loc, idToken.symbol, token.string);
John Kessenich34fb0362016-05-03 23:17:20 -06001881 } else if (acceptFunctionCall(idToken, node)) {
1882 // function_call (nothing else to do yet)
1883 } else {
1884 expected("function call arguments");
1885 return false;
1886 }
John Kessenich21472ae2016-06-04 11:46:33 -06001887 } else {
1888 // nothing found, can't post operate
1889 return false;
John Kessenich87142c72016-03-12 20:24:24 -07001890 }
1891
John Kessenich21472ae2016-06-04 11:46:33 -06001892 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06001893 do {
1894 TSourceLoc loc = token.loc;
1895 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07001896
John Kessenich34fb0362016-05-03 23:17:20 -06001897 // Consume only a valid post-unary operator, otherwise we are done.
1898 switch (postOp) {
1899 case EOpIndexDirectStruct:
1900 case EOpIndexIndirect:
1901 case EOpPostIncrement:
1902 case EOpPostDecrement:
1903 advanceToken();
1904 break;
1905 default:
1906 return true;
1907 }
John Kessenich87142c72016-03-12 20:24:24 -07001908
John Kessenich34fb0362016-05-03 23:17:20 -06001909 // We have a valid post-unary operator, process it.
1910 switch (postOp) {
1911 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06001912 {
John Kessenich19b92ff2016-06-19 11:50:34 -06001913 // DOT IDENTIFIER
1914 // includes swizzles and struct members
John Kessenich93a162a2016-06-17 17:16:27 -06001915 HlslToken field;
1916 if (! acceptIdentifier(field)) {
1917 expected("swizzle or member");
1918 return false;
1919 }
LoopDawg4886f692016-06-29 10:58:58 -06001920
1921 TIntermTyped* base = node; // preserve for method function calls
John Kessenich93a162a2016-06-17 17:16:27 -06001922 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06001923
1924 // In the event of a method node, we look for an open paren and accept the function call.
1925 if (node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) {
1926 if (! acceptFunctionCall(field, node, base)) {
1927 expected("function parameters");
1928 return false;
1929 }
1930 }
1931
John Kessenich34fb0362016-05-03 23:17:20 -06001932 break;
John Kessenich93a162a2016-06-17 17:16:27 -06001933 }
John Kessenich34fb0362016-05-03 23:17:20 -06001934 case EOpIndexIndirect:
1935 {
John Kessenich19b92ff2016-06-19 11:50:34 -06001936 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06001937 TIntermTyped* indexNode = nullptr;
1938 if (! acceptExpression(indexNode) ||
1939 ! peekTokenClass(EHTokRightBracket)) {
1940 expected("expression followed by ']'");
1941 return false;
1942 }
John Kessenich19b92ff2016-06-19 11:50:34 -06001943 advanceToken();
1944 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
1945 break;
John Kessenich34fb0362016-05-03 23:17:20 -06001946 }
1947 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06001948 // INC_OP
1949 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06001950 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06001951 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06001952 node = intermediate.addUnaryMath(postOp, node, loc);
1953 break;
1954 default:
1955 assert(0);
1956 break;
1957 }
1958 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07001959}
1960
John Kessenichd016be12016-03-13 11:24:20 -06001961// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06001962// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06001963//
1964bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
1965{
1966 // type
1967 TType type;
1968 if (acceptType(type)) {
1969 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
1970 if (constructorFunction == nullptr)
1971 return false;
1972
1973 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06001974 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06001975 if (! acceptArguments(constructorFunction, arguments)) {
1976 expected("constructor arguments");
1977 return false;
1978 }
1979
1980 // hook it up
1981 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
1982
1983 return true;
1984 }
1985
1986 return false;
1987}
1988
John Kessenich34fb0362016-05-03 23:17:20 -06001989// The function_call identifier was already recognized, and passed in as idToken.
1990//
1991// function_call
1992// : [idToken] arguments
1993//
LoopDawg4886f692016-06-29 10:58:58 -06001994bool HlslGrammar::acceptFunctionCall(HlslToken idToken, TIntermTyped*& node, TIntermTyped* base)
John Kessenich34fb0362016-05-03 23:17:20 -06001995{
John Kessenich4678ca92016-05-13 09:33:42 -06001996 // arguments
1997 TFunction* function = new TFunction(idToken.string, TType(EbtVoid));
1998 TIntermTyped* arguments = nullptr;
LoopDawg4886f692016-06-29 10:58:58 -06001999
2000 // methods have an implicit first argument of the calling object.
2001 if (base != nullptr)
2002 parseContext.handleFunctionArgument(function, arguments, base);
2003
John Kessenich4678ca92016-05-13 09:33:42 -06002004 if (! acceptArguments(function, arguments))
2005 return false;
2006
2007 node = parseContext.handleFunctionCall(idToken.loc, function, arguments);
2008
2009 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002010}
2011
John Kessenich87142c72016-03-12 20:24:24 -07002012// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002013// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002014//
John Kessenichd016be12016-03-13 11:24:20 -06002015// The arguments are pushed onto the 'function' argument list and
2016// onto the 'arguments' aggregate.
2017//
John Kessenich4678ca92016-05-13 09:33:42 -06002018bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002019{
John Kessenich078d7f22016-03-14 10:02:11 -06002020 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002021 if (! acceptTokenClass(EHTokLeftParen))
2022 return false;
2023
2024 do {
John Kessenichd016be12016-03-13 11:24:20 -06002025 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002026 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002027 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002028 break;
John Kessenichd016be12016-03-13 11:24:20 -06002029
2030 // hook it up
2031 parseContext.handleFunctionArgument(function, arguments, arg);
2032
John Kessenich078d7f22016-03-14 10:02:11 -06002033 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002034 if (! acceptTokenClass(EHTokComma))
2035 break;
2036 } while (true);
2037
John Kessenich078d7f22016-03-14 10:02:11 -06002038 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002039 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002040 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002041 return false;
2042 }
2043
2044 return true;
2045}
2046
2047bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2048{
2049 switch (token.tokenClass) {
2050 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002051 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002052 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002053 case EHTokUintConstant:
2054 node = intermediate.addConstantUnion(token.u, token.loc, true);
2055 break;
John Kessenich87142c72016-03-12 20:24:24 -07002056 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002057 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002058 break;
2059 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002060 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002061 break;
2062 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002063 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002064 break;
2065
2066 default:
2067 return false;
2068 }
2069
2070 advanceToken();
2071
2072 return true;
2073}
2074
John Kessenich5f934b02016-03-13 17:58:25 -06002075// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002076// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002077//
John Kessenich21472ae2016-06-04 11:46:33 -06002078bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002079{
John Kessenich21472ae2016-06-04 11:46:33 -06002080 TIntermAggregate* compoundStatement = nullptr;
2081
John Kessenich34fb0362016-05-03 23:17:20 -06002082 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002083 if (! acceptTokenClass(EHTokLeftBrace))
2084 return false;
2085
2086 // statement statement ...
2087 TIntermNode* statement = nullptr;
2088 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002089 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2090 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2091 branch->getFlowOp() == EOpDefault)) {
2092 // hook up individual subsequences within a switch statement
2093 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2094 compoundStatement = nullptr;
2095 } else {
2096 // hook it up to the growing compound statement
2097 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2098 }
John Kessenich5f934b02016-03-13 17:58:25 -06002099 }
John Kessenich34fb0362016-05-03 23:17:20 -06002100 if (compoundStatement)
2101 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06002102
John Kessenich21472ae2016-06-04 11:46:33 -06002103 retStatement = compoundStatement;
2104
John Kessenich34fb0362016-05-03 23:17:20 -06002105 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002106 return acceptTokenClass(EHTokRightBrace);
2107}
2108
John Kessenich0d2b6de2016-06-05 11:23:11 -06002109bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
2110{
2111 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06002112 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06002113 parseContext.popScope();
2114
2115 return result;
2116}
2117
John Kessenich077e0522016-06-09 02:02:17 -06002118bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06002119{
John Kessenich077e0522016-06-09 02:02:17 -06002120 parseContext.pushScope();
2121 bool result = acceptCompoundStatement(statement);
2122 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06002123
2124 return result;
2125}
2126
John Kessenich5f934b02016-03-13 17:58:25 -06002127// statement
John Kessenich21472ae2016-06-04 11:46:33 -06002128// : attributes attributed_statement
2129//
2130// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002131// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002132// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06002133// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06002134// | declaration_statement
2135// | selection_statement
2136// | switch_statement
2137// | case_label
2138// | iteration_statement
2139// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002140//
2141bool HlslGrammar::acceptStatement(TIntermNode*& statement)
2142{
John Kessenich21472ae2016-06-04 11:46:33 -06002143 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06002144
John Kessenich21472ae2016-06-04 11:46:33 -06002145 // attributes
2146 acceptAttributes();
John Kessenich5f934b02016-03-13 17:58:25 -06002147
John Kessenich21472ae2016-06-04 11:46:33 -06002148 // attributed_statement
2149 switch (peek()) {
2150 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06002151 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002152
John Kessenich21472ae2016-06-04 11:46:33 -06002153 case EHTokIf:
2154 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002155
John Kessenich21472ae2016-06-04 11:46:33 -06002156 case EHTokSwitch:
2157 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002158
John Kessenich21472ae2016-06-04 11:46:33 -06002159 case EHTokFor:
2160 case EHTokDo:
2161 case EHTokWhile:
2162 return acceptIterationStatement(statement);
2163
2164 case EHTokContinue:
2165 case EHTokBreak:
2166 case EHTokDiscard:
2167 case EHTokReturn:
2168 return acceptJumpStatement(statement);
2169
2170 case EHTokCase:
2171 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06002172 case EHTokDefault:
2173 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06002174
2175 case EHTokSemicolon:
2176 return acceptTokenClass(EHTokSemicolon);
2177
2178 case EHTokRightBrace:
2179 // Performance: not strictly necessary, but stops a bunch of hunting early,
2180 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06002181 return false;
2182
John Kessenich21472ae2016-06-04 11:46:33 -06002183 default:
2184 {
2185 // declaration
2186 if (acceptDeclaration(statement))
2187 return true;
2188
2189 // expression
2190 TIntermTyped* node;
2191 if (acceptExpression(node))
2192 statement = node;
2193 else
2194 return false;
2195
2196 // SEMICOLON (following an expression)
2197 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002198 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06002199 return false;
2200 }
2201 }
2202 }
2203
John Kessenich5f934b02016-03-13 17:58:25 -06002204 return true;
John Kessenich87142c72016-03-12 20:24:24 -07002205}
2206
John Kessenich21472ae2016-06-04 11:46:33 -06002207// attributes
2208// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
2209//
2210// attribute:
2211// : UNROLL
2212// | UNROLL LEFT_PAREN literal RIGHT_PAREN
2213// | FASTOPT
2214// | ALLOW_UAV_CONDITION
2215// | BRANCH
2216// | FLATTEN
2217// | FORCECASE
2218// | CALL
2219//
2220void HlslGrammar::acceptAttributes()
2221{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002222 // For now, accept the [ XXX(X) ] syntax, but drop.
2223 // TODO: subset to correct set? Pass on?
2224 do {
2225 // LEFT_BRACKET?
2226 if (! acceptTokenClass(EHTokLeftBracket))
2227 return;
2228
2229 // attribute
2230 if (peekTokenClass(EHTokIdentifier)) {
2231 // 'token.string' is the attribute
2232 advanceToken();
2233 } else if (! peekTokenClass(EHTokRightBracket)) {
2234 expected("identifier");
2235 advanceToken();
2236 }
2237
2238 // (x)
2239 if (acceptTokenClass(EHTokLeftParen)) {
2240 TIntermTyped* node;
2241 if (! acceptLiteral(node))
2242 expected("literal");
2243 // 'node' has the literal in it
2244 if (! acceptTokenClass(EHTokRightParen))
2245 expected(")");
2246 }
2247
2248 // RIGHT_BRACKET
2249 if (acceptTokenClass(EHTokRightBracket))
2250 continue;
2251
2252 expected("]");
2253 return;
2254
2255 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06002256}
2257
John Kessenich0d2b6de2016-06-05 11:23:11 -06002258// selection_statement
2259// : IF LEFT_PAREN expression RIGHT_PAREN statement
2260// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
2261//
John Kessenich21472ae2016-06-04 11:46:33 -06002262bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
2263{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002264 TSourceLoc loc = token.loc;
2265
2266 // IF
2267 if (! acceptTokenClass(EHTokIf))
2268 return false;
2269
2270 // so that something declared in the condition is scoped to the lifetimes
2271 // of the then-else statements
2272 parseContext.pushScope();
2273
2274 // LEFT_PAREN expression RIGHT_PAREN
2275 TIntermTyped* condition;
2276 if (! acceptParenExpression(condition))
2277 return false;
2278
2279 // create the child statements
2280 TIntermNodePair thenElse = { nullptr, nullptr };
2281
2282 // then statement
2283 if (! acceptScopedStatement(thenElse.node1)) {
2284 expected("then statement");
2285 return false;
2286 }
2287
2288 // ELSE
2289 if (acceptTokenClass(EHTokElse)) {
2290 // else statement
2291 if (! acceptScopedStatement(thenElse.node2)) {
2292 expected("else statement");
2293 return false;
2294 }
2295 }
2296
2297 // Put the pieces together
2298 statement = intermediate.addSelection(condition, thenElse, loc);
2299 parseContext.popScope();
2300
2301 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002302}
2303
John Kessenichd02dc5d2016-07-01 00:04:11 -06002304// switch_statement
2305// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
2306//
John Kessenich21472ae2016-06-04 11:46:33 -06002307bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
2308{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002309 // SWITCH
2310 TSourceLoc loc = token.loc;
2311 if (! acceptTokenClass(EHTokSwitch))
2312 return false;
2313
2314 // LEFT_PAREN expression RIGHT_PAREN
2315 parseContext.pushScope();
2316 TIntermTyped* switchExpression;
2317 if (! acceptParenExpression(switchExpression)) {
2318 parseContext.popScope();
2319 return false;
2320 }
2321
2322 // compound_statement
2323 parseContext.pushSwitchSequence(new TIntermSequence);
2324 bool statementOkay = acceptCompoundStatement(statement);
2325 if (statementOkay)
2326 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
2327
2328 parseContext.popSwitchSequence();
2329 parseContext.popScope();
2330
2331 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06002332}
2333
John Kessenich119f8f62016-06-05 15:44:07 -06002334// iteration_statement
2335// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
2336// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
2337// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
2338//
2339// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06002340bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
2341{
John Kessenich119f8f62016-06-05 15:44:07 -06002342 TSourceLoc loc = token.loc;
2343 TIntermTyped* condition = nullptr;
2344
2345 EHlslTokenClass loop = peek();
2346 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
2347
2348 // WHILE or DO or FOR
2349 advanceToken();
2350
2351 switch (loop) {
2352 case EHTokWhile:
2353 // so that something declared in the condition is scoped to the lifetime
2354 // of the while sub-statement
2355 parseContext.pushScope();
2356 parseContext.nestLooping();
2357
2358 // LEFT_PAREN condition RIGHT_PAREN
2359 if (! acceptParenExpression(condition))
2360 return false;
2361
2362 // statement
2363 if (! acceptScopedStatement(statement)) {
2364 expected("while sub-statement");
2365 return false;
2366 }
2367
2368 parseContext.unnestLooping();
2369 parseContext.popScope();
2370
2371 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
2372
2373 return true;
2374
2375 case EHTokDo:
2376 parseContext.nestLooping();
2377
2378 if (! acceptTokenClass(EHTokLeftBrace))
2379 expected("{");
2380
2381 // statement
2382 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
2383 expected("do sub-statement");
2384 return false;
2385 }
2386
2387 if (! acceptTokenClass(EHTokRightBrace))
2388 expected("}");
2389
2390 // WHILE
2391 if (! acceptTokenClass(EHTokWhile)) {
2392 expected("while");
2393 return false;
2394 }
2395
2396 // LEFT_PAREN condition RIGHT_PAREN
2397 TIntermTyped* condition;
2398 if (! acceptParenExpression(condition))
2399 return false;
2400
2401 if (! acceptTokenClass(EHTokSemicolon))
2402 expected(";");
2403
2404 parseContext.unnestLooping();
2405
2406 statement = intermediate.addLoop(statement, condition, 0, false, loc);
2407
2408 return true;
2409
2410 case EHTokFor:
2411 {
2412 // LEFT_PAREN
2413 if (! acceptTokenClass(EHTokLeftParen))
2414 expected("(");
2415
2416 // so that something declared in the condition is scoped to the lifetime
2417 // of the for sub-statement
2418 parseContext.pushScope();
2419
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002420 // initializer
2421 TIntermNode* initNode = nullptr;
2422 if (! acceptControlDeclaration(initNode)) {
2423 TIntermTyped* initExpr = nullptr;
2424 acceptExpression(initExpr);
2425 initNode = initExpr;
2426 }
2427 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06002428 if (! acceptTokenClass(EHTokSemicolon))
2429 expected(";");
2430
2431 parseContext.nestLooping();
2432
2433 // condition SEMI_COLON
2434 acceptExpression(condition);
2435 if (! acceptTokenClass(EHTokSemicolon))
2436 expected(";");
2437
2438 // iterator SEMI_COLON
2439 TIntermTyped* iterator = nullptr;
2440 acceptExpression(iterator);
2441 if (! acceptTokenClass(EHTokRightParen))
2442 expected(")");
2443
2444 // statement
2445 if (! acceptScopedStatement(statement)) {
2446 expected("for sub-statement");
2447 return false;
2448 }
2449
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002450 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06002451
2452 parseContext.popScope();
2453 parseContext.unnestLooping();
2454
2455 return true;
2456 }
2457
2458 default:
2459 return false;
2460 }
John Kessenich21472ae2016-06-04 11:46:33 -06002461}
2462
2463// jump_statement
2464// : CONTINUE SEMICOLON
2465// | BREAK SEMICOLON
2466// | DISCARD SEMICOLON
2467// | RETURN SEMICOLON
2468// | RETURN expression SEMICOLON
2469//
2470bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
2471{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002472 EHlslTokenClass jump = peek();
2473 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06002474 case EHTokContinue:
2475 case EHTokBreak:
2476 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06002477 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002478 advanceToken();
2479 break;
John Kessenich21472ae2016-06-04 11:46:33 -06002480 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002481 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06002482 return false;
2483 }
John Kessenich21472ae2016-06-04 11:46:33 -06002484
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002485 switch (jump) {
2486 case EHTokContinue:
2487 statement = intermediate.addBranch(EOpContinue, token.loc);
2488 break;
2489 case EHTokBreak:
2490 statement = intermediate.addBranch(EOpBreak, token.loc);
2491 break;
2492 case EHTokDiscard:
2493 statement = intermediate.addBranch(EOpKill, token.loc);
2494 break;
2495
2496 case EHTokReturn:
2497 {
2498 // expression
2499 TIntermTyped* node;
2500 if (acceptExpression(node)) {
2501 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06002502 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002503 } else
2504 statement = intermediate.addBranch(EOpReturn, token.loc);
2505 break;
2506 }
2507
2508 default:
2509 assert(0);
2510 return false;
2511 }
2512
2513 // SEMICOLON
2514 if (! acceptTokenClass(EHTokSemicolon))
2515 expected(";");
2516
2517 return true;
2518}
John Kessenich21472ae2016-06-04 11:46:33 -06002519
John Kessenichd02dc5d2016-07-01 00:04:11 -06002520// case_label
2521// : CASE expression COLON
2522//
John Kessenich21472ae2016-06-04 11:46:33 -06002523bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
2524{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002525 TSourceLoc loc = token.loc;
2526 if (! acceptTokenClass(EHTokCase))
2527 return false;
2528
2529 TIntermTyped* expression;
2530 if (! acceptExpression(expression)) {
2531 expected("case expression");
2532 return false;
2533 }
2534
2535 if (! acceptTokenClass(EHTokColon)) {
2536 expected(":");
2537 return false;
2538 }
2539
2540 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
2541
2542 return true;
2543}
2544
2545// default_label
2546// : DEFAULT COLON
2547//
2548bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
2549{
2550 TSourceLoc loc = token.loc;
2551 if (! acceptTokenClass(EHTokDefault))
2552 return false;
2553
2554 if (! acceptTokenClass(EHTokColon)) {
2555 expected(":");
2556 return false;
2557 }
2558
2559 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
2560
2561 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002562}
2563
John Kessenich19b92ff2016-06-19 11:50:34 -06002564// array_specifier
2565// : LEFT_BRACKET integer_expression RGHT_BRACKET post_decls // optional
2566//
2567void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
2568{
2569 arraySizes = nullptr;
2570
2571 if (! acceptTokenClass(EHTokLeftBracket))
2572 return;
2573
2574 TSourceLoc loc = token.loc;
2575 TIntermTyped* sizeExpr;
2576 if (! acceptAssignmentExpression(sizeExpr)) {
2577 expected("array-sizing expression");
2578 return;
2579 }
2580
2581 if (! acceptTokenClass(EHTokRightBracket)) {
2582 expected("]");
2583 return;
2584 }
2585
2586 TArraySize arraySize;
2587 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
2588 arraySizes = new TArraySizes;
2589 arraySizes->addInnerSize(arraySize);
2590}
2591
John Kessenich630dd7d2016-06-12 23:52:12 -06002592// post_decls
John Kessenichb38f0712016-07-30 10:29:54 -06002593// : COLON semantic // optional
2594// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
2595// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt RIGHT_PAREN // optional
2596// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06002597//
2598void HlslGrammar::acceptPostDecls(TType& type)
John Kessenich078d7f22016-03-14 10:02:11 -06002599{
John Kessenich630dd7d2016-06-12 23:52:12 -06002600 do {
2601 // COLON
2602 if (acceptTokenClass(EHTokColon)) {
2603 HlslToken idToken;
2604 if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06002605 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002606 if (! acceptTokenClass(EHTokLeftParen)) {
2607 expected("(");
2608 return;
2609 }
John Kessenich82d6baf2016-07-29 13:03:05 -06002610 HlslToken locationToken;
2611 if (! acceptIdentifier(locationToken)) {
2612 expected("c[subcomponent][.component]");
2613 return;
2614 }
2615 HlslToken componentToken;
2616 if (acceptTokenClass(EHTokDot)) {
2617 if (! acceptIdentifier(componentToken)) {
2618 expected("component");
2619 return;
2620 }
2621 }
John Kessenich630dd7d2016-06-12 23:52:12 -06002622 if (! acceptTokenClass(EHTokRightParen)) {
2623 expected(")");
2624 break;
2625 }
John Kessenich82d6baf2016-07-29 13:03:05 -06002626 parseContext.handlePackOffset(locationToken.loc, type, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002627 } else if (! acceptIdentifier(idToken)) {
2628 expected("semantic or packoffset or register");
2629 return;
2630 } else if (*idToken.string == "register") {
John Kessenichb38f0712016-07-30 10:29:54 -06002631 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002632 if (! acceptTokenClass(EHTokLeftParen)) {
2633 expected("(");
2634 return;
2635 }
John Kessenichb38f0712016-07-30 10:29:54 -06002636 HlslToken registerDesc; // for Type#
2637 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06002638 if (! acceptIdentifier(registerDesc)) {
2639 expected("register number description");
2640 return;
2641 }
John Kessenich96e9f472016-07-29 14:28:39 -06002642 if (acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06002643 // Then we didn't really see the registerDesc yet, it was
2644 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06002645 profile = registerDesc;
2646 if (! acceptIdentifier(registerDesc)) {
2647 expected("register number description");
2648 return;
2649 }
2650 }
John Kessenichb38f0712016-07-30 10:29:54 -06002651 int subComponent = 0;
2652 if (acceptTokenClass(EHTokLeftBracket)) {
2653 // LEFT_BRACKET subcomponent RIGHT_BRACKET
2654 if (! peekTokenClass(EHTokIntConstant)) {
2655 expected("literal integer");
2656 return;
2657 }
2658 subComponent = token.i;
2659 advanceToken();
2660 if (! acceptTokenClass(EHTokRightBracket)) {
2661 expected("]");
2662 break;
2663 }
2664 }
John Kessenich630dd7d2016-06-12 23:52:12 -06002665 if (! acceptTokenClass(EHTokRightParen)) {
2666 expected(")");
2667 break;
2668 }
John Kessenichb38f0712016-07-30 10:29:54 -06002669 parseContext.handleRegister(registerDesc.loc, type, profile.string, *registerDesc.string, subComponent);
John Kessenich630dd7d2016-06-12 23:52:12 -06002670 } else {
2671 // semantic, in idToken.string
John Kessenich81d47142016-08-29 16:07:29 -06002672 parseContext.handleSemantic(idToken.loc, type, *idToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002673 }
2674 } else if (acceptTokenClass(EHTokLeftAngle)) {
2675 // TODO: process annotations, just accepting them for now
2676 do {
2677 if (peekTokenClass(EHTokNone))
2678 return;
2679 if (acceptTokenClass(EHTokRightAngle))
2680 break;
2681 advanceToken();
2682 } while (true);
2683 } else
2684 break;
John Kessenich078d7f22016-03-14 10:02:11 -06002685
John Kessenich630dd7d2016-06-12 23:52:12 -06002686 } while (true);
John Kessenich078d7f22016-03-14 10:02:11 -06002687}
2688
John Kesseniche01a9bc2016-03-12 20:11:22 -07002689} // end namespace glslang