blob: c9c4716e2b07f35ab3a97b09ad59200e0f482da7 [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
steve-lunarg4f2da272016-10-10 15:24:57 -0600910 // if (txType.getVectorSize() != 1 && txType.getVectorSize() != 4 && !image) {
911 // // TODO: handle vec2/3 types
912 // expected("vector size not yet supported in texture type");
913 // return false;
914 // }
LoopDawg4886f692016-06-29 10:58:58 -0600915
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-lunarg4f2da272016-10-10 15:24:57 -0600940 const bool shadow = false; // declared on the sampler
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-lunarg4f2da272016-10-10 15:24:57 -0600945 // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
946 if (image || dim == EsdBuffer)
947 format = parseContext.getLayoutFromTxType(token.loc, txType);
steve-lunargbb0183f2016-10-04 16:58:14 -0600948
949 // Non-image Buffers are combined
950 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -0600951 sampler.set(txType.getBasicType(), dim, array);
952 } else {
953 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -0600954 if (image) {
955 sampler.setImage(txType.getBasicType(), dim, array, shadow, ms);
956 } else {
957 sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms);
958 }
steve-lunargd53f7172016-07-27 15:46:48 -0600959 }
LoopDawg4886f692016-06-29 10:58:58 -0600960
961 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -0600962 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -0600963
964 return true;
965}
966
967
John Kessenich87142c72016-03-12 20:24:24 -0700968// If token is for a type, update 'type' with the type information,
969// and return true and advance.
970// Otherwise, return false, and don't advance
971bool HlslGrammar::acceptType(TType& type)
972{
John Kessenich9c86c6a2016-05-03 22:49:24 -0600973 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600974 case EHTokVector:
975 return acceptVectorTemplateType(type);
976 break;
977
978 case EHTokMatrix:
979 return acceptMatrixTemplateType(type);
980 break;
981
LoopDawg4886f692016-06-29 10:58:58 -0600982 case EHTokSampler: // fall through
983 case EHTokSampler1d: // ...
984 case EHTokSampler2d: // ...
985 case EHTokSampler3d: // ...
986 case EHTokSamplerCube: // ...
987 case EHTokSamplerState: // ...
988 case EHTokSamplerComparisonState: // ...
989 return acceptSamplerType(type);
990 break;
991
992 case EHTokBuffer: // fall through
993 case EHTokTexture1d: // ...
994 case EHTokTexture1darray: // ...
995 case EHTokTexture2d: // ...
996 case EHTokTexture2darray: // ...
997 case EHTokTexture3d: // ...
998 case EHTokTextureCube: // ...
999 case EHTokTextureCubearray: // ...
1000 case EHTokTexture2DMS: // ...
1001 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001002 case EHTokRWTexture1d: // ...
1003 case EHTokRWTexture1darray: // ...
1004 case EHTokRWTexture2d: // ...
1005 case EHTokRWTexture2darray: // ...
1006 case EHTokRWTexture3d: // ...
1007 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001008 return acceptTextureType(type);
1009 break;
1010
John Kesseniche6e74942016-06-11 16:43:14 -06001011 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001012 case EHTokCBuffer:
1013 case EHTokTBuffer:
John Kesseniche6e74942016-06-11 16:43:14 -06001014 return acceptStruct(type);
1015 break;
1016
1017 case EHTokIdentifier:
1018 // An identifier could be for a user-defined type.
1019 // Note we cache the symbol table lookup, to save for a later rule
1020 // when this is not a type.
1021 token.symbol = parseContext.symbolTable.find(*token.string);
1022 if (token.symbol && token.symbol->getAsVariable() && token.symbol->getAsVariable()->isUserType()) {
1023 type.shallowCopy(token.symbol->getType());
1024 advanceToken();
1025 return true;
1026 } else
1027 return false;
1028
John Kessenich71351de2016-06-08 12:50:56 -06001029 case EHTokVoid:
1030 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001031 break;
John Kessenich71351de2016-06-08 12:50:56 -06001032
John Kessenicha1e2d492016-09-20 13:22:58 -06001033 case EHTokString:
1034 new(&type) TType(EbtString);
1035 break;
1036
John Kessenich87142c72016-03-12 20:24:24 -07001037 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001038 new(&type) TType(EbtFloat);
1039 break;
John Kessenich87142c72016-03-12 20:24:24 -07001040 case EHTokFloat1:
1041 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001042 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001043 break;
John Kessenich87142c72016-03-12 20:24:24 -07001044 case EHTokFloat2:
1045 new(&type) TType(EbtFloat, EvqTemporary, 2);
1046 break;
1047 case EHTokFloat3:
1048 new(&type) TType(EbtFloat, EvqTemporary, 3);
1049 break;
1050 case EHTokFloat4:
1051 new(&type) TType(EbtFloat, EvqTemporary, 4);
1052 break;
1053
John Kessenich71351de2016-06-08 12:50:56 -06001054 case EHTokDouble:
1055 new(&type) TType(EbtDouble);
1056 break;
1057 case EHTokDouble1:
1058 new(&type) TType(EbtDouble);
1059 type.makeVector();
1060 break;
1061 case EHTokDouble2:
1062 new(&type) TType(EbtDouble, EvqTemporary, 2);
1063 break;
1064 case EHTokDouble3:
1065 new(&type) TType(EbtDouble, EvqTemporary, 3);
1066 break;
1067 case EHTokDouble4:
1068 new(&type) TType(EbtDouble, EvqTemporary, 4);
1069 break;
1070
1071 case EHTokInt:
1072 case EHTokDword:
1073 new(&type) TType(EbtInt);
1074 break;
1075 case EHTokInt1:
1076 new(&type) TType(EbtInt);
1077 type.makeVector();
1078 break;
John Kessenich87142c72016-03-12 20:24:24 -07001079 case EHTokInt2:
1080 new(&type) TType(EbtInt, EvqTemporary, 2);
1081 break;
1082 case EHTokInt3:
1083 new(&type) TType(EbtInt, EvqTemporary, 3);
1084 break;
1085 case EHTokInt4:
1086 new(&type) TType(EbtInt, EvqTemporary, 4);
1087 break;
1088
John Kessenich71351de2016-06-08 12:50:56 -06001089 case EHTokUint:
1090 new(&type) TType(EbtUint);
1091 break;
1092 case EHTokUint1:
1093 new(&type) TType(EbtUint);
1094 type.makeVector();
1095 break;
1096 case EHTokUint2:
1097 new(&type) TType(EbtUint, EvqTemporary, 2);
1098 break;
1099 case EHTokUint3:
1100 new(&type) TType(EbtUint, EvqTemporary, 3);
1101 break;
1102 case EHTokUint4:
1103 new(&type) TType(EbtUint, EvqTemporary, 4);
1104 break;
1105
LoopDawg6daaa4f2016-06-23 19:13:48 -06001106
John Kessenich71351de2016-06-08 12:50:56 -06001107 case EHTokBool:
1108 new(&type) TType(EbtBool);
1109 break;
1110 case EHTokBool1:
1111 new(&type) TType(EbtBool);
1112 type.makeVector();
1113 break;
John Kessenich87142c72016-03-12 20:24:24 -07001114 case EHTokBool2:
1115 new(&type) TType(EbtBool, EvqTemporary, 2);
1116 break;
1117 case EHTokBool3:
1118 new(&type) TType(EbtBool, EvqTemporary, 3);
1119 break;
1120 case EHTokBool4:
1121 new(&type) TType(EbtBool, EvqTemporary, 4);
1122 break;
1123
John Kessenich0133c122016-05-20 12:17:26 -06001124 case EHTokInt1x1:
1125 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1126 break;
1127 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001128 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001129 break;
1130 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001131 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001132 break;
1133 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001134 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001135 break;
1136 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001137 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001138 break;
1139 case EHTokInt2x2:
1140 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1141 break;
1142 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001143 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001144 break;
1145 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001146 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001147 break;
1148 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001149 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001150 break;
1151 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001152 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001153 break;
1154 case EHTokInt3x3:
1155 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1156 break;
1157 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001158 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001159 break;
1160 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001161 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001162 break;
1163 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001164 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001165 break;
1166 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001167 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001168 break;
1169 case EHTokInt4x4:
1170 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1171 break;
1172
John Kessenich71351de2016-06-08 12:50:56 -06001173 case EHTokUint1x1:
1174 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1175 break;
1176 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001177 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001178 break;
1179 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001180 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001181 break;
1182 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001183 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001184 break;
1185 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001186 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001187 break;
1188 case EHTokUint2x2:
1189 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1190 break;
1191 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001192 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001193 break;
1194 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001195 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001196 break;
1197 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001198 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001199 break;
1200 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001201 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001202 break;
1203 case EHTokUint3x3:
1204 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1205 break;
1206 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001207 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001208 break;
1209 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001210 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001211 break;
1212 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001213 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001214 break;
1215 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001216 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001217 break;
1218 case EHTokUint4x4:
1219 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1220 break;
1221
1222 case EHTokBool1x1:
1223 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1224 break;
1225 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001226 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001227 break;
1228 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001229 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001230 break;
1231 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001232 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001233 break;
1234 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001235 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001236 break;
1237 case EHTokBool2x2:
1238 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1239 break;
1240 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001241 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001242 break;
1243 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001244 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001245 break;
1246 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001247 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001248 break;
1249 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001250 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001251 break;
1252 case EHTokBool3x3:
1253 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1254 break;
1255 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001256 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001257 break;
1258 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001259 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001260 break;
1261 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001262 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001263 break;
1264 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001265 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001266 break;
1267 case EHTokBool4x4:
1268 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1269 break;
1270
John Kessenich0133c122016-05-20 12:17:26 -06001271 case EHTokFloat1x1:
1272 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1273 break;
1274 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001275 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001276 break;
1277 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001278 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001279 break;
1280 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001281 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001282 break;
1283 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001284 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001285 break;
John Kessenich87142c72016-03-12 20:24:24 -07001286 case EHTokFloat2x2:
1287 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1288 break;
1289 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001290 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001291 break;
1292 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001293 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001294 break;
John Kessenich0133c122016-05-20 12:17:26 -06001295 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001296 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001297 break;
John Kessenich87142c72016-03-12 20:24:24 -07001298 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001299 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001300 break;
1301 case EHTokFloat3x3:
1302 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1303 break;
1304 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001305 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001306 break;
John Kessenich0133c122016-05-20 12:17:26 -06001307 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001308 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001309 break;
John Kessenich87142c72016-03-12 20:24:24 -07001310 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001311 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001312 break;
1313 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001314 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001315 break;
1316 case EHTokFloat4x4:
1317 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1318 break;
1319
John Kessenich0133c122016-05-20 12:17:26 -06001320 case EHTokDouble1x1:
1321 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1322 break;
1323 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001324 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001325 break;
1326 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001327 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001328 break;
1329 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001330 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001331 break;
1332 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001333 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001334 break;
1335 case EHTokDouble2x2:
1336 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1337 break;
1338 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001339 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001340 break;
1341 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001342 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001343 break;
1344 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001345 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001346 break;
1347 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001348 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001349 break;
1350 case EHTokDouble3x3:
1351 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1352 break;
1353 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001354 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001355 break;
1356 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001357 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001358 break;
1359 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001360 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001361 break;
1362 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001363 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001364 break;
1365 case EHTokDouble4x4:
1366 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1367 break;
1368
John Kessenich87142c72016-03-12 20:24:24 -07001369 default:
1370 return false;
1371 }
1372
1373 advanceToken();
1374
1375 return true;
1376}
1377
John Kesseniche6e74942016-06-11 16:43:14 -06001378// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001379// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1380// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1381//
1382// struct_type
1383// : STRUCT
1384// | CBUFFER
1385// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001386//
1387bool HlslGrammar::acceptStruct(TType& type)
1388{
John Kessenichb804de62016-09-05 12:19:18 -06001389 // This storage qualifier will tell us whether it's an AST
1390 // block type or just a generic structure type.
1391 TStorageQualifier storageQualifier = EvqTemporary;
John Kessenich3d157c52016-07-25 16:05:33 -06001392
1393 // CBUFFER
1394 if (acceptTokenClass(EHTokCBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001395 storageQualifier = EvqUniform;
John Kessenich3d157c52016-07-25 16:05:33 -06001396 // TBUFFER
1397 else if (acceptTokenClass(EHTokTBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001398 storageQualifier = EvqBuffer;
John Kesseniche6e74942016-06-11 16:43:14 -06001399 // STRUCT
John Kessenich3d157c52016-07-25 16:05:33 -06001400 else if (! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001401 return false;
1402
1403 // IDENTIFIER
1404 TString structName = "";
1405 if (peekTokenClass(EHTokIdentifier)) {
1406 structName = *token.string;
1407 advanceToken();
1408 }
1409
John Kessenich3d157c52016-07-25 16:05:33 -06001410 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001411 TQualifier postDeclQualifier;
1412 postDeclQualifier.clear();
1413 acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06001414
John Kesseniche6e74942016-06-11 16:43:14 -06001415 // LEFT_BRACE
1416 if (! acceptTokenClass(EHTokLeftBrace)) {
1417 expected("{");
1418 return false;
1419 }
1420
1421 // struct_declaration_list
1422 TTypeList* typeList;
1423 if (! acceptStructDeclarationList(typeList)) {
1424 expected("struct member declarations");
1425 return false;
1426 }
1427
1428 // RIGHT_BRACE
1429 if (! acceptTokenClass(EHTokRightBrace)) {
1430 expected("}");
1431 return false;
1432 }
1433
1434 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001435 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001436 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001437 else {
John Kessenich7735b942016-09-05 12:40:06 -06001438 postDeclQualifier.storage = storageQualifier;
1439 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001440 }
John Kesseniche6e74942016-06-11 16:43:14 -06001441
John Kessenich3d157c52016-07-25 16:05:33 -06001442 // If it was named, which means the type can be reused later, add
1443 // it to the symbol table. (Unless it's a block, in which
1444 // case the name is not a type.)
1445 if (type.getBasicType() != EbtBlock && structName.size() > 0) {
John Kesseniche6e74942016-06-11 16:43:14 -06001446 TVariable* userTypeDef = new TVariable(&structName, type, true);
1447 if (! parseContext.symbolTable.insert(*userTypeDef))
1448 parseContext.error(token.loc, "redefinition", structName.c_str(), "struct");
1449 }
1450
1451 return true;
1452}
1453
1454// struct_declaration_list
1455// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
1456//
1457// struct_declaration
1458// : fully_specified_type struct_declarator COMMA struct_declarator ...
1459//
1460// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06001461// : IDENTIFIER post_decls
1462// | IDENTIFIER array_specifier post_decls
John Kesseniche6e74942016-06-11 16:43:14 -06001463//
1464bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList)
1465{
1466 typeList = new TTypeList();
1467
1468 do {
1469 // success on seeing the RIGHT_BRACE coming up
1470 if (peekTokenClass(EHTokRightBrace))
1471 return true;
1472
1473 // struct_declaration
1474
1475 // fully_specified_type
1476 TType memberType;
1477 if (! acceptFullySpecifiedType(memberType)) {
1478 expected("member type");
1479 return false;
1480 }
1481
1482 // struct_declarator COMMA struct_declarator ...
1483 do {
1484 // peek IDENTIFIER
1485 if (! peekTokenClass(EHTokIdentifier)) {
1486 expected("member name");
1487 return false;
1488 }
1489
1490 // add it to the list of members
1491 TTypeLoc member = { new TType(EbtVoid), token.loc };
1492 member.type->shallowCopy(memberType);
1493 member.type->setFieldName(*token.string);
1494 typeList->push_back(member);
1495
1496 // accept IDENTIFIER
1497 advanceToken();
1498
1499 // array_specifier
John Kessenich19b92ff2016-06-19 11:50:34 -06001500 TArraySizes* arraySizes = nullptr;
1501 acceptArraySpecifier(arraySizes);
1502 if (arraySizes)
1503 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06001504
John Kessenich7735b942016-09-05 12:40:06 -06001505 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06001506
John Kesseniche6e74942016-06-11 16:43:14 -06001507 // success on seeing the SEMICOLON coming up
1508 if (peekTokenClass(EHTokSemicolon))
1509 break;
1510
1511 // COMMA
1512 if (! acceptTokenClass(EHTokComma)) {
1513 expected(",");
1514 return false;
1515 }
1516
1517 } while (true);
1518
1519 // SEMI_COLON
1520 if (! acceptTokenClass(EHTokSemicolon)) {
1521 expected(";");
1522 return false;
1523 }
1524
1525 } while (true);
1526}
1527
John Kessenich5f934b02016-03-13 17:58:25 -06001528// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06001529// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06001530// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001531//
1532bool HlslGrammar::acceptFunctionParameters(TFunction& function)
1533{
John Kessenich078d7f22016-03-14 10:02:11 -06001534 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001535 if (! acceptTokenClass(EHTokLeftParen))
1536 return false;
1537
John Kessenich71351de2016-06-08 12:50:56 -06001538 // VOID RIGHT_PAREN
1539 if (! acceptTokenClass(EHTokVoid)) {
1540 do {
1541 // parameter_declaration
1542 if (! acceptParameterDeclaration(function))
1543 break;
John Kessenich5f934b02016-03-13 17:58:25 -06001544
John Kessenich71351de2016-06-08 12:50:56 -06001545 // COMMA
1546 if (! acceptTokenClass(EHTokComma))
1547 break;
1548 } while (true);
1549 }
John Kessenich5f934b02016-03-13 17:58:25 -06001550
John Kessenich078d7f22016-03-14 10:02:11 -06001551 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001552 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001553 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06001554 return false;
1555 }
1556
1557 return true;
1558}
1559
1560// parameter_declaration
John Kessenichc3387d32016-06-17 14:21:02 -06001561// : fully_specified_type post_decls
John Kessenich19b92ff2016-06-19 11:50:34 -06001562// | fully_specified_type identifier array_specifier post_decls
John Kessenich5f934b02016-03-13 17:58:25 -06001563//
1564bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
1565{
1566 // fully_specified_type
1567 TType* type = new TType;
1568 if (! acceptFullySpecifiedType(*type))
1569 return false;
1570
1571 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06001572 HlslToken idToken;
1573 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06001574
John Kessenich19b92ff2016-06-19 11:50:34 -06001575 // array_specifier
1576 TArraySizes* arraySizes = nullptr;
1577 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06001578 if (arraySizes) {
1579 if (arraySizes->isImplicit()) {
1580 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
1581 return false;
1582 }
1583
John Kessenich19b92ff2016-06-19 11:50:34 -06001584 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06001585 }
John Kessenich19b92ff2016-06-19 11:50:34 -06001586
1587 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001588 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06001589
John Kessenich5aa59e22016-06-17 15:50:47 -06001590 parseContext.paramFix(*type);
1591
John Kessenichaecd4972016-03-14 10:46:34 -06001592 TParameter param = { idToken.string, type };
John Kessenich5f934b02016-03-13 17:58:25 -06001593 function.addParameter(param);
1594
1595 return true;
1596}
1597
1598// Do the work to create the function definition in addition to
1599// parsing the body (compound_statement).
1600bool HlslGrammar::acceptFunctionDefinition(TFunction& function, TIntermNode*& node)
1601{
John Kessenicha3051662016-09-02 19:13:36 -06001602 TFunction& functionDeclarator = parseContext.handleFunctionDeclarator(token.loc, function, false /* not prototype */);
John Kessenich1a4b7752016-09-02 19:05:24 -06001603 TSourceLoc loc = token.loc;
John Kessenich5f934b02016-03-13 17:58:25 -06001604
John Kessenich077e0522016-06-09 02:02:17 -06001605 // This does a pushScope()
John Kessenicha3051662016-09-02 19:13:36 -06001606 node = parseContext.handleFunctionDefinition(loc, functionDeclarator);
John Kessenich5f934b02016-03-13 17:58:25 -06001607
1608 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06001609 TIntermNode* functionBody = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06001610 if (acceptCompoundStatement(functionBody)) {
John Kessenicha3051662016-09-02 19:13:36 -06001611 parseContext.handleFunctionBody(loc, functionDeclarator, functionBody, node);
John Kessenich5f934b02016-03-13 17:58:25 -06001612 return true;
1613 }
1614
1615 return false;
1616}
1617
John Kessenich0d2b6de2016-06-05 11:23:11 -06001618// Accept an expression with parenthesis around it, where
1619// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001620// syntactically required ones like in "if ( expression )".
1621//
1622// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06001623//
1624// Note this one is not set up to be speculative; as it gives
1625// errors if not found.
1626//
1627bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
1628{
1629 // LEFT_PAREN
1630 if (! acceptTokenClass(EHTokLeftParen))
1631 expected("(");
1632
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001633 bool decl = false;
1634 TIntermNode* declNode = nullptr;
1635 decl = acceptControlDeclaration(declNode);
1636 if (decl) {
1637 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
1638 expected("initialized declaration");
1639 return false;
1640 } else
1641 expression = declNode->getAsTyped();
1642 } else {
1643 // no declaration
1644 if (! acceptExpression(expression)) {
1645 expected("expression");
1646 return false;
1647 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06001648 }
1649
1650 // RIGHT_PAREN
1651 if (! acceptTokenClass(EHTokRightParen))
1652 expected(")");
1653
1654 return true;
1655}
1656
John Kessenich34fb0362016-05-03 23:17:20 -06001657// The top-level full expression recognizer.
1658//
John Kessenich87142c72016-03-12 20:24:24 -07001659// expression
John Kessenich34fb0362016-05-03 23:17:20 -06001660// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07001661//
1662bool HlslGrammar::acceptExpression(TIntermTyped*& node)
1663{
LoopDawgef764a22016-06-03 09:17:51 -06001664 node = nullptr;
1665
John Kessenich34fb0362016-05-03 23:17:20 -06001666 // assignment_expression
1667 if (! acceptAssignmentExpression(node))
1668 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001669
John Kessenich34fb0362016-05-03 23:17:20 -06001670 if (! peekTokenClass(EHTokComma))
1671 return true;
1672
1673 do {
1674 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06001675 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06001676 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06001677
John Kessenich34fb0362016-05-03 23:17:20 -06001678 // ... assignment_expression
1679 TIntermTyped* rightNode = nullptr;
1680 if (! acceptAssignmentExpression(rightNode)) {
1681 expected("assignment expression");
1682 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001683 }
1684
John Kessenich34fb0362016-05-03 23:17:20 -06001685 node = intermediate.addComma(node, rightNode, loc);
1686
1687 if (! peekTokenClass(EHTokComma))
1688 return true;
1689 } while (true);
1690}
1691
John Kessenich07354242016-07-01 19:58:06 -06001692// initializer
1693// : LEFT_BRACE initializer_list RIGHT_BRACE
1694//
1695// initializer_list
1696// : assignment_expression COMMA assignment_expression COMMA ...
1697//
1698bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
1699{
1700 // LEFT_BRACE
1701 if (! acceptTokenClass(EHTokLeftBrace))
1702 return false;
1703
1704 // initializer_list
1705 TSourceLoc loc = token.loc;
1706 node = nullptr;
1707 do {
1708 // assignment_expression
1709 TIntermTyped* expr;
1710 if (! acceptAssignmentExpression(expr)) {
1711 expected("assignment expression in initializer list");
1712 return false;
1713 }
1714 node = intermediate.growAggregate(node, expr, loc);
1715
1716 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001717 if (acceptTokenClass(EHTokComma)) {
1718 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
1719 return true;
John Kessenich07354242016-07-01 19:58:06 -06001720 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06001721 }
John Kessenich07354242016-07-01 19:58:06 -06001722
1723 // RIGHT_BRACE
1724 if (acceptTokenClass(EHTokRightBrace))
1725 return true;
1726
1727 expected(", or }");
1728 return false;
1729 } while (true);
1730}
1731
John Kessenich34fb0362016-05-03 23:17:20 -06001732// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06001733// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06001734//
1735// a op (b op (c op d))
1736//
1737// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06001738// : initializer
1739// | conditional_expression
1740// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06001741//
1742bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
1743{
John Kessenich07354242016-07-01 19:58:06 -06001744 // initializer
1745 if (peekTokenClass(EHTokLeftBrace)) {
1746 if (acceptInitializer(node))
1747 return true;
1748
1749 expected("initializer");
1750 return false;
1751 }
1752
John Kessenich00957f82016-07-27 10:39:57 -06001753 // conditional_expression
1754 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06001755 return false;
1756
John Kessenich07354242016-07-01 19:58:06 -06001757 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06001758 TOperator assignOp = HlslOpMap::assignment(peek());
1759 if (assignOp == EOpNull)
1760 return true;
1761
John Kessenich00957f82016-07-27 10:39:57 -06001762 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06001763 TSourceLoc loc = token.loc;
1764 advanceToken();
1765
John Kessenich00957f82016-07-27 10:39:57 -06001766 // conditional_expression assign_op conditional_expression ...
1767 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06001768 // gets the right-to-left associativity.
1769 TIntermTyped* rightNode = nullptr;
1770 if (! acceptAssignmentExpression(rightNode)) {
1771 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06001772 return false;
John Kessenich87142c72016-03-12 20:24:24 -07001773 }
1774
John Kessenichd21baed2016-09-16 03:05:12 -06001775 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
steve-lunarg90707962016-10-07 19:35:40 -06001776 node = parseContext.handleLvalue(loc, "assign", node);
1777
John Kessenichfea226b2016-07-28 17:53:56 -06001778 if (node == nullptr) {
1779 parseContext.error(loc, "could not create assignment", "", "");
1780 return false;
1781 }
John Kessenich34fb0362016-05-03 23:17:20 -06001782
1783 if (! peekTokenClass(EHTokComma))
1784 return true;
1785
1786 return true;
1787}
1788
John Kessenich00957f82016-07-27 10:39:57 -06001789// Accept a conditional expression, which associates right-to-left,
1790// accomplished by the "true" expression calling down to lower
1791// precedence levels than this level.
1792//
1793// conditional_expression
1794// : binary_expression
1795// | binary_expression QUESTION expression COLON assignment_expression
1796//
1797bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
1798{
1799 // binary_expression
1800 if (! acceptBinaryExpression(node, PlLogicalOr))
1801 return false;
1802
1803 if (! acceptTokenClass(EHTokQuestion))
1804 return true;
1805
1806 TIntermTyped* trueNode = nullptr;
1807 if (! acceptExpression(trueNode)) {
1808 expected("expression after ?");
1809 return false;
1810 }
1811 TSourceLoc loc = token.loc;
1812
1813 if (! acceptTokenClass(EHTokColon)) {
1814 expected(":");
1815 return false;
1816 }
1817
1818 TIntermTyped* falseNode = nullptr;
1819 if (! acceptAssignmentExpression(falseNode)) {
1820 expected("expression after :");
1821 return false;
1822 }
1823
1824 node = intermediate.addSelection(node, trueNode, falseNode, loc);
1825
1826 return true;
1827}
1828
John Kessenich34fb0362016-05-03 23:17:20 -06001829// Accept a binary expression, for binary operations that
1830// associate left-to-right. This is, it is implicit, for example
1831//
1832// ((a op b) op c) op d
1833//
1834// binary_expression
1835// : expression op expression op expression ...
1836//
1837// where 'expression' is the next higher level in precedence.
1838//
1839bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
1840{
1841 if (precedenceLevel > PlMul)
1842 return acceptUnaryExpression(node);
1843
1844 // assignment_expression
1845 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
1846 return false;
1847
John Kessenich34fb0362016-05-03 23:17:20 -06001848 do {
John Kessenich64076ed2016-07-28 21:43:17 -06001849 TOperator op = HlslOpMap::binary(peek());
1850 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
1851 if (tokenLevel < precedenceLevel)
1852 return true;
1853
John Kessenich34fb0362016-05-03 23:17:20 -06001854 // ... op
1855 TSourceLoc loc = token.loc;
1856 advanceToken();
1857
1858 // ... expression
1859 TIntermTyped* rightNode = nullptr;
1860 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
1861 expected("expression");
1862 return false;
1863 }
1864
1865 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06001866 if (node == nullptr) {
1867 parseContext.error(loc, "Could not perform requested binary operation", "", "");
1868 return false;
1869 }
John Kessenich34fb0362016-05-03 23:17:20 -06001870 } while (true);
1871}
1872
1873// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06001874// : (type) unary_expression
1875// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06001876// | - unary_expression
1877// | ! unary_expression
1878// | ~ unary_expression
1879// | ++ unary_expression
1880// | -- unary_expression
1881// | postfix_expression
1882//
1883bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
1884{
John Kessenich1cc1a282016-06-03 16:55:49 -06001885 // (type) unary_expression
1886 // Have to look two steps ahead, because this could be, e.g., a
1887 // postfix_expression instead, since that also starts with at "(".
1888 if (acceptTokenClass(EHTokLeftParen)) {
1889 TType castType;
1890 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06001891 if (acceptTokenClass(EHTokRightParen)) {
1892 // We've matched "(type)" now, get the expression to cast
1893 TSourceLoc loc = token.loc;
1894 if (! acceptUnaryExpression(node))
1895 return false;
1896
1897 // Hook it up like a constructor
1898 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
1899 if (constructorFunction == nullptr) {
1900 expected("type that can be constructed");
1901 return false;
1902 }
1903 TIntermTyped* arguments = nullptr;
1904 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
1905 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
1906
1907 return true;
1908 } else {
1909 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
1910 // the '(int' part. We must back up twice.
1911 recedeToken();
1912 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06001913 }
John Kessenich1cc1a282016-06-03 16:55:49 -06001914 } else {
1915 // This isn't a type cast, but it still started "(", so if it is a
1916 // unary expression, it can only be a postfix_expression, so try that.
1917 // Back it up first.
1918 recedeToken();
1919 return acceptPostfixExpression(node);
1920 }
1921 }
1922
1923 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06001924 TOperator unaryOp = HlslOpMap::preUnary(peek());
1925
John Kessenich1cc1a282016-06-03 16:55:49 -06001926 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06001927 if (unaryOp == EOpNull)
1928 return acceptPostfixExpression(node);
1929
1930 // op unary_expression
1931 TSourceLoc loc = token.loc;
1932 advanceToken();
1933 if (! acceptUnaryExpression(node))
1934 return false;
1935
1936 // + is a no-op
1937 if (unaryOp == EOpAdd)
1938 return true;
1939
1940 node = intermediate.addUnaryMath(unaryOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06001941 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06001942
1943 return node != nullptr;
1944}
1945
1946// postfix_expression
1947// : LEFT_PAREN expression RIGHT_PAREN
1948// | literal
1949// | constructor
1950// | identifier
1951// | function_call
1952// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
1953// | postfix_expression DOT IDENTIFIER
1954// | postfix_expression INC_OP
1955// | postfix_expression DEC_OP
1956//
1957bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
1958{
1959 // Not implemented as self-recursive:
1960 // The logical "right recursion" is done with an loop at the end
1961
1962 // idToken will pick up either a variable or a function name in a function call
1963 HlslToken idToken;
1964
John Kessenich21472ae2016-06-04 11:46:33 -06001965 // Find something before the postfix operations, as they can't operate
1966 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07001967 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06001968 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07001969 if (! acceptExpression(node)) {
1970 expected("expression");
1971 return false;
1972 }
1973 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001974 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07001975 return false;
1976 }
John Kessenich34fb0362016-05-03 23:17:20 -06001977 } else if (acceptLiteral(node)) {
1978 // literal (nothing else to do yet), go on to the
1979 } else if (acceptConstructor(node)) {
1980 // constructor (nothing else to do yet)
1981 } else if (acceptIdentifier(idToken)) {
1982 // identifier or function_call name
1983 if (! peekTokenClass(EHTokLeftParen)) {
John Kesseniche6e74942016-06-11 16:43:14 -06001984 node = parseContext.handleVariable(idToken.loc, idToken.symbol, token.string);
John Kessenich34fb0362016-05-03 23:17:20 -06001985 } else if (acceptFunctionCall(idToken, node)) {
1986 // function_call (nothing else to do yet)
1987 } else {
1988 expected("function call arguments");
1989 return false;
1990 }
John Kessenich21472ae2016-06-04 11:46:33 -06001991 } else {
1992 // nothing found, can't post operate
1993 return false;
John Kessenich87142c72016-03-12 20:24:24 -07001994 }
1995
John Kessenich21472ae2016-06-04 11:46:33 -06001996 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06001997 do {
1998 TSourceLoc loc = token.loc;
1999 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07002000
John Kessenich34fb0362016-05-03 23:17:20 -06002001 // Consume only a valid post-unary operator, otherwise we are done.
2002 switch (postOp) {
2003 case EOpIndexDirectStruct:
2004 case EOpIndexIndirect:
2005 case EOpPostIncrement:
2006 case EOpPostDecrement:
2007 advanceToken();
2008 break;
2009 default:
2010 return true;
2011 }
John Kessenich87142c72016-03-12 20:24:24 -07002012
John Kessenich34fb0362016-05-03 23:17:20 -06002013 // We have a valid post-unary operator, process it.
2014 switch (postOp) {
2015 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06002016 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002017 // DOT IDENTIFIER
2018 // includes swizzles and struct members
John Kessenich93a162a2016-06-17 17:16:27 -06002019 HlslToken field;
2020 if (! acceptIdentifier(field)) {
2021 expected("swizzle or member");
2022 return false;
2023 }
LoopDawg4886f692016-06-29 10:58:58 -06002024
2025 TIntermTyped* base = node; // preserve for method function calls
John Kessenich93a162a2016-06-17 17:16:27 -06002026 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06002027
2028 // In the event of a method node, we look for an open paren and accept the function call.
2029 if (node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) {
2030 if (! acceptFunctionCall(field, node, base)) {
2031 expected("function parameters");
2032 return false;
2033 }
2034 }
2035
John Kessenich34fb0362016-05-03 23:17:20 -06002036 break;
John Kessenich93a162a2016-06-17 17:16:27 -06002037 }
John Kessenich34fb0362016-05-03 23:17:20 -06002038 case EOpIndexIndirect:
2039 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002040 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06002041 TIntermTyped* indexNode = nullptr;
2042 if (! acceptExpression(indexNode) ||
2043 ! peekTokenClass(EHTokRightBracket)) {
2044 expected("expression followed by ']'");
2045 return false;
2046 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002047 advanceToken();
2048 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
2049 break;
John Kessenich34fb0362016-05-03 23:17:20 -06002050 }
2051 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002052 // INC_OP
2053 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06002054 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002055 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06002056 node = intermediate.addUnaryMath(postOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06002057 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002058 break;
2059 default:
2060 assert(0);
2061 break;
2062 }
2063 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07002064}
2065
John Kessenichd016be12016-03-13 11:24:20 -06002066// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06002067// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06002068//
2069bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
2070{
2071 // type
2072 TType type;
2073 if (acceptType(type)) {
2074 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
2075 if (constructorFunction == nullptr)
2076 return false;
2077
2078 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06002079 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06002080 if (! acceptArguments(constructorFunction, arguments)) {
2081 expected("constructor arguments");
2082 return false;
2083 }
2084
2085 // hook it up
2086 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
2087
2088 return true;
2089 }
2090
2091 return false;
2092}
2093
John Kessenich34fb0362016-05-03 23:17:20 -06002094// The function_call identifier was already recognized, and passed in as idToken.
2095//
2096// function_call
2097// : [idToken] arguments
2098//
LoopDawg4886f692016-06-29 10:58:58 -06002099bool HlslGrammar::acceptFunctionCall(HlslToken idToken, TIntermTyped*& node, TIntermTyped* base)
John Kessenich34fb0362016-05-03 23:17:20 -06002100{
John Kessenich4678ca92016-05-13 09:33:42 -06002101 // arguments
2102 TFunction* function = new TFunction(idToken.string, TType(EbtVoid));
2103 TIntermTyped* arguments = nullptr;
LoopDawg4886f692016-06-29 10:58:58 -06002104
2105 // methods have an implicit first argument of the calling object.
2106 if (base != nullptr)
2107 parseContext.handleFunctionArgument(function, arguments, base);
2108
John Kessenich4678ca92016-05-13 09:33:42 -06002109 if (! acceptArguments(function, arguments))
2110 return false;
2111
2112 node = parseContext.handleFunctionCall(idToken.loc, function, arguments);
2113
2114 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002115}
2116
John Kessenich87142c72016-03-12 20:24:24 -07002117// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002118// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002119//
John Kessenichd016be12016-03-13 11:24:20 -06002120// The arguments are pushed onto the 'function' argument list and
2121// onto the 'arguments' aggregate.
2122//
John Kessenich4678ca92016-05-13 09:33:42 -06002123bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002124{
John Kessenich078d7f22016-03-14 10:02:11 -06002125 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002126 if (! acceptTokenClass(EHTokLeftParen))
2127 return false;
2128
2129 do {
John Kessenichd016be12016-03-13 11:24:20 -06002130 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002131 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002132 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002133 break;
John Kessenichd016be12016-03-13 11:24:20 -06002134
2135 // hook it up
2136 parseContext.handleFunctionArgument(function, arguments, arg);
2137
John Kessenich078d7f22016-03-14 10:02:11 -06002138 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002139 if (! acceptTokenClass(EHTokComma))
2140 break;
2141 } while (true);
2142
John Kessenich078d7f22016-03-14 10:02:11 -06002143 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002144 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002145 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002146 return false;
2147 }
2148
2149 return true;
2150}
2151
2152bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2153{
2154 switch (token.tokenClass) {
2155 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002156 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002157 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002158 case EHTokUintConstant:
2159 node = intermediate.addConstantUnion(token.u, token.loc, true);
2160 break;
John Kessenich87142c72016-03-12 20:24:24 -07002161 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002162 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002163 break;
2164 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002165 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002166 break;
2167 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002168 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002169 break;
John Kessenich86f71382016-09-19 20:23:18 -06002170 case EHTokStringConstant:
2171 node = nullptr;
2172 break;
John Kessenich87142c72016-03-12 20:24:24 -07002173
2174 default:
2175 return false;
2176 }
2177
2178 advanceToken();
2179
2180 return true;
2181}
2182
John Kessenich5f934b02016-03-13 17:58:25 -06002183// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002184// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002185//
John Kessenich21472ae2016-06-04 11:46:33 -06002186bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002187{
John Kessenich21472ae2016-06-04 11:46:33 -06002188 TIntermAggregate* compoundStatement = nullptr;
2189
John Kessenich34fb0362016-05-03 23:17:20 -06002190 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002191 if (! acceptTokenClass(EHTokLeftBrace))
2192 return false;
2193
2194 // statement statement ...
2195 TIntermNode* statement = nullptr;
2196 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002197 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2198 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2199 branch->getFlowOp() == EOpDefault)) {
2200 // hook up individual subsequences within a switch statement
2201 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2202 compoundStatement = nullptr;
2203 } else {
2204 // hook it up to the growing compound statement
2205 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2206 }
John Kessenich5f934b02016-03-13 17:58:25 -06002207 }
John Kessenich34fb0362016-05-03 23:17:20 -06002208 if (compoundStatement)
2209 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06002210
John Kessenich21472ae2016-06-04 11:46:33 -06002211 retStatement = compoundStatement;
2212
John Kessenich34fb0362016-05-03 23:17:20 -06002213 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002214 return acceptTokenClass(EHTokRightBrace);
2215}
2216
John Kessenich0d2b6de2016-06-05 11:23:11 -06002217bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
2218{
2219 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06002220 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06002221 parseContext.popScope();
2222
2223 return result;
2224}
2225
John Kessenich077e0522016-06-09 02:02:17 -06002226bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06002227{
John Kessenich077e0522016-06-09 02:02:17 -06002228 parseContext.pushScope();
2229 bool result = acceptCompoundStatement(statement);
2230 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06002231
2232 return result;
2233}
2234
John Kessenich5f934b02016-03-13 17:58:25 -06002235// statement
John Kessenich21472ae2016-06-04 11:46:33 -06002236// : attributes attributed_statement
2237//
2238// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002239// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002240// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06002241// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06002242// | declaration_statement
2243// | selection_statement
2244// | switch_statement
2245// | case_label
2246// | iteration_statement
2247// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002248//
2249bool HlslGrammar::acceptStatement(TIntermNode*& statement)
2250{
John Kessenich21472ae2016-06-04 11:46:33 -06002251 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06002252
John Kessenich21472ae2016-06-04 11:46:33 -06002253 // attributes
2254 acceptAttributes();
John Kessenich5f934b02016-03-13 17:58:25 -06002255
John Kessenich21472ae2016-06-04 11:46:33 -06002256 // attributed_statement
2257 switch (peek()) {
2258 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06002259 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002260
John Kessenich21472ae2016-06-04 11:46:33 -06002261 case EHTokIf:
2262 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002263
John Kessenich21472ae2016-06-04 11:46:33 -06002264 case EHTokSwitch:
2265 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002266
John Kessenich21472ae2016-06-04 11:46:33 -06002267 case EHTokFor:
2268 case EHTokDo:
2269 case EHTokWhile:
2270 return acceptIterationStatement(statement);
2271
2272 case EHTokContinue:
2273 case EHTokBreak:
2274 case EHTokDiscard:
2275 case EHTokReturn:
2276 return acceptJumpStatement(statement);
2277
2278 case EHTokCase:
2279 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06002280 case EHTokDefault:
2281 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06002282
2283 case EHTokSemicolon:
2284 return acceptTokenClass(EHTokSemicolon);
2285
2286 case EHTokRightBrace:
2287 // Performance: not strictly necessary, but stops a bunch of hunting early,
2288 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06002289 return false;
2290
John Kessenich21472ae2016-06-04 11:46:33 -06002291 default:
2292 {
2293 // declaration
2294 if (acceptDeclaration(statement))
2295 return true;
2296
2297 // expression
2298 TIntermTyped* node;
2299 if (acceptExpression(node))
2300 statement = node;
2301 else
2302 return false;
2303
2304 // SEMICOLON (following an expression)
2305 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002306 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06002307 return false;
2308 }
2309 }
2310 }
2311
John Kessenich5f934b02016-03-13 17:58:25 -06002312 return true;
John Kessenich87142c72016-03-12 20:24:24 -07002313}
2314
John Kessenich21472ae2016-06-04 11:46:33 -06002315// attributes
2316// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
2317//
2318// attribute:
2319// : UNROLL
2320// | UNROLL LEFT_PAREN literal RIGHT_PAREN
2321// | FASTOPT
2322// | ALLOW_UAV_CONDITION
2323// | BRANCH
2324// | FLATTEN
2325// | FORCECASE
2326// | CALL
2327//
2328void HlslGrammar::acceptAttributes()
2329{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002330 // For now, accept the [ XXX(X) ] syntax, but drop.
2331 // TODO: subset to correct set? Pass on?
2332 do {
2333 // LEFT_BRACKET?
2334 if (! acceptTokenClass(EHTokLeftBracket))
2335 return;
2336
2337 // attribute
2338 if (peekTokenClass(EHTokIdentifier)) {
2339 // 'token.string' is the attribute
2340 advanceToken();
2341 } else if (! peekTokenClass(EHTokRightBracket)) {
2342 expected("identifier");
2343 advanceToken();
2344 }
2345
2346 // (x)
2347 if (acceptTokenClass(EHTokLeftParen)) {
2348 TIntermTyped* node;
2349 if (! acceptLiteral(node))
2350 expected("literal");
2351 // 'node' has the literal in it
2352 if (! acceptTokenClass(EHTokRightParen))
2353 expected(")");
2354 }
2355
2356 // RIGHT_BRACKET
2357 if (acceptTokenClass(EHTokRightBracket))
2358 continue;
2359
2360 expected("]");
2361 return;
2362
2363 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06002364}
2365
John Kessenich0d2b6de2016-06-05 11:23:11 -06002366// selection_statement
2367// : IF LEFT_PAREN expression RIGHT_PAREN statement
2368// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
2369//
John Kessenich21472ae2016-06-04 11:46:33 -06002370bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
2371{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002372 TSourceLoc loc = token.loc;
2373
2374 // IF
2375 if (! acceptTokenClass(EHTokIf))
2376 return false;
2377
2378 // so that something declared in the condition is scoped to the lifetimes
2379 // of the then-else statements
2380 parseContext.pushScope();
2381
2382 // LEFT_PAREN expression RIGHT_PAREN
2383 TIntermTyped* condition;
2384 if (! acceptParenExpression(condition))
2385 return false;
2386
2387 // create the child statements
2388 TIntermNodePair thenElse = { nullptr, nullptr };
2389
2390 // then statement
2391 if (! acceptScopedStatement(thenElse.node1)) {
2392 expected("then statement");
2393 return false;
2394 }
2395
2396 // ELSE
2397 if (acceptTokenClass(EHTokElse)) {
2398 // else statement
2399 if (! acceptScopedStatement(thenElse.node2)) {
2400 expected("else statement");
2401 return false;
2402 }
2403 }
2404
2405 // Put the pieces together
2406 statement = intermediate.addSelection(condition, thenElse, loc);
2407 parseContext.popScope();
2408
2409 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002410}
2411
John Kessenichd02dc5d2016-07-01 00:04:11 -06002412// switch_statement
2413// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
2414//
John Kessenich21472ae2016-06-04 11:46:33 -06002415bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
2416{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002417 // SWITCH
2418 TSourceLoc loc = token.loc;
2419 if (! acceptTokenClass(EHTokSwitch))
2420 return false;
2421
2422 // LEFT_PAREN expression RIGHT_PAREN
2423 parseContext.pushScope();
2424 TIntermTyped* switchExpression;
2425 if (! acceptParenExpression(switchExpression)) {
2426 parseContext.popScope();
2427 return false;
2428 }
2429
2430 // compound_statement
2431 parseContext.pushSwitchSequence(new TIntermSequence);
2432 bool statementOkay = acceptCompoundStatement(statement);
2433 if (statementOkay)
2434 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
2435
2436 parseContext.popSwitchSequence();
2437 parseContext.popScope();
2438
2439 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06002440}
2441
John Kessenich119f8f62016-06-05 15:44:07 -06002442// iteration_statement
2443// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
2444// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
2445// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
2446//
2447// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06002448bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
2449{
John Kessenich119f8f62016-06-05 15:44:07 -06002450 TSourceLoc loc = token.loc;
2451 TIntermTyped* condition = nullptr;
2452
2453 EHlslTokenClass loop = peek();
2454 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
2455
2456 // WHILE or DO or FOR
2457 advanceToken();
2458
2459 switch (loop) {
2460 case EHTokWhile:
2461 // so that something declared in the condition is scoped to the lifetime
2462 // of the while sub-statement
2463 parseContext.pushScope();
2464 parseContext.nestLooping();
2465
2466 // LEFT_PAREN condition RIGHT_PAREN
2467 if (! acceptParenExpression(condition))
2468 return false;
2469
2470 // statement
2471 if (! acceptScopedStatement(statement)) {
2472 expected("while sub-statement");
2473 return false;
2474 }
2475
2476 parseContext.unnestLooping();
2477 parseContext.popScope();
2478
2479 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
2480
2481 return true;
2482
2483 case EHTokDo:
2484 parseContext.nestLooping();
2485
2486 if (! acceptTokenClass(EHTokLeftBrace))
2487 expected("{");
2488
2489 // statement
2490 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
2491 expected("do sub-statement");
2492 return false;
2493 }
2494
2495 if (! acceptTokenClass(EHTokRightBrace))
2496 expected("}");
2497
2498 // WHILE
2499 if (! acceptTokenClass(EHTokWhile)) {
2500 expected("while");
2501 return false;
2502 }
2503
2504 // LEFT_PAREN condition RIGHT_PAREN
2505 TIntermTyped* condition;
2506 if (! acceptParenExpression(condition))
2507 return false;
2508
2509 if (! acceptTokenClass(EHTokSemicolon))
2510 expected(";");
2511
2512 parseContext.unnestLooping();
2513
2514 statement = intermediate.addLoop(statement, condition, 0, false, loc);
2515
2516 return true;
2517
2518 case EHTokFor:
2519 {
2520 // LEFT_PAREN
2521 if (! acceptTokenClass(EHTokLeftParen))
2522 expected("(");
2523
2524 // so that something declared in the condition is scoped to the lifetime
2525 // of the for sub-statement
2526 parseContext.pushScope();
2527
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002528 // initializer
2529 TIntermNode* initNode = nullptr;
2530 if (! acceptControlDeclaration(initNode)) {
2531 TIntermTyped* initExpr = nullptr;
2532 acceptExpression(initExpr);
2533 initNode = initExpr;
2534 }
2535 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06002536 if (! acceptTokenClass(EHTokSemicolon))
2537 expected(";");
2538
2539 parseContext.nestLooping();
2540
2541 // condition SEMI_COLON
2542 acceptExpression(condition);
2543 if (! acceptTokenClass(EHTokSemicolon))
2544 expected(";");
2545
2546 // iterator SEMI_COLON
2547 TIntermTyped* iterator = nullptr;
2548 acceptExpression(iterator);
2549 if (! acceptTokenClass(EHTokRightParen))
2550 expected(")");
2551
2552 // statement
2553 if (! acceptScopedStatement(statement)) {
2554 expected("for sub-statement");
2555 return false;
2556 }
2557
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002558 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06002559
2560 parseContext.popScope();
2561 parseContext.unnestLooping();
2562
2563 return true;
2564 }
2565
2566 default:
2567 return false;
2568 }
John Kessenich21472ae2016-06-04 11:46:33 -06002569}
2570
2571// jump_statement
2572// : CONTINUE SEMICOLON
2573// | BREAK SEMICOLON
2574// | DISCARD SEMICOLON
2575// | RETURN SEMICOLON
2576// | RETURN expression SEMICOLON
2577//
2578bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
2579{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002580 EHlslTokenClass jump = peek();
2581 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06002582 case EHTokContinue:
2583 case EHTokBreak:
2584 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06002585 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002586 advanceToken();
2587 break;
John Kessenich21472ae2016-06-04 11:46:33 -06002588 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002589 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06002590 return false;
2591 }
John Kessenich21472ae2016-06-04 11:46:33 -06002592
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002593 switch (jump) {
2594 case EHTokContinue:
2595 statement = intermediate.addBranch(EOpContinue, token.loc);
2596 break;
2597 case EHTokBreak:
2598 statement = intermediate.addBranch(EOpBreak, token.loc);
2599 break;
2600 case EHTokDiscard:
2601 statement = intermediate.addBranch(EOpKill, token.loc);
2602 break;
2603
2604 case EHTokReturn:
2605 {
2606 // expression
2607 TIntermTyped* node;
2608 if (acceptExpression(node)) {
2609 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06002610 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002611 } else
2612 statement = intermediate.addBranch(EOpReturn, token.loc);
2613 break;
2614 }
2615
2616 default:
2617 assert(0);
2618 return false;
2619 }
2620
2621 // SEMICOLON
2622 if (! acceptTokenClass(EHTokSemicolon))
2623 expected(";");
2624
2625 return true;
2626}
John Kessenich21472ae2016-06-04 11:46:33 -06002627
John Kessenichd02dc5d2016-07-01 00:04:11 -06002628// case_label
2629// : CASE expression COLON
2630//
John Kessenich21472ae2016-06-04 11:46:33 -06002631bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
2632{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002633 TSourceLoc loc = token.loc;
2634 if (! acceptTokenClass(EHTokCase))
2635 return false;
2636
2637 TIntermTyped* expression;
2638 if (! acceptExpression(expression)) {
2639 expected("case expression");
2640 return false;
2641 }
2642
2643 if (! acceptTokenClass(EHTokColon)) {
2644 expected(":");
2645 return false;
2646 }
2647
2648 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
2649
2650 return true;
2651}
2652
2653// default_label
2654// : DEFAULT COLON
2655//
2656bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
2657{
2658 TSourceLoc loc = token.loc;
2659 if (! acceptTokenClass(EHTokDefault))
2660 return false;
2661
2662 if (! acceptTokenClass(EHTokColon)) {
2663 expected(":");
2664 return false;
2665 }
2666
2667 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
2668
2669 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002670}
2671
John Kessenich19b92ff2016-06-19 11:50:34 -06002672// array_specifier
2673// : LEFT_BRACKET integer_expression RGHT_BRACKET post_decls // optional
steve-lunarg265c0612016-09-27 10:57:35 -06002674// : LEFT_BRACKET RGHT_BRACKET post_decls // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06002675//
2676void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
2677{
2678 arraySizes = nullptr;
2679
2680 if (! acceptTokenClass(EHTokLeftBracket))
2681 return;
2682
2683 TSourceLoc loc = token.loc;
steve-lunarg265c0612016-09-27 10:57:35 -06002684 TIntermTyped* sizeExpr = nullptr;
2685
2686 // Array sizing expression is optional. If ommitted, array is implicitly sized.
2687 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
John Kessenich19b92ff2016-06-19 11:50:34 -06002688
2689 if (! acceptTokenClass(EHTokRightBracket)) {
2690 expected("]");
2691 return;
2692 }
2693
John Kessenich19b92ff2016-06-19 11:50:34 -06002694 arraySizes = new TArraySizes;
steve-lunarg265c0612016-09-27 10:57:35 -06002695
2696 if (hasArraySize) {
2697 TArraySize arraySize;
2698 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
2699 arraySizes->addInnerSize(arraySize);
2700 } else {
2701 arraySizes->addInnerSize(); // implicitly sized
2702 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002703}
2704
John Kessenich630dd7d2016-06-12 23:52:12 -06002705// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06002706// : COLON semantic // optional
2707// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
2708// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06002709// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06002710// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06002711//
John Kessenich7735b942016-09-05 12:40:06 -06002712void HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06002713{
John Kessenich630dd7d2016-06-12 23:52:12 -06002714 do {
2715 // COLON
2716 if (acceptTokenClass(EHTokColon)) {
2717 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06002718 if (peekTokenClass(EHTokLayout))
2719 acceptLayoutQualifierList(qualifier);
2720 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06002721 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002722 if (! acceptTokenClass(EHTokLeftParen)) {
2723 expected("(");
2724 return;
2725 }
John Kessenich82d6baf2016-07-29 13:03:05 -06002726 HlslToken locationToken;
2727 if (! acceptIdentifier(locationToken)) {
2728 expected("c[subcomponent][.component]");
2729 return;
2730 }
2731 HlslToken componentToken;
2732 if (acceptTokenClass(EHTokDot)) {
2733 if (! acceptIdentifier(componentToken)) {
2734 expected("component");
2735 return;
2736 }
2737 }
John Kessenich630dd7d2016-06-12 23:52:12 -06002738 if (! acceptTokenClass(EHTokRightParen)) {
2739 expected(")");
2740 break;
2741 }
John Kessenich7735b942016-09-05 12:40:06 -06002742 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002743 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06002744 expected("layout, semantic, packoffset, or register");
John Kessenich630dd7d2016-06-12 23:52:12 -06002745 return;
2746 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06002747 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
2748 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002749 if (! acceptTokenClass(EHTokLeftParen)) {
2750 expected("(");
2751 return;
2752 }
John Kessenichb38f0712016-07-30 10:29:54 -06002753 HlslToken registerDesc; // for Type#
2754 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06002755 if (! acceptIdentifier(registerDesc)) {
2756 expected("register number description");
2757 return;
2758 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06002759 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
2760 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06002761 // Then we didn't really see the registerDesc yet, it was
2762 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06002763 profile = registerDesc;
2764 if (! acceptIdentifier(registerDesc)) {
2765 expected("register number description");
2766 return;
2767 }
2768 }
John Kessenichb38f0712016-07-30 10:29:54 -06002769 int subComponent = 0;
2770 if (acceptTokenClass(EHTokLeftBracket)) {
2771 // LEFT_BRACKET subcomponent RIGHT_BRACKET
2772 if (! peekTokenClass(EHTokIntConstant)) {
2773 expected("literal integer");
2774 return;
2775 }
2776 subComponent = token.i;
2777 advanceToken();
2778 if (! acceptTokenClass(EHTokRightBracket)) {
2779 expected("]");
2780 break;
2781 }
2782 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06002783 // (COMMA SPACEN)opt
2784 HlslToken spaceDesc;
2785 if (acceptTokenClass(EHTokComma)) {
2786 if (! acceptIdentifier(spaceDesc)) {
2787 expected ("space identifier");
2788 return;
2789 }
2790 }
2791 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06002792 if (! acceptTokenClass(EHTokRightParen)) {
2793 expected(")");
2794 break;
2795 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06002796 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002797 } else {
2798 // semantic, in idToken.string
John Kessenich7735b942016-09-05 12:40:06 -06002799 parseContext.handleSemantic(idToken.loc, qualifier, *idToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06002800 }
John Kessenicha1e2d492016-09-20 13:22:58 -06002801 } else if (peekTokenClass(EHTokLeftAngle))
2802 acceptAnnotations(qualifier);
2803 else
John Kessenich630dd7d2016-06-12 23:52:12 -06002804 break;
John Kessenich078d7f22016-03-14 10:02:11 -06002805
John Kessenich630dd7d2016-06-12 23:52:12 -06002806 } while (true);
John Kessenich078d7f22016-03-14 10:02:11 -06002807}
2808
John Kesseniche01a9bc2016-03-12 20:11:22 -07002809} // end namespace glslang