blob: 626d299120ef76ac472a47d8eede57c3c2833d72 [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 Kesseniche82061d2016-09-27 14:38:57 -0600274 TType declaredType;
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
John Kesseniche82061d2016-09-27 14:38:57 -0600283 // if (acceptSamplerDeclarationDX9(declaredType))
Dan Bakerc7e50162016-08-05 14:52:38 -0400284 // return true;
LoopDawg4886f692016-06-29 10:58:58 -0600285
286 // fully_specified_type
John Kesseniche82061d2016-09-27 14:38:57 -0600287 if (! acceptFullySpecifiedType(declaredType))
John Kessenich87142c72016-03-12 20:24:24 -0700288 return false;
LoopDawg4886f692016-06-29 10:58:58 -0600289
John Kessenich87142c72016-03-12 20:24:24 -0700290 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -0600291 HlslToken idToken;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600292 while (acceptIdentifier(idToken)) {
John Kessenich5f934b02016-03-13 17:58:25 -0600293 // function_parameters
John Kesseniche82061d2016-09-27 14:38:57 -0600294 TFunction& function = *new TFunction(idToken.string, declaredType);
John Kessenich9e079532016-09-02 20:05:19 -0600295 if (acceptFunctionParameters(function)) {
John Kessenich630dd7d2016-06-12 23:52:12 -0600296 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -0600297 acceptPostDecls(function.getWritableType().getQualifier());
John Kessenich078d7f22016-03-14 10:02:11 -0600298
John Kessenichd5ed0b62016-07-04 17:32:45 -0600299 // compound_statement (function body definition) or just a prototype?
300 if (peekTokenClass(EHTokLeftBrace)) {
301 if (list)
302 parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", "");
John Kessenich5e69ec62016-07-05 00:02:40 -0600303 if (typedefDecl)
304 parseContext.error(idToken.loc, "function body can't be in a typedef", "{", "");
John Kessenich9e079532016-09-02 20:05:19 -0600305 return acceptFunctionDefinition(function, node);
John Kessenich5e69ec62016-07-05 00:02:40 -0600306 } else {
307 if (typedefDecl)
308 parseContext.error(idToken.loc, "function typedefs not implemented", "{", "");
John Kessenich9e079532016-09-02 20:05:19 -0600309 parseContext.handleFunctionDeclarator(idToken.loc, function, true);
John Kessenich5e69ec62016-07-05 00:02:40 -0600310 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600311 } else {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600312 // A variable declaration. Fix the storage qualifier if it's a global.
313 if (declaredType.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel())
314 declaredType.getQualifier().storage = EvqUniform;
315
John Kesseniche82061d2016-09-27 14:38:57 -0600316 // We can handle multiple variables per type declaration, so
317 // the number of types can expand when arrayness is different.
318 TType variableType;
319 variableType.shallowCopy(declaredType);
John Kessenich5f934b02016-03-13 17:58:25 -0600320
John Kesseniche82061d2016-09-27 14:38:57 -0600321 // recognize array_specifier
John Kessenichd5ed0b62016-07-04 17:32:45 -0600322 TArraySizes* arraySizes = nullptr;
323 acceptArraySpecifier(arraySizes);
John Kessenich5f934b02016-03-13 17:58:25 -0600324
John Kesseniche82061d2016-09-27 14:38:57 -0600325 // Fix arrayness in the variableType
326 if (declaredType.isImplicitlySizedArray()) {
327 // Because "int[] a = int[2](...), b = int[3](...)" makes two arrays a and b
328 // of different sizes, for this case sharing the shallow copy of arrayness
329 // with the parseType oversubscribes it, so get a deep copy of the arrayness.
330 variableType.newArraySizes(declaredType.getArraySizes());
331 }
332 if (arraySizes || variableType.isArray()) {
333 // In the most general case, arrayness is potentially coming both from the
334 // declared type and from the variable: "int[] a[];" or just one or the other.
335 // Merge it all to the variableType, so all arrayness is part of the variableType.
336 parseContext.arrayDimMerge(variableType, arraySizes);
337 }
338
LoopDawg4886f692016-06-29 10:58:58 -0600339 // samplers accept immediate sampler state
John Kesseniche82061d2016-09-27 14:38:57 -0600340 if (variableType.getBasicType() == EbtSampler) {
LoopDawg4886f692016-06-29 10:58:58 -0600341 if (! acceptSamplerState())
342 return false;
343 }
344
John Kessenichd5ed0b62016-07-04 17:32:45 -0600345 // post_decls
John Kesseniche82061d2016-09-27 14:38:57 -0600346 acceptPostDecls(variableType.getQualifier());
John Kessenichd5ed0b62016-07-04 17:32:45 -0600347
348 // EQUAL assignment_expression
349 TIntermTyped* expressionNode = nullptr;
350 if (acceptTokenClass(EHTokAssign)) {
John Kessenich5e69ec62016-07-05 00:02:40 -0600351 if (typedefDecl)
352 parseContext.error(idToken.loc, "can't have an initializer", "typedef", "");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600353 if (! acceptAssignmentExpression(expressionNode)) {
354 expected("initializer");
355 return false;
356 }
357 }
358
John Kessenich6dbc0a72016-09-27 19:13:05 -0600359 // Hand off the actual declaration
360
361 // TODO: things scoped within an annotation need their own name space;
362 // TODO: strings are not yet handled.
363 if (variableType.getBasicType() != EbtString && parseContext.getAnnotationNestingLevel() == 0) {
364 if (typedefDecl)
365 parseContext.declareTypedef(idToken.loc, *idToken.string, variableType);
366 else if (variableType.getBasicType() == EbtBlock)
367 parseContext.declareBlock(idToken.loc, variableType, idToken.string);
368 else {
John Kessenichf571d0c2016-10-01 12:35:01 -0600369 if (variableType.getQualifier().storage == EvqUniform && ! variableType.isOpaque()) {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600370 // this isn't really an individual variable, but a member of the $Global buffer
371 parseContext.growGlobalUniformBlock(idToken.loc, variableType, *idToken.string);
372 } else {
373 // Declare the variable and add any initializer code to the AST.
374 // The top-level node is always made into an aggregate, as that's
375 // historically how the AST has been.
376 node = intermediate.growAggregate(node,
377 parseContext.declareVariable(idToken.loc, *idToken.string, variableType,
378 expressionNode),
379 idToken.loc);
380 }
381 }
John Kessenich5e69ec62016-07-05 00:02:40 -0600382 }
John Kessenich5f934b02016-03-13 17:58:25 -0600383 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600384
385 if (acceptTokenClass(EHTokComma)) {
386 list = true;
387 continue;
388 }
389 };
390
391 // The top-level node is a sequence.
392 if (node != nullptr)
393 node->getAsAggregate()->setOperator(EOpSequence);
John Kessenich87142c72016-03-12 20:24:24 -0700394
John Kessenich078d7f22016-03-14 10:02:11 -0600395 // SEMICOLON
John Kessenichd5ed0b62016-07-04 17:32:45 -0600396 if (! acceptTokenClass(EHTokSemicolon)) {
397 expected(";");
398 return false;
399 }
400
John Kesseniche01a9bc2016-03-12 20:11:22 -0700401 return true;
402}
403
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600404// control_declaration
405// : fully_specified_type identifier EQUAL expression
406//
407bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node)
408{
409 node = nullptr;
410
411 // fully_specified_type
412 TType type;
413 if (! acceptFullySpecifiedType(type))
414 return false;
415
416 // identifier
417 HlslToken idToken;
418 if (! acceptIdentifier(idToken)) {
419 expected("identifier");
420 return false;
421 }
422
423 // EQUAL
424 TIntermTyped* expressionNode = nullptr;
425 if (! acceptTokenClass(EHTokAssign)) {
426 expected("=");
427 return false;
428 }
429
430 // expression
431 if (! acceptExpression(expressionNode)) {
432 expected("initializer");
433 return false;
434 }
435
John Kesseniche82061d2016-09-27 14:38:57 -0600436 node = parseContext.declareVariable(idToken.loc, *idToken.string, type, expressionNode);
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600437
438 return true;
439}
440
John Kessenich87142c72016-03-12 20:24:24 -0700441// fully_specified_type
442// : type_specifier
443// | type_qualifier type_specifier
444//
445bool HlslGrammar::acceptFullySpecifiedType(TType& type)
446{
447 // type_qualifier
448 TQualifier qualifier;
449 qualifier.clear();
John Kessenichb9e39122016-08-17 10:22:08 -0600450 if (! acceptQualifier(qualifier))
451 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600452 TSourceLoc loc = token.loc;
John Kessenich87142c72016-03-12 20:24:24 -0700453
454 // type_specifier
455 if (! acceptType(type))
456 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600457 if (type.getBasicType() == EbtBlock) {
458 // the type was a block, which set some parts of the qualifier
John Kessenich34e7ee72016-09-16 17:10:39 -0600459 parseContext.mergeQualifiers(type.getQualifier(), qualifier);
John Kessenich3d157c52016-07-25 16:05:33 -0600460 // further, it can create an anonymous instance of the block
461 if (peekTokenClass(EHTokSemicolon))
462 parseContext.declareBlock(loc, type);
steve-lunargbb0183f2016-10-04 16:58:14 -0600463 } else {
464 // Some qualifiers are set when parsing the type. Merge those with
465 // whatever comes from acceptQualifier.
466 assert(qualifier.layoutFormat == ElfNone);
467 qualifier.layoutFormat = type.getQualifier().layoutFormat;
468
John Kessenich3d157c52016-07-25 16:05:33 -0600469 type.getQualifier() = qualifier;
steve-lunargbb0183f2016-10-04 16:58:14 -0600470 }
John Kessenich87142c72016-03-12 20:24:24 -0700471
472 return true;
473}
474
John Kessenich630dd7d2016-06-12 23:52:12 -0600475// type_qualifier
476// : qualifier qualifier ...
477//
478// Zero or more of these, so this can't return false.
479//
John Kessenichb9e39122016-08-17 10:22:08 -0600480bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
John Kessenich87142c72016-03-12 20:24:24 -0700481{
John Kessenich630dd7d2016-06-12 23:52:12 -0600482 do {
483 switch (peek()) {
484 case EHTokStatic:
John Kessenich6dbc0a72016-09-27 19:13:05 -0600485 qualifier.storage = parseContext.symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
John Kessenich630dd7d2016-06-12 23:52:12 -0600486 break;
487 case EHTokExtern:
488 // TODO: no meaning in glslang?
489 break;
490 case EHTokShared:
491 // TODO: hint
492 break;
493 case EHTokGroupShared:
494 qualifier.storage = EvqShared;
495 break;
496 case EHTokUniform:
497 qualifier.storage = EvqUniform;
498 break;
499 case EHTokConst:
500 qualifier.storage = EvqConst;
501 break;
502 case EHTokVolatile:
503 qualifier.volatil = true;
504 break;
505 case EHTokLinear:
John Kessenich630dd7d2016-06-12 23:52:12 -0600506 qualifier.smooth = true;
507 break;
508 case EHTokCentroid:
509 qualifier.centroid = true;
510 break;
511 case EHTokNointerpolation:
512 qualifier.flat = true;
513 break;
514 case EHTokNoperspective:
515 qualifier.nopersp = true;
516 break;
517 case EHTokSample:
518 qualifier.sample = true;
519 break;
520 case EHTokRowMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600521 qualifier.layoutMatrix = ElmColumnMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600522 break;
523 case EHTokColumnMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600524 qualifier.layoutMatrix = ElmRowMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600525 break;
526 case EHTokPrecise:
527 qualifier.noContraction = true;
528 break;
LoopDawg9249c702016-07-12 20:44:32 -0600529 case EHTokIn:
530 qualifier.storage = EvqIn;
531 break;
532 case EHTokOut:
533 qualifier.storage = EvqOut;
534 break;
535 case EHTokInOut:
536 qualifier.storage = EvqInOut;
537 break;
John Kessenichb9e39122016-08-17 10:22:08 -0600538 case EHTokLayout:
539 if (! acceptLayoutQualifierList(qualifier))
540 return false;
541 continue;
John Kessenich630dd7d2016-06-12 23:52:12 -0600542 default:
John Kessenichb9e39122016-08-17 10:22:08 -0600543 return true;
John Kessenich630dd7d2016-06-12 23:52:12 -0600544 }
545 advanceToken();
546 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -0700547}
548
John Kessenichb9e39122016-08-17 10:22:08 -0600549// layout_qualifier_list
John Kesseniche3218e22016-09-05 14:37:03 -0600550// : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
John Kessenichb9e39122016-08-17 10:22:08 -0600551//
552// layout_qualifier
553// : identifier
John Kessenich841db352016-09-02 21:12:23 -0600554// | identifier EQUAL expression
John Kessenichb9e39122016-08-17 10:22:08 -0600555//
556// Zero or more of these, so this can't return false.
557//
558bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
559{
560 if (! acceptTokenClass(EHTokLayout))
561 return false;
562
563 // LEFT_PAREN
564 if (! acceptTokenClass(EHTokLeftParen))
565 return false;
566
567 do {
568 // identifier
569 HlslToken idToken;
570 if (! acceptIdentifier(idToken))
571 break;
572
573 // EQUAL expression
574 if (acceptTokenClass(EHTokAssign)) {
575 TIntermTyped* expr;
576 if (! acceptConditionalExpression(expr)) {
577 expected("expression");
578 return false;
579 }
580 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
581 } else
582 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
583
584 // COMMA
585 if (! acceptTokenClass(EHTokComma))
586 break;
587 } while (true);
588
589 // RIGHT_PAREN
590 if (! acceptTokenClass(EHTokRightParen)) {
591 expected(")");
592 return false;
593 }
594
595 return true;
596}
597
LoopDawg6daaa4f2016-06-23 19:13:48 -0600598// template_type
599// : FLOAT
600// | DOUBLE
601// | INT
602// | DWORD
603// | UINT
604// | BOOL
605//
606bool HlslGrammar::acceptTemplateType(TBasicType& basicType)
607{
608 switch (peek()) {
609 case EHTokFloat:
610 basicType = EbtFloat;
611 break;
612 case EHTokDouble:
613 basicType = EbtDouble;
614 break;
615 case EHTokInt:
616 case EHTokDword:
617 basicType = EbtInt;
618 break;
619 case EHTokUint:
620 basicType = EbtUint;
621 break;
622 case EHTokBool:
623 basicType = EbtBool;
624 break;
625 default:
626 return false;
627 }
628
629 advanceToken();
630
631 return true;
632}
633
634// vector_template_type
635// : VECTOR
636// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
637//
638bool HlslGrammar::acceptVectorTemplateType(TType& type)
639{
640 if (! acceptTokenClass(EHTokVector))
641 return false;
642
643 if (! acceptTokenClass(EHTokLeftAngle)) {
644 // in HLSL, 'vector' alone means float4.
645 new(&type) TType(EbtFloat, EvqTemporary, 4);
646 return true;
647 }
648
649 TBasicType basicType;
650 if (! acceptTemplateType(basicType)) {
651 expected("scalar type");
652 return false;
653 }
654
655 // COMMA
656 if (! acceptTokenClass(EHTokComma)) {
657 expected(",");
658 return false;
659 }
660
661 // integer
662 if (! peekTokenClass(EHTokIntConstant)) {
663 expected("literal integer");
664 return false;
665 }
666
667 TIntermTyped* vecSize;
668 if (! acceptLiteral(vecSize))
669 return false;
670
671 const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
672
673 new(&type) TType(basicType, EvqTemporary, vecSizeI);
674
675 if (vecSizeI == 1)
676 type.makeVector();
677
678 if (!acceptTokenClass(EHTokRightAngle)) {
679 expected("right angle bracket");
680 return false;
681 }
682
683 return true;
684}
685
686// matrix_template_type
687// : MATRIX
688// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
689//
690bool HlslGrammar::acceptMatrixTemplateType(TType& type)
691{
692 if (! acceptTokenClass(EHTokMatrix))
693 return false;
694
695 if (! acceptTokenClass(EHTokLeftAngle)) {
696 // in HLSL, 'matrix' alone means float4x4.
697 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
698 return true;
699 }
700
701 TBasicType basicType;
702 if (! acceptTemplateType(basicType)) {
703 expected("scalar type");
704 return false;
705 }
706
707 // COMMA
708 if (! acceptTokenClass(EHTokComma)) {
709 expected(",");
710 return false;
711 }
712
713 // integer rows
714 if (! peekTokenClass(EHTokIntConstant)) {
715 expected("literal integer");
716 return false;
717 }
718
719 TIntermTyped* rows;
720 if (! acceptLiteral(rows))
721 return false;
722
723 // COMMA
724 if (! acceptTokenClass(EHTokComma)) {
725 expected(",");
726 return false;
727 }
728
729 // integer cols
730 if (! peekTokenClass(EHTokIntConstant)) {
731 expected("literal integer");
732 return false;
733 }
734
735 TIntermTyped* cols;
736 if (! acceptLiteral(cols))
737 return false;
738
739 new(&type) TType(basicType, EvqTemporary, 0,
steve-lunarg297ae212016-08-24 14:36:13 -0600740 rows->getAsConstantUnion()->getConstArray()[0].getIConst(),
741 cols->getAsConstantUnion()->getConstArray()[0].getIConst());
LoopDawg6daaa4f2016-06-23 19:13:48 -0600742
743 if (!acceptTokenClass(EHTokRightAngle)) {
744 expected("right angle bracket");
745 return false;
746 }
747
748 return true;
749}
750
John Kessenicha1e2d492016-09-20 13:22:58 -0600751// annotations
752// : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
John Kessenich86f71382016-09-19 20:23:18 -0600753//
John Kessenicha1e2d492016-09-20 13:22:58 -0600754bool HlslGrammar::acceptAnnotations(TQualifier&)
John Kessenich86f71382016-09-19 20:23:18 -0600755{
John Kessenicha1e2d492016-09-20 13:22:58 -0600756 if (! acceptTokenClass(EHTokLeftAngle))
John Kessenich86f71382016-09-19 20:23:18 -0600757 return false;
758
John Kessenicha1e2d492016-09-20 13:22:58 -0600759 // note that we are nesting a name space
760 parseContext.nestAnnotations();
John Kessenich86f71382016-09-19 20:23:18 -0600761
762 // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
763 do {
764 // eat any extra SEMI_COLON; don't know if the grammar calls for this or not
765 while (acceptTokenClass(EHTokSemicolon))
766 ;
767
768 if (acceptTokenClass(EHTokRightAngle))
John Kessenicha1e2d492016-09-20 13:22:58 -0600769 break;
John Kessenich86f71382016-09-19 20:23:18 -0600770
771 // declaration
772 TIntermNode* node;
773 if (! acceptDeclaration(node)) {
John Kessenicha1e2d492016-09-20 13:22:58 -0600774 expected("declaration in annotation");
John Kessenich86f71382016-09-19 20:23:18 -0600775 return false;
776 }
777 } while (true);
John Kessenicha1e2d492016-09-20 13:22:58 -0600778
779 parseContext.unnestAnnotations();
780 return true;
John Kessenich86f71382016-09-19 20:23:18 -0600781}
LoopDawg6daaa4f2016-06-23 19:13:48 -0600782
LoopDawg4886f692016-06-29 10:58:58 -0600783// sampler_type
784// : SAMPLER
785// | SAMPLER1D
786// | SAMPLER2D
787// | SAMPLER3D
788// | SAMPLERCUBE
789// | SAMPLERSTATE
790// | SAMPLERCOMPARISONSTATE
791bool HlslGrammar::acceptSamplerType(TType& type)
792{
793 // read sampler type
794 const EHlslTokenClass samplerType = peek();
795
LoopDawga78b0292016-07-19 14:28:05 -0600796 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -0600797 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -0600798
LoopDawga78b0292016-07-19 14:28:05 -0600799 bool isShadow = false;
800
LoopDawg4886f692016-06-29 10:58:58 -0600801 switch (samplerType) {
802 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -0600803 case EHTokSampler1d: /*dim = Esd1D*/; break;
804 case EHTokSampler2d: /*dim = Esd2D*/; break;
805 case EHTokSampler3d: /*dim = Esd3D*/; break;
806 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -0600807 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -0600808 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -0600809 default:
810 return false; // not a sampler declaration
811 }
812
813 advanceToken(); // consume the sampler type keyword
814
815 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -0600816
817 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -0600818 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -0600819
820 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
821
822 return true;
823}
824
825// texture_type
826// | BUFFER
827// | TEXTURE1D
828// | TEXTURE1DARRAY
829// | TEXTURE2D
830// | TEXTURE2DARRAY
831// | TEXTURE3D
832// | TEXTURECUBE
833// | TEXTURECUBEARRAY
834// | TEXTURE2DMS
835// | TEXTURE2DMSARRAY
steve-lunargbb0183f2016-10-04 16:58:14 -0600836// | RWBUFFER
837// | RWTEXTURE1D
838// | RWTEXTURE1DARRAY
839// | RWTEXTURE2D
840// | RWTEXTURE2DARRAY
841// | RWTEXTURE3D
842
LoopDawg4886f692016-06-29 10:58:58 -0600843bool HlslGrammar::acceptTextureType(TType& type)
844{
845 const EHlslTokenClass textureType = peek();
846
847 TSamplerDim dim = EsdNone;
848 bool array = false;
849 bool ms = false;
steve-lunargbb0183f2016-10-04 16:58:14 -0600850 bool image = false;
LoopDawg4886f692016-06-29 10:58:58 -0600851
852 switch (textureType) {
853 case EHTokBuffer: dim = EsdBuffer; break;
854 case EHTokTexture1d: dim = Esd1D; break;
855 case EHTokTexture1darray: dim = Esd1D; array = true; break;
856 case EHTokTexture2d: dim = Esd2D; break;
857 case EHTokTexture2darray: dim = Esd2D; array = true; break;
858 case EHTokTexture3d: dim = Esd3D; break;
859 case EHTokTextureCube: dim = EsdCube; break;
860 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
861 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
862 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
steve-lunargbb0183f2016-10-04 16:58:14 -0600863 case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
864 case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
865 case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
866 case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
867 case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
868 case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
LoopDawg4886f692016-06-29 10:58:58 -0600869 default:
870 return false; // not a texture declaration
871 }
872
873 advanceToken(); // consume the texture object keyword
874
875 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
876
877 TIntermTyped* msCount = nullptr;
878
steve-lunargbb0183f2016-10-04 16:58:14 -0600879 // texture type: required for multisample types and RWBuffer/RWTextures!
LoopDawg4886f692016-06-29 10:58:58 -0600880 if (acceptTokenClass(EHTokLeftAngle)) {
881 if (! acceptType(txType)) {
882 expected("scalar or vector type");
883 return false;
884 }
885
886 const TBasicType basicRetType = txType.getBasicType() ;
887
888 if (basicRetType != EbtFloat && basicRetType != EbtUint && basicRetType != EbtInt) {
889 unimplemented("basic type in texture");
890 return false;
891 }
892
steve-lunargd53f7172016-07-27 15:46:48 -0600893 // Buffers can handle small mats if they fit in 4 components
894 if (dim == EsdBuffer && txType.isMatrix()) {
895 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
896 expected("components < 4 in matrix buffer type");
897 return false;
898 }
899
900 // TODO: except we don't handle it yet...
901 unimplemented("matrix type in buffer");
902 return false;
903 }
904
LoopDawg4886f692016-06-29 10:58:58 -0600905 if (!txType.isScalar() && !txType.isVector()) {
906 expected("scalar or vector type");
907 return false;
908 }
909
910 if (txType.getVectorSize() != 1 && txType.getVectorSize() != 4) {
911 // TODO: handle vec2/3 types
912 expected("vector size not yet supported in texture type");
913 return false;
914 }
915
916 if (ms && acceptTokenClass(EHTokComma)) {
917 // read sample count for multisample types, if given
918 if (! peekTokenClass(EHTokIntConstant)) {
919 expected("multisample count");
920 return false;
921 }
922
923 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
924 return false;
925 }
926
927 if (! acceptTokenClass(EHTokRightAngle)) {
928 expected("right angle bracket");
929 return false;
930 }
931 } else if (ms) {
932 expected("texture type for multisample");
933 return false;
steve-lunargbb0183f2016-10-04 16:58:14 -0600934 } else if (image) {
935 expected("type for RWTexture/RWBuffer");
936 return false;
LoopDawg4886f692016-06-29 10:58:58 -0600937 }
938
939 TArraySizes* arraySizes = nullptr;
steve-lunargbb0183f2016-10-04 16:58:14 -0600940 const bool shadow = !image && (txType.isScalar() || (txType.isVector() && txType.getVectorSize() == 1));
LoopDawg4886f692016-06-29 10:58:58 -0600941
942 TSampler sampler;
steve-lunargbb0183f2016-10-04 16:58:14 -0600943 TLayoutFormat format = ElfNone;
steve-lunargd53f7172016-07-27 15:46:48 -0600944
steve-lunargbb0183f2016-10-04 16:58:14 -0600945 // RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
946 if (image) {
947 if (txType.getVectorSize() != 4)
948 expected("4 component image");
949
950 switch (txType.getBasicType()) {
951 case EbtFloat: format = ElfRgba32f; break;
952 case EbtInt: format = ElfRgba32i; break;
953 case EbtUint: format = ElfRgba32ui; break;
954 default:
955 expected("unknown basic type in image format");
956 }
957 }
958
959 // Non-image Buffers are combined
960 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -0600961 sampler.set(txType.getBasicType(), dim, array);
962 } else {
963 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -0600964 if (image) {
965 sampler.setImage(txType.getBasicType(), dim, array, shadow, ms);
966 } else {
967 sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms);
968 }
steve-lunargd53f7172016-07-27 15:46:48 -0600969 }
LoopDawg4886f692016-06-29 10:58:58 -0600970
971 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -0600972 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -0600973
974 return true;
975}
976
977
John Kessenich87142c72016-03-12 20:24:24 -0700978// If token is for a type, update 'type' with the type information,
979// and return true and advance.
980// Otherwise, return false, and don't advance
981bool HlslGrammar::acceptType(TType& type)
982{
John Kessenich9c86c6a2016-05-03 22:49:24 -0600983 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600984 case EHTokVector:
985 return acceptVectorTemplateType(type);
986 break;
987
988 case EHTokMatrix:
989 return acceptMatrixTemplateType(type);
990 break;
991
LoopDawg4886f692016-06-29 10:58:58 -0600992 case EHTokSampler: // fall through
993 case EHTokSampler1d: // ...
994 case EHTokSampler2d: // ...
995 case EHTokSampler3d: // ...
996 case EHTokSamplerCube: // ...
997 case EHTokSamplerState: // ...
998 case EHTokSamplerComparisonState: // ...
999 return acceptSamplerType(type);
1000 break;
1001
1002 case EHTokBuffer: // fall through
1003 case EHTokTexture1d: // ...
1004 case EHTokTexture1darray: // ...
1005 case EHTokTexture2d: // ...
1006 case EHTokTexture2darray: // ...
1007 case EHTokTexture3d: // ...
1008 case EHTokTextureCube: // ...
1009 case EHTokTextureCubearray: // ...
1010 case EHTokTexture2DMS: // ...
1011 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001012 case EHTokRWTexture1d: // ...
1013 case EHTokRWTexture1darray: // ...
1014 case EHTokRWTexture2d: // ...
1015 case EHTokRWTexture2darray: // ...
1016 case EHTokRWTexture3d: // ...
1017 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001018 return acceptTextureType(type);
1019 break;
1020
John Kesseniche6e74942016-06-11 16:43:14 -06001021 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001022 case EHTokCBuffer:
1023 case EHTokTBuffer:
John Kesseniche6e74942016-06-11 16:43:14 -06001024 return acceptStruct(type);
1025 break;
1026
1027 case EHTokIdentifier:
1028 // An identifier could be for a user-defined type.
1029 // Note we cache the symbol table lookup, to save for a later rule
1030 // when this is not a type.
1031 token.symbol = parseContext.symbolTable.find(*token.string);
1032 if (token.symbol && token.symbol->getAsVariable() && token.symbol->getAsVariable()->isUserType()) {
1033 type.shallowCopy(token.symbol->getType());
1034 advanceToken();
1035 return true;
1036 } else
1037 return false;
1038
John Kessenich71351de2016-06-08 12:50:56 -06001039 case EHTokVoid:
1040 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001041 break;
John Kessenich71351de2016-06-08 12:50:56 -06001042
John Kessenicha1e2d492016-09-20 13:22:58 -06001043 case EHTokString:
1044 new(&type) TType(EbtString);
1045 break;
1046
John Kessenich87142c72016-03-12 20:24:24 -07001047 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001048 new(&type) TType(EbtFloat);
1049 break;
John Kessenich87142c72016-03-12 20:24:24 -07001050 case EHTokFloat1:
1051 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001052 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001053 break;
John Kessenich87142c72016-03-12 20:24:24 -07001054 case EHTokFloat2:
1055 new(&type) TType(EbtFloat, EvqTemporary, 2);
1056 break;
1057 case EHTokFloat3:
1058 new(&type) TType(EbtFloat, EvqTemporary, 3);
1059 break;
1060 case EHTokFloat4:
1061 new(&type) TType(EbtFloat, EvqTemporary, 4);
1062 break;
1063
John Kessenich71351de2016-06-08 12:50:56 -06001064 case EHTokDouble:
1065 new(&type) TType(EbtDouble);
1066 break;
1067 case EHTokDouble1:
1068 new(&type) TType(EbtDouble);
1069 type.makeVector();
1070 break;
1071 case EHTokDouble2:
1072 new(&type) TType(EbtDouble, EvqTemporary, 2);
1073 break;
1074 case EHTokDouble3:
1075 new(&type) TType(EbtDouble, EvqTemporary, 3);
1076 break;
1077 case EHTokDouble4:
1078 new(&type) TType(EbtDouble, EvqTemporary, 4);
1079 break;
1080
1081 case EHTokInt:
1082 case EHTokDword:
1083 new(&type) TType(EbtInt);
1084 break;
1085 case EHTokInt1:
1086 new(&type) TType(EbtInt);
1087 type.makeVector();
1088 break;
John Kessenich87142c72016-03-12 20:24:24 -07001089 case EHTokInt2:
1090 new(&type) TType(EbtInt, EvqTemporary, 2);
1091 break;
1092 case EHTokInt3:
1093 new(&type) TType(EbtInt, EvqTemporary, 3);
1094 break;
1095 case EHTokInt4:
1096 new(&type) TType(EbtInt, EvqTemporary, 4);
1097 break;
1098
John Kessenich71351de2016-06-08 12:50:56 -06001099 case EHTokUint:
1100 new(&type) TType(EbtUint);
1101 break;
1102 case EHTokUint1:
1103 new(&type) TType(EbtUint);
1104 type.makeVector();
1105 break;
1106 case EHTokUint2:
1107 new(&type) TType(EbtUint, EvqTemporary, 2);
1108 break;
1109 case EHTokUint3:
1110 new(&type) TType(EbtUint, EvqTemporary, 3);
1111 break;
1112 case EHTokUint4:
1113 new(&type) TType(EbtUint, EvqTemporary, 4);
1114 break;
1115
LoopDawg6daaa4f2016-06-23 19:13:48 -06001116
John Kessenich71351de2016-06-08 12:50:56 -06001117 case EHTokBool:
1118 new(&type) TType(EbtBool);
1119 break;
1120 case EHTokBool1:
1121 new(&type) TType(EbtBool);
1122 type.makeVector();
1123 break;
John Kessenich87142c72016-03-12 20:24:24 -07001124 case EHTokBool2:
1125 new(&type) TType(EbtBool, EvqTemporary, 2);
1126 break;
1127 case EHTokBool3:
1128 new(&type) TType(EbtBool, EvqTemporary, 3);
1129 break;
1130 case EHTokBool4:
1131 new(&type) TType(EbtBool, EvqTemporary, 4);
1132 break;
1133
John Kessenich0133c122016-05-20 12:17:26 -06001134 case EHTokInt1x1:
1135 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1136 break;
1137 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001138 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001139 break;
1140 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001141 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001142 break;
1143 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001144 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001145 break;
1146 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001147 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001148 break;
1149 case EHTokInt2x2:
1150 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1151 break;
1152 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001153 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001154 break;
1155 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001156 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001157 break;
1158 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001159 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001160 break;
1161 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001162 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001163 break;
1164 case EHTokInt3x3:
1165 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1166 break;
1167 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001168 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001169 break;
1170 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001171 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001172 break;
1173 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001174 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001175 break;
1176 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001177 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001178 break;
1179 case EHTokInt4x4:
1180 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1181 break;
1182
John Kessenich71351de2016-06-08 12:50:56 -06001183 case EHTokUint1x1:
1184 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1185 break;
1186 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001187 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001188 break;
1189 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001190 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001191 break;
1192 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001193 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001194 break;
1195 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001196 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001197 break;
1198 case EHTokUint2x2:
1199 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1200 break;
1201 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001202 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001203 break;
1204 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001205 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001206 break;
1207 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001208 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001209 break;
1210 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001211 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001212 break;
1213 case EHTokUint3x3:
1214 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1215 break;
1216 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001217 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001218 break;
1219 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001220 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001221 break;
1222 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001223 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001224 break;
1225 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001226 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001227 break;
1228 case EHTokUint4x4:
1229 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1230 break;
1231
1232 case EHTokBool1x1:
1233 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1234 break;
1235 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001236 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001237 break;
1238 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001239 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001240 break;
1241 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001242 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001243 break;
1244 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001245 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001246 break;
1247 case EHTokBool2x2:
1248 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1249 break;
1250 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001251 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001252 break;
1253 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001254 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001255 break;
1256 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001257 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001258 break;
1259 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001260 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001261 break;
1262 case EHTokBool3x3:
1263 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1264 break;
1265 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001266 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001267 break;
1268 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001269 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001270 break;
1271 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001272 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001273 break;
1274 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001275 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001276 break;
1277 case EHTokBool4x4:
1278 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1279 break;
1280
John Kessenich0133c122016-05-20 12:17:26 -06001281 case EHTokFloat1x1:
1282 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1283 break;
1284 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001285 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001286 break;
1287 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001288 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001289 break;
1290 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001291 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001292 break;
1293 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001294 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001295 break;
John Kessenich87142c72016-03-12 20:24:24 -07001296 case EHTokFloat2x2:
1297 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1298 break;
1299 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001300 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001301 break;
1302 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001303 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001304 break;
John Kessenich0133c122016-05-20 12:17:26 -06001305 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001306 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001307 break;
John Kessenich87142c72016-03-12 20:24:24 -07001308 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001309 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001310 break;
1311 case EHTokFloat3x3:
1312 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1313 break;
1314 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001315 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001316 break;
John Kessenich0133c122016-05-20 12:17:26 -06001317 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001318 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001319 break;
John Kessenich87142c72016-03-12 20:24:24 -07001320 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001321 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001322 break;
1323 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001324 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001325 break;
1326 case EHTokFloat4x4:
1327 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1328 break;
1329
John Kessenich0133c122016-05-20 12:17:26 -06001330 case EHTokDouble1x1:
1331 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1332 break;
1333 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001334 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001335 break;
1336 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001337 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001338 break;
1339 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001340 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001341 break;
1342 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001343 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001344 break;
1345 case EHTokDouble2x2:
1346 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1347 break;
1348 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001349 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001350 break;
1351 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001352 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001353 break;
1354 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001355 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001356 break;
1357 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001358 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001359 break;
1360 case EHTokDouble3x3:
1361 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1362 break;
1363 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001364 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001365 break;
1366 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001367 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001368 break;
1369 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001370 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001371 break;
1372 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001373 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001374 break;
1375 case EHTokDouble4x4:
1376 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1377 break;
1378
John Kessenich87142c72016-03-12 20:24:24 -07001379 default:
1380 return false;
1381 }
1382
1383 advanceToken();
1384
1385 return true;
1386}
1387
John Kesseniche6e74942016-06-11 16:43:14 -06001388// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001389// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1390// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1391//
1392// struct_type
1393// : STRUCT
1394// | CBUFFER
1395// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001396//
1397bool HlslGrammar::acceptStruct(TType& type)
1398{
John Kessenichb804de62016-09-05 12:19:18 -06001399 // This storage qualifier will tell us whether it's an AST
1400 // block type or just a generic structure type.
1401 TStorageQualifier storageQualifier = EvqTemporary;
John Kessenich3d157c52016-07-25 16:05:33 -06001402
1403 // CBUFFER
1404 if (acceptTokenClass(EHTokCBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001405 storageQualifier = EvqUniform;
John Kessenich3d157c52016-07-25 16:05:33 -06001406 // TBUFFER
1407 else if (acceptTokenClass(EHTokTBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001408 storageQualifier = EvqBuffer;
John Kesseniche6e74942016-06-11 16:43:14 -06001409 // STRUCT
John Kessenich3d157c52016-07-25 16:05:33 -06001410 else if (! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001411 return false;
1412
1413 // IDENTIFIER
1414 TString structName = "";
1415 if (peekTokenClass(EHTokIdentifier)) {
1416 structName = *token.string;
1417 advanceToken();
1418 }
1419
John Kessenich3d157c52016-07-25 16:05:33 -06001420 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001421 TQualifier postDeclQualifier;
1422 postDeclQualifier.clear();
1423 acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06001424
John Kesseniche6e74942016-06-11 16:43:14 -06001425 // LEFT_BRACE
1426 if (! acceptTokenClass(EHTokLeftBrace)) {
1427 expected("{");
1428 return false;
1429 }
1430
1431 // struct_declaration_list
1432 TTypeList* typeList;
1433 if (! acceptStructDeclarationList(typeList)) {
1434 expected("struct member declarations");
1435 return false;
1436 }
1437
1438 // RIGHT_BRACE
1439 if (! acceptTokenClass(EHTokRightBrace)) {
1440 expected("}");
1441 return false;
1442 }
1443
1444 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001445 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001446 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001447 else {
John Kessenich7735b942016-09-05 12:40:06 -06001448 postDeclQualifier.storage = storageQualifier;
1449 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001450 }
John Kesseniche6e74942016-06-11 16:43:14 -06001451
John Kessenich3d157c52016-07-25 16:05:33 -06001452 // If it was named, which means the type can be reused later, add
1453 // it to the symbol table. (Unless it's a block, in which
1454 // case the name is not a type.)
1455 if (type.getBasicType() != EbtBlock && structName.size() > 0) {
John Kesseniche6e74942016-06-11 16:43:14 -06001456 TVariable* userTypeDef = new TVariable(&structName, type, true);
1457 if (! parseContext.symbolTable.insert(*userTypeDef))
1458 parseContext.error(token.loc, "redefinition", structName.c_str(), "struct");
1459 }
1460
1461 return true;
1462}
1463
1464// struct_declaration_list
1465// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
1466//
1467// struct_declaration
1468// : fully_specified_type struct_declarator COMMA struct_declarator ...
1469//
1470// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06001471// : IDENTIFIER post_decls
1472// | IDENTIFIER array_specifier post_decls
John Kesseniche6e74942016-06-11 16:43:14 -06001473//
1474bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList)
1475{
1476 typeList = new TTypeList();
1477
1478 do {
1479 // success on seeing the RIGHT_BRACE coming up
1480 if (peekTokenClass(EHTokRightBrace))
1481 return true;
1482
1483 // struct_declaration
1484
1485 // fully_specified_type
1486 TType memberType;
1487 if (! acceptFullySpecifiedType(memberType)) {
1488 expected("member type");
1489 return false;
1490 }
1491
1492 // struct_declarator COMMA struct_declarator ...
1493 do {
1494 // peek IDENTIFIER
1495 if (! peekTokenClass(EHTokIdentifier)) {
1496 expected("member name");
1497 return false;
1498 }
1499
1500 // add it to the list of members
1501 TTypeLoc member = { new TType(EbtVoid), token.loc };
1502 member.type->shallowCopy(memberType);
1503 member.type->setFieldName(*token.string);
1504 typeList->push_back(member);
1505
1506 // accept IDENTIFIER
1507 advanceToken();
1508
1509 // array_specifier
John Kessenich19b92ff2016-06-19 11:50:34 -06001510 TArraySizes* arraySizes = nullptr;
1511 acceptArraySpecifier(arraySizes);
1512 if (arraySizes)
1513 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06001514
John Kessenich7735b942016-09-05 12:40:06 -06001515 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06001516
John Kesseniche6e74942016-06-11 16:43:14 -06001517 // success on seeing the SEMICOLON coming up
1518 if (peekTokenClass(EHTokSemicolon))
1519 break;
1520
1521 // COMMA
1522 if (! acceptTokenClass(EHTokComma)) {
1523 expected(",");
1524 return false;
1525 }
1526
1527 } while (true);
1528
1529 // SEMI_COLON
1530 if (! acceptTokenClass(EHTokSemicolon)) {
1531 expected(";");
1532 return false;
1533 }
1534
1535 } while (true);
1536}
1537
John Kessenich5f934b02016-03-13 17:58:25 -06001538// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06001539// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06001540// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001541//
1542bool HlslGrammar::acceptFunctionParameters(TFunction& function)
1543{
John Kessenich078d7f22016-03-14 10:02:11 -06001544 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001545 if (! acceptTokenClass(EHTokLeftParen))
1546 return false;
1547
John Kessenich71351de2016-06-08 12:50:56 -06001548 // VOID RIGHT_PAREN
1549 if (! acceptTokenClass(EHTokVoid)) {
1550 do {
1551 // parameter_declaration
1552 if (! acceptParameterDeclaration(function))
1553 break;
John Kessenich5f934b02016-03-13 17:58:25 -06001554
John Kessenich71351de2016-06-08 12:50:56 -06001555 // COMMA
1556 if (! acceptTokenClass(EHTokComma))
1557 break;
1558 } while (true);
1559 }
John Kessenich5f934b02016-03-13 17:58:25 -06001560
John Kessenich078d7f22016-03-14 10:02:11 -06001561 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001562 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001563 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06001564 return false;
1565 }
1566
1567 return true;
1568}
1569
1570// parameter_declaration
John Kessenichc3387d32016-06-17 14:21:02 -06001571// : fully_specified_type post_decls
John Kessenich19b92ff2016-06-19 11:50:34 -06001572// | fully_specified_type identifier array_specifier post_decls
John Kessenich5f934b02016-03-13 17:58:25 -06001573//
1574bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
1575{
1576 // fully_specified_type
1577 TType* type = new TType;
1578 if (! acceptFullySpecifiedType(*type))
1579 return false;
1580
1581 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06001582 HlslToken idToken;
1583 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06001584
John Kessenich19b92ff2016-06-19 11:50:34 -06001585 // array_specifier
1586 TArraySizes* arraySizes = nullptr;
1587 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06001588 if (arraySizes) {
1589 if (arraySizes->isImplicit()) {
1590 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
1591 return false;
1592 }
1593
John Kessenich19b92ff2016-06-19 11:50:34 -06001594 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06001595 }
John Kessenich19b92ff2016-06-19 11:50:34 -06001596
1597 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001598 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06001599
John Kessenich5aa59e22016-06-17 15:50:47 -06001600 parseContext.paramFix(*type);
1601
John Kessenichaecd4972016-03-14 10:46:34 -06001602 TParameter param = { idToken.string, type };
John Kessenich5f934b02016-03-13 17:58:25 -06001603 function.addParameter(param);
1604
1605 return true;
1606}
1607
1608// Do the work to create the function definition in addition to
1609// parsing the body (compound_statement).
1610bool HlslGrammar::acceptFunctionDefinition(TFunction& function, TIntermNode*& node)
1611{
John Kessenicha3051662016-09-02 19:13:36 -06001612 TFunction& functionDeclarator = parseContext.handleFunctionDeclarator(token.loc, function, false /* not prototype */);
John Kessenich1a4b7752016-09-02 19:05:24 -06001613 TSourceLoc loc = token.loc;
John Kessenich5f934b02016-03-13 17:58:25 -06001614
John Kessenich077e0522016-06-09 02:02:17 -06001615 // This does a pushScope()
John Kessenicha3051662016-09-02 19:13:36 -06001616 node = parseContext.handleFunctionDefinition(loc, functionDeclarator);
John Kessenich5f934b02016-03-13 17:58:25 -06001617
1618 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06001619 TIntermNode* functionBody = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06001620 if (acceptCompoundStatement(functionBody)) {
John Kessenicha3051662016-09-02 19:13:36 -06001621 parseContext.handleFunctionBody(loc, functionDeclarator, functionBody, node);
John Kessenich5f934b02016-03-13 17:58:25 -06001622 return true;
1623 }
1624
1625 return false;
1626}
1627
John Kessenich0d2b6de2016-06-05 11:23:11 -06001628// Accept an expression with parenthesis around it, where
1629// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001630// syntactically required ones like in "if ( expression )".
1631//
1632// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06001633//
1634// Note this one is not set up to be speculative; as it gives
1635// errors if not found.
1636//
1637bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
1638{
1639 // LEFT_PAREN
1640 if (! acceptTokenClass(EHTokLeftParen))
1641 expected("(");
1642
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001643 bool decl = false;
1644 TIntermNode* declNode = nullptr;
1645 decl = acceptControlDeclaration(declNode);
1646 if (decl) {
1647 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
1648 expected("initialized declaration");
1649 return false;
1650 } else
1651 expression = declNode->getAsTyped();
1652 } else {
1653 // no declaration
1654 if (! acceptExpression(expression)) {
1655 expected("expression");
1656 return false;
1657 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06001658 }
1659
1660 // RIGHT_PAREN
1661 if (! acceptTokenClass(EHTokRightParen))
1662 expected(")");
1663
1664 return true;
1665}
1666
John Kessenich34fb0362016-05-03 23:17:20 -06001667// The top-level full expression recognizer.
1668//
John Kessenich87142c72016-03-12 20:24:24 -07001669// expression
John Kessenich34fb0362016-05-03 23:17:20 -06001670// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07001671//
1672bool HlslGrammar::acceptExpression(TIntermTyped*& node)
1673{
LoopDawgef764a22016-06-03 09:17:51 -06001674 node = nullptr;
1675
John Kessenich34fb0362016-05-03 23:17:20 -06001676 // assignment_expression
1677 if (! acceptAssignmentExpression(node))
1678 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001679
John Kessenich34fb0362016-05-03 23:17:20 -06001680 if (! peekTokenClass(EHTokComma))
1681 return true;
1682
1683 do {
1684 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06001685 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06001686 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06001687
John Kessenich34fb0362016-05-03 23:17:20 -06001688 // ... assignment_expression
1689 TIntermTyped* rightNode = nullptr;
1690 if (! acceptAssignmentExpression(rightNode)) {
1691 expected("assignment expression");
1692 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001693 }
1694
John Kessenich34fb0362016-05-03 23:17:20 -06001695 node = intermediate.addComma(node, rightNode, loc);
1696
1697 if (! peekTokenClass(EHTokComma))
1698 return true;
1699 } while (true);
1700}
1701
John Kessenich07354242016-07-01 19:58:06 -06001702// initializer
1703// : LEFT_BRACE initializer_list RIGHT_BRACE
1704//
1705// initializer_list
1706// : assignment_expression COMMA assignment_expression COMMA ...
1707//
1708bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
1709{
1710 // LEFT_BRACE
1711 if (! acceptTokenClass(EHTokLeftBrace))
1712 return false;
1713
1714 // initializer_list
1715 TSourceLoc loc = token.loc;
1716 node = nullptr;
1717 do {
1718 // assignment_expression
1719 TIntermTyped* expr;
1720 if (! acceptAssignmentExpression(expr)) {
1721 expected("assignment expression in initializer list");
1722 return false;
1723 }
1724 node = intermediate.growAggregate(node, expr, loc);
1725
1726 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001727 if (acceptTokenClass(EHTokComma)) {
1728 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
1729 return true;
John Kessenich07354242016-07-01 19:58:06 -06001730 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001731 }
John Kessenich07354242016-07-01 19:58:06 -06001732
1733 // RIGHT_BRACE
1734 if (acceptTokenClass(EHTokRightBrace))
1735 return true;
1736
1737 expected(", or }");
1738 return false;
1739 } while (true);
1740}
1741
John Kessenich34fb0362016-05-03 23:17:20 -06001742// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06001743// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06001744//
1745// a op (b op (c op d))
1746//
1747// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06001748// : initializer
1749// | conditional_expression
1750// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06001751//
1752bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
1753{
John Kessenich07354242016-07-01 19:58:06 -06001754 // initializer
1755 if (peekTokenClass(EHTokLeftBrace)) {
1756 if (acceptInitializer(node))
1757 return true;
1758
1759 expected("initializer");
1760 return false;
1761 }
1762
John Kessenich00957f82016-07-27 10:39:57 -06001763 // conditional_expression
1764 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06001765 return false;
1766
John Kessenich07354242016-07-01 19:58:06 -06001767 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06001768 TOperator assignOp = HlslOpMap::assignment(peek());
1769 if (assignOp == EOpNull)
1770 return true;
1771
John Kessenich00957f82016-07-27 10:39:57 -06001772 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06001773 TSourceLoc loc = token.loc;
1774 advanceToken();
1775
John Kessenich00957f82016-07-27 10:39:57 -06001776 // conditional_expression assign_op conditional_expression ...
1777 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06001778 // gets the right-to-left associativity.
1779 TIntermTyped* rightNode = nullptr;
1780 if (! acceptAssignmentExpression(rightNode)) {
1781 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06001782 return false;
John Kessenich87142c72016-03-12 20:24:24 -07001783 }
1784
John Kessenichd21baed2016-09-16 03:05:12 -06001785 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
John Kessenichfea226b2016-07-28 17:53:56 -06001786 if (node == nullptr) {
1787 parseContext.error(loc, "could not create assignment", "", "");
1788 return false;
1789 }
John Kessenich34fb0362016-05-03 23:17:20 -06001790
1791 if (! peekTokenClass(EHTokComma))
1792 return true;
1793
1794 return true;
1795}
1796
John Kessenich00957f82016-07-27 10:39:57 -06001797// Accept a conditional expression, which associates right-to-left,
1798// accomplished by the "true" expression calling down to lower
1799// precedence levels than this level.
1800//
1801// conditional_expression
1802// : binary_expression
1803// | binary_expression QUESTION expression COLON assignment_expression
1804//
1805bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
1806{
1807 // binary_expression
1808 if (! acceptBinaryExpression(node, PlLogicalOr))
1809 return false;
1810
1811 if (! acceptTokenClass(EHTokQuestion))
1812 return true;
1813
1814 TIntermTyped* trueNode = nullptr;
1815 if (! acceptExpression(trueNode)) {
1816 expected("expression after ?");
1817 return false;
1818 }
1819 TSourceLoc loc = token.loc;
1820
1821 if (! acceptTokenClass(EHTokColon)) {
1822 expected(":");
1823 return false;
1824 }
1825
1826 TIntermTyped* falseNode = nullptr;
1827 if (! acceptAssignmentExpression(falseNode)) {
1828 expected("expression after :");
1829 return false;
1830 }
1831
1832 node = intermediate.addSelection(node, trueNode, falseNode, loc);
1833
1834 return true;
1835}
1836
John Kessenich34fb0362016-05-03 23:17:20 -06001837// Accept a binary expression, for binary operations that
1838// associate left-to-right. This is, it is implicit, for example
1839//
1840// ((a op b) op c) op d
1841//
1842// binary_expression
1843// : expression op expression op expression ...
1844//
1845// where 'expression' is the next higher level in precedence.
1846//
1847bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
1848{
1849 if (precedenceLevel > PlMul)
1850 return acceptUnaryExpression(node);
1851
1852 // assignment_expression
1853 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
1854 return false;
1855
John Kessenich34fb0362016-05-03 23:17:20 -06001856 do {
John Kessenich64076ed2016-07-28 21:43:17 -06001857 TOperator op = HlslOpMap::binary(peek());
1858 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
1859 if (tokenLevel < precedenceLevel)
1860 return true;
1861
John Kessenich34fb0362016-05-03 23:17:20 -06001862 // ... op
1863 TSourceLoc loc = token.loc;
1864 advanceToken();
1865
1866 // ... expression
1867 TIntermTyped* rightNode = nullptr;
1868 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
1869 expected("expression");
1870 return false;
1871 }
1872
1873 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06001874 if (node == nullptr) {
1875 parseContext.error(loc, "Could not perform requested binary operation", "", "");
1876 return false;
1877 }
John Kessenich34fb0362016-05-03 23:17:20 -06001878 } while (true);
1879}
1880
1881// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06001882// : (type) unary_expression
1883// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06001884// | - unary_expression
1885// | ! unary_expression
1886// | ~ unary_expression
1887// | ++ unary_expression
1888// | -- unary_expression
1889// | postfix_expression
1890//
1891bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
1892{
John Kessenich1cc1a282016-06-03 16:55:49 -06001893 // (type) unary_expression
1894 // Have to look two steps ahead, because this could be, e.g., a
1895 // postfix_expression instead, since that also starts with at "(".
1896 if (acceptTokenClass(EHTokLeftParen)) {
1897 TType castType;
1898 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06001899 if (acceptTokenClass(EHTokRightParen)) {
1900 // We've matched "(type)" now, get the expression to cast
1901 TSourceLoc loc = token.loc;
1902 if (! acceptUnaryExpression(node))
1903 return false;
1904
1905 // Hook it up like a constructor
1906 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
1907 if (constructorFunction == nullptr) {
1908 expected("type that can be constructed");
1909 return false;
1910 }
1911 TIntermTyped* arguments = nullptr;
1912 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
1913 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
1914
1915 return true;
1916 } else {
1917 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
1918 // the '(int' part. We must back up twice.
1919 recedeToken();
1920 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06001921 }
John Kessenich1cc1a282016-06-03 16:55:49 -06001922 } else {
1923 // This isn't a type cast, but it still started "(", so if it is a
1924 // unary expression, it can only be a postfix_expression, so try that.
1925 // Back it up first.
1926 recedeToken();
1927 return acceptPostfixExpression(node);
1928 }
1929 }
1930
1931 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06001932 TOperator unaryOp = HlslOpMap::preUnary(peek());
1933
John Kessenich1cc1a282016-06-03 16:55:49 -06001934 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06001935 if (unaryOp == EOpNull)
1936 return acceptPostfixExpression(node);
1937
1938 // op unary_expression
1939 TSourceLoc loc = token.loc;
1940 advanceToken();
1941 if (! acceptUnaryExpression(node))
1942 return false;
1943
1944 // + is a no-op
1945 if (unaryOp == EOpAdd)
1946 return true;
1947
1948 node = intermediate.addUnaryMath(unaryOp, node, loc);
1949
1950 return node != nullptr;
1951}
1952
1953// postfix_expression
1954// : LEFT_PAREN expression RIGHT_PAREN
1955// | literal
1956// | constructor
1957// | identifier
1958// | function_call
1959// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
1960// | postfix_expression DOT IDENTIFIER
1961// | postfix_expression INC_OP
1962// | postfix_expression DEC_OP
1963//
1964bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
1965{
1966 // Not implemented as self-recursive:
1967 // The logical "right recursion" is done with an loop at the end
1968
1969 // idToken will pick up either a variable or a function name in a function call
1970 HlslToken idToken;
1971
John Kessenich21472ae2016-06-04 11:46:33 -06001972 // Find something before the postfix operations, as they can't operate
1973 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07001974 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06001975 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07001976 if (! acceptExpression(node)) {
1977 expected("expression");
1978 return false;
1979 }
1980 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001981 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07001982 return false;
1983 }
John Kessenich34fb0362016-05-03 23:17:20 -06001984 } else if (acceptLiteral(node)) {
1985 // literal (nothing else to do yet), go on to the
1986 } else if (acceptConstructor(node)) {
1987 // constructor (nothing else to do yet)
1988 } else if (acceptIdentifier(idToken)) {
1989 // identifier or function_call name
1990 if (! peekTokenClass(EHTokLeftParen)) {
John Kesseniche6e74942016-06-11 16:43:14 -06001991 node = parseContext.handleVariable(idToken.loc, idToken.symbol, token.string);
John Kessenich34fb0362016-05-03 23:17:20 -06001992 } else if (acceptFunctionCall(idToken, node)) {
1993 // function_call (nothing else to do yet)
1994 } else {
1995 expected("function call arguments");
1996 return false;
1997 }
John Kessenich21472ae2016-06-04 11:46:33 -06001998 } else {
1999 // nothing found, can't post operate
2000 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002001 }
2002
John Kessenich21472ae2016-06-04 11:46:33 -06002003 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06002004 do {
2005 TSourceLoc loc = token.loc;
2006 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07002007
John Kessenich34fb0362016-05-03 23:17:20 -06002008 // Consume only a valid post-unary operator, otherwise we are done.
2009 switch (postOp) {
2010 case EOpIndexDirectStruct:
2011 case EOpIndexIndirect:
2012 case EOpPostIncrement:
2013 case EOpPostDecrement:
2014 advanceToken();
2015 break;
2016 default:
2017 return true;
2018 }
John Kessenich87142c72016-03-12 20:24:24 -07002019
John Kessenich34fb0362016-05-03 23:17:20 -06002020 // We have a valid post-unary operator, process it.
2021 switch (postOp) {
2022 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06002023 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002024 // DOT IDENTIFIER
2025 // includes swizzles and struct members
John Kessenich93a162a2016-06-17 17:16:27 -06002026 HlslToken field;
2027 if (! acceptIdentifier(field)) {
2028 expected("swizzle or member");
2029 return false;
2030 }
LoopDawg4886f692016-06-29 10:58:58 -06002031
2032 TIntermTyped* base = node; // preserve for method function calls
John Kessenich93a162a2016-06-17 17:16:27 -06002033 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06002034
2035 // In the event of a method node, we look for an open paren and accept the function call.
2036 if (node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) {
2037 if (! acceptFunctionCall(field, node, base)) {
2038 expected("function parameters");
2039 return false;
2040 }
2041 }
2042
John Kessenich34fb0362016-05-03 23:17:20 -06002043 break;
John Kessenich93a162a2016-06-17 17:16:27 -06002044 }
John Kessenich34fb0362016-05-03 23:17:20 -06002045 case EOpIndexIndirect:
2046 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002047 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06002048 TIntermTyped* indexNode = nullptr;
2049 if (! acceptExpression(indexNode) ||
2050 ! peekTokenClass(EHTokRightBracket)) {
2051 expected("expression followed by ']'");
2052 return false;
2053 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002054 advanceToken();
2055 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
2056 break;
John Kessenich34fb0362016-05-03 23:17:20 -06002057 }
2058 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002059 // INC_OP
2060 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06002061 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002062 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06002063 node = intermediate.addUnaryMath(postOp, node, loc);
2064 break;
2065 default:
2066 assert(0);
2067 break;
2068 }
2069 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07002070}
2071
John Kessenichd016be12016-03-13 11:24:20 -06002072// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06002073// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06002074//
2075bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
2076{
2077 // type
2078 TType type;
2079 if (acceptType(type)) {
2080 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
2081 if (constructorFunction == nullptr)
2082 return false;
2083
2084 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06002085 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06002086 if (! acceptArguments(constructorFunction, arguments)) {
2087 expected("constructor arguments");
2088 return false;
2089 }
2090
2091 // hook it up
2092 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
2093
2094 return true;
2095 }
2096
2097 return false;
2098}
2099
John Kessenich34fb0362016-05-03 23:17:20 -06002100// The function_call identifier was already recognized, and passed in as idToken.
2101//
2102// function_call
2103// : [idToken] arguments
2104//
LoopDawg4886f692016-06-29 10:58:58 -06002105bool HlslGrammar::acceptFunctionCall(HlslToken idToken, TIntermTyped*& node, TIntermTyped* base)
John Kessenich34fb0362016-05-03 23:17:20 -06002106{
John Kessenich4678ca92016-05-13 09:33:42 -06002107 // arguments
2108 TFunction* function = new TFunction(idToken.string, TType(EbtVoid));
2109 TIntermTyped* arguments = nullptr;
LoopDawg4886f692016-06-29 10:58:58 -06002110
2111 // methods have an implicit first argument of the calling object.
2112 if (base != nullptr)
2113 parseContext.handleFunctionArgument(function, arguments, base);
2114
John Kessenich4678ca92016-05-13 09:33:42 -06002115 if (! acceptArguments(function, arguments))
2116 return false;
2117
2118 node = parseContext.handleFunctionCall(idToken.loc, function, arguments);
2119
2120 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002121}
2122
John Kessenich87142c72016-03-12 20:24:24 -07002123// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002124// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002125//
John Kessenichd016be12016-03-13 11:24:20 -06002126// The arguments are pushed onto the 'function' argument list and
2127// onto the 'arguments' aggregate.
2128//
John Kessenich4678ca92016-05-13 09:33:42 -06002129bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002130{
John Kessenich078d7f22016-03-14 10:02:11 -06002131 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002132 if (! acceptTokenClass(EHTokLeftParen))
2133 return false;
2134
2135 do {
John Kessenichd016be12016-03-13 11:24:20 -06002136 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002137 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002138 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002139 break;
John Kessenichd016be12016-03-13 11:24:20 -06002140
2141 // hook it up
2142 parseContext.handleFunctionArgument(function, arguments, arg);
2143
John Kessenich078d7f22016-03-14 10:02:11 -06002144 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002145 if (! acceptTokenClass(EHTokComma))
2146 break;
2147 } while (true);
2148
John Kessenich078d7f22016-03-14 10:02:11 -06002149 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002150 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002151 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002152 return false;
2153 }
2154
2155 return true;
2156}
2157
2158bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2159{
2160 switch (token.tokenClass) {
2161 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002162 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002163 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002164 case EHTokUintConstant:
2165 node = intermediate.addConstantUnion(token.u, token.loc, true);
2166 break;
John Kessenich87142c72016-03-12 20:24:24 -07002167 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002168 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002169 break;
2170 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002171 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002172 break;
2173 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002174 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002175 break;
John Kessenich86f71382016-09-19 20:23:18 -06002176 case EHTokStringConstant:
2177 node = nullptr;
2178 break;
John Kessenich87142c72016-03-12 20:24:24 -07002179
2180 default:
2181 return false;
2182 }
2183
2184 advanceToken();
2185
2186 return true;
2187}
2188
John Kessenich5f934b02016-03-13 17:58:25 -06002189// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002190// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002191//
John Kessenich21472ae2016-06-04 11:46:33 -06002192bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002193{
John Kessenich21472ae2016-06-04 11:46:33 -06002194 TIntermAggregate* compoundStatement = nullptr;
2195
John Kessenich34fb0362016-05-03 23:17:20 -06002196 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002197 if (! acceptTokenClass(EHTokLeftBrace))
2198 return false;
2199
2200 // statement statement ...
2201 TIntermNode* statement = nullptr;
2202 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002203 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2204 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2205 branch->getFlowOp() == EOpDefault)) {
2206 // hook up individual subsequences within a switch statement
2207 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2208 compoundStatement = nullptr;
2209 } else {
2210 // hook it up to the growing compound statement
2211 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2212 }
John Kessenich5f934b02016-03-13 17:58:25 -06002213 }
John Kessenich34fb0362016-05-03 23:17:20 -06002214 if (compoundStatement)
2215 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06002216
John Kessenich21472ae2016-06-04 11:46:33 -06002217 retStatement = compoundStatement;
2218
John Kessenich34fb0362016-05-03 23:17:20 -06002219 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002220 return acceptTokenClass(EHTokRightBrace);
2221}
2222
John Kessenich0d2b6de2016-06-05 11:23:11 -06002223bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
2224{
2225 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06002226 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06002227 parseContext.popScope();
2228
2229 return result;
2230}
2231
John Kessenich077e0522016-06-09 02:02:17 -06002232bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06002233{
John Kessenich077e0522016-06-09 02:02:17 -06002234 parseContext.pushScope();
2235 bool result = acceptCompoundStatement(statement);
2236 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06002237
2238 return result;
2239}
2240
John Kessenich5f934b02016-03-13 17:58:25 -06002241// statement
John Kessenich21472ae2016-06-04 11:46:33 -06002242// : attributes attributed_statement
2243//
2244// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002245// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002246// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06002247// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06002248// | declaration_statement
2249// | selection_statement
2250// | switch_statement
2251// | case_label
2252// | iteration_statement
2253// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002254//
2255bool HlslGrammar::acceptStatement(TIntermNode*& statement)
2256{
John Kessenich21472ae2016-06-04 11:46:33 -06002257 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06002258
John Kessenich21472ae2016-06-04 11:46:33 -06002259 // attributes
2260 acceptAttributes();
John Kessenich5f934b02016-03-13 17:58:25 -06002261
John Kessenich21472ae2016-06-04 11:46:33 -06002262 // attributed_statement
2263 switch (peek()) {
2264 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06002265 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002266
John Kessenich21472ae2016-06-04 11:46:33 -06002267 case EHTokIf:
2268 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002269
John Kessenich21472ae2016-06-04 11:46:33 -06002270 case EHTokSwitch:
2271 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002272
John Kessenich21472ae2016-06-04 11:46:33 -06002273 case EHTokFor:
2274 case EHTokDo:
2275 case EHTokWhile:
2276 return acceptIterationStatement(statement);
2277
2278 case EHTokContinue:
2279 case EHTokBreak:
2280 case EHTokDiscard:
2281 case EHTokReturn:
2282 return acceptJumpStatement(statement);
2283
2284 case EHTokCase:
2285 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06002286 case EHTokDefault:
2287 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06002288
2289 case EHTokSemicolon:
2290 return acceptTokenClass(EHTokSemicolon);
2291
2292 case EHTokRightBrace:
2293 // Performance: not strictly necessary, but stops a bunch of hunting early,
2294 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06002295 return false;
2296
John Kessenich21472ae2016-06-04 11:46:33 -06002297 default:
2298 {
2299 // declaration
2300 if (acceptDeclaration(statement))
2301 return true;
2302
2303 // expression
2304 TIntermTyped* node;
2305 if (acceptExpression(node))
2306 statement = node;
2307 else
2308 return false;
2309
2310 // SEMICOLON (following an expression)
2311 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002312 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06002313 return false;
2314 }
2315 }
2316 }
2317
John Kessenich5f934b02016-03-13 17:58:25 -06002318 return true;
John Kessenich87142c72016-03-12 20:24:24 -07002319}
2320
John Kessenich21472ae2016-06-04 11:46:33 -06002321// attributes
2322// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
2323//
2324// attribute:
2325// : UNROLL
2326// | UNROLL LEFT_PAREN literal RIGHT_PAREN
2327// | FASTOPT
2328// | ALLOW_UAV_CONDITION
2329// | BRANCH
2330// | FLATTEN
2331// | FORCECASE
2332// | CALL
2333//
2334void HlslGrammar::acceptAttributes()
2335{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002336 // For now, accept the [ XXX(X) ] syntax, but drop.
2337 // TODO: subset to correct set? Pass on?
2338 do {
2339 // LEFT_BRACKET?
2340 if (! acceptTokenClass(EHTokLeftBracket))
2341 return;
2342
2343 // attribute
2344 if (peekTokenClass(EHTokIdentifier)) {
2345 // 'token.string' is the attribute
2346 advanceToken();
2347 } else if (! peekTokenClass(EHTokRightBracket)) {
2348 expected("identifier");
2349 advanceToken();
2350 }
2351
2352 // (x)
2353 if (acceptTokenClass(EHTokLeftParen)) {
2354 TIntermTyped* node;
2355 if (! acceptLiteral(node))
2356 expected("literal");
2357 // 'node' has the literal in it
2358 if (! acceptTokenClass(EHTokRightParen))
2359 expected(")");
2360 }
2361
2362 // RIGHT_BRACKET
2363 if (acceptTokenClass(EHTokRightBracket))
2364 continue;
2365
2366 expected("]");
2367 return;
2368
2369 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06002370}
2371
John Kessenich0d2b6de2016-06-05 11:23:11 -06002372// selection_statement
2373// : IF LEFT_PAREN expression RIGHT_PAREN statement
2374// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
2375//
John Kessenich21472ae2016-06-04 11:46:33 -06002376bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
2377{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002378 TSourceLoc loc = token.loc;
2379
2380 // IF
2381 if (! acceptTokenClass(EHTokIf))
2382 return false;
2383
2384 // so that something declared in the condition is scoped to the lifetimes
2385 // of the then-else statements
2386 parseContext.pushScope();
2387
2388 // LEFT_PAREN expression RIGHT_PAREN
2389 TIntermTyped* condition;
2390 if (! acceptParenExpression(condition))
2391 return false;
2392
2393 // create the child statements
2394 TIntermNodePair thenElse = { nullptr, nullptr };
2395
2396 // then statement
2397 if (! acceptScopedStatement(thenElse.node1)) {
2398 expected("then statement");
2399 return false;
2400 }
2401
2402 // ELSE
2403 if (acceptTokenClass(EHTokElse)) {
2404 // else statement
2405 if (! acceptScopedStatement(thenElse.node2)) {
2406 expected("else statement");
2407 return false;
2408 }
2409 }
2410
2411 // Put the pieces together
2412 statement = intermediate.addSelection(condition, thenElse, loc);
2413 parseContext.popScope();
2414
2415 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002416}
2417
John Kessenichd02dc5d2016-07-01 00:04:11 -06002418// switch_statement
2419// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
2420//
John Kessenich21472ae2016-06-04 11:46:33 -06002421bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
2422{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002423 // SWITCH
2424 TSourceLoc loc = token.loc;
2425 if (! acceptTokenClass(EHTokSwitch))
2426 return false;
2427
2428 // LEFT_PAREN expression RIGHT_PAREN
2429 parseContext.pushScope();
2430 TIntermTyped* switchExpression;
2431 if (! acceptParenExpression(switchExpression)) {
2432 parseContext.popScope();
2433 return false;
2434 }
2435
2436 // compound_statement
2437 parseContext.pushSwitchSequence(new TIntermSequence);
2438 bool statementOkay = acceptCompoundStatement(statement);
2439 if (statementOkay)
2440 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
2441
2442 parseContext.popSwitchSequence();
2443 parseContext.popScope();
2444
2445 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06002446}
2447
John Kessenich119f8f62016-06-05 15:44:07 -06002448// iteration_statement
2449// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
2450// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
2451// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
2452//
2453// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06002454bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
2455{
John Kessenich119f8f62016-06-05 15:44:07 -06002456 TSourceLoc loc = token.loc;
2457 TIntermTyped* condition = nullptr;
2458
2459 EHlslTokenClass loop = peek();
2460 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
2461
2462 // WHILE or DO or FOR
2463 advanceToken();
2464
2465 switch (loop) {
2466 case EHTokWhile:
2467 // so that something declared in the condition is scoped to the lifetime
2468 // of the while sub-statement
2469 parseContext.pushScope();
2470 parseContext.nestLooping();
2471
2472 // LEFT_PAREN condition RIGHT_PAREN
2473 if (! acceptParenExpression(condition))
2474 return false;
2475
2476 // statement
2477 if (! acceptScopedStatement(statement)) {
2478 expected("while sub-statement");
2479 return false;
2480 }
2481
2482 parseContext.unnestLooping();
2483 parseContext.popScope();
2484
2485 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
2486
2487 return true;
2488
2489 case EHTokDo:
2490 parseContext.nestLooping();
2491
2492 if (! acceptTokenClass(EHTokLeftBrace))
2493 expected("{");
2494
2495 // statement
2496 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
2497 expected("do sub-statement");
2498 return false;
2499 }
2500
2501 if (! acceptTokenClass(EHTokRightBrace))
2502 expected("}");
2503
2504 // WHILE
2505 if (! acceptTokenClass(EHTokWhile)) {
2506 expected("while");
2507 return false;
2508 }
2509
2510 // LEFT_PAREN condition RIGHT_PAREN
2511 TIntermTyped* condition;
2512 if (! acceptParenExpression(condition))
2513 return false;
2514
2515 if (! acceptTokenClass(EHTokSemicolon))
2516 expected(";");
2517
2518 parseContext.unnestLooping();
2519
2520 statement = intermediate.addLoop(statement, condition, 0, false, loc);
2521
2522 return true;
2523
2524 case EHTokFor:
2525 {
2526 // LEFT_PAREN
2527 if (! acceptTokenClass(EHTokLeftParen))
2528 expected("(");
2529
2530 // so that something declared in the condition is scoped to the lifetime
2531 // of the for sub-statement
2532 parseContext.pushScope();
2533
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002534 // initializer
2535 TIntermNode* initNode = nullptr;
2536 if (! acceptControlDeclaration(initNode)) {
2537 TIntermTyped* initExpr = nullptr;
2538 acceptExpression(initExpr);
2539 initNode = initExpr;
2540 }
2541 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06002542 if (! acceptTokenClass(EHTokSemicolon))
2543 expected(";");
2544
2545 parseContext.nestLooping();
2546
2547 // condition SEMI_COLON
2548 acceptExpression(condition);
2549 if (! acceptTokenClass(EHTokSemicolon))
2550 expected(";");
2551
2552 // iterator SEMI_COLON
2553 TIntermTyped* iterator = nullptr;
2554 acceptExpression(iterator);
2555 if (! acceptTokenClass(EHTokRightParen))
2556 expected(")");
2557
2558 // statement
2559 if (! acceptScopedStatement(statement)) {
2560 expected("for sub-statement");
2561 return false;
2562 }
2563
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002564 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06002565
2566 parseContext.popScope();
2567 parseContext.unnestLooping();
2568
2569 return true;
2570 }
2571
2572 default:
2573 return false;
2574 }
John Kessenich21472ae2016-06-04 11:46:33 -06002575}
2576
2577// jump_statement
2578// : CONTINUE SEMICOLON
2579// | BREAK SEMICOLON
2580// | DISCARD SEMICOLON
2581// | RETURN SEMICOLON
2582// | RETURN expression SEMICOLON
2583//
2584bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
2585{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002586 EHlslTokenClass jump = peek();
2587 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06002588 case EHTokContinue:
2589 case EHTokBreak:
2590 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06002591 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002592 advanceToken();
2593 break;
John Kessenich21472ae2016-06-04 11:46:33 -06002594 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002595 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06002596 return false;
2597 }
John Kessenich21472ae2016-06-04 11:46:33 -06002598
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002599 switch (jump) {
2600 case EHTokContinue:
2601 statement = intermediate.addBranch(EOpContinue, token.loc);
2602 break;
2603 case EHTokBreak:
2604 statement = intermediate.addBranch(EOpBreak, token.loc);
2605 break;
2606 case EHTokDiscard:
2607 statement = intermediate.addBranch(EOpKill, token.loc);
2608 break;
2609
2610 case EHTokReturn:
2611 {
2612 // expression
2613 TIntermTyped* node;
2614 if (acceptExpression(node)) {
2615 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06002616 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002617 } else
2618 statement = intermediate.addBranch(EOpReturn, token.loc);
2619 break;
2620 }
2621
2622 default:
2623 assert(0);
2624 return false;
2625 }
2626
2627 // SEMICOLON
2628 if (! acceptTokenClass(EHTokSemicolon))
2629 expected(";");
2630
2631 return true;
2632}
John Kessenich21472ae2016-06-04 11:46:33 -06002633
John Kessenichd02dc5d2016-07-01 00:04:11 -06002634// case_label
2635// : CASE expression COLON
2636//
John Kessenich21472ae2016-06-04 11:46:33 -06002637bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
2638{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002639 TSourceLoc loc = token.loc;
2640 if (! acceptTokenClass(EHTokCase))
2641 return false;
2642
2643 TIntermTyped* expression;
2644 if (! acceptExpression(expression)) {
2645 expected("case expression");
2646 return false;
2647 }
2648
2649 if (! acceptTokenClass(EHTokColon)) {
2650 expected(":");
2651 return false;
2652 }
2653
2654 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
2655
2656 return true;
2657}
2658
2659// default_label
2660// : DEFAULT COLON
2661//
2662bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
2663{
2664 TSourceLoc loc = token.loc;
2665 if (! acceptTokenClass(EHTokDefault))
2666 return false;
2667
2668 if (! acceptTokenClass(EHTokColon)) {
2669 expected(":");
2670 return false;
2671 }
2672
2673 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
2674
2675 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002676}
2677
John Kessenich19b92ff2016-06-19 11:50:34 -06002678// array_specifier
2679// : LEFT_BRACKET integer_expression RGHT_BRACKET post_decls // optional
steve-lunarg265c0612016-09-27 10:57:35 -06002680// : LEFT_BRACKET RGHT_BRACKET post_decls // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06002681//
2682void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
2683{
2684 arraySizes = nullptr;
2685
2686 if (! acceptTokenClass(EHTokLeftBracket))
2687 return;
2688
2689 TSourceLoc loc = token.loc;
steve-lunarg265c0612016-09-27 10:57:35 -06002690 TIntermTyped* sizeExpr = nullptr;
2691
2692 // Array sizing expression is optional. If ommitted, array is implicitly sized.
2693 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
John Kessenich19b92ff2016-06-19 11:50:34 -06002694
2695 if (! acceptTokenClass(EHTokRightBracket)) {
2696 expected("]");
2697 return;
2698 }
2699
John Kessenich19b92ff2016-06-19 11:50:34 -06002700 arraySizes = new TArraySizes;
steve-lunarg265c0612016-09-27 10:57:35 -06002701
2702 if (hasArraySize) {
2703 TArraySize arraySize;
2704 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
2705 arraySizes->addInnerSize(arraySize);
2706 } else {
2707 arraySizes->addInnerSize(); // implicitly sized
2708 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002709}
2710
John Kessenich630dd7d2016-06-12 23:52:12 -06002711// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06002712// : COLON semantic // optional
2713// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
2714// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06002715// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06002716// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06002717//
John Kessenich7735b942016-09-05 12:40:06 -06002718void HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06002719{
John Kessenich630dd7d2016-06-12 23:52:12 -06002720 do {
2721 // COLON
2722 if (acceptTokenClass(EHTokColon)) {
2723 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06002724 if (peekTokenClass(EHTokLayout))
2725 acceptLayoutQualifierList(qualifier);
2726 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06002727 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002728 if (! acceptTokenClass(EHTokLeftParen)) {
2729 expected("(");
2730 return;
2731 }
John Kessenich82d6baf2016-07-29 13:03:05 -06002732 HlslToken locationToken;
2733 if (! acceptIdentifier(locationToken)) {
2734 expected("c[subcomponent][.component]");
2735 return;
2736 }
2737 HlslToken componentToken;
2738 if (acceptTokenClass(EHTokDot)) {
2739 if (! acceptIdentifier(componentToken)) {
2740 expected("component");
2741 return;
2742 }
2743 }
John Kessenich630dd7d2016-06-12 23:52:12 -06002744 if (! acceptTokenClass(EHTokRightParen)) {
2745 expected(")");
2746 break;
2747 }
John Kessenich7735b942016-09-05 12:40:06 -06002748 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002749 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06002750 expected("layout, semantic, packoffset, or register");
John Kessenich630dd7d2016-06-12 23:52:12 -06002751 return;
2752 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06002753 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
2754 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002755 if (! acceptTokenClass(EHTokLeftParen)) {
2756 expected("(");
2757 return;
2758 }
John Kessenichb38f0712016-07-30 10:29:54 -06002759 HlslToken registerDesc; // for Type#
2760 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06002761 if (! acceptIdentifier(registerDesc)) {
2762 expected("register number description");
2763 return;
2764 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06002765 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
2766 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06002767 // Then we didn't really see the registerDesc yet, it was
2768 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06002769 profile = registerDesc;
2770 if (! acceptIdentifier(registerDesc)) {
2771 expected("register number description");
2772 return;
2773 }
2774 }
John Kessenichb38f0712016-07-30 10:29:54 -06002775 int subComponent = 0;
2776 if (acceptTokenClass(EHTokLeftBracket)) {
2777 // LEFT_BRACKET subcomponent RIGHT_BRACKET
2778 if (! peekTokenClass(EHTokIntConstant)) {
2779 expected("literal integer");
2780 return;
2781 }
2782 subComponent = token.i;
2783 advanceToken();
2784 if (! acceptTokenClass(EHTokRightBracket)) {
2785 expected("]");
2786 break;
2787 }
2788 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06002789 // (COMMA SPACEN)opt
2790 HlslToken spaceDesc;
2791 if (acceptTokenClass(EHTokComma)) {
2792 if (! acceptIdentifier(spaceDesc)) {
2793 expected ("space identifier");
2794 return;
2795 }
2796 }
2797 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002798 if (! acceptTokenClass(EHTokRightParen)) {
2799 expected(")");
2800 break;
2801 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06002802 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002803 } else {
2804 // semantic, in idToken.string
John Kessenich7735b942016-09-05 12:40:06 -06002805 parseContext.handleSemantic(idToken.loc, qualifier, *idToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002806 }
John Kessenicha1e2d492016-09-20 13:22:58 -06002807 } else if (peekTokenClass(EHTokLeftAngle))
2808 acceptAnnotations(qualifier);
2809 else
John Kessenich630dd7d2016-06-12 23:52:12 -06002810 break;
John Kessenich078d7f22016-03-14 10:02:11 -06002811
John Kessenich630dd7d2016-06-12 23:52:12 -06002812 } while (true);
John Kessenich078d7f22016-03-14 10:02:11 -06002813}
2814
John Kesseniche01a9bc2016-03-12 20:11:22 -07002815} // end namespace glslang