blob: a4fe38ecd12f7bacc87a895e60cb4cdb3e42fe21 [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 Kessenich3d157c52016-07-25 16:05:33 -06001295 // This qualifier.storage will tell us whether it's an AST block or
1296 // just a struct.
1297 TQualifier qualifier;
1298 qualifier.clear();
1299
1300 // CBUFFER
1301 if (acceptTokenClass(EHTokCBuffer))
1302 qualifier.storage = EvqUniform;
1303 // TBUFFER
1304 else if (acceptTokenClass(EHTokTBuffer))
1305 qualifier.storage = EvqBuffer;
John Kesseniche6e74942016-06-11 16:43:14 -06001306 // STRUCT
John Kessenich3d157c52016-07-25 16:05:33 -06001307 else if (! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001308 return false;
1309
1310 // IDENTIFIER
1311 TString structName = "";
1312 if (peekTokenClass(EHTokIdentifier)) {
1313 structName = *token.string;
1314 advanceToken();
1315 }
1316
John Kessenich3d157c52016-07-25 16:05:33 -06001317 // post_decls
1318 acceptPostDecls(type);
1319
John Kesseniche6e74942016-06-11 16:43:14 -06001320 // LEFT_BRACE
1321 if (! acceptTokenClass(EHTokLeftBrace)) {
1322 expected("{");
1323 return false;
1324 }
1325
1326 // struct_declaration_list
1327 TTypeList* typeList;
1328 if (! acceptStructDeclarationList(typeList)) {
1329 expected("struct member declarations");
1330 return false;
1331 }
1332
1333 // RIGHT_BRACE
1334 if (! acceptTokenClass(EHTokRightBrace)) {
1335 expected("}");
1336 return false;
1337 }
1338
1339 // create the user-defined type
John Kessenich3d157c52016-07-25 16:05:33 -06001340 if (qualifier.storage == EvqTemporary)
1341 new(&type) TType(typeList, structName);
1342 else
1343 new(&type) TType(typeList, structName, qualifier); // sets EbtBlock
John Kesseniche6e74942016-06-11 16:43:14 -06001344
John Kessenich3d157c52016-07-25 16:05:33 -06001345 // If it was named, which means the type can be reused later, add
1346 // it to the symbol table. (Unless it's a block, in which
1347 // case the name is not a type.)
1348 if (type.getBasicType() != EbtBlock && structName.size() > 0) {
John Kesseniche6e74942016-06-11 16:43:14 -06001349 TVariable* userTypeDef = new TVariable(&structName, type, true);
1350 if (! parseContext.symbolTable.insert(*userTypeDef))
1351 parseContext.error(token.loc, "redefinition", structName.c_str(), "struct");
1352 }
1353
1354 return true;
1355}
1356
1357// struct_declaration_list
1358// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
1359//
1360// struct_declaration
1361// : fully_specified_type struct_declarator COMMA struct_declarator ...
1362//
1363// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06001364// : IDENTIFIER post_decls
1365// | IDENTIFIER array_specifier post_decls
John Kesseniche6e74942016-06-11 16:43:14 -06001366//
1367bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList)
1368{
1369 typeList = new TTypeList();
1370
1371 do {
1372 // success on seeing the RIGHT_BRACE coming up
1373 if (peekTokenClass(EHTokRightBrace))
1374 return true;
1375
1376 // struct_declaration
1377
1378 // fully_specified_type
1379 TType memberType;
1380 if (! acceptFullySpecifiedType(memberType)) {
1381 expected("member type");
1382 return false;
1383 }
1384
1385 // struct_declarator COMMA struct_declarator ...
1386 do {
1387 // peek IDENTIFIER
1388 if (! peekTokenClass(EHTokIdentifier)) {
1389 expected("member name");
1390 return false;
1391 }
1392
1393 // add it to the list of members
1394 TTypeLoc member = { new TType(EbtVoid), token.loc };
1395 member.type->shallowCopy(memberType);
1396 member.type->setFieldName(*token.string);
1397 typeList->push_back(member);
1398
1399 // accept IDENTIFIER
1400 advanceToken();
1401
1402 // array_specifier
John Kessenich19b92ff2016-06-19 11:50:34 -06001403 TArraySizes* arraySizes = nullptr;
1404 acceptArraySpecifier(arraySizes);
1405 if (arraySizes)
1406 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06001407
John Kessenich630dd7d2016-06-12 23:52:12 -06001408 acceptPostDecls(*member.type);
1409
John Kesseniche6e74942016-06-11 16:43:14 -06001410 // success on seeing the SEMICOLON coming up
1411 if (peekTokenClass(EHTokSemicolon))
1412 break;
1413
1414 // COMMA
1415 if (! acceptTokenClass(EHTokComma)) {
1416 expected(",");
1417 return false;
1418 }
1419
1420 } while (true);
1421
1422 // SEMI_COLON
1423 if (! acceptTokenClass(EHTokSemicolon)) {
1424 expected(";");
1425 return false;
1426 }
1427
1428 } while (true);
1429}
1430
John Kessenich5f934b02016-03-13 17:58:25 -06001431// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06001432// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06001433// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001434//
1435bool HlslGrammar::acceptFunctionParameters(TFunction& function)
1436{
John Kessenich078d7f22016-03-14 10:02:11 -06001437 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001438 if (! acceptTokenClass(EHTokLeftParen))
1439 return false;
1440
John Kessenich71351de2016-06-08 12:50:56 -06001441 // VOID RIGHT_PAREN
1442 if (! acceptTokenClass(EHTokVoid)) {
1443 do {
1444 // parameter_declaration
1445 if (! acceptParameterDeclaration(function))
1446 break;
John Kessenich5f934b02016-03-13 17:58:25 -06001447
John Kessenich71351de2016-06-08 12:50:56 -06001448 // COMMA
1449 if (! acceptTokenClass(EHTokComma))
1450 break;
1451 } while (true);
1452 }
John Kessenich5f934b02016-03-13 17:58:25 -06001453
John Kessenich078d7f22016-03-14 10:02:11 -06001454 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001455 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001456 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06001457 return false;
1458 }
1459
1460 return true;
1461}
1462
1463// parameter_declaration
John Kessenichc3387d32016-06-17 14:21:02 -06001464// : fully_specified_type post_decls
John Kessenich19b92ff2016-06-19 11:50:34 -06001465// | fully_specified_type identifier array_specifier post_decls
John Kessenich5f934b02016-03-13 17:58:25 -06001466//
1467bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
1468{
1469 // fully_specified_type
1470 TType* type = new TType;
1471 if (! acceptFullySpecifiedType(*type))
1472 return false;
1473
1474 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06001475 HlslToken idToken;
1476 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06001477
John Kessenich19b92ff2016-06-19 11:50:34 -06001478 // array_specifier
1479 TArraySizes* arraySizes = nullptr;
1480 acceptArraySpecifier(arraySizes);
1481 if (arraySizes)
1482 type->newArraySizes(*arraySizes);
1483
1484 // post_decls
John Kessenichc3387d32016-06-17 14:21:02 -06001485 acceptPostDecls(*type);
1486
John Kessenich5aa59e22016-06-17 15:50:47 -06001487 parseContext.paramFix(*type);
1488
John Kessenichaecd4972016-03-14 10:46:34 -06001489 TParameter param = { idToken.string, type };
John Kessenich5f934b02016-03-13 17:58:25 -06001490 function.addParameter(param);
1491
1492 return true;
1493}
1494
1495// Do the work to create the function definition in addition to
1496// parsing the body (compound_statement).
1497bool HlslGrammar::acceptFunctionDefinition(TFunction& function, TIntermNode*& node)
1498{
John Kessenicha3051662016-09-02 19:13:36 -06001499 TFunction& functionDeclarator = parseContext.handleFunctionDeclarator(token.loc, function, false /* not prototype */);
John Kessenich1a4b7752016-09-02 19:05:24 -06001500 TSourceLoc loc = token.loc;
John Kessenich5f934b02016-03-13 17:58:25 -06001501
John Kessenich077e0522016-06-09 02:02:17 -06001502 // This does a pushScope()
John Kessenicha3051662016-09-02 19:13:36 -06001503 node = parseContext.handleFunctionDefinition(loc, functionDeclarator);
John Kessenich5f934b02016-03-13 17:58:25 -06001504
1505 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06001506 TIntermNode* functionBody = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06001507 if (acceptCompoundStatement(functionBody)) {
John Kessenicha3051662016-09-02 19:13:36 -06001508 parseContext.handleFunctionBody(loc, functionDeclarator, functionBody, node);
John Kessenich5f934b02016-03-13 17:58:25 -06001509 return true;
1510 }
1511
1512 return false;
1513}
1514
John Kessenich0d2b6de2016-06-05 11:23:11 -06001515// Accept an expression with parenthesis around it, where
1516// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001517// syntactically required ones like in "if ( expression )".
1518//
1519// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06001520//
1521// Note this one is not set up to be speculative; as it gives
1522// errors if not found.
1523//
1524bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
1525{
1526 // LEFT_PAREN
1527 if (! acceptTokenClass(EHTokLeftParen))
1528 expected("(");
1529
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001530 bool decl = false;
1531 TIntermNode* declNode = nullptr;
1532 decl = acceptControlDeclaration(declNode);
1533 if (decl) {
1534 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
1535 expected("initialized declaration");
1536 return false;
1537 } else
1538 expression = declNode->getAsTyped();
1539 } else {
1540 // no declaration
1541 if (! acceptExpression(expression)) {
1542 expected("expression");
1543 return false;
1544 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06001545 }
1546
1547 // RIGHT_PAREN
1548 if (! acceptTokenClass(EHTokRightParen))
1549 expected(")");
1550
1551 return true;
1552}
1553
John Kessenich34fb0362016-05-03 23:17:20 -06001554// The top-level full expression recognizer.
1555//
John Kessenich87142c72016-03-12 20:24:24 -07001556// expression
John Kessenich34fb0362016-05-03 23:17:20 -06001557// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07001558//
1559bool HlslGrammar::acceptExpression(TIntermTyped*& node)
1560{
LoopDawgef764a22016-06-03 09:17:51 -06001561 node = nullptr;
1562
John Kessenich34fb0362016-05-03 23:17:20 -06001563 // assignment_expression
1564 if (! acceptAssignmentExpression(node))
1565 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001566
John Kessenich34fb0362016-05-03 23:17:20 -06001567 if (! peekTokenClass(EHTokComma))
1568 return true;
1569
1570 do {
1571 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06001572 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06001573 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06001574
John Kessenich34fb0362016-05-03 23:17:20 -06001575 // ... assignment_expression
1576 TIntermTyped* rightNode = nullptr;
1577 if (! acceptAssignmentExpression(rightNode)) {
1578 expected("assignment expression");
1579 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001580 }
1581
John Kessenich34fb0362016-05-03 23:17:20 -06001582 node = intermediate.addComma(node, rightNode, loc);
1583
1584 if (! peekTokenClass(EHTokComma))
1585 return true;
1586 } while (true);
1587}
1588
John Kessenich07354242016-07-01 19:58:06 -06001589// initializer
1590// : LEFT_BRACE initializer_list RIGHT_BRACE
1591//
1592// initializer_list
1593// : assignment_expression COMMA assignment_expression COMMA ...
1594//
1595bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
1596{
1597 // LEFT_BRACE
1598 if (! acceptTokenClass(EHTokLeftBrace))
1599 return false;
1600
1601 // initializer_list
1602 TSourceLoc loc = token.loc;
1603 node = nullptr;
1604 do {
1605 // assignment_expression
1606 TIntermTyped* expr;
1607 if (! acceptAssignmentExpression(expr)) {
1608 expected("assignment expression in initializer list");
1609 return false;
1610 }
1611 node = intermediate.growAggregate(node, expr, loc);
1612
1613 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001614 if (acceptTokenClass(EHTokComma)) {
1615 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
1616 return true;
John Kessenich07354242016-07-01 19:58:06 -06001617 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001618 }
John Kessenich07354242016-07-01 19:58:06 -06001619
1620 // RIGHT_BRACE
1621 if (acceptTokenClass(EHTokRightBrace))
1622 return true;
1623
1624 expected(", or }");
1625 return false;
1626 } while (true);
1627}
1628
John Kessenich34fb0362016-05-03 23:17:20 -06001629// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06001630// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06001631//
1632// a op (b op (c op d))
1633//
1634// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06001635// : initializer
1636// | conditional_expression
1637// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06001638//
1639bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
1640{
John Kessenich07354242016-07-01 19:58:06 -06001641 // initializer
1642 if (peekTokenClass(EHTokLeftBrace)) {
1643 if (acceptInitializer(node))
1644 return true;
1645
1646 expected("initializer");
1647 return false;
1648 }
1649
John Kessenich00957f82016-07-27 10:39:57 -06001650 // conditional_expression
1651 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06001652 return false;
1653
John Kessenich07354242016-07-01 19:58:06 -06001654 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06001655 TOperator assignOp = HlslOpMap::assignment(peek());
1656 if (assignOp == EOpNull)
1657 return true;
1658
John Kessenich00957f82016-07-27 10:39:57 -06001659 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06001660 TSourceLoc loc = token.loc;
1661 advanceToken();
1662
John Kessenich00957f82016-07-27 10:39:57 -06001663 // conditional_expression assign_op conditional_expression ...
1664 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06001665 // gets the right-to-left associativity.
1666 TIntermTyped* rightNode = nullptr;
1667 if (! acceptAssignmentExpression(rightNode)) {
1668 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06001669 return false;
John Kessenich87142c72016-03-12 20:24:24 -07001670 }
1671
John Kessenich34fb0362016-05-03 23:17:20 -06001672 node = intermediate.addAssign(assignOp, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06001673 if (node == nullptr) {
1674 parseContext.error(loc, "could not create assignment", "", "");
1675 return false;
1676 }
John Kessenich34fb0362016-05-03 23:17:20 -06001677
1678 if (! peekTokenClass(EHTokComma))
1679 return true;
1680
1681 return true;
1682}
1683
John Kessenich00957f82016-07-27 10:39:57 -06001684// Accept a conditional expression, which associates right-to-left,
1685// accomplished by the "true" expression calling down to lower
1686// precedence levels than this level.
1687//
1688// conditional_expression
1689// : binary_expression
1690// | binary_expression QUESTION expression COLON assignment_expression
1691//
1692bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
1693{
1694 // binary_expression
1695 if (! acceptBinaryExpression(node, PlLogicalOr))
1696 return false;
1697
1698 if (! acceptTokenClass(EHTokQuestion))
1699 return true;
1700
1701 TIntermTyped* trueNode = nullptr;
1702 if (! acceptExpression(trueNode)) {
1703 expected("expression after ?");
1704 return false;
1705 }
1706 TSourceLoc loc = token.loc;
1707
1708 if (! acceptTokenClass(EHTokColon)) {
1709 expected(":");
1710 return false;
1711 }
1712
1713 TIntermTyped* falseNode = nullptr;
1714 if (! acceptAssignmentExpression(falseNode)) {
1715 expected("expression after :");
1716 return false;
1717 }
1718
1719 node = intermediate.addSelection(node, trueNode, falseNode, loc);
1720
1721 return true;
1722}
1723
John Kessenich34fb0362016-05-03 23:17:20 -06001724// Accept a binary expression, for binary operations that
1725// associate left-to-right. This is, it is implicit, for example
1726//
1727// ((a op b) op c) op d
1728//
1729// binary_expression
1730// : expression op expression op expression ...
1731//
1732// where 'expression' is the next higher level in precedence.
1733//
1734bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
1735{
1736 if (precedenceLevel > PlMul)
1737 return acceptUnaryExpression(node);
1738
1739 // assignment_expression
1740 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
1741 return false;
1742
John Kessenich34fb0362016-05-03 23:17:20 -06001743 do {
John Kessenich64076ed2016-07-28 21:43:17 -06001744 TOperator op = HlslOpMap::binary(peek());
1745 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
1746 if (tokenLevel < precedenceLevel)
1747 return true;
1748
John Kessenich34fb0362016-05-03 23:17:20 -06001749 // ... op
1750 TSourceLoc loc = token.loc;
1751 advanceToken();
1752
1753 // ... expression
1754 TIntermTyped* rightNode = nullptr;
1755 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
1756 expected("expression");
1757 return false;
1758 }
1759
1760 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06001761 if (node == nullptr) {
1762 parseContext.error(loc, "Could not perform requested binary operation", "", "");
1763 return false;
1764 }
John Kessenich34fb0362016-05-03 23:17:20 -06001765 } while (true);
1766}
1767
1768// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06001769// : (type) unary_expression
1770// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06001771// | - unary_expression
1772// | ! unary_expression
1773// | ~ unary_expression
1774// | ++ unary_expression
1775// | -- unary_expression
1776// | postfix_expression
1777//
1778bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
1779{
John Kessenich1cc1a282016-06-03 16:55:49 -06001780 // (type) unary_expression
1781 // Have to look two steps ahead, because this could be, e.g., a
1782 // postfix_expression instead, since that also starts with at "(".
1783 if (acceptTokenClass(EHTokLeftParen)) {
1784 TType castType;
1785 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06001786 if (acceptTokenClass(EHTokRightParen)) {
1787 // We've matched "(type)" now, get the expression to cast
1788 TSourceLoc loc = token.loc;
1789 if (! acceptUnaryExpression(node))
1790 return false;
1791
1792 // Hook it up like a constructor
1793 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
1794 if (constructorFunction == nullptr) {
1795 expected("type that can be constructed");
1796 return false;
1797 }
1798 TIntermTyped* arguments = nullptr;
1799 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
1800 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
1801
1802 return true;
1803 } else {
1804 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
1805 // the '(int' part. We must back up twice.
1806 recedeToken();
1807 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06001808 }
John Kessenich1cc1a282016-06-03 16:55:49 -06001809 } else {
1810 // This isn't a type cast, but it still started "(", so if it is a
1811 // unary expression, it can only be a postfix_expression, so try that.
1812 // Back it up first.
1813 recedeToken();
1814 return acceptPostfixExpression(node);
1815 }
1816 }
1817
1818 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06001819 TOperator unaryOp = HlslOpMap::preUnary(peek());
1820
John Kessenich1cc1a282016-06-03 16:55:49 -06001821 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06001822 if (unaryOp == EOpNull)
1823 return acceptPostfixExpression(node);
1824
1825 // op unary_expression
1826 TSourceLoc loc = token.loc;
1827 advanceToken();
1828 if (! acceptUnaryExpression(node))
1829 return false;
1830
1831 // + is a no-op
1832 if (unaryOp == EOpAdd)
1833 return true;
1834
1835 node = intermediate.addUnaryMath(unaryOp, node, loc);
1836
1837 return node != nullptr;
1838}
1839
1840// postfix_expression
1841// : LEFT_PAREN expression RIGHT_PAREN
1842// | literal
1843// | constructor
1844// | identifier
1845// | function_call
1846// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
1847// | postfix_expression DOT IDENTIFIER
1848// | postfix_expression INC_OP
1849// | postfix_expression DEC_OP
1850//
1851bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
1852{
1853 // Not implemented as self-recursive:
1854 // The logical "right recursion" is done with an loop at the end
1855
1856 // idToken will pick up either a variable or a function name in a function call
1857 HlslToken idToken;
1858
John Kessenich21472ae2016-06-04 11:46:33 -06001859 // Find something before the postfix operations, as they can't operate
1860 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07001861 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06001862 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07001863 if (! acceptExpression(node)) {
1864 expected("expression");
1865 return false;
1866 }
1867 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001868 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07001869 return false;
1870 }
John Kessenich34fb0362016-05-03 23:17:20 -06001871 } else if (acceptLiteral(node)) {
1872 // literal (nothing else to do yet), go on to the
1873 } else if (acceptConstructor(node)) {
1874 // constructor (nothing else to do yet)
1875 } else if (acceptIdentifier(idToken)) {
1876 // identifier or function_call name
1877 if (! peekTokenClass(EHTokLeftParen)) {
John Kesseniche6e74942016-06-11 16:43:14 -06001878 node = parseContext.handleVariable(idToken.loc, idToken.symbol, token.string);
John Kessenich34fb0362016-05-03 23:17:20 -06001879 } else if (acceptFunctionCall(idToken, node)) {
1880 // function_call (nothing else to do yet)
1881 } else {
1882 expected("function call arguments");
1883 return false;
1884 }
John Kessenich21472ae2016-06-04 11:46:33 -06001885 } else {
1886 // nothing found, can't post operate
1887 return false;
John Kessenich87142c72016-03-12 20:24:24 -07001888 }
1889
John Kessenich21472ae2016-06-04 11:46:33 -06001890 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06001891 do {
1892 TSourceLoc loc = token.loc;
1893 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07001894
John Kessenich34fb0362016-05-03 23:17:20 -06001895 // Consume only a valid post-unary operator, otherwise we are done.
1896 switch (postOp) {
1897 case EOpIndexDirectStruct:
1898 case EOpIndexIndirect:
1899 case EOpPostIncrement:
1900 case EOpPostDecrement:
1901 advanceToken();
1902 break;
1903 default:
1904 return true;
1905 }
John Kessenich87142c72016-03-12 20:24:24 -07001906
John Kessenich34fb0362016-05-03 23:17:20 -06001907 // We have a valid post-unary operator, process it.
1908 switch (postOp) {
1909 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06001910 {
John Kessenich19b92ff2016-06-19 11:50:34 -06001911 // DOT IDENTIFIER
1912 // includes swizzles and struct members
John Kessenich93a162a2016-06-17 17:16:27 -06001913 HlslToken field;
1914 if (! acceptIdentifier(field)) {
1915 expected("swizzle or member");
1916 return false;
1917 }
LoopDawg4886f692016-06-29 10:58:58 -06001918
1919 TIntermTyped* base = node; // preserve for method function calls
John Kessenich93a162a2016-06-17 17:16:27 -06001920 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06001921
1922 // In the event of a method node, we look for an open paren and accept the function call.
1923 if (node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) {
1924 if (! acceptFunctionCall(field, node, base)) {
1925 expected("function parameters");
1926 return false;
1927 }
1928 }
1929
John Kessenich34fb0362016-05-03 23:17:20 -06001930 break;
John Kessenich93a162a2016-06-17 17:16:27 -06001931 }
John Kessenich34fb0362016-05-03 23:17:20 -06001932 case EOpIndexIndirect:
1933 {
John Kessenich19b92ff2016-06-19 11:50:34 -06001934 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06001935 TIntermTyped* indexNode = nullptr;
1936 if (! acceptExpression(indexNode) ||
1937 ! peekTokenClass(EHTokRightBracket)) {
1938 expected("expression followed by ']'");
1939 return false;
1940 }
John Kessenich19b92ff2016-06-19 11:50:34 -06001941 advanceToken();
1942 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
1943 break;
John Kessenich34fb0362016-05-03 23:17:20 -06001944 }
1945 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06001946 // INC_OP
1947 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06001948 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06001949 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06001950 node = intermediate.addUnaryMath(postOp, node, loc);
1951 break;
1952 default:
1953 assert(0);
1954 break;
1955 }
1956 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07001957}
1958
John Kessenichd016be12016-03-13 11:24:20 -06001959// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06001960// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06001961//
1962bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
1963{
1964 // type
1965 TType type;
1966 if (acceptType(type)) {
1967 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
1968 if (constructorFunction == nullptr)
1969 return false;
1970
1971 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06001972 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06001973 if (! acceptArguments(constructorFunction, arguments)) {
1974 expected("constructor arguments");
1975 return false;
1976 }
1977
1978 // hook it up
1979 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
1980
1981 return true;
1982 }
1983
1984 return false;
1985}
1986
John Kessenich34fb0362016-05-03 23:17:20 -06001987// The function_call identifier was already recognized, and passed in as idToken.
1988//
1989// function_call
1990// : [idToken] arguments
1991//
LoopDawg4886f692016-06-29 10:58:58 -06001992bool HlslGrammar::acceptFunctionCall(HlslToken idToken, TIntermTyped*& node, TIntermTyped* base)
John Kessenich34fb0362016-05-03 23:17:20 -06001993{
John Kessenich4678ca92016-05-13 09:33:42 -06001994 // arguments
1995 TFunction* function = new TFunction(idToken.string, TType(EbtVoid));
1996 TIntermTyped* arguments = nullptr;
LoopDawg4886f692016-06-29 10:58:58 -06001997
1998 // methods have an implicit first argument of the calling object.
1999 if (base != nullptr)
2000 parseContext.handleFunctionArgument(function, arguments, base);
2001
John Kessenich4678ca92016-05-13 09:33:42 -06002002 if (! acceptArguments(function, arguments))
2003 return false;
2004
2005 node = parseContext.handleFunctionCall(idToken.loc, function, arguments);
2006
2007 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002008}
2009
John Kessenich87142c72016-03-12 20:24:24 -07002010// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002011// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002012//
John Kessenichd016be12016-03-13 11:24:20 -06002013// The arguments are pushed onto the 'function' argument list and
2014// onto the 'arguments' aggregate.
2015//
John Kessenich4678ca92016-05-13 09:33:42 -06002016bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002017{
John Kessenich078d7f22016-03-14 10:02:11 -06002018 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002019 if (! acceptTokenClass(EHTokLeftParen))
2020 return false;
2021
2022 do {
John Kessenichd016be12016-03-13 11:24:20 -06002023 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002024 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002025 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002026 break;
John Kessenichd016be12016-03-13 11:24:20 -06002027
2028 // hook it up
2029 parseContext.handleFunctionArgument(function, arguments, arg);
2030
John Kessenich078d7f22016-03-14 10:02:11 -06002031 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002032 if (! acceptTokenClass(EHTokComma))
2033 break;
2034 } while (true);
2035
John Kessenich078d7f22016-03-14 10:02:11 -06002036 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002037 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002038 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002039 return false;
2040 }
2041
2042 return true;
2043}
2044
2045bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2046{
2047 switch (token.tokenClass) {
2048 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002049 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002050 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002051 case EHTokUintConstant:
2052 node = intermediate.addConstantUnion(token.u, token.loc, true);
2053 break;
John Kessenich87142c72016-03-12 20:24:24 -07002054 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002055 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002056 break;
2057 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002058 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002059 break;
2060 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002061 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002062 break;
2063
2064 default:
2065 return false;
2066 }
2067
2068 advanceToken();
2069
2070 return true;
2071}
2072
John Kessenich5f934b02016-03-13 17:58:25 -06002073// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002074// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002075//
John Kessenich21472ae2016-06-04 11:46:33 -06002076bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002077{
John Kessenich21472ae2016-06-04 11:46:33 -06002078 TIntermAggregate* compoundStatement = nullptr;
2079
John Kessenich34fb0362016-05-03 23:17:20 -06002080 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002081 if (! acceptTokenClass(EHTokLeftBrace))
2082 return false;
2083
2084 // statement statement ...
2085 TIntermNode* statement = nullptr;
2086 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002087 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2088 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2089 branch->getFlowOp() == EOpDefault)) {
2090 // hook up individual subsequences within a switch statement
2091 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2092 compoundStatement = nullptr;
2093 } else {
2094 // hook it up to the growing compound statement
2095 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2096 }
John Kessenich5f934b02016-03-13 17:58:25 -06002097 }
John Kessenich34fb0362016-05-03 23:17:20 -06002098 if (compoundStatement)
2099 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06002100
John Kessenich21472ae2016-06-04 11:46:33 -06002101 retStatement = compoundStatement;
2102
John Kessenich34fb0362016-05-03 23:17:20 -06002103 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002104 return acceptTokenClass(EHTokRightBrace);
2105}
2106
John Kessenich0d2b6de2016-06-05 11:23:11 -06002107bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
2108{
2109 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06002110 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06002111 parseContext.popScope();
2112
2113 return result;
2114}
2115
John Kessenich077e0522016-06-09 02:02:17 -06002116bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06002117{
John Kessenich077e0522016-06-09 02:02:17 -06002118 parseContext.pushScope();
2119 bool result = acceptCompoundStatement(statement);
2120 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06002121
2122 return result;
2123}
2124
John Kessenich5f934b02016-03-13 17:58:25 -06002125// statement
John Kessenich21472ae2016-06-04 11:46:33 -06002126// : attributes attributed_statement
2127//
2128// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002129// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002130// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06002131// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06002132// | declaration_statement
2133// | selection_statement
2134// | switch_statement
2135// | case_label
2136// | iteration_statement
2137// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002138//
2139bool HlslGrammar::acceptStatement(TIntermNode*& statement)
2140{
John Kessenich21472ae2016-06-04 11:46:33 -06002141 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06002142
John Kessenich21472ae2016-06-04 11:46:33 -06002143 // attributes
2144 acceptAttributes();
John Kessenich5f934b02016-03-13 17:58:25 -06002145
John Kessenich21472ae2016-06-04 11:46:33 -06002146 // attributed_statement
2147 switch (peek()) {
2148 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06002149 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002150
John Kessenich21472ae2016-06-04 11:46:33 -06002151 case EHTokIf:
2152 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002153
John Kessenich21472ae2016-06-04 11:46:33 -06002154 case EHTokSwitch:
2155 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002156
John Kessenich21472ae2016-06-04 11:46:33 -06002157 case EHTokFor:
2158 case EHTokDo:
2159 case EHTokWhile:
2160 return acceptIterationStatement(statement);
2161
2162 case EHTokContinue:
2163 case EHTokBreak:
2164 case EHTokDiscard:
2165 case EHTokReturn:
2166 return acceptJumpStatement(statement);
2167
2168 case EHTokCase:
2169 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06002170 case EHTokDefault:
2171 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06002172
2173 case EHTokSemicolon:
2174 return acceptTokenClass(EHTokSemicolon);
2175
2176 case EHTokRightBrace:
2177 // Performance: not strictly necessary, but stops a bunch of hunting early,
2178 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06002179 return false;
2180
John Kessenich21472ae2016-06-04 11:46:33 -06002181 default:
2182 {
2183 // declaration
2184 if (acceptDeclaration(statement))
2185 return true;
2186
2187 // expression
2188 TIntermTyped* node;
2189 if (acceptExpression(node))
2190 statement = node;
2191 else
2192 return false;
2193
2194 // SEMICOLON (following an expression)
2195 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002196 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06002197 return false;
2198 }
2199 }
2200 }
2201
John Kessenich5f934b02016-03-13 17:58:25 -06002202 return true;
John Kessenich87142c72016-03-12 20:24:24 -07002203}
2204
John Kessenich21472ae2016-06-04 11:46:33 -06002205// attributes
2206// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
2207//
2208// attribute:
2209// : UNROLL
2210// | UNROLL LEFT_PAREN literal RIGHT_PAREN
2211// | FASTOPT
2212// | ALLOW_UAV_CONDITION
2213// | BRANCH
2214// | FLATTEN
2215// | FORCECASE
2216// | CALL
2217//
2218void HlslGrammar::acceptAttributes()
2219{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002220 // For now, accept the [ XXX(X) ] syntax, but drop.
2221 // TODO: subset to correct set? Pass on?
2222 do {
2223 // LEFT_BRACKET?
2224 if (! acceptTokenClass(EHTokLeftBracket))
2225 return;
2226
2227 // attribute
2228 if (peekTokenClass(EHTokIdentifier)) {
2229 // 'token.string' is the attribute
2230 advanceToken();
2231 } else if (! peekTokenClass(EHTokRightBracket)) {
2232 expected("identifier");
2233 advanceToken();
2234 }
2235
2236 // (x)
2237 if (acceptTokenClass(EHTokLeftParen)) {
2238 TIntermTyped* node;
2239 if (! acceptLiteral(node))
2240 expected("literal");
2241 // 'node' has the literal in it
2242 if (! acceptTokenClass(EHTokRightParen))
2243 expected(")");
2244 }
2245
2246 // RIGHT_BRACKET
2247 if (acceptTokenClass(EHTokRightBracket))
2248 continue;
2249
2250 expected("]");
2251 return;
2252
2253 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06002254}
2255
John Kessenich0d2b6de2016-06-05 11:23:11 -06002256// selection_statement
2257// : IF LEFT_PAREN expression RIGHT_PAREN statement
2258// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
2259//
John Kessenich21472ae2016-06-04 11:46:33 -06002260bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
2261{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002262 TSourceLoc loc = token.loc;
2263
2264 // IF
2265 if (! acceptTokenClass(EHTokIf))
2266 return false;
2267
2268 // so that something declared in the condition is scoped to the lifetimes
2269 // of the then-else statements
2270 parseContext.pushScope();
2271
2272 // LEFT_PAREN expression RIGHT_PAREN
2273 TIntermTyped* condition;
2274 if (! acceptParenExpression(condition))
2275 return false;
2276
2277 // create the child statements
2278 TIntermNodePair thenElse = { nullptr, nullptr };
2279
2280 // then statement
2281 if (! acceptScopedStatement(thenElse.node1)) {
2282 expected("then statement");
2283 return false;
2284 }
2285
2286 // ELSE
2287 if (acceptTokenClass(EHTokElse)) {
2288 // else statement
2289 if (! acceptScopedStatement(thenElse.node2)) {
2290 expected("else statement");
2291 return false;
2292 }
2293 }
2294
2295 // Put the pieces together
2296 statement = intermediate.addSelection(condition, thenElse, loc);
2297 parseContext.popScope();
2298
2299 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002300}
2301
John Kessenichd02dc5d2016-07-01 00:04:11 -06002302// switch_statement
2303// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
2304//
John Kessenich21472ae2016-06-04 11:46:33 -06002305bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
2306{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002307 // SWITCH
2308 TSourceLoc loc = token.loc;
2309 if (! acceptTokenClass(EHTokSwitch))
2310 return false;
2311
2312 // LEFT_PAREN expression RIGHT_PAREN
2313 parseContext.pushScope();
2314 TIntermTyped* switchExpression;
2315 if (! acceptParenExpression(switchExpression)) {
2316 parseContext.popScope();
2317 return false;
2318 }
2319
2320 // compound_statement
2321 parseContext.pushSwitchSequence(new TIntermSequence);
2322 bool statementOkay = acceptCompoundStatement(statement);
2323 if (statementOkay)
2324 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
2325
2326 parseContext.popSwitchSequence();
2327 parseContext.popScope();
2328
2329 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06002330}
2331
John Kessenich119f8f62016-06-05 15:44:07 -06002332// iteration_statement
2333// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
2334// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
2335// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
2336//
2337// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06002338bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
2339{
John Kessenich119f8f62016-06-05 15:44:07 -06002340 TSourceLoc loc = token.loc;
2341 TIntermTyped* condition = nullptr;
2342
2343 EHlslTokenClass loop = peek();
2344 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
2345
2346 // WHILE or DO or FOR
2347 advanceToken();
2348
2349 switch (loop) {
2350 case EHTokWhile:
2351 // so that something declared in the condition is scoped to the lifetime
2352 // of the while sub-statement
2353 parseContext.pushScope();
2354 parseContext.nestLooping();
2355
2356 // LEFT_PAREN condition RIGHT_PAREN
2357 if (! acceptParenExpression(condition))
2358 return false;
2359
2360 // statement
2361 if (! acceptScopedStatement(statement)) {
2362 expected("while sub-statement");
2363 return false;
2364 }
2365
2366 parseContext.unnestLooping();
2367 parseContext.popScope();
2368
2369 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
2370
2371 return true;
2372
2373 case EHTokDo:
2374 parseContext.nestLooping();
2375
2376 if (! acceptTokenClass(EHTokLeftBrace))
2377 expected("{");
2378
2379 // statement
2380 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
2381 expected("do sub-statement");
2382 return false;
2383 }
2384
2385 if (! acceptTokenClass(EHTokRightBrace))
2386 expected("}");
2387
2388 // WHILE
2389 if (! acceptTokenClass(EHTokWhile)) {
2390 expected("while");
2391 return false;
2392 }
2393
2394 // LEFT_PAREN condition RIGHT_PAREN
2395 TIntermTyped* condition;
2396 if (! acceptParenExpression(condition))
2397 return false;
2398
2399 if (! acceptTokenClass(EHTokSemicolon))
2400 expected(";");
2401
2402 parseContext.unnestLooping();
2403
2404 statement = intermediate.addLoop(statement, condition, 0, false, loc);
2405
2406 return true;
2407
2408 case EHTokFor:
2409 {
2410 // LEFT_PAREN
2411 if (! acceptTokenClass(EHTokLeftParen))
2412 expected("(");
2413
2414 // so that something declared in the condition is scoped to the lifetime
2415 // of the for sub-statement
2416 parseContext.pushScope();
2417
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002418 // initializer
2419 TIntermNode* initNode = nullptr;
2420 if (! acceptControlDeclaration(initNode)) {
2421 TIntermTyped* initExpr = nullptr;
2422 acceptExpression(initExpr);
2423 initNode = initExpr;
2424 }
2425 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06002426 if (! acceptTokenClass(EHTokSemicolon))
2427 expected(";");
2428
2429 parseContext.nestLooping();
2430
2431 // condition SEMI_COLON
2432 acceptExpression(condition);
2433 if (! acceptTokenClass(EHTokSemicolon))
2434 expected(";");
2435
2436 // iterator SEMI_COLON
2437 TIntermTyped* iterator = nullptr;
2438 acceptExpression(iterator);
2439 if (! acceptTokenClass(EHTokRightParen))
2440 expected(")");
2441
2442 // statement
2443 if (! acceptScopedStatement(statement)) {
2444 expected("for sub-statement");
2445 return false;
2446 }
2447
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002448 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06002449
2450 parseContext.popScope();
2451 parseContext.unnestLooping();
2452
2453 return true;
2454 }
2455
2456 default:
2457 return false;
2458 }
John Kessenich21472ae2016-06-04 11:46:33 -06002459}
2460
2461// jump_statement
2462// : CONTINUE SEMICOLON
2463// | BREAK SEMICOLON
2464// | DISCARD SEMICOLON
2465// | RETURN SEMICOLON
2466// | RETURN expression SEMICOLON
2467//
2468bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
2469{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002470 EHlslTokenClass jump = peek();
2471 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06002472 case EHTokContinue:
2473 case EHTokBreak:
2474 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06002475 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002476 advanceToken();
2477 break;
John Kessenich21472ae2016-06-04 11:46:33 -06002478 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002479 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06002480 return false;
2481 }
John Kessenich21472ae2016-06-04 11:46:33 -06002482
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002483 switch (jump) {
2484 case EHTokContinue:
2485 statement = intermediate.addBranch(EOpContinue, token.loc);
2486 break;
2487 case EHTokBreak:
2488 statement = intermediate.addBranch(EOpBreak, token.loc);
2489 break;
2490 case EHTokDiscard:
2491 statement = intermediate.addBranch(EOpKill, token.loc);
2492 break;
2493
2494 case EHTokReturn:
2495 {
2496 // expression
2497 TIntermTyped* node;
2498 if (acceptExpression(node)) {
2499 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06002500 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002501 } else
2502 statement = intermediate.addBranch(EOpReturn, token.loc);
2503 break;
2504 }
2505
2506 default:
2507 assert(0);
2508 return false;
2509 }
2510
2511 // SEMICOLON
2512 if (! acceptTokenClass(EHTokSemicolon))
2513 expected(";");
2514
2515 return true;
2516}
John Kessenich21472ae2016-06-04 11:46:33 -06002517
John Kessenichd02dc5d2016-07-01 00:04:11 -06002518// case_label
2519// : CASE expression COLON
2520//
John Kessenich21472ae2016-06-04 11:46:33 -06002521bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
2522{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002523 TSourceLoc loc = token.loc;
2524 if (! acceptTokenClass(EHTokCase))
2525 return false;
2526
2527 TIntermTyped* expression;
2528 if (! acceptExpression(expression)) {
2529 expected("case expression");
2530 return false;
2531 }
2532
2533 if (! acceptTokenClass(EHTokColon)) {
2534 expected(":");
2535 return false;
2536 }
2537
2538 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
2539
2540 return true;
2541}
2542
2543// default_label
2544// : DEFAULT COLON
2545//
2546bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
2547{
2548 TSourceLoc loc = token.loc;
2549 if (! acceptTokenClass(EHTokDefault))
2550 return false;
2551
2552 if (! acceptTokenClass(EHTokColon)) {
2553 expected(":");
2554 return false;
2555 }
2556
2557 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
2558
2559 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002560}
2561
John Kessenich19b92ff2016-06-19 11:50:34 -06002562// array_specifier
2563// : LEFT_BRACKET integer_expression RGHT_BRACKET post_decls // optional
2564//
2565void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
2566{
2567 arraySizes = nullptr;
2568
2569 if (! acceptTokenClass(EHTokLeftBracket))
2570 return;
2571
2572 TSourceLoc loc = token.loc;
2573 TIntermTyped* sizeExpr;
2574 if (! acceptAssignmentExpression(sizeExpr)) {
2575 expected("array-sizing expression");
2576 return;
2577 }
2578
2579 if (! acceptTokenClass(EHTokRightBracket)) {
2580 expected("]");
2581 return;
2582 }
2583
2584 TArraySize arraySize;
2585 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
2586 arraySizes = new TArraySizes;
2587 arraySizes->addInnerSize(arraySize);
2588}
2589
John Kessenich630dd7d2016-06-12 23:52:12 -06002590// post_decls
John Kessenichb38f0712016-07-30 10:29:54 -06002591// : COLON semantic // optional
2592// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
2593// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt RIGHT_PAREN // optional
2594// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06002595//
2596void HlslGrammar::acceptPostDecls(TType& type)
John Kessenich078d7f22016-03-14 10:02:11 -06002597{
John Kessenich630dd7d2016-06-12 23:52:12 -06002598 do {
2599 // COLON
2600 if (acceptTokenClass(EHTokColon)) {
2601 HlslToken idToken;
2602 if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06002603 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002604 if (! acceptTokenClass(EHTokLeftParen)) {
2605 expected("(");
2606 return;
2607 }
John Kessenich82d6baf2016-07-29 13:03:05 -06002608 HlslToken locationToken;
2609 if (! acceptIdentifier(locationToken)) {
2610 expected("c[subcomponent][.component]");
2611 return;
2612 }
2613 HlslToken componentToken;
2614 if (acceptTokenClass(EHTokDot)) {
2615 if (! acceptIdentifier(componentToken)) {
2616 expected("component");
2617 return;
2618 }
2619 }
John Kessenich630dd7d2016-06-12 23:52:12 -06002620 if (! acceptTokenClass(EHTokRightParen)) {
2621 expected(")");
2622 break;
2623 }
John Kessenich82d6baf2016-07-29 13:03:05 -06002624 parseContext.handlePackOffset(locationToken.loc, type, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002625 } else if (! acceptIdentifier(idToken)) {
2626 expected("semantic or packoffset or register");
2627 return;
2628 } else if (*idToken.string == "register") {
John Kessenichb38f0712016-07-30 10:29:54 -06002629 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002630 if (! acceptTokenClass(EHTokLeftParen)) {
2631 expected("(");
2632 return;
2633 }
John Kessenichb38f0712016-07-30 10:29:54 -06002634 HlslToken registerDesc; // for Type#
2635 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06002636 if (! acceptIdentifier(registerDesc)) {
2637 expected("register number description");
2638 return;
2639 }
John Kessenich96e9f472016-07-29 14:28:39 -06002640 if (acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06002641 // Then we didn't really see the registerDesc yet, it was
2642 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06002643 profile = registerDesc;
2644 if (! acceptIdentifier(registerDesc)) {
2645 expected("register number description");
2646 return;
2647 }
2648 }
John Kessenichb38f0712016-07-30 10:29:54 -06002649 int subComponent = 0;
2650 if (acceptTokenClass(EHTokLeftBracket)) {
2651 // LEFT_BRACKET subcomponent RIGHT_BRACKET
2652 if (! peekTokenClass(EHTokIntConstant)) {
2653 expected("literal integer");
2654 return;
2655 }
2656 subComponent = token.i;
2657 advanceToken();
2658 if (! acceptTokenClass(EHTokRightBracket)) {
2659 expected("]");
2660 break;
2661 }
2662 }
John Kessenich630dd7d2016-06-12 23:52:12 -06002663 if (! acceptTokenClass(EHTokRightParen)) {
2664 expected(")");
2665 break;
2666 }
John Kessenichb38f0712016-07-30 10:29:54 -06002667 parseContext.handleRegister(registerDesc.loc, type, profile.string, *registerDesc.string, subComponent);
John Kessenich630dd7d2016-06-12 23:52:12 -06002668 } else {
2669 // semantic, in idToken.string
John Kessenich81d47142016-08-29 16:07:29 -06002670 parseContext.handleSemantic(idToken.loc, type, *idToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002671 }
2672 } else if (acceptTokenClass(EHTokLeftAngle)) {
2673 // TODO: process annotations, just accepting them for now
2674 do {
2675 if (peekTokenClass(EHTokNone))
2676 return;
2677 if (acceptTokenClass(EHTokRightAngle))
2678 break;
2679 advanceToken();
2680 } while (true);
2681 } else
2682 break;
John Kessenich078d7f22016-03-14 10:02:11 -06002683
John Kessenich630dd7d2016-06-12 23:52:12 -06002684 } while (true);
John Kessenich078d7f22016-03-14 10:02:11 -06002685}
2686
John Kesseniche01a9bc2016-03-12 20:11:22 -07002687} // end namespace glslang