blob: 6ae7f8002194b92702b23872405816faba54d820 [file] [log] [blame]
John Kesseniche01a9bc2016-03-12 20:11:22 -07001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2016 Google, Inc.
3// Copyright (C) 2016 LunarG, Inc.
John Kesseniche01a9bc2016-03-12 20:11:22 -07004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kesseniche01a9bc2016-03-12 20:11:22 -07006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kesseniche01a9bc2016-03-12 20:11:22 -070010//
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//
John Kessenich927608b2017-01-06 12:34:14 -070023// 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.
John Kesseniche01a9bc2016-03-12 20:11:22 -070035//
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"
steve-lunarg1868b142016-10-20 13:07:10 -060056#include "hlslAttributes.h"
John Kesseniche01a9bc2016-03-12 20:11:22 -070057
58namespace glslang {
59
60// Root entry point to this recursive decent parser.
61// Return true if compilation unit was successfully accepted.
62bool HlslGrammar::parse()
63{
64 advanceToken();
65 return acceptCompilationUnit();
66}
67
68void HlslGrammar::expected(const char* syntax)
69{
70 parseContext.error(token.loc, "Expected", syntax, "");
71}
72
LoopDawg4886f692016-06-29 10:58:58 -060073void HlslGrammar::unimplemented(const char* error)
74{
75 parseContext.error(token.loc, "Unimplemented", error, "");
76}
77
John Kessenichaecd4972016-03-14 10:46:34 -060078// Only process the next token if it is an identifier.
79// Return true if it was an identifier.
80bool HlslGrammar::acceptIdentifier(HlslToken& idToken)
81{
82 if (peekTokenClass(EHTokIdentifier)) {
83 idToken = token;
84 advanceToken();
85 return true;
86 }
87
steve-lunarg5ca85ad2016-12-26 18:45:52 -070088 // Even though "sample", "bool", "float", etc keywords (for types, interpolation modifiers),
89 // they ARE still accepted as identifiers. This is not a dense space: e.g, "void" is not a
90 // valid identifier, nor is "linear". This code special cases the known instances of this, so
91 // e.g, "int sample;" or "float float;" is accepted. Other cases can be added here if needed.
John Kessenichecba76f2017-01-06 00:34:48 -070092
steve-lunarg5ca85ad2016-12-26 18:45:52 -070093 TString* idString = nullptr;
94 switch (peek()) {
95 case EHTokSample: idString = NewPoolTString("sample"); break;
96 case EHTokHalf: idString = NewPoolTString("half"); break;
97 case EHTokBool: idString = NewPoolTString("bool"); break;
98 case EHTokFloat: idString = NewPoolTString("float"); break;
99 case EHTokDouble: idString = NewPoolTString("double"); break;
100 case EHTokInt: idString = NewPoolTString("int"); break;
101 case EHTokUint: idString = NewPoolTString("uint"); break;
102 case EHTokMin16float: idString = NewPoolTString("min16float"); break;
103 case EHTokMin10float: idString = NewPoolTString("min10float"); break;
104 case EHTokMin16int: idString = NewPoolTString("min16int"); break;
105 case EHTokMin12int: idString = NewPoolTString("min12int"); break;
106 default:
107 return false;
steve-lunarg75fd2232016-11-16 13:22:11 -0700108 }
109
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700110 token.string = idString;
111 token.tokenClass = EHTokIdentifier;
112 token.symbol = nullptr;
113 idToken = token;
114
115 advanceToken();
116
117 return true;
John Kessenichaecd4972016-03-14 10:46:34 -0600118}
119
John Kesseniche01a9bc2016-03-12 20:11:22 -0700120// compilationUnit
121// : list of externalDeclaration
steve-lunargcb88de52016-08-03 07:04:18 -0600122// | SEMICOLONS
John Kesseniche01a9bc2016-03-12 20:11:22 -0700123//
124bool HlslGrammar::acceptCompilationUnit()
125{
John Kessenichd016be12016-03-13 11:24:20 -0600126 TIntermNode* unitNode = nullptr;
127
John Kessenich9c86c6a2016-05-03 22:49:24 -0600128 while (! peekTokenClass(EHTokNone)) {
steve-lunargcb88de52016-08-03 07:04:18 -0600129 // HLSL allows semicolons between global declarations, e.g, between functions.
130 if (acceptTokenClass(EHTokSemicolon))
131 continue;
132
John Kessenichd016be12016-03-13 11:24:20 -0600133 // externalDeclaration
134 TIntermNode* declarationNode;
135 if (! acceptDeclaration(declarationNode))
John Kesseniche01a9bc2016-03-12 20:11:22 -0700136 return false;
John Kessenichd016be12016-03-13 11:24:20 -0600137
138 // hook it up
John Kessenich078d7f22016-03-14 10:02:11 -0600139 unitNode = intermediate.growAggregate(unitNode, declarationNode);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700140 }
141
John Kessenichd016be12016-03-13 11:24:20 -0600142 // set root of AST
John Kessenich078d7f22016-03-14 10:02:11 -0600143 intermediate.setTreeRoot(unitNode);
John Kessenichd016be12016-03-13 11:24:20 -0600144
John Kesseniche01a9bc2016-03-12 20:11:22 -0700145 return true;
146}
147
LoopDawg4886f692016-06-29 10:58:58 -0600148// sampler_state
John Kessenichecba76f2017-01-06 00:34:48 -0700149// : LEFT_BRACE [sampler_state_assignment ... ] RIGHT_BRACE
LoopDawg4886f692016-06-29 10:58:58 -0600150//
151// sampler_state_assignment
152// : sampler_state_identifier EQUAL value SEMICOLON
153//
154// sampler_state_identifier
155// : ADDRESSU
156// | ADDRESSV
157// | ADDRESSW
158// | BORDERCOLOR
159// | FILTER
160// | MAXANISOTROPY
161// | MAXLOD
162// | MINLOD
163// | MIPLODBIAS
164//
165bool HlslGrammar::acceptSamplerState()
166{
167 // TODO: this should be genericized to accept a list of valid tokens and
168 // return token/value pairs. Presently it is specific to texture values.
169
170 if (! acceptTokenClass(EHTokLeftBrace))
171 return true;
172
173 parseContext.warn(token.loc, "unimplemented", "immediate sampler state", "");
John Kessenichecba76f2017-01-06 00:34:48 -0700174
LoopDawg4886f692016-06-29 10:58:58 -0600175 do {
176 // read state name
177 HlslToken state;
178 if (! acceptIdentifier(state))
179 break; // end of list
180
181 // FXC accepts any case
182 TString stateName = *state.string;
183 std::transform(stateName.begin(), stateName.end(), stateName.begin(), ::tolower);
184
185 if (! acceptTokenClass(EHTokAssign)) {
186 expected("assign");
187 return false;
188 }
189
190 if (stateName == "minlod" || stateName == "maxlod") {
191 if (! peekTokenClass(EHTokIntConstant)) {
192 expected("integer");
193 return false;
194 }
195
196 TIntermTyped* lod = nullptr;
197 if (! acceptLiteral(lod)) // should never fail, since we just looked for an integer
198 return false;
199 } else if (stateName == "maxanisotropy") {
200 if (! peekTokenClass(EHTokIntConstant)) {
201 expected("integer");
202 return false;
203 }
204
205 TIntermTyped* maxAnisotropy = nullptr;
206 if (! acceptLiteral(maxAnisotropy)) // should never fail, since we just looked for an integer
207 return false;
208 } else if (stateName == "filter") {
209 HlslToken filterMode;
210 if (! acceptIdentifier(filterMode)) {
211 expected("filter mode");
212 return false;
213 }
214 } else if (stateName == "addressu" || stateName == "addressv" || stateName == "addressw") {
215 HlslToken addrMode;
216 if (! acceptIdentifier(addrMode)) {
217 expected("texture address mode");
218 return false;
219 }
220 } else if (stateName == "miplodbias") {
221 TIntermTyped* lodBias = nullptr;
222 if (! acceptLiteral(lodBias)) {
223 expected("lod bias");
224 return false;
225 }
226 } else if (stateName == "bordercolor") {
227 return false;
228 } else {
229 expected("texture state");
230 return false;
231 }
232
233 // SEMICOLON
234 if (! acceptTokenClass(EHTokSemicolon)) {
235 expected("semicolon");
236 return false;
237 }
238 } while (true);
239
240 if (! acceptTokenClass(EHTokRightBrace))
241 return false;
242
243 return true;
244}
245
246// sampler_declaration_dx9
247// : SAMPLER identifier EQUAL sampler_type sampler_state
248//
John Kesseniche4821e42016-07-16 10:19:43 -0600249bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/)
LoopDawg4886f692016-06-29 10:58:58 -0600250{
251 if (! acceptTokenClass(EHTokSampler))
252 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700253
LoopDawg4886f692016-06-29 10:58:58 -0600254 // TODO: remove this when DX9 style declarations are implemented.
255 unimplemented("Direct3D 9 sampler declaration");
256
257 // read sampler name
258 HlslToken name;
259 if (! acceptIdentifier(name)) {
260 expected("sampler name");
261 return false;
262 }
263
264 if (! acceptTokenClass(EHTokAssign)) {
265 expected("=");
266 return false;
267 }
268
269 return false;
270}
271
John Kesseniche01a9bc2016-03-12 20:11:22 -0700272// declaration
LoopDawg4886f692016-06-29 10:58:58 -0600273// : sampler_declaration_dx9 post_decls SEMICOLON
274// | fully_specified_type declarator_list SEMICOLON
John Kessenich630dd7d2016-06-12 23:52:12 -0600275// | fully_specified_type identifier function_parameters post_decls compound_statement // function definition
LoopDawg4886f692016-06-29 10:58:58 -0600276// | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition
John Kessenich5e69ec62016-07-05 00:02:40 -0600277// | typedef declaration
John Kessenich87142c72016-03-12 20:24:24 -0700278//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600279// declarator_list
280// : declarator COMMA declarator COMMA declarator... // zero or more declarators
John Kessenich532543c2016-07-01 19:06:44 -0600281//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600282// declarator
John Kessenich532543c2016-07-01 19:06:44 -0600283// : identifier array_specifier post_decls
284// | identifier array_specifier post_decls EQUAL assignment_expression
John Kessenichd5ed0b62016-07-04 17:32:45 -0600285// | identifier function_parameters post_decls // function prototype
John Kessenich532543c2016-07-01 19:06:44 -0600286//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600287// Parsing has to go pretty far in to know whether it's a variable, prototype, or
288// function definition, so the implementation below doesn't perfectly divide up the grammar
John Kessenich532543c2016-07-01 19:06:44 -0600289// as above. (The 'identifier' in the first item in init_declarator list is the
290// same as 'identifier' for function declarations.)
291//
292// 'node' could get populated if the declaration creates code, like an initializer
John Kessenichd016be12016-03-13 11:24:20 -0600293// or a function body.
294//
295bool HlslGrammar::acceptDeclaration(TIntermNode*& node)
John Kesseniche01a9bc2016-03-12 20:11:22 -0700296{
John Kessenichd016be12016-03-13 11:24:20 -0600297 node = nullptr;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600298 bool list = false;
John Kessenichd016be12016-03-13 11:24:20 -0600299
steve-lunarg1868b142016-10-20 13:07:10 -0600300 // attributes
301 TAttributeMap attributes;
302 acceptAttributes(attributes);
303
John Kessenich5e69ec62016-07-05 00:02:40 -0600304 // typedef
305 bool typedefDecl = acceptTokenClass(EHTokTypedef);
306
John Kesseniche82061d2016-09-27 14:38:57 -0600307 TType declaredType;
LoopDawg4886f692016-06-29 10:58:58 -0600308
309 // DX9 sampler declaration use a different syntax
John Kessenich267590d2016-08-05 17:34:34 -0600310 // DX9 shaders need to run through HLSL compiler (fxc) via a back compat mode, it isn't going to
311 // be possible to simultaneously compile D3D10+ style shaders and DX9 shaders. If we want to compile DX9
312 // HLSL shaders, this will have to be a master level switch
313 // As such, the sampler keyword in D3D10+ turns into an automatic sampler type, and is commonly used
John Kessenichecba76f2017-01-06 00:34:48 -0700314 // For that reason, this line is commented out
Dan Bakerc7e50162016-08-05 14:52:38 -0400315
John Kesseniche82061d2016-09-27 14:38:57 -0600316 // if (acceptSamplerDeclarationDX9(declaredType))
Dan Bakerc7e50162016-08-05 14:52:38 -0400317 // return true;
LoopDawg4886f692016-06-29 10:58:58 -0600318
319 // fully_specified_type
John Kesseniche82061d2016-09-27 14:38:57 -0600320 if (! acceptFullySpecifiedType(declaredType))
John Kessenich87142c72016-03-12 20:24:24 -0700321 return false;
LoopDawg4886f692016-06-29 10:58:58 -0600322
John Kessenich87142c72016-03-12 20:24:24 -0700323 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -0600324 HlslToken idToken;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600325 while (acceptIdentifier(idToken)) {
steve-lunargf1e0c872016-10-31 15:13:43 -0600326 TString* fnName = idToken.string;
327
328 // Potentially rename shader entry point function. No-op most of the time.
329 parseContext.renameShaderFunction(fnName);
330
John Kessenich5f934b02016-03-13 17:58:25 -0600331 // function_parameters
steve-lunargf1e0c872016-10-31 15:13:43 -0600332 TFunction& function = *new TFunction(fnName, declaredType);
John Kessenich9e079532016-09-02 20:05:19 -0600333 if (acceptFunctionParameters(function)) {
John Kessenich630dd7d2016-06-12 23:52:12 -0600334 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -0600335 acceptPostDecls(function.getWritableType().getQualifier());
John Kessenich078d7f22016-03-14 10:02:11 -0600336
John Kessenichd5ed0b62016-07-04 17:32:45 -0600337 // compound_statement (function body definition) or just a prototype?
338 if (peekTokenClass(EHTokLeftBrace)) {
339 if (list)
340 parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", "");
John Kessenich5e69ec62016-07-05 00:02:40 -0600341 if (typedefDecl)
342 parseContext.error(idToken.loc, "function body can't be in a typedef", "{", "");
steve-lunarg1868b142016-10-20 13:07:10 -0600343 return acceptFunctionDefinition(function, node, attributes);
John Kessenich5e69ec62016-07-05 00:02:40 -0600344 } else {
345 if (typedefDecl)
346 parseContext.error(idToken.loc, "function typedefs not implemented", "{", "");
John Kessenich9e079532016-09-02 20:05:19 -0600347 parseContext.handleFunctionDeclarator(idToken.loc, function, true);
John Kessenich5e69ec62016-07-05 00:02:40 -0600348 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600349 } else {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600350 // A variable declaration. Fix the storage qualifier if it's a global.
351 if (declaredType.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel())
352 declaredType.getQualifier().storage = EvqUniform;
353
John Kessenichecba76f2017-01-06 00:34:48 -0700354 // We can handle multiple variables per type declaration, so
John Kesseniche82061d2016-09-27 14:38:57 -0600355 // the number of types can expand when arrayness is different.
356 TType variableType;
357 variableType.shallowCopy(declaredType);
John Kessenich5f934b02016-03-13 17:58:25 -0600358
John Kesseniche82061d2016-09-27 14:38:57 -0600359 // recognize array_specifier
John Kessenichd5ed0b62016-07-04 17:32:45 -0600360 TArraySizes* arraySizes = nullptr;
361 acceptArraySpecifier(arraySizes);
John Kessenich5f934b02016-03-13 17:58:25 -0600362
John Kesseniche82061d2016-09-27 14:38:57 -0600363 // Fix arrayness in the variableType
364 if (declaredType.isImplicitlySizedArray()) {
365 // Because "int[] a = int[2](...), b = int[3](...)" makes two arrays a and b
366 // of different sizes, for this case sharing the shallow copy of arrayness
367 // with the parseType oversubscribes it, so get a deep copy of the arrayness.
368 variableType.newArraySizes(declaredType.getArraySizes());
369 }
370 if (arraySizes || variableType.isArray()) {
371 // In the most general case, arrayness is potentially coming both from the
372 // declared type and from the variable: "int[] a[];" or just one or the other.
373 // Merge it all to the variableType, so all arrayness is part of the variableType.
374 parseContext.arrayDimMerge(variableType, arraySizes);
375 }
376
LoopDawg4886f692016-06-29 10:58:58 -0600377 // samplers accept immediate sampler state
John Kesseniche82061d2016-09-27 14:38:57 -0600378 if (variableType.getBasicType() == EbtSampler) {
LoopDawg4886f692016-06-29 10:58:58 -0600379 if (! acceptSamplerState())
380 return false;
381 }
382
John Kessenichd5ed0b62016-07-04 17:32:45 -0600383 // post_decls
John Kesseniche82061d2016-09-27 14:38:57 -0600384 acceptPostDecls(variableType.getQualifier());
John Kessenichd5ed0b62016-07-04 17:32:45 -0600385
386 // EQUAL assignment_expression
387 TIntermTyped* expressionNode = nullptr;
388 if (acceptTokenClass(EHTokAssign)) {
John Kessenich5e69ec62016-07-05 00:02:40 -0600389 if (typedefDecl)
390 parseContext.error(idToken.loc, "can't have an initializer", "typedef", "");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600391 if (! acceptAssignmentExpression(expressionNode)) {
392 expected("initializer");
393 return false;
394 }
395 }
396
John Kessenich6dbc0a72016-09-27 19:13:05 -0600397 // Hand off the actual declaration
398
399 // TODO: things scoped within an annotation need their own name space;
400 // TODO: strings are not yet handled.
401 if (variableType.getBasicType() != EbtString && parseContext.getAnnotationNestingLevel() == 0) {
402 if (typedefDecl)
403 parseContext.declareTypedef(idToken.loc, *idToken.string, variableType);
404 else if (variableType.getBasicType() == EbtBlock)
405 parseContext.declareBlock(idToken.loc, variableType, idToken.string);
406 else {
steve-lunarga2b01a02016-11-28 17:09:54 -0700407 if (variableType.getQualifier().storage == EvqUniform && ! variableType.containsOpaque()) {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600408 // this isn't really an individual variable, but a member of the $Global buffer
409 parseContext.growGlobalUniformBlock(idToken.loc, variableType, *idToken.string);
410 } else {
411 // Declare the variable and add any initializer code to the AST.
412 // The top-level node is always made into an aggregate, as that's
413 // historically how the AST has been.
414 node = intermediate.growAggregate(node,
415 parseContext.declareVariable(idToken.loc, *idToken.string, variableType,
416 expressionNode),
417 idToken.loc);
418 }
419 }
John Kessenich5e69ec62016-07-05 00:02:40 -0600420 }
John Kessenich5f934b02016-03-13 17:58:25 -0600421 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600422
423 if (acceptTokenClass(EHTokComma)) {
424 list = true;
425 continue;
426 }
427 };
428
429 // The top-level node is a sequence.
430 if (node != nullptr)
431 node->getAsAggregate()->setOperator(EOpSequence);
John Kessenich87142c72016-03-12 20:24:24 -0700432
John Kessenich078d7f22016-03-14 10:02:11 -0600433 // SEMICOLON
John Kessenichd5ed0b62016-07-04 17:32:45 -0600434 if (! acceptTokenClass(EHTokSemicolon)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700435 // This may have been a false detection of what appeared to be a declaration, but
436 // was actually an assignment such as "float = 4", where "float" is an identifier.
437 // We put the token back to let further parsing happen for cases where that may
438 // happen. This errors on the side of caution, and mostly triggers the error.
439
440 if (peek() == EHTokAssign || peek() == EHTokLeftBracket || peek() == EHTokDot || peek() == EHTokComma)
441 recedeToken();
442 else
443 expected(";");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600444 return false;
445 }
John Kessenichecba76f2017-01-06 00:34:48 -0700446
John Kesseniche01a9bc2016-03-12 20:11:22 -0700447 return true;
448}
449
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600450// control_declaration
451// : fully_specified_type identifier EQUAL expression
452//
453bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node)
454{
455 node = nullptr;
456
457 // fully_specified_type
458 TType type;
459 if (! acceptFullySpecifiedType(type))
460 return false;
461
462 // identifier
463 HlslToken idToken;
464 if (! acceptIdentifier(idToken)) {
465 expected("identifier");
466 return false;
467 }
468
469 // EQUAL
470 TIntermTyped* expressionNode = nullptr;
471 if (! acceptTokenClass(EHTokAssign)) {
472 expected("=");
473 return false;
474 }
475
476 // expression
477 if (! acceptExpression(expressionNode)) {
478 expected("initializer");
479 return false;
480 }
481
John Kesseniche82061d2016-09-27 14:38:57 -0600482 node = parseContext.declareVariable(idToken.loc, *idToken.string, type, expressionNode);
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600483
484 return true;
485}
486
John Kessenich87142c72016-03-12 20:24:24 -0700487// fully_specified_type
488// : type_specifier
489// | type_qualifier type_specifier
490//
491bool HlslGrammar::acceptFullySpecifiedType(TType& type)
492{
493 // type_qualifier
494 TQualifier qualifier;
495 qualifier.clear();
John Kessenichb9e39122016-08-17 10:22:08 -0600496 if (! acceptQualifier(qualifier))
497 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600498 TSourceLoc loc = token.loc;
John Kessenich87142c72016-03-12 20:24:24 -0700499
500 // type_specifier
steve-lunarga64ed3e2016-12-18 17:51:14 -0700501 if (! acceptType(type)) {
502 // If this is not a type, we may have inadvertently gone down a wrong path
steve-lunarg132d3312016-12-19 15:48:01 -0700503 // by parsing "sample", which can be treated like either an identifier or a
steve-lunarga64ed3e2016-12-18 17:51:14 -0700504 // qualifier. Back it out, if we did.
505 if (qualifier.sample)
506 recedeToken();
507
John Kessenich87142c72016-03-12 20:24:24 -0700508 return false;
steve-lunarga64ed3e2016-12-18 17:51:14 -0700509 }
John Kessenich3d157c52016-07-25 16:05:33 -0600510 if (type.getBasicType() == EbtBlock) {
511 // the type was a block, which set some parts of the qualifier
John Kessenich34e7ee72016-09-16 17:10:39 -0600512 parseContext.mergeQualifiers(type.getQualifier(), qualifier);
John Kessenich3d157c52016-07-25 16:05:33 -0600513 // further, it can create an anonymous instance of the block
514 if (peekTokenClass(EHTokSemicolon))
515 parseContext.declareBlock(loc, type);
steve-lunargbb0183f2016-10-04 16:58:14 -0600516 } else {
517 // Some qualifiers are set when parsing the type. Merge those with
518 // whatever comes from acceptQualifier.
519 assert(qualifier.layoutFormat == ElfNone);
steve-lunargf49cdf42016-11-17 15:04:20 -0700520
steve-lunargbb0183f2016-10-04 16:58:14 -0600521 qualifier.layoutFormat = type.getQualifier().layoutFormat;
steve-lunarg3226b082016-10-26 19:18:55 -0600522 qualifier.precision = type.getQualifier().precision;
steve-lunargf49cdf42016-11-17 15:04:20 -0700523
524 if (type.getQualifier().storage == EvqVaryingOut)
525 qualifier.storage = type.getQualifier().storage;
526
527 type.getQualifier() = qualifier;
steve-lunargbb0183f2016-10-04 16:58:14 -0600528 }
John Kessenich87142c72016-03-12 20:24:24 -0700529
530 return true;
531}
532
John Kessenich630dd7d2016-06-12 23:52:12 -0600533// type_qualifier
534// : qualifier qualifier ...
535//
536// Zero or more of these, so this can't return false.
537//
John Kessenichb9e39122016-08-17 10:22:08 -0600538bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
John Kessenich87142c72016-03-12 20:24:24 -0700539{
John Kessenich630dd7d2016-06-12 23:52:12 -0600540 do {
541 switch (peek()) {
542 case EHTokStatic:
John Kessenich6dbc0a72016-09-27 19:13:05 -0600543 qualifier.storage = parseContext.symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
John Kessenich630dd7d2016-06-12 23:52:12 -0600544 break;
545 case EHTokExtern:
546 // TODO: no meaning in glslang?
547 break;
548 case EHTokShared:
549 // TODO: hint
550 break;
551 case EHTokGroupShared:
552 qualifier.storage = EvqShared;
553 break;
554 case EHTokUniform:
555 qualifier.storage = EvqUniform;
556 break;
557 case EHTokConst:
558 qualifier.storage = EvqConst;
559 break;
560 case EHTokVolatile:
561 qualifier.volatil = true;
562 break;
563 case EHTokLinear:
John Kessenich630dd7d2016-06-12 23:52:12 -0600564 qualifier.smooth = true;
565 break;
566 case EHTokCentroid:
567 qualifier.centroid = true;
568 break;
569 case EHTokNointerpolation:
570 qualifier.flat = true;
571 break;
572 case EHTokNoperspective:
573 qualifier.nopersp = true;
574 break;
575 case EHTokSample:
576 qualifier.sample = true;
577 break;
578 case EHTokRowMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600579 qualifier.layoutMatrix = ElmColumnMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600580 break;
581 case EHTokColumnMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600582 qualifier.layoutMatrix = ElmRowMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600583 break;
584 case EHTokPrecise:
585 qualifier.noContraction = true;
586 break;
LoopDawg9249c702016-07-12 20:44:32 -0600587 case EHTokIn:
588 qualifier.storage = EvqIn;
589 break;
590 case EHTokOut:
591 qualifier.storage = EvqOut;
592 break;
593 case EHTokInOut:
594 qualifier.storage = EvqInOut;
595 break;
John Kessenichb9e39122016-08-17 10:22:08 -0600596 case EHTokLayout:
597 if (! acceptLayoutQualifierList(qualifier))
598 return false;
599 continue;
steve-lunargf49cdf42016-11-17 15:04:20 -0700600
601 // GS geometries: these are specified on stage input variables, and are an error (not verified here)
602 // for output variables.
603 case EHTokPoint:
604 qualifier.storage = EvqIn;
605 if (!parseContext.handleInputGeometry(token.loc, ElgPoints))
606 return false;
607 break;
608 case EHTokLine:
609 qualifier.storage = EvqIn;
610 if (!parseContext.handleInputGeometry(token.loc, ElgLines))
611 return false;
612 break;
613 case EHTokTriangle:
614 qualifier.storage = EvqIn;
615 if (!parseContext.handleInputGeometry(token.loc, ElgTriangles))
616 return false;
617 break;
618 case EHTokLineAdj:
619 qualifier.storage = EvqIn;
620 if (!parseContext.handleInputGeometry(token.loc, ElgLinesAdjacency))
621 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700622 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700623 case EHTokTriangleAdj:
624 qualifier.storage = EvqIn;
625 if (!parseContext.handleInputGeometry(token.loc, ElgTrianglesAdjacency))
626 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700627 break;
628
John Kessenich630dd7d2016-06-12 23:52:12 -0600629 default:
John Kessenichb9e39122016-08-17 10:22:08 -0600630 return true;
John Kessenich630dd7d2016-06-12 23:52:12 -0600631 }
632 advanceToken();
633 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -0700634}
635
John Kessenichb9e39122016-08-17 10:22:08 -0600636// layout_qualifier_list
John Kesseniche3218e22016-09-05 14:37:03 -0600637// : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
John Kessenichb9e39122016-08-17 10:22:08 -0600638//
639// layout_qualifier
640// : identifier
John Kessenich841db352016-09-02 21:12:23 -0600641// | identifier EQUAL expression
John Kessenichb9e39122016-08-17 10:22:08 -0600642//
643// Zero or more of these, so this can't return false.
644//
645bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
646{
647 if (! acceptTokenClass(EHTokLayout))
648 return false;
649
650 // LEFT_PAREN
651 if (! acceptTokenClass(EHTokLeftParen))
652 return false;
653
654 do {
655 // identifier
656 HlslToken idToken;
657 if (! acceptIdentifier(idToken))
658 break;
659
660 // EQUAL expression
661 if (acceptTokenClass(EHTokAssign)) {
662 TIntermTyped* expr;
663 if (! acceptConditionalExpression(expr)) {
664 expected("expression");
665 return false;
666 }
667 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
668 } else
669 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
670
671 // COMMA
672 if (! acceptTokenClass(EHTokComma))
673 break;
674 } while (true);
675
676 // RIGHT_PAREN
677 if (! acceptTokenClass(EHTokRightParen)) {
678 expected(")");
679 return false;
680 }
681
682 return true;
683}
684
LoopDawg6daaa4f2016-06-23 19:13:48 -0600685// template_type
686// : FLOAT
687// | DOUBLE
688// | INT
689// | DWORD
690// | UINT
691// | BOOL
692//
steve-lunargf49cdf42016-11-17 15:04:20 -0700693bool HlslGrammar::acceptTemplateVecMatBasicType(TBasicType& basicType)
LoopDawg6daaa4f2016-06-23 19:13:48 -0600694{
695 switch (peek()) {
696 case EHTokFloat:
697 basicType = EbtFloat;
698 break;
699 case EHTokDouble:
700 basicType = EbtDouble;
701 break;
702 case EHTokInt:
703 case EHTokDword:
704 basicType = EbtInt;
705 break;
706 case EHTokUint:
707 basicType = EbtUint;
708 break;
709 case EHTokBool:
710 basicType = EbtBool;
711 break;
712 default:
713 return false;
714 }
715
716 advanceToken();
717
718 return true;
719}
720
721// vector_template_type
722// : VECTOR
723// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
724//
725bool HlslGrammar::acceptVectorTemplateType(TType& type)
726{
727 if (! acceptTokenClass(EHTokVector))
728 return false;
729
730 if (! acceptTokenClass(EHTokLeftAngle)) {
731 // in HLSL, 'vector' alone means float4.
732 new(&type) TType(EbtFloat, EvqTemporary, 4);
733 return true;
734 }
735
736 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700737 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600738 expected("scalar type");
739 return false;
740 }
741
742 // COMMA
743 if (! acceptTokenClass(EHTokComma)) {
744 expected(",");
745 return false;
746 }
747
748 // integer
749 if (! peekTokenClass(EHTokIntConstant)) {
750 expected("literal integer");
751 return false;
752 }
753
754 TIntermTyped* vecSize;
755 if (! acceptLiteral(vecSize))
756 return false;
757
758 const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
759
760 new(&type) TType(basicType, EvqTemporary, vecSizeI);
761
762 if (vecSizeI == 1)
763 type.makeVector();
764
765 if (!acceptTokenClass(EHTokRightAngle)) {
766 expected("right angle bracket");
767 return false;
768 }
769
770 return true;
771}
772
773// matrix_template_type
774// : MATRIX
775// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
776//
777bool HlslGrammar::acceptMatrixTemplateType(TType& type)
778{
779 if (! acceptTokenClass(EHTokMatrix))
780 return false;
781
782 if (! acceptTokenClass(EHTokLeftAngle)) {
783 // in HLSL, 'matrix' alone means float4x4.
784 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
785 return true;
786 }
787
788 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700789 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600790 expected("scalar type");
791 return false;
792 }
793
794 // COMMA
795 if (! acceptTokenClass(EHTokComma)) {
796 expected(",");
797 return false;
798 }
799
800 // integer rows
801 if (! peekTokenClass(EHTokIntConstant)) {
802 expected("literal integer");
803 return false;
804 }
805
806 TIntermTyped* rows;
807 if (! acceptLiteral(rows))
808 return false;
809
810 // COMMA
811 if (! acceptTokenClass(EHTokComma)) {
812 expected(",");
813 return false;
814 }
John Kessenichecba76f2017-01-06 00:34:48 -0700815
LoopDawg6daaa4f2016-06-23 19:13:48 -0600816 // integer cols
817 if (! peekTokenClass(EHTokIntConstant)) {
818 expected("literal integer");
819 return false;
820 }
821
822 TIntermTyped* cols;
823 if (! acceptLiteral(cols))
824 return false;
825
826 new(&type) TType(basicType, EvqTemporary, 0,
steve-lunarg297ae212016-08-24 14:36:13 -0600827 rows->getAsConstantUnion()->getConstArray()[0].getIConst(),
828 cols->getAsConstantUnion()->getConstArray()[0].getIConst());
LoopDawg6daaa4f2016-06-23 19:13:48 -0600829
830 if (!acceptTokenClass(EHTokRightAngle)) {
831 expected("right angle bracket");
832 return false;
833 }
834
835 return true;
836}
837
steve-lunargf49cdf42016-11-17 15:04:20 -0700838// layout_geometry
839// : LINESTREAM
840// | POINTSTREAM
841// | TRIANGLESTREAM
842//
843bool HlslGrammar::acceptOutputPrimitiveGeometry(TLayoutGeometry& geometry)
844{
845 // read geometry type
846 const EHlslTokenClass geometryType = peek();
847
848 switch (geometryType) {
849 case EHTokPointStream: geometry = ElgPoints; break;
850 case EHTokLineStream: geometry = ElgLineStrip; break;
851 case EHTokTriangleStream: geometry = ElgTriangleStrip; break;
852 default:
853 return false; // not a layout geometry
854 }
855
856 advanceToken(); // consume the layout keyword
857 return true;
858}
859
860// stream_out_template_type
861// : output_primitive_geometry_type LEFT_ANGLE type RIGHT_ANGLE
862//
863bool HlslGrammar::acceptStreamOutTemplateType(TType& type, TLayoutGeometry& geometry)
864{
865 geometry = ElgNone;
866
867 if (! acceptOutputPrimitiveGeometry(geometry))
868 return false;
869
870 if (! acceptTokenClass(EHTokLeftAngle))
871 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700872
steve-lunargf49cdf42016-11-17 15:04:20 -0700873 if (! acceptType(type)) {
874 expected("stream output type");
875 return false;
876 }
877
878 type.getQualifier().storage = EvqVaryingOut;
879
880 if (! acceptTokenClass(EHTokRightAngle)) {
881 expected("right angle bracket");
882 return false;
883 }
884
885 return true;
886}
John Kessenichecba76f2017-01-06 00:34:48 -0700887
John Kessenicha1e2d492016-09-20 13:22:58 -0600888// annotations
889// : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
John Kessenich86f71382016-09-19 20:23:18 -0600890//
John Kessenicha1e2d492016-09-20 13:22:58 -0600891bool HlslGrammar::acceptAnnotations(TQualifier&)
John Kessenich86f71382016-09-19 20:23:18 -0600892{
John Kessenicha1e2d492016-09-20 13:22:58 -0600893 if (! acceptTokenClass(EHTokLeftAngle))
John Kessenich86f71382016-09-19 20:23:18 -0600894 return false;
895
John Kessenicha1e2d492016-09-20 13:22:58 -0600896 // note that we are nesting a name space
897 parseContext.nestAnnotations();
John Kessenich86f71382016-09-19 20:23:18 -0600898
899 // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
900 do {
901 // eat any extra SEMI_COLON; don't know if the grammar calls for this or not
902 while (acceptTokenClass(EHTokSemicolon))
903 ;
904
905 if (acceptTokenClass(EHTokRightAngle))
John Kessenicha1e2d492016-09-20 13:22:58 -0600906 break;
John Kessenich86f71382016-09-19 20:23:18 -0600907
908 // declaration
909 TIntermNode* node;
910 if (! acceptDeclaration(node)) {
John Kessenicha1e2d492016-09-20 13:22:58 -0600911 expected("declaration in annotation");
John Kessenich86f71382016-09-19 20:23:18 -0600912 return false;
913 }
914 } while (true);
John Kessenicha1e2d492016-09-20 13:22:58 -0600915
916 parseContext.unnestAnnotations();
917 return true;
John Kessenich86f71382016-09-19 20:23:18 -0600918}
LoopDawg6daaa4f2016-06-23 19:13:48 -0600919
LoopDawg4886f692016-06-29 10:58:58 -0600920// sampler_type
921// : SAMPLER
922// | SAMPLER1D
923// | SAMPLER2D
924// | SAMPLER3D
925// | SAMPLERCUBE
926// | SAMPLERSTATE
927// | SAMPLERCOMPARISONSTATE
928bool HlslGrammar::acceptSamplerType(TType& type)
929{
930 // read sampler type
931 const EHlslTokenClass samplerType = peek();
932
LoopDawga78b0292016-07-19 14:28:05 -0600933 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -0600934 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -0600935
LoopDawga78b0292016-07-19 14:28:05 -0600936 bool isShadow = false;
937
LoopDawg4886f692016-06-29 10:58:58 -0600938 switch (samplerType) {
939 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -0600940 case EHTokSampler1d: /*dim = Esd1D*/; break;
941 case EHTokSampler2d: /*dim = Esd2D*/; break;
942 case EHTokSampler3d: /*dim = Esd3D*/; break;
943 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -0600944 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -0600945 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -0600946 default:
947 return false; // not a sampler declaration
948 }
949
950 advanceToken(); // consume the sampler type keyword
951
952 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -0600953
954 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -0600955 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -0600956
957 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
958
959 return true;
960}
961
962// texture_type
963// | BUFFER
964// | TEXTURE1D
965// | TEXTURE1DARRAY
966// | TEXTURE2D
967// | TEXTURE2DARRAY
968// | TEXTURE3D
969// | TEXTURECUBE
970// | TEXTURECUBEARRAY
971// | TEXTURE2DMS
972// | TEXTURE2DMSARRAY
steve-lunargbb0183f2016-10-04 16:58:14 -0600973// | RWBUFFER
974// | RWTEXTURE1D
975// | RWTEXTURE1DARRAY
976// | RWTEXTURE2D
977// | RWTEXTURE2DARRAY
978// | RWTEXTURE3D
979
LoopDawg4886f692016-06-29 10:58:58 -0600980bool HlslGrammar::acceptTextureType(TType& type)
981{
982 const EHlslTokenClass textureType = peek();
983
984 TSamplerDim dim = EsdNone;
985 bool array = false;
986 bool ms = false;
steve-lunargbb0183f2016-10-04 16:58:14 -0600987 bool image = false;
LoopDawg4886f692016-06-29 10:58:58 -0600988
989 switch (textureType) {
990 case EHTokBuffer: dim = EsdBuffer; break;
991 case EHTokTexture1d: dim = Esd1D; break;
992 case EHTokTexture1darray: dim = Esd1D; array = true; break;
993 case EHTokTexture2d: dim = Esd2D; break;
994 case EHTokTexture2darray: dim = Esd2D; array = true; break;
John Kessenichecba76f2017-01-06 00:34:48 -0700995 case EHTokTexture3d: dim = Esd3D; break;
LoopDawg4886f692016-06-29 10:58:58 -0600996 case EHTokTextureCube: dim = EsdCube; break;
997 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
998 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
999 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
steve-lunargbb0183f2016-10-04 16:58:14 -06001000 case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
1001 case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
1002 case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
1003 case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
1004 case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
1005 case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001006 default:
1007 return false; // not a texture declaration
1008 }
1009
1010 advanceToken(); // consume the texture object keyword
1011
1012 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
John Kessenichecba76f2017-01-06 00:34:48 -07001013
LoopDawg4886f692016-06-29 10:58:58 -06001014 TIntermTyped* msCount = nullptr;
1015
steve-lunargbb0183f2016-10-04 16:58:14 -06001016 // texture type: required for multisample types and RWBuffer/RWTextures!
LoopDawg4886f692016-06-29 10:58:58 -06001017 if (acceptTokenClass(EHTokLeftAngle)) {
1018 if (! acceptType(txType)) {
1019 expected("scalar or vector type");
1020 return false;
1021 }
1022
1023 const TBasicType basicRetType = txType.getBasicType() ;
1024
1025 if (basicRetType != EbtFloat && basicRetType != EbtUint && basicRetType != EbtInt) {
1026 unimplemented("basic type in texture");
1027 return false;
1028 }
1029
steve-lunargd53f7172016-07-27 15:46:48 -06001030 // Buffers can handle small mats if they fit in 4 components
1031 if (dim == EsdBuffer && txType.isMatrix()) {
1032 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
1033 expected("components < 4 in matrix buffer type");
1034 return false;
1035 }
1036
1037 // TODO: except we don't handle it yet...
1038 unimplemented("matrix type in buffer");
1039 return false;
1040 }
1041
LoopDawg4886f692016-06-29 10:58:58 -06001042 if (!txType.isScalar() && !txType.isVector()) {
1043 expected("scalar or vector type");
1044 return false;
1045 }
1046
LoopDawg4886f692016-06-29 10:58:58 -06001047 if (ms && acceptTokenClass(EHTokComma)) {
1048 // read sample count for multisample types, if given
1049 if (! peekTokenClass(EHTokIntConstant)) {
1050 expected("multisample count");
1051 return false;
1052 }
1053
1054 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
1055 return false;
1056 }
1057
1058 if (! acceptTokenClass(EHTokRightAngle)) {
1059 expected("right angle bracket");
1060 return false;
1061 }
1062 } else if (ms) {
1063 expected("texture type for multisample");
1064 return false;
steve-lunargbb0183f2016-10-04 16:58:14 -06001065 } else if (image) {
1066 expected("type for RWTexture/RWBuffer");
1067 return false;
LoopDawg4886f692016-06-29 10:58:58 -06001068 }
1069
1070 TArraySizes* arraySizes = nullptr;
steve-lunarg4f2da272016-10-10 15:24:57 -06001071 const bool shadow = false; // declared on the sampler
LoopDawg4886f692016-06-29 10:58:58 -06001072
1073 TSampler sampler;
steve-lunargbb0183f2016-10-04 16:58:14 -06001074 TLayoutFormat format = ElfNone;
steve-lunargd53f7172016-07-27 15:46:48 -06001075
steve-lunarg4f2da272016-10-10 15:24:57 -06001076 // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
1077 if (image || dim == EsdBuffer)
1078 format = parseContext.getLayoutFromTxType(token.loc, txType);
steve-lunargbb0183f2016-10-04 16:58:14 -06001079
1080 // Non-image Buffers are combined
1081 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -06001082 sampler.set(txType.getBasicType(), dim, array);
1083 } else {
1084 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -06001085 if (image) {
1086 sampler.setImage(txType.getBasicType(), dim, array, shadow, ms);
1087 } else {
1088 sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms);
1089 }
steve-lunargd53f7172016-07-27 15:46:48 -06001090 }
steve-lunarg8b0227c2016-10-14 16:40:32 -06001091
1092 // Remember the declared vector size.
1093 sampler.vectorSize = txType.getVectorSize();
John Kessenichecba76f2017-01-06 00:34:48 -07001094
LoopDawg4886f692016-06-29 10:58:58 -06001095 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -06001096 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -06001097
1098 return true;
1099}
1100
John Kessenich87142c72016-03-12 20:24:24 -07001101// If token is for a type, update 'type' with the type information,
1102// and return true and advance.
1103// Otherwise, return false, and don't advance
1104bool HlslGrammar::acceptType(TType& type)
1105{
steve-lunarg3226b082016-10-26 19:18:55 -06001106 // Basic types for min* types, broken out here in case of future
1107 // changes, e.g, to use native halfs.
1108 static const TBasicType min16float_bt = EbtFloat;
1109 static const TBasicType min10float_bt = EbtFloat;
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001110 static const TBasicType half_bt = EbtFloat;
steve-lunarg3226b082016-10-26 19:18:55 -06001111 static const TBasicType min16int_bt = EbtInt;
1112 static const TBasicType min12int_bt = EbtInt;
1113 static const TBasicType min16uint_bt = EbtUint;
1114
John Kessenich9c86c6a2016-05-03 22:49:24 -06001115 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -06001116 case EHTokVector:
1117 return acceptVectorTemplateType(type);
1118 break;
1119
1120 case EHTokMatrix:
1121 return acceptMatrixTemplateType(type);
1122 break;
1123
steve-lunargf49cdf42016-11-17 15:04:20 -07001124 case EHTokPointStream: // fall through
1125 case EHTokLineStream: // ...
1126 case EHTokTriangleStream: // ...
1127 {
1128 TLayoutGeometry geometry;
1129 if (! acceptStreamOutTemplateType(type, geometry))
1130 return false;
1131
1132 if (! parseContext.handleOutputGeometry(token.loc, geometry))
1133 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001134
steve-lunargf49cdf42016-11-17 15:04:20 -07001135 return true;
1136 }
1137
LoopDawg4886f692016-06-29 10:58:58 -06001138 case EHTokSampler: // fall through
1139 case EHTokSampler1d: // ...
1140 case EHTokSampler2d: // ...
1141 case EHTokSampler3d: // ...
1142 case EHTokSamplerCube: // ...
1143 case EHTokSamplerState: // ...
1144 case EHTokSamplerComparisonState: // ...
1145 return acceptSamplerType(type);
1146 break;
1147
1148 case EHTokBuffer: // fall through
1149 case EHTokTexture1d: // ...
1150 case EHTokTexture1darray: // ...
1151 case EHTokTexture2d: // ...
1152 case EHTokTexture2darray: // ...
1153 case EHTokTexture3d: // ...
1154 case EHTokTextureCube: // ...
1155 case EHTokTextureCubearray: // ...
1156 case EHTokTexture2DMS: // ...
1157 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001158 case EHTokRWTexture1d: // ...
1159 case EHTokRWTexture1darray: // ...
1160 case EHTokRWTexture2d: // ...
1161 case EHTokRWTexture2darray: // ...
1162 case EHTokRWTexture3d: // ...
1163 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001164 return acceptTextureType(type);
1165 break;
1166
John Kesseniche6e74942016-06-11 16:43:14 -06001167 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001168 case EHTokCBuffer:
1169 case EHTokTBuffer:
John Kesseniche6e74942016-06-11 16:43:14 -06001170 return acceptStruct(type);
1171 break;
1172
1173 case EHTokIdentifier:
1174 // An identifier could be for a user-defined type.
1175 // Note we cache the symbol table lookup, to save for a later rule
1176 // when this is not a type.
1177 token.symbol = parseContext.symbolTable.find(*token.string);
1178 if (token.symbol && token.symbol->getAsVariable() && token.symbol->getAsVariable()->isUserType()) {
1179 type.shallowCopy(token.symbol->getType());
1180 advanceToken();
1181 return true;
1182 } else
1183 return false;
1184
John Kessenich71351de2016-06-08 12:50:56 -06001185 case EHTokVoid:
1186 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001187 break;
John Kessenich71351de2016-06-08 12:50:56 -06001188
John Kessenicha1e2d492016-09-20 13:22:58 -06001189 case EHTokString:
1190 new(&type) TType(EbtString);
1191 break;
1192
John Kessenich87142c72016-03-12 20:24:24 -07001193 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001194 new(&type) TType(EbtFloat);
1195 break;
John Kessenich87142c72016-03-12 20:24:24 -07001196 case EHTokFloat1:
1197 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001198 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001199 break;
John Kessenich87142c72016-03-12 20:24:24 -07001200 case EHTokFloat2:
1201 new(&type) TType(EbtFloat, EvqTemporary, 2);
1202 break;
1203 case EHTokFloat3:
1204 new(&type) TType(EbtFloat, EvqTemporary, 3);
1205 break;
1206 case EHTokFloat4:
1207 new(&type) TType(EbtFloat, EvqTemporary, 4);
1208 break;
1209
John Kessenich71351de2016-06-08 12:50:56 -06001210 case EHTokDouble:
1211 new(&type) TType(EbtDouble);
1212 break;
1213 case EHTokDouble1:
1214 new(&type) TType(EbtDouble);
1215 type.makeVector();
1216 break;
1217 case EHTokDouble2:
1218 new(&type) TType(EbtDouble, EvqTemporary, 2);
1219 break;
1220 case EHTokDouble3:
1221 new(&type) TType(EbtDouble, EvqTemporary, 3);
1222 break;
1223 case EHTokDouble4:
1224 new(&type) TType(EbtDouble, EvqTemporary, 4);
1225 break;
1226
1227 case EHTokInt:
1228 case EHTokDword:
1229 new(&type) TType(EbtInt);
1230 break;
1231 case EHTokInt1:
1232 new(&type) TType(EbtInt);
1233 type.makeVector();
1234 break;
John Kessenich87142c72016-03-12 20:24:24 -07001235 case EHTokInt2:
1236 new(&type) TType(EbtInt, EvqTemporary, 2);
1237 break;
1238 case EHTokInt3:
1239 new(&type) TType(EbtInt, EvqTemporary, 3);
1240 break;
1241 case EHTokInt4:
1242 new(&type) TType(EbtInt, EvqTemporary, 4);
1243 break;
1244
John Kessenich71351de2016-06-08 12:50:56 -06001245 case EHTokUint:
1246 new(&type) TType(EbtUint);
1247 break;
1248 case EHTokUint1:
1249 new(&type) TType(EbtUint);
1250 type.makeVector();
1251 break;
1252 case EHTokUint2:
1253 new(&type) TType(EbtUint, EvqTemporary, 2);
1254 break;
1255 case EHTokUint3:
1256 new(&type) TType(EbtUint, EvqTemporary, 3);
1257 break;
1258 case EHTokUint4:
1259 new(&type) TType(EbtUint, EvqTemporary, 4);
1260 break;
1261
1262 case EHTokBool:
1263 new(&type) TType(EbtBool);
1264 break;
1265 case EHTokBool1:
1266 new(&type) TType(EbtBool);
1267 type.makeVector();
1268 break;
John Kessenich87142c72016-03-12 20:24:24 -07001269 case EHTokBool2:
1270 new(&type) TType(EbtBool, EvqTemporary, 2);
1271 break;
1272 case EHTokBool3:
1273 new(&type) TType(EbtBool, EvqTemporary, 3);
1274 break;
1275 case EHTokBool4:
1276 new(&type) TType(EbtBool, EvqTemporary, 4);
1277 break;
1278
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001279 case EHTokHalf:
1280 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1281 break;
1282 case EHTokHalf1:
1283 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1284 type.makeVector();
1285 break;
1286 case EHTokHalf2:
1287 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 2);
1288 break;
1289 case EHTokHalf3:
1290 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 3);
1291 break;
1292 case EHTokHalf4:
1293 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 4);
1294 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001295
steve-lunarg3226b082016-10-26 19:18:55 -06001296 case EHTokMin16float:
1297 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1298 break;
1299 case EHTokMin16float1:
1300 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1301 type.makeVector();
1302 break;
1303 case EHTokMin16float2:
1304 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 2);
1305 break;
1306 case EHTokMin16float3:
1307 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 3);
1308 break;
1309 case EHTokMin16float4:
1310 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 4);
1311 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001312
steve-lunarg3226b082016-10-26 19:18:55 -06001313 case EHTokMin10float:
1314 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1315 break;
1316 case EHTokMin10float1:
1317 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1318 type.makeVector();
1319 break;
1320 case EHTokMin10float2:
1321 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 2);
1322 break;
1323 case EHTokMin10float3:
1324 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 3);
1325 break;
1326 case EHTokMin10float4:
1327 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 4);
1328 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001329
steve-lunarg3226b082016-10-26 19:18:55 -06001330 case EHTokMin16int:
1331 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1332 break;
1333 case EHTokMin16int1:
1334 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1335 type.makeVector();
1336 break;
1337 case EHTokMin16int2:
1338 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 2);
1339 break;
1340 case EHTokMin16int3:
1341 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 3);
1342 break;
1343 case EHTokMin16int4:
1344 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 4);
1345 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001346
steve-lunarg3226b082016-10-26 19:18:55 -06001347 case EHTokMin12int:
1348 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1349 break;
1350 case EHTokMin12int1:
1351 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1352 type.makeVector();
1353 break;
1354 case EHTokMin12int2:
1355 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 2);
1356 break;
1357 case EHTokMin12int3:
1358 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 3);
1359 break;
1360 case EHTokMin12int4:
1361 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 4);
1362 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001363
steve-lunarg3226b082016-10-26 19:18:55 -06001364 case EHTokMin16uint:
1365 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1366 break;
1367 case EHTokMin16uint1:
1368 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1369 type.makeVector();
1370 break;
1371 case EHTokMin16uint2:
1372 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 2);
1373 break;
1374 case EHTokMin16uint3:
1375 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 3);
1376 break;
1377 case EHTokMin16uint4:
1378 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 4);
1379 break;
1380
John Kessenich0133c122016-05-20 12:17:26 -06001381 case EHTokInt1x1:
1382 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1383 break;
1384 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001385 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001386 break;
1387 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001388 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001389 break;
1390 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001391 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001392 break;
1393 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001394 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001395 break;
1396 case EHTokInt2x2:
1397 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1398 break;
1399 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001400 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001401 break;
1402 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001403 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001404 break;
1405 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001406 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001407 break;
1408 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001409 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001410 break;
1411 case EHTokInt3x3:
1412 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1413 break;
1414 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001415 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001416 break;
1417 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001418 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001419 break;
1420 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001421 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001422 break;
1423 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001424 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001425 break;
1426 case EHTokInt4x4:
1427 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1428 break;
1429
John Kessenich71351de2016-06-08 12:50:56 -06001430 case EHTokUint1x1:
1431 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1432 break;
1433 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001434 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001435 break;
1436 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001437 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001438 break;
1439 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001440 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001441 break;
1442 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001443 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001444 break;
1445 case EHTokUint2x2:
1446 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1447 break;
1448 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001449 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001450 break;
1451 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001452 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001453 break;
1454 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001455 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001456 break;
1457 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001458 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001459 break;
1460 case EHTokUint3x3:
1461 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1462 break;
1463 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001464 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001465 break;
1466 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001467 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001468 break;
1469 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001470 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001471 break;
1472 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001473 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001474 break;
1475 case EHTokUint4x4:
1476 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1477 break;
1478
1479 case EHTokBool1x1:
1480 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1481 break;
1482 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001483 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001484 break;
1485 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001486 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001487 break;
1488 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001489 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001490 break;
1491 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001492 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001493 break;
1494 case EHTokBool2x2:
1495 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1496 break;
1497 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001498 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001499 break;
1500 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001501 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001502 break;
1503 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001504 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001505 break;
1506 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001507 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001508 break;
1509 case EHTokBool3x3:
1510 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1511 break;
1512 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001513 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001514 break;
1515 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001516 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001517 break;
1518 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001519 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001520 break;
1521 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001522 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001523 break;
1524 case EHTokBool4x4:
1525 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1526 break;
1527
John Kessenich0133c122016-05-20 12:17:26 -06001528 case EHTokFloat1x1:
1529 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1530 break;
1531 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001532 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001533 break;
1534 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001535 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001536 break;
1537 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001538 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001539 break;
1540 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001541 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001542 break;
John Kessenich87142c72016-03-12 20:24:24 -07001543 case EHTokFloat2x2:
1544 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1545 break;
1546 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001547 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001548 break;
1549 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001550 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001551 break;
John Kessenich0133c122016-05-20 12:17:26 -06001552 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001553 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001554 break;
John Kessenich87142c72016-03-12 20:24:24 -07001555 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001556 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001557 break;
1558 case EHTokFloat3x3:
1559 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1560 break;
1561 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001562 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001563 break;
John Kessenich0133c122016-05-20 12:17:26 -06001564 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001565 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001566 break;
John Kessenich87142c72016-03-12 20:24:24 -07001567 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001568 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001569 break;
1570 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001571 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001572 break;
1573 case EHTokFloat4x4:
1574 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1575 break;
1576
John Kessenich0133c122016-05-20 12:17:26 -06001577 case EHTokDouble1x1:
1578 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1579 break;
1580 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001581 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001582 break;
1583 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001584 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001585 break;
1586 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001587 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001588 break;
1589 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001590 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001591 break;
1592 case EHTokDouble2x2:
1593 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1594 break;
1595 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001596 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001597 break;
1598 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001599 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001600 break;
1601 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001602 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001603 break;
1604 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001605 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001606 break;
1607 case EHTokDouble3x3:
1608 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1609 break;
1610 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001611 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001612 break;
1613 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001614 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001615 break;
1616 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001617 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001618 break;
1619 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001620 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001621 break;
1622 case EHTokDouble4x4:
1623 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1624 break;
1625
John Kessenich87142c72016-03-12 20:24:24 -07001626 default:
1627 return false;
1628 }
1629
1630 advanceToken();
1631
1632 return true;
1633}
1634
John Kesseniche6e74942016-06-11 16:43:14 -06001635// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001636// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1637// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1638//
1639// struct_type
1640// : STRUCT
1641// | CBUFFER
1642// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001643//
1644bool HlslGrammar::acceptStruct(TType& type)
1645{
John Kessenichb804de62016-09-05 12:19:18 -06001646 // This storage qualifier will tell us whether it's an AST
1647 // block type or just a generic structure type.
1648 TStorageQualifier storageQualifier = EvqTemporary;
John Kessenich3d157c52016-07-25 16:05:33 -06001649
1650 // CBUFFER
1651 if (acceptTokenClass(EHTokCBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001652 storageQualifier = EvqUniform;
John Kessenich3d157c52016-07-25 16:05:33 -06001653 // TBUFFER
1654 else if (acceptTokenClass(EHTokTBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001655 storageQualifier = EvqBuffer;
John Kesseniche6e74942016-06-11 16:43:14 -06001656 // STRUCT
John Kessenich3d157c52016-07-25 16:05:33 -06001657 else if (! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001658 return false;
1659
1660 // IDENTIFIER
1661 TString structName = "";
1662 if (peekTokenClass(EHTokIdentifier)) {
1663 structName = *token.string;
1664 advanceToken();
1665 }
1666
John Kessenich3d157c52016-07-25 16:05:33 -06001667 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001668 TQualifier postDeclQualifier;
1669 postDeclQualifier.clear();
1670 acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06001671
John Kesseniche6e74942016-06-11 16:43:14 -06001672 // LEFT_BRACE
1673 if (! acceptTokenClass(EHTokLeftBrace)) {
1674 expected("{");
1675 return false;
1676 }
1677
1678 // struct_declaration_list
1679 TTypeList* typeList;
1680 if (! acceptStructDeclarationList(typeList)) {
1681 expected("struct member declarations");
1682 return false;
1683 }
1684
1685 // RIGHT_BRACE
1686 if (! acceptTokenClass(EHTokRightBrace)) {
1687 expected("}");
1688 return false;
1689 }
1690
1691 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001692 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001693 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001694 else {
John Kessenich7735b942016-09-05 12:40:06 -06001695 postDeclQualifier.storage = storageQualifier;
1696 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001697 }
John Kesseniche6e74942016-06-11 16:43:14 -06001698
John Kessenich3d157c52016-07-25 16:05:33 -06001699 // If it was named, which means the type can be reused later, add
1700 // it to the symbol table. (Unless it's a block, in which
1701 // case the name is not a type.)
1702 if (type.getBasicType() != EbtBlock && structName.size() > 0) {
John Kesseniche6e74942016-06-11 16:43:14 -06001703 TVariable* userTypeDef = new TVariable(&structName, type, true);
1704 if (! parseContext.symbolTable.insert(*userTypeDef))
1705 parseContext.error(token.loc, "redefinition", structName.c_str(), "struct");
1706 }
1707
1708 return true;
1709}
1710
1711// struct_declaration_list
1712// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
1713//
1714// struct_declaration
1715// : fully_specified_type struct_declarator COMMA struct_declarator ...
1716//
1717// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06001718// : IDENTIFIER post_decls
1719// | IDENTIFIER array_specifier post_decls
John Kesseniche6e74942016-06-11 16:43:14 -06001720//
1721bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList)
1722{
1723 typeList = new TTypeList();
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001724 HlslToken idToken;
John Kesseniche6e74942016-06-11 16:43:14 -06001725
1726 do {
1727 // success on seeing the RIGHT_BRACE coming up
1728 if (peekTokenClass(EHTokRightBrace))
1729 return true;
1730
1731 // struct_declaration
1732
1733 // fully_specified_type
1734 TType memberType;
1735 if (! acceptFullySpecifiedType(memberType)) {
1736 expected("member type");
1737 return false;
1738 }
1739
1740 // struct_declarator COMMA struct_declarator ...
1741 do {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001742 if (! acceptIdentifier(idToken)) {
John Kesseniche6e74942016-06-11 16:43:14 -06001743 expected("member name");
1744 return false;
1745 }
1746
1747 // add it to the list of members
1748 TTypeLoc member = { new TType(EbtVoid), token.loc };
1749 member.type->shallowCopy(memberType);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001750 member.type->setFieldName(*idToken.string);
John Kesseniche6e74942016-06-11 16:43:14 -06001751 typeList->push_back(member);
1752
John Kesseniche6e74942016-06-11 16:43:14 -06001753 // array_specifier
John Kessenich19b92ff2016-06-19 11:50:34 -06001754 TArraySizes* arraySizes = nullptr;
1755 acceptArraySpecifier(arraySizes);
1756 if (arraySizes)
1757 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06001758
John Kessenich7735b942016-09-05 12:40:06 -06001759 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06001760
John Kesseniche6e74942016-06-11 16:43:14 -06001761 // success on seeing the SEMICOLON coming up
1762 if (peekTokenClass(EHTokSemicolon))
1763 break;
1764
1765 // COMMA
1766 if (! acceptTokenClass(EHTokComma)) {
1767 expected(",");
1768 return false;
1769 }
1770
1771 } while (true);
1772
1773 // SEMI_COLON
1774 if (! acceptTokenClass(EHTokSemicolon)) {
1775 expected(";");
1776 return false;
1777 }
1778
1779 } while (true);
1780}
1781
John Kessenich5f934b02016-03-13 17:58:25 -06001782// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06001783// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06001784// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001785//
1786bool HlslGrammar::acceptFunctionParameters(TFunction& function)
1787{
John Kessenich078d7f22016-03-14 10:02:11 -06001788 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001789 if (! acceptTokenClass(EHTokLeftParen))
1790 return false;
1791
John Kessenich71351de2016-06-08 12:50:56 -06001792 // VOID RIGHT_PAREN
1793 if (! acceptTokenClass(EHTokVoid)) {
1794 do {
1795 // parameter_declaration
1796 if (! acceptParameterDeclaration(function))
1797 break;
John Kessenich5f934b02016-03-13 17:58:25 -06001798
John Kessenich71351de2016-06-08 12:50:56 -06001799 // COMMA
1800 if (! acceptTokenClass(EHTokComma))
1801 break;
1802 } while (true);
1803 }
John Kessenich5f934b02016-03-13 17:58:25 -06001804
John Kessenich078d7f22016-03-14 10:02:11 -06001805 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06001806 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06001807 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06001808 return false;
1809 }
1810
1811 return true;
1812}
1813
steve-lunarg26d31452016-12-23 18:56:57 -07001814// default_parameter_declaration
1815// : EQUAL conditional_expression
1816// : EQUAL initializer
1817bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTyped*& node)
1818{
1819 node = nullptr;
1820
1821 // Valid not to have a default_parameter_declaration
1822 if (!acceptTokenClass(EHTokAssign))
1823 return true;
1824
1825 if (!acceptConditionalExpression(node)) {
1826 if (!acceptInitializer(node))
1827 return false;
1828
1829 // For initializer lists, we have to const-fold into a constructor for the type, so build
1830 // that.
1831 TFunction* constructor = parseContext.handleConstructorCall(token.loc, type);
1832 if (constructor == nullptr) // cannot construct
1833 return false;
1834
1835 TIntermTyped* arguments = nullptr;
John Kessenichecba76f2017-01-06 00:34:48 -07001836 for (int i = 0; i < int(node->getAsAggregate()->getSequence().size()); i++)
steve-lunarg26d31452016-12-23 18:56:57 -07001837 parseContext.handleFunctionArgument(constructor, arguments, node->getAsAggregate()->getSequence()[i]->getAsTyped());
John Kessenichecba76f2017-01-06 00:34:48 -07001838
steve-lunarg26d31452016-12-23 18:56:57 -07001839 node = parseContext.handleFunctionCall(token.loc, constructor, node);
1840 }
1841
1842 // If this is simply a constant, we can use it directly.
1843 if (node->getAsConstantUnion())
1844 return true;
1845
1846 // Otherwise, it has to be const-foldable.
1847 TIntermTyped* origNode = node;
1848
1849 node = intermediate.fold(node->getAsAggregate());
1850
1851 if (node != nullptr && origNode != node)
1852 return true;
1853
1854 parseContext.error(token.loc, "invalid default parameter value", "", "");
1855
1856 return false;
1857}
1858
John Kessenich5f934b02016-03-13 17:58:25 -06001859// parameter_declaration
steve-lunarg26d31452016-12-23 18:56:57 -07001860// : fully_specified_type post_decls [ = default_parameter_declaration ]
1861// | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
John Kessenich5f934b02016-03-13 17:58:25 -06001862//
1863bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
1864{
1865 // fully_specified_type
1866 TType* type = new TType;
1867 if (! acceptFullySpecifiedType(*type))
1868 return false;
1869
1870 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06001871 HlslToken idToken;
1872 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06001873
John Kessenich19b92ff2016-06-19 11:50:34 -06001874 // array_specifier
1875 TArraySizes* arraySizes = nullptr;
1876 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06001877 if (arraySizes) {
1878 if (arraySizes->isImplicit()) {
1879 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
1880 return false;
1881 }
1882
John Kessenich19b92ff2016-06-19 11:50:34 -06001883 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06001884 }
John Kessenich19b92ff2016-06-19 11:50:34 -06001885
1886 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001887 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06001888
steve-lunarg26d31452016-12-23 18:56:57 -07001889 TIntermTyped* defaultValue;
1890 if (!acceptDefaultParameterDeclaration(*type, defaultValue))
1891 return false;
1892
John Kessenich5aa59e22016-06-17 15:50:47 -06001893 parseContext.paramFix(*type);
1894
steve-lunarg26d31452016-12-23 18:56:57 -07001895 // If any prior parameters have default values, all the parameters after that must as well.
1896 if (defaultValue == nullptr && function.getDefaultParamCount() > 0) {
1897 parseContext.error(idToken.loc, "invalid parameter after default value parameters", idToken.string->c_str(), "");
1898 return false;
1899 }
1900
1901 TParameter param = { idToken.string, type, defaultValue };
John Kessenich5f934b02016-03-13 17:58:25 -06001902 function.addParameter(param);
1903
1904 return true;
1905}
1906
1907// Do the work to create the function definition in addition to
1908// parsing the body (compound_statement).
steve-lunarg1868b142016-10-20 13:07:10 -06001909bool HlslGrammar::acceptFunctionDefinition(TFunction& function, TIntermNode*& node, const TAttributeMap& attributes)
John Kessenich5f934b02016-03-13 17:58:25 -06001910{
John Kessenicha3051662016-09-02 19:13:36 -06001911 TFunction& functionDeclarator = parseContext.handleFunctionDeclarator(token.loc, function, false /* not prototype */);
John Kessenich1a4b7752016-09-02 19:05:24 -06001912 TSourceLoc loc = token.loc;
John Kessenich5f934b02016-03-13 17:58:25 -06001913
John Kessenich077e0522016-06-09 02:02:17 -06001914 // This does a pushScope()
steve-lunarg1868b142016-10-20 13:07:10 -06001915 node = parseContext.handleFunctionDefinition(loc, functionDeclarator, attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06001916
1917 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06001918 TIntermNode* functionBody = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06001919 if (acceptCompoundStatement(functionBody)) {
John Kessenicha3051662016-09-02 19:13:36 -06001920 parseContext.handleFunctionBody(loc, functionDeclarator, functionBody, node);
John Kessenich5f934b02016-03-13 17:58:25 -06001921 return true;
1922 }
1923
1924 return false;
1925}
1926
John Kessenich0d2b6de2016-06-05 11:23:11 -06001927// Accept an expression with parenthesis around it, where
1928// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001929// syntactically required ones like in "if ( expression )".
1930//
1931// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06001932//
1933// Note this one is not set up to be speculative; as it gives
1934// errors if not found.
1935//
1936bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
1937{
1938 // LEFT_PAREN
1939 if (! acceptTokenClass(EHTokLeftParen))
1940 expected("(");
1941
John Kessenich5bc4d9a2016-06-20 01:22:38 -06001942 bool decl = false;
1943 TIntermNode* declNode = nullptr;
1944 decl = acceptControlDeclaration(declNode);
1945 if (decl) {
1946 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
1947 expected("initialized declaration");
1948 return false;
1949 } else
1950 expression = declNode->getAsTyped();
1951 } else {
1952 // no declaration
1953 if (! acceptExpression(expression)) {
1954 expected("expression");
1955 return false;
1956 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06001957 }
1958
1959 // RIGHT_PAREN
1960 if (! acceptTokenClass(EHTokRightParen))
1961 expected(")");
1962
1963 return true;
1964}
1965
John Kessenich34fb0362016-05-03 23:17:20 -06001966// The top-level full expression recognizer.
1967//
John Kessenich87142c72016-03-12 20:24:24 -07001968// expression
John Kessenich34fb0362016-05-03 23:17:20 -06001969// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07001970//
1971bool HlslGrammar::acceptExpression(TIntermTyped*& node)
1972{
LoopDawgef764a22016-06-03 09:17:51 -06001973 node = nullptr;
1974
John Kessenich34fb0362016-05-03 23:17:20 -06001975 // assignment_expression
1976 if (! acceptAssignmentExpression(node))
1977 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001978
John Kessenich34fb0362016-05-03 23:17:20 -06001979 if (! peekTokenClass(EHTokComma))
1980 return true;
1981
1982 do {
1983 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06001984 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06001985 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06001986
John Kessenich34fb0362016-05-03 23:17:20 -06001987 // ... assignment_expression
1988 TIntermTyped* rightNode = nullptr;
1989 if (! acceptAssignmentExpression(rightNode)) {
1990 expected("assignment expression");
1991 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06001992 }
1993
John Kessenich34fb0362016-05-03 23:17:20 -06001994 node = intermediate.addComma(node, rightNode, loc);
1995
1996 if (! peekTokenClass(EHTokComma))
1997 return true;
1998 } while (true);
1999}
2000
John Kessenich07354242016-07-01 19:58:06 -06002001// initializer
John Kessenich98ad4852016-11-27 17:39:07 -07002002// : LEFT_BRACE RIGHT_BRACE
2003// | LEFT_BRACE initializer_list RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002004//
2005// initializer_list
2006// : assignment_expression COMMA assignment_expression COMMA ...
2007//
2008bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
2009{
2010 // LEFT_BRACE
2011 if (! acceptTokenClass(EHTokLeftBrace))
2012 return false;
2013
John Kessenich98ad4852016-11-27 17:39:07 -07002014 // RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002015 TSourceLoc loc = token.loc;
John Kessenich98ad4852016-11-27 17:39:07 -07002016 if (acceptTokenClass(EHTokRightBrace)) {
2017 // a zero-length initializer list
2018 node = intermediate.makeAggregate(loc);
2019 return true;
2020 }
2021
2022 // initializer_list
John Kessenich07354242016-07-01 19:58:06 -06002023 node = nullptr;
2024 do {
2025 // assignment_expression
2026 TIntermTyped* expr;
2027 if (! acceptAssignmentExpression(expr)) {
2028 expected("assignment expression in initializer list");
2029 return false;
2030 }
2031 node = intermediate.growAggregate(node, expr, loc);
2032
2033 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002034 if (acceptTokenClass(EHTokComma)) {
2035 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
2036 return true;
John Kessenich07354242016-07-01 19:58:06 -06002037 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002038 }
John Kessenich07354242016-07-01 19:58:06 -06002039
2040 // RIGHT_BRACE
2041 if (acceptTokenClass(EHTokRightBrace))
2042 return true;
2043
2044 expected(", or }");
2045 return false;
2046 } while (true);
2047}
2048
John Kessenich34fb0362016-05-03 23:17:20 -06002049// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06002050// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06002051//
2052// a op (b op (c op d))
2053//
2054// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06002055// : initializer
2056// | conditional_expression
2057// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06002058//
2059bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
2060{
John Kessenich07354242016-07-01 19:58:06 -06002061 // initializer
2062 if (peekTokenClass(EHTokLeftBrace)) {
2063 if (acceptInitializer(node))
2064 return true;
2065
2066 expected("initializer");
2067 return false;
2068 }
2069
John Kessenich00957f82016-07-27 10:39:57 -06002070 // conditional_expression
2071 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06002072 return false;
2073
John Kessenich07354242016-07-01 19:58:06 -06002074 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06002075 TOperator assignOp = HlslOpMap::assignment(peek());
2076 if (assignOp == EOpNull)
2077 return true;
2078
John Kessenich00957f82016-07-27 10:39:57 -06002079 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06002080 TSourceLoc loc = token.loc;
2081 advanceToken();
2082
John Kessenich00957f82016-07-27 10:39:57 -06002083 // conditional_expression assign_op conditional_expression ...
2084 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06002085 // gets the right-to-left associativity.
2086 TIntermTyped* rightNode = nullptr;
2087 if (! acceptAssignmentExpression(rightNode)) {
2088 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06002089 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002090 }
2091
John Kessenichd21baed2016-09-16 03:05:12 -06002092 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
steve-lunarg90707962016-10-07 19:35:40 -06002093 node = parseContext.handleLvalue(loc, "assign", node);
2094
John Kessenichfea226b2016-07-28 17:53:56 -06002095 if (node == nullptr) {
2096 parseContext.error(loc, "could not create assignment", "", "");
2097 return false;
2098 }
John Kessenich34fb0362016-05-03 23:17:20 -06002099
2100 if (! peekTokenClass(EHTokComma))
2101 return true;
2102
2103 return true;
2104}
2105
John Kessenich00957f82016-07-27 10:39:57 -06002106// Accept a conditional expression, which associates right-to-left,
2107// accomplished by the "true" expression calling down to lower
2108// precedence levels than this level.
2109//
2110// conditional_expression
2111// : binary_expression
2112// | binary_expression QUESTION expression COLON assignment_expression
2113//
2114bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
2115{
2116 // binary_expression
2117 if (! acceptBinaryExpression(node, PlLogicalOr))
2118 return false;
2119
2120 if (! acceptTokenClass(EHTokQuestion))
2121 return true;
2122
2123 TIntermTyped* trueNode = nullptr;
2124 if (! acceptExpression(trueNode)) {
2125 expected("expression after ?");
2126 return false;
2127 }
2128 TSourceLoc loc = token.loc;
2129
2130 if (! acceptTokenClass(EHTokColon)) {
2131 expected(":");
2132 return false;
2133 }
2134
2135 TIntermTyped* falseNode = nullptr;
2136 if (! acceptAssignmentExpression(falseNode)) {
2137 expected("expression after :");
2138 return false;
2139 }
2140
2141 node = intermediate.addSelection(node, trueNode, falseNode, loc);
2142
2143 return true;
2144}
2145
John Kessenich34fb0362016-05-03 23:17:20 -06002146// Accept a binary expression, for binary operations that
2147// associate left-to-right. This is, it is implicit, for example
2148//
2149// ((a op b) op c) op d
2150//
2151// binary_expression
2152// : expression op expression op expression ...
2153//
2154// where 'expression' is the next higher level in precedence.
2155//
2156bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
2157{
2158 if (precedenceLevel > PlMul)
2159 return acceptUnaryExpression(node);
2160
2161 // assignment_expression
2162 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
2163 return false;
2164
John Kessenich34fb0362016-05-03 23:17:20 -06002165 do {
John Kessenich64076ed2016-07-28 21:43:17 -06002166 TOperator op = HlslOpMap::binary(peek());
2167 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
2168 if (tokenLevel < precedenceLevel)
2169 return true;
2170
John Kessenich34fb0362016-05-03 23:17:20 -06002171 // ... op
2172 TSourceLoc loc = token.loc;
2173 advanceToken();
2174
2175 // ... expression
2176 TIntermTyped* rightNode = nullptr;
2177 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
2178 expected("expression");
2179 return false;
2180 }
2181
2182 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06002183 if (node == nullptr) {
2184 parseContext.error(loc, "Could not perform requested binary operation", "", "");
2185 return false;
2186 }
John Kessenich34fb0362016-05-03 23:17:20 -06002187 } while (true);
2188}
2189
2190// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06002191// : (type) unary_expression
2192// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06002193// | - unary_expression
2194// | ! unary_expression
2195// | ~ unary_expression
2196// | ++ unary_expression
2197// | -- unary_expression
2198// | postfix_expression
2199//
2200bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
2201{
John Kessenich1cc1a282016-06-03 16:55:49 -06002202 // (type) unary_expression
2203 // Have to look two steps ahead, because this could be, e.g., a
2204 // postfix_expression instead, since that also starts with at "(".
2205 if (acceptTokenClass(EHTokLeftParen)) {
2206 TType castType;
2207 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06002208 if (acceptTokenClass(EHTokRightParen)) {
2209 // We've matched "(type)" now, get the expression to cast
2210 TSourceLoc loc = token.loc;
2211 if (! acceptUnaryExpression(node))
2212 return false;
2213
2214 // Hook it up like a constructor
2215 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
2216 if (constructorFunction == nullptr) {
2217 expected("type that can be constructed");
2218 return false;
2219 }
2220 TIntermTyped* arguments = nullptr;
2221 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
2222 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
2223
2224 return true;
2225 } else {
2226 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
2227 // the '(int' part. We must back up twice.
2228 recedeToken();
2229 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06002230 }
John Kessenich1cc1a282016-06-03 16:55:49 -06002231 } else {
2232 // This isn't a type cast, but it still started "(", so if it is a
2233 // unary expression, it can only be a postfix_expression, so try that.
2234 // Back it up first.
2235 recedeToken();
2236 return acceptPostfixExpression(node);
2237 }
2238 }
2239
2240 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06002241 TOperator unaryOp = HlslOpMap::preUnary(peek());
John Kessenichecba76f2017-01-06 00:34:48 -07002242
John Kessenich1cc1a282016-06-03 16:55:49 -06002243 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06002244 if (unaryOp == EOpNull)
2245 return acceptPostfixExpression(node);
2246
2247 // op unary_expression
2248 TSourceLoc loc = token.loc;
2249 advanceToken();
2250 if (! acceptUnaryExpression(node))
2251 return false;
2252
2253 // + is a no-op
2254 if (unaryOp == EOpAdd)
2255 return true;
2256
2257 node = intermediate.addUnaryMath(unaryOp, node, loc);
steve-lunarge5921f12016-10-15 10:29:58 -06002258
2259 // These unary ops require lvalues
2260 if (unaryOp == EOpPreIncrement || unaryOp == EOpPreDecrement)
2261 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002262
2263 return node != nullptr;
2264}
2265
2266// postfix_expression
2267// : LEFT_PAREN expression RIGHT_PAREN
2268// | literal
2269// | constructor
2270// | identifier
2271// | function_call
2272// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
2273// | postfix_expression DOT IDENTIFIER
2274// | postfix_expression INC_OP
2275// | postfix_expression DEC_OP
2276//
2277bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
2278{
2279 // Not implemented as self-recursive:
2280 // The logical "right recursion" is done with an loop at the end
2281
2282 // idToken will pick up either a variable or a function name in a function call
2283 HlslToken idToken;
2284
John Kessenich21472ae2016-06-04 11:46:33 -06002285 // Find something before the postfix operations, as they can't operate
2286 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07002287 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06002288 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002289 if (! acceptExpression(node)) {
2290 expected("expression");
2291 return false;
2292 }
2293 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002294 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002295 return false;
2296 }
John Kessenich34fb0362016-05-03 23:17:20 -06002297 } else if (acceptLiteral(node)) {
John Kessenichecba76f2017-01-06 00:34:48 -07002298 // literal (nothing else to do yet), go on to the
John Kessenich34fb0362016-05-03 23:17:20 -06002299 } else if (acceptConstructor(node)) {
2300 // constructor (nothing else to do yet)
2301 } else if (acceptIdentifier(idToken)) {
2302 // identifier or function_call name
2303 if (! peekTokenClass(EHTokLeftParen)) {
steve-lunarga64ed3e2016-12-18 17:51:14 -07002304 node = parseContext.handleVariable(idToken.loc, idToken.symbol, idToken.string);
John Kessenich34fb0362016-05-03 23:17:20 -06002305 } else if (acceptFunctionCall(idToken, node)) {
2306 // function_call (nothing else to do yet)
2307 } else {
2308 expected("function call arguments");
2309 return false;
2310 }
John Kessenich21472ae2016-06-04 11:46:33 -06002311 } else {
2312 // nothing found, can't post operate
2313 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002314 }
2315
steve-lunarga2b01a02016-11-28 17:09:54 -07002316 // This is to guarantee we do this no matter how we get out of the stack frame.
2317 // This way there's no bug if an early return forgets to do it.
2318 struct tFinalize {
2319 tFinalize(HlslParseContext& p) : parseContext(p) { }
2320 ~tFinalize() { parseContext.finalizeFlattening(); }
2321 HlslParseContext& parseContext;
2322 } finalize(parseContext);
2323
2324 // Initialize the flattening accumulation data, so we can track data across multiple bracket or
2325 // dot operators. This can also be nested, e.g, for [], so we have to track each nesting
2326 // level: hence the init and finalize. Even though in practice these must be
2327 // constants, they are parsed no matter what.
2328 parseContext.initFlattening();
2329
John Kessenich21472ae2016-06-04 11:46:33 -06002330 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06002331 do {
2332 TSourceLoc loc = token.loc;
2333 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07002334
John Kessenich34fb0362016-05-03 23:17:20 -06002335 // Consume only a valid post-unary operator, otherwise we are done.
2336 switch (postOp) {
2337 case EOpIndexDirectStruct:
2338 case EOpIndexIndirect:
2339 case EOpPostIncrement:
2340 case EOpPostDecrement:
2341 advanceToken();
2342 break;
2343 default:
2344 return true;
2345 }
John Kessenich87142c72016-03-12 20:24:24 -07002346
John Kessenich34fb0362016-05-03 23:17:20 -06002347 // We have a valid post-unary operator, process it.
2348 switch (postOp) {
2349 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06002350 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002351 // DOT IDENTIFIER
2352 // includes swizzles and struct members
John Kessenich93a162a2016-06-17 17:16:27 -06002353 HlslToken field;
2354 if (! acceptIdentifier(field)) {
2355 expected("swizzle or member");
2356 return false;
2357 }
LoopDawg4886f692016-06-29 10:58:58 -06002358
2359 TIntermTyped* base = node; // preserve for method function calls
John Kessenich93a162a2016-06-17 17:16:27 -06002360 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06002361
2362 // In the event of a method node, we look for an open paren and accept the function call.
steve-lunarga2b01a02016-11-28 17:09:54 -07002363 if (node != nullptr && node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) {
LoopDawg4886f692016-06-29 10:58:58 -06002364 if (! acceptFunctionCall(field, node, base)) {
2365 expected("function parameters");
2366 return false;
2367 }
2368 }
2369
John Kessenich34fb0362016-05-03 23:17:20 -06002370 break;
John Kessenich93a162a2016-06-17 17:16:27 -06002371 }
John Kessenich34fb0362016-05-03 23:17:20 -06002372 case EOpIndexIndirect:
2373 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002374 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06002375 TIntermTyped* indexNode = nullptr;
2376 if (! acceptExpression(indexNode) ||
2377 ! peekTokenClass(EHTokRightBracket)) {
2378 expected("expression followed by ']'");
2379 return false;
2380 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002381 advanceToken();
2382 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
2383 break;
John Kessenich34fb0362016-05-03 23:17:20 -06002384 }
2385 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002386 // INC_OP
2387 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06002388 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002389 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06002390 node = intermediate.addUnaryMath(postOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06002391 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002392 break;
2393 default:
2394 assert(0);
2395 break;
2396 }
2397 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07002398}
2399
John Kessenichd016be12016-03-13 11:24:20 -06002400// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06002401// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06002402//
2403bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
2404{
2405 // type
2406 TType type;
2407 if (acceptType(type)) {
2408 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
2409 if (constructorFunction == nullptr)
2410 return false;
2411
2412 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06002413 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06002414 if (! acceptArguments(constructorFunction, arguments)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002415 // It's possible this is a type keyword used as an identifier. Put the token back
2416 // for later use.
2417 recedeToken();
John Kessenichd016be12016-03-13 11:24:20 -06002418 return false;
2419 }
2420
2421 // hook it up
2422 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
2423
2424 return true;
2425 }
2426
2427 return false;
2428}
2429
John Kessenich34fb0362016-05-03 23:17:20 -06002430// The function_call identifier was already recognized, and passed in as idToken.
2431//
2432// function_call
2433// : [idToken] arguments
2434//
LoopDawg4886f692016-06-29 10:58:58 -06002435bool HlslGrammar::acceptFunctionCall(HlslToken idToken, TIntermTyped*& node, TIntermTyped* base)
John Kessenich34fb0362016-05-03 23:17:20 -06002436{
John Kessenich4678ca92016-05-13 09:33:42 -06002437 // arguments
2438 TFunction* function = new TFunction(idToken.string, TType(EbtVoid));
2439 TIntermTyped* arguments = nullptr;
LoopDawg4886f692016-06-29 10:58:58 -06002440
2441 // methods have an implicit first argument of the calling object.
2442 if (base != nullptr)
2443 parseContext.handleFunctionArgument(function, arguments, base);
2444
John Kessenich4678ca92016-05-13 09:33:42 -06002445 if (! acceptArguments(function, arguments))
2446 return false;
2447
2448 node = parseContext.handleFunctionCall(idToken.loc, function, arguments);
2449
2450 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002451}
2452
John Kessenich87142c72016-03-12 20:24:24 -07002453// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002454// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002455//
John Kessenichd016be12016-03-13 11:24:20 -06002456// The arguments are pushed onto the 'function' argument list and
2457// onto the 'arguments' aggregate.
2458//
John Kessenich4678ca92016-05-13 09:33:42 -06002459bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002460{
John Kessenich078d7f22016-03-14 10:02:11 -06002461 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002462 if (! acceptTokenClass(EHTokLeftParen))
2463 return false;
2464
2465 do {
John Kessenichd016be12016-03-13 11:24:20 -06002466 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002467 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002468 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002469 break;
John Kessenichd016be12016-03-13 11:24:20 -06002470
2471 // hook it up
2472 parseContext.handleFunctionArgument(function, arguments, arg);
2473
John Kessenich078d7f22016-03-14 10:02:11 -06002474 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002475 if (! acceptTokenClass(EHTokComma))
2476 break;
2477 } while (true);
2478
John Kessenich078d7f22016-03-14 10:02:11 -06002479 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002480 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002481 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002482 return false;
2483 }
2484
2485 return true;
2486}
2487
2488bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2489{
2490 switch (token.tokenClass) {
2491 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002492 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002493 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002494 case EHTokUintConstant:
2495 node = intermediate.addConstantUnion(token.u, token.loc, true);
2496 break;
John Kessenich87142c72016-03-12 20:24:24 -07002497 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002498 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002499 break;
2500 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002501 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002502 break;
2503 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002504 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002505 break;
John Kessenich86f71382016-09-19 20:23:18 -06002506 case EHTokStringConstant:
2507 node = nullptr;
2508 break;
John Kessenich87142c72016-03-12 20:24:24 -07002509
2510 default:
2511 return false;
2512 }
2513
2514 advanceToken();
2515
2516 return true;
2517}
2518
John Kessenich5f934b02016-03-13 17:58:25 -06002519// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002520// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002521//
John Kessenich21472ae2016-06-04 11:46:33 -06002522bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002523{
John Kessenich21472ae2016-06-04 11:46:33 -06002524 TIntermAggregate* compoundStatement = nullptr;
2525
John Kessenich34fb0362016-05-03 23:17:20 -06002526 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002527 if (! acceptTokenClass(EHTokLeftBrace))
2528 return false;
2529
2530 // statement statement ...
2531 TIntermNode* statement = nullptr;
2532 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002533 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2534 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2535 branch->getFlowOp() == EOpDefault)) {
2536 // hook up individual subsequences within a switch statement
2537 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2538 compoundStatement = nullptr;
2539 } else {
2540 // hook it up to the growing compound statement
2541 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2542 }
John Kessenich5f934b02016-03-13 17:58:25 -06002543 }
John Kessenich34fb0362016-05-03 23:17:20 -06002544 if (compoundStatement)
2545 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06002546
John Kessenich21472ae2016-06-04 11:46:33 -06002547 retStatement = compoundStatement;
2548
John Kessenich34fb0362016-05-03 23:17:20 -06002549 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002550 return acceptTokenClass(EHTokRightBrace);
2551}
2552
John Kessenich0d2b6de2016-06-05 11:23:11 -06002553bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
2554{
2555 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06002556 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06002557 parseContext.popScope();
2558
2559 return result;
2560}
2561
John Kessenich077e0522016-06-09 02:02:17 -06002562bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06002563{
John Kessenich077e0522016-06-09 02:02:17 -06002564 parseContext.pushScope();
2565 bool result = acceptCompoundStatement(statement);
2566 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06002567
2568 return result;
2569}
2570
John Kessenich5f934b02016-03-13 17:58:25 -06002571// statement
John Kessenich21472ae2016-06-04 11:46:33 -06002572// : attributes attributed_statement
2573//
2574// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002575// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002576// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06002577// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06002578// | declaration_statement
2579// | selection_statement
2580// | switch_statement
2581// | case_label
2582// | iteration_statement
2583// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002584//
2585bool HlslGrammar::acceptStatement(TIntermNode*& statement)
2586{
John Kessenich21472ae2016-06-04 11:46:33 -06002587 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06002588
John Kessenich21472ae2016-06-04 11:46:33 -06002589 // attributes
steve-lunarg1868b142016-10-20 13:07:10 -06002590 TAttributeMap attributes;
2591 acceptAttributes(attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06002592
John Kessenich21472ae2016-06-04 11:46:33 -06002593 // attributed_statement
2594 switch (peek()) {
2595 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06002596 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002597
John Kessenich21472ae2016-06-04 11:46:33 -06002598 case EHTokIf:
2599 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002600
John Kessenich21472ae2016-06-04 11:46:33 -06002601 case EHTokSwitch:
2602 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002603
John Kessenich21472ae2016-06-04 11:46:33 -06002604 case EHTokFor:
2605 case EHTokDo:
2606 case EHTokWhile:
2607 return acceptIterationStatement(statement);
2608
2609 case EHTokContinue:
2610 case EHTokBreak:
2611 case EHTokDiscard:
2612 case EHTokReturn:
2613 return acceptJumpStatement(statement);
2614
2615 case EHTokCase:
2616 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06002617 case EHTokDefault:
2618 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06002619
2620 case EHTokSemicolon:
2621 return acceptTokenClass(EHTokSemicolon);
2622
2623 case EHTokRightBrace:
2624 // Performance: not strictly necessary, but stops a bunch of hunting early,
2625 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06002626 return false;
2627
John Kessenich21472ae2016-06-04 11:46:33 -06002628 default:
2629 {
2630 // declaration
2631 if (acceptDeclaration(statement))
2632 return true;
2633
2634 // expression
2635 TIntermTyped* node;
2636 if (acceptExpression(node))
2637 statement = node;
2638 else
2639 return false;
2640
2641 // SEMICOLON (following an expression)
2642 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002643 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06002644 return false;
2645 }
2646 }
2647 }
2648
John Kessenich5f934b02016-03-13 17:58:25 -06002649 return true;
John Kessenich87142c72016-03-12 20:24:24 -07002650}
2651
John Kessenich21472ae2016-06-04 11:46:33 -06002652// attributes
2653// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
2654//
2655// attribute:
2656// : UNROLL
2657// | UNROLL LEFT_PAREN literal RIGHT_PAREN
2658// | FASTOPT
2659// | ALLOW_UAV_CONDITION
2660// | BRANCH
2661// | FLATTEN
2662// | FORCECASE
2663// | CALL
steve-lunarg1868b142016-10-20 13:07:10 -06002664// | DOMAIN
2665// | EARLYDEPTHSTENCIL
2666// | INSTANCE
2667// | MAXTESSFACTOR
2668// | OUTPUTCONTROLPOINTS
2669// | OUTPUTTOPOLOGY
2670// | PARTITIONING
2671// | PATCHCONSTANTFUNC
2672// | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
John Kessenich21472ae2016-06-04 11:46:33 -06002673//
steve-lunarg1868b142016-10-20 13:07:10 -06002674void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06002675{
steve-lunarg1868b142016-10-20 13:07:10 -06002676 // For now, accept the [ XXX(X) ] syntax, but drop all but
2677 // numthreads, which is used to set the CS local size.
John Kessenich0d2b6de2016-06-05 11:23:11 -06002678 // TODO: subset to correct set? Pass on?
2679 do {
steve-lunarg1868b142016-10-20 13:07:10 -06002680 HlslToken idToken;
2681
John Kessenich0d2b6de2016-06-05 11:23:11 -06002682 // LEFT_BRACKET?
2683 if (! acceptTokenClass(EHTokLeftBracket))
2684 return;
2685
2686 // attribute
steve-lunarg1868b142016-10-20 13:07:10 -06002687 if (acceptIdentifier(idToken)) {
2688 // 'idToken.string' is the attribute
John Kessenich0d2b6de2016-06-05 11:23:11 -06002689 } else if (! peekTokenClass(EHTokRightBracket)) {
2690 expected("identifier");
2691 advanceToken();
2692 }
2693
steve-lunarga22f7db2016-11-11 08:17:44 -07002694 TIntermAggregate* expressions = nullptr;
steve-lunarg1868b142016-10-20 13:07:10 -06002695
2696 // (x, ...)
John Kessenich0d2b6de2016-06-05 11:23:11 -06002697 if (acceptTokenClass(EHTokLeftParen)) {
steve-lunarga22f7db2016-11-11 08:17:44 -07002698 expressions = new TIntermAggregate;
steve-lunarg1868b142016-10-20 13:07:10 -06002699
John Kessenich0d2b6de2016-06-05 11:23:11 -06002700 TIntermTyped* node;
steve-lunarga22f7db2016-11-11 08:17:44 -07002701 bool expectingExpression = false;
John Kessenichecba76f2017-01-06 00:34:48 -07002702
steve-lunarga22f7db2016-11-11 08:17:44 -07002703 while (acceptAssignmentExpression(node)) {
2704 expectingExpression = false;
2705 expressions->getSequence().push_back(node);
steve-lunarg1868b142016-10-20 13:07:10 -06002706 if (acceptTokenClass(EHTokComma))
steve-lunarga22f7db2016-11-11 08:17:44 -07002707 expectingExpression = true;
steve-lunarg1868b142016-10-20 13:07:10 -06002708 }
2709
steve-lunarga22f7db2016-11-11 08:17:44 -07002710 // 'expressions' is an aggregate with the expressions in it
John Kessenich0d2b6de2016-06-05 11:23:11 -06002711 if (! acceptTokenClass(EHTokRightParen))
2712 expected(")");
steve-lunarga22f7db2016-11-11 08:17:44 -07002713
2714 // Error for partial or missing expression
2715 if (expectingExpression || expressions->getSequence().empty())
2716 expected("expression");
John Kessenich0d2b6de2016-06-05 11:23:11 -06002717 }
2718
2719 // RIGHT_BRACKET
steve-lunarg1868b142016-10-20 13:07:10 -06002720 if (!acceptTokenClass(EHTokRightBracket)) {
2721 expected("]");
2722 return;
2723 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06002724
steve-lunarg1868b142016-10-20 13:07:10 -06002725 // Add any values we found into the attribute map. This accepts
2726 // (and ignores) values not mapping to a known TAttributeType;
steve-lunarga22f7db2016-11-11 08:17:44 -07002727 attributes.setAttribute(idToken.string, expressions);
John Kessenich0d2b6de2016-06-05 11:23:11 -06002728 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06002729}
2730
John Kessenich0d2b6de2016-06-05 11:23:11 -06002731// selection_statement
2732// : IF LEFT_PAREN expression RIGHT_PAREN statement
2733// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
2734//
John Kessenich21472ae2016-06-04 11:46:33 -06002735bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
2736{
John Kessenich0d2b6de2016-06-05 11:23:11 -06002737 TSourceLoc loc = token.loc;
2738
2739 // IF
2740 if (! acceptTokenClass(EHTokIf))
2741 return false;
2742
2743 // so that something declared in the condition is scoped to the lifetimes
2744 // of the then-else statements
2745 parseContext.pushScope();
2746
2747 // LEFT_PAREN expression RIGHT_PAREN
2748 TIntermTyped* condition;
2749 if (! acceptParenExpression(condition))
2750 return false;
2751
2752 // create the child statements
2753 TIntermNodePair thenElse = { nullptr, nullptr };
2754
2755 // then statement
2756 if (! acceptScopedStatement(thenElse.node1)) {
2757 expected("then statement");
2758 return false;
2759 }
2760
2761 // ELSE
2762 if (acceptTokenClass(EHTokElse)) {
2763 // else statement
2764 if (! acceptScopedStatement(thenElse.node2)) {
2765 expected("else statement");
2766 return false;
2767 }
2768 }
2769
2770 // Put the pieces together
2771 statement = intermediate.addSelection(condition, thenElse, loc);
2772 parseContext.popScope();
2773
2774 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06002775}
2776
John Kessenichd02dc5d2016-07-01 00:04:11 -06002777// switch_statement
2778// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
2779//
John Kessenich21472ae2016-06-04 11:46:33 -06002780bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
2781{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002782 // SWITCH
2783 TSourceLoc loc = token.loc;
2784 if (! acceptTokenClass(EHTokSwitch))
2785 return false;
2786
2787 // LEFT_PAREN expression RIGHT_PAREN
2788 parseContext.pushScope();
2789 TIntermTyped* switchExpression;
2790 if (! acceptParenExpression(switchExpression)) {
2791 parseContext.popScope();
2792 return false;
2793 }
2794
2795 // compound_statement
2796 parseContext.pushSwitchSequence(new TIntermSequence);
2797 bool statementOkay = acceptCompoundStatement(statement);
2798 if (statementOkay)
2799 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
2800
2801 parseContext.popSwitchSequence();
2802 parseContext.popScope();
2803
2804 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06002805}
2806
John Kessenich119f8f62016-06-05 15:44:07 -06002807// iteration_statement
2808// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
2809// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
2810// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
2811//
2812// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06002813bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
2814{
John Kessenich119f8f62016-06-05 15:44:07 -06002815 TSourceLoc loc = token.loc;
2816 TIntermTyped* condition = nullptr;
2817
2818 EHlslTokenClass loop = peek();
2819 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
2820
2821 // WHILE or DO or FOR
2822 advanceToken();
2823
2824 switch (loop) {
2825 case EHTokWhile:
2826 // so that something declared in the condition is scoped to the lifetime
2827 // of the while sub-statement
2828 parseContext.pushScope();
2829 parseContext.nestLooping();
2830
2831 // LEFT_PAREN condition RIGHT_PAREN
2832 if (! acceptParenExpression(condition))
2833 return false;
2834
2835 // statement
2836 if (! acceptScopedStatement(statement)) {
2837 expected("while sub-statement");
2838 return false;
2839 }
2840
2841 parseContext.unnestLooping();
2842 parseContext.popScope();
2843
2844 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
2845
2846 return true;
2847
2848 case EHTokDo:
2849 parseContext.nestLooping();
2850
2851 if (! acceptTokenClass(EHTokLeftBrace))
2852 expected("{");
2853
2854 // statement
2855 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
2856 expected("do sub-statement");
2857 return false;
2858 }
2859
2860 if (! acceptTokenClass(EHTokRightBrace))
2861 expected("}");
2862
2863 // WHILE
2864 if (! acceptTokenClass(EHTokWhile)) {
2865 expected("while");
2866 return false;
2867 }
2868
2869 // LEFT_PAREN condition RIGHT_PAREN
2870 TIntermTyped* condition;
2871 if (! acceptParenExpression(condition))
2872 return false;
2873
2874 if (! acceptTokenClass(EHTokSemicolon))
2875 expected(";");
2876
2877 parseContext.unnestLooping();
2878
2879 statement = intermediate.addLoop(statement, condition, 0, false, loc);
2880
2881 return true;
2882
2883 case EHTokFor:
2884 {
2885 // LEFT_PAREN
2886 if (! acceptTokenClass(EHTokLeftParen))
2887 expected("(");
2888
2889 // so that something declared in the condition is scoped to the lifetime
2890 // of the for sub-statement
2891 parseContext.pushScope();
2892
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002893 // initializer
2894 TIntermNode* initNode = nullptr;
2895 if (! acceptControlDeclaration(initNode)) {
2896 TIntermTyped* initExpr = nullptr;
2897 acceptExpression(initExpr);
2898 initNode = initExpr;
2899 }
2900 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06002901 if (! acceptTokenClass(EHTokSemicolon))
2902 expected(";");
2903
2904 parseContext.nestLooping();
2905
2906 // condition SEMI_COLON
2907 acceptExpression(condition);
2908 if (! acceptTokenClass(EHTokSemicolon))
2909 expected(";");
2910
2911 // iterator SEMI_COLON
2912 TIntermTyped* iterator = nullptr;
2913 acceptExpression(iterator);
2914 if (! acceptTokenClass(EHTokRightParen))
2915 expected(")");
2916
2917 // statement
2918 if (! acceptScopedStatement(statement)) {
2919 expected("for sub-statement");
2920 return false;
2921 }
2922
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002923 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06002924
2925 parseContext.popScope();
2926 parseContext.unnestLooping();
2927
2928 return true;
2929 }
2930
2931 default:
2932 return false;
2933 }
John Kessenich21472ae2016-06-04 11:46:33 -06002934}
2935
2936// jump_statement
2937// : CONTINUE SEMICOLON
2938// | BREAK SEMICOLON
2939// | DISCARD SEMICOLON
2940// | RETURN SEMICOLON
2941// | RETURN expression SEMICOLON
2942//
2943bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
2944{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002945 EHlslTokenClass jump = peek();
2946 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06002947 case EHTokContinue:
2948 case EHTokBreak:
2949 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06002950 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002951 advanceToken();
2952 break;
John Kessenich21472ae2016-06-04 11:46:33 -06002953 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002954 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06002955 return false;
2956 }
John Kessenich21472ae2016-06-04 11:46:33 -06002957
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002958 switch (jump) {
2959 case EHTokContinue:
2960 statement = intermediate.addBranch(EOpContinue, token.loc);
2961 break;
2962 case EHTokBreak:
2963 statement = intermediate.addBranch(EOpBreak, token.loc);
2964 break;
2965 case EHTokDiscard:
2966 statement = intermediate.addBranch(EOpKill, token.loc);
2967 break;
2968
2969 case EHTokReturn:
2970 {
2971 // expression
2972 TIntermTyped* node;
2973 if (acceptExpression(node)) {
2974 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06002975 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002976 } else
2977 statement = intermediate.addBranch(EOpReturn, token.loc);
2978 break;
2979 }
2980
2981 default:
2982 assert(0);
2983 return false;
2984 }
2985
2986 // SEMICOLON
2987 if (! acceptTokenClass(EHTokSemicolon))
2988 expected(";");
John Kessenichecba76f2017-01-06 00:34:48 -07002989
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002990 return true;
2991}
John Kessenich21472ae2016-06-04 11:46:33 -06002992
John Kessenichd02dc5d2016-07-01 00:04:11 -06002993// case_label
2994// : CASE expression COLON
2995//
John Kessenich21472ae2016-06-04 11:46:33 -06002996bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
2997{
John Kessenichd02dc5d2016-07-01 00:04:11 -06002998 TSourceLoc loc = token.loc;
2999 if (! acceptTokenClass(EHTokCase))
3000 return false;
3001
3002 TIntermTyped* expression;
3003 if (! acceptExpression(expression)) {
3004 expected("case expression");
3005 return false;
3006 }
3007
3008 if (! acceptTokenClass(EHTokColon)) {
3009 expected(":");
3010 return false;
3011 }
3012
3013 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
3014
3015 return true;
3016}
3017
3018// default_label
3019// : DEFAULT COLON
3020//
3021bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
3022{
3023 TSourceLoc loc = token.loc;
3024 if (! acceptTokenClass(EHTokDefault))
3025 return false;
3026
3027 if (! acceptTokenClass(EHTokColon)) {
3028 expected(":");
3029 return false;
3030 }
3031
3032 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
3033
3034 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003035}
3036
John Kessenich19b92ff2016-06-19 11:50:34 -06003037// array_specifier
steve-lunarg7b211a32016-10-13 12:26:18 -06003038// : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
3039// : LEFT_BRACKET RGHT_BRACKET // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06003040//
3041void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
3042{
3043 arraySizes = nullptr;
3044
steve-lunarg7b211a32016-10-13 12:26:18 -06003045 // Early-out if there aren't any array dimensions
3046 if (!peekTokenClass(EHTokLeftBracket))
John Kessenich19b92ff2016-06-19 11:50:34 -06003047 return;
3048
steve-lunarg7b211a32016-10-13 12:26:18 -06003049 // If we get here, we have at least one array dimension. This will track the sizes we find.
John Kessenich19b92ff2016-06-19 11:50:34 -06003050 arraySizes = new TArraySizes;
steve-lunarg7b211a32016-10-13 12:26:18 -06003051
3052 // Collect each array dimension.
3053 while (acceptTokenClass(EHTokLeftBracket)) {
3054 TSourceLoc loc = token.loc;
3055 TIntermTyped* sizeExpr = nullptr;
3056
3057 // Array sizing expression is optional. If ommitted, array will be later sized by initializer list.
3058 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
3059
3060 if (! acceptTokenClass(EHTokRightBracket)) {
3061 expected("]");
3062 return;
3063 }
3064
3065 if (hasArraySize) {
3066 TArraySize arraySize;
3067 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
3068 arraySizes->addInnerSize(arraySize);
3069 } else {
3070 arraySizes->addInnerSize(0); // sized by initializers.
3071 }
steve-lunarg265c0612016-09-27 10:57:35 -06003072 }
John Kessenich19b92ff2016-06-19 11:50:34 -06003073}
3074
John Kessenich630dd7d2016-06-12 23:52:12 -06003075// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06003076// : COLON semantic // optional
3077// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
3078// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06003079// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06003080// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06003081//
John Kessenich7735b942016-09-05 12:40:06 -06003082void HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06003083{
John Kessenich630dd7d2016-06-12 23:52:12 -06003084 do {
John Kessenichecba76f2017-01-06 00:34:48 -07003085 // COLON
John Kessenich630dd7d2016-06-12 23:52:12 -06003086 if (acceptTokenClass(EHTokColon)) {
3087 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06003088 if (peekTokenClass(EHTokLayout))
3089 acceptLayoutQualifierList(qualifier);
3090 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06003091 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003092 if (! acceptTokenClass(EHTokLeftParen)) {
3093 expected("(");
3094 return;
3095 }
John Kessenich82d6baf2016-07-29 13:03:05 -06003096 HlslToken locationToken;
3097 if (! acceptIdentifier(locationToken)) {
3098 expected("c[subcomponent][.component]");
3099 return;
3100 }
3101 HlslToken componentToken;
3102 if (acceptTokenClass(EHTokDot)) {
3103 if (! acceptIdentifier(componentToken)) {
3104 expected("component");
3105 return;
3106 }
3107 }
John Kessenich630dd7d2016-06-12 23:52:12 -06003108 if (! acceptTokenClass(EHTokRightParen)) {
3109 expected(")");
3110 break;
3111 }
John Kessenich7735b942016-09-05 12:40:06 -06003112 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003113 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06003114 expected("layout, semantic, packoffset, or register");
John Kessenich630dd7d2016-06-12 23:52:12 -06003115 return;
3116 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06003117 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
3118 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003119 if (! acceptTokenClass(EHTokLeftParen)) {
3120 expected("(");
3121 return;
3122 }
John Kessenichb38f0712016-07-30 10:29:54 -06003123 HlslToken registerDesc; // for Type#
3124 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06003125 if (! acceptIdentifier(registerDesc)) {
3126 expected("register number description");
3127 return;
3128 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003129 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
3130 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06003131 // Then we didn't really see the registerDesc yet, it was
3132 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06003133 profile = registerDesc;
3134 if (! acceptIdentifier(registerDesc)) {
3135 expected("register number description");
3136 return;
3137 }
3138 }
John Kessenichb38f0712016-07-30 10:29:54 -06003139 int subComponent = 0;
3140 if (acceptTokenClass(EHTokLeftBracket)) {
3141 // LEFT_BRACKET subcomponent RIGHT_BRACKET
3142 if (! peekTokenClass(EHTokIntConstant)) {
3143 expected("literal integer");
3144 return;
3145 }
3146 subComponent = token.i;
3147 advanceToken();
3148 if (! acceptTokenClass(EHTokRightBracket)) {
3149 expected("]");
3150 break;
3151 }
3152 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003153 // (COMMA SPACEN)opt
3154 HlslToken spaceDesc;
3155 if (acceptTokenClass(EHTokComma)) {
3156 if (! acceptIdentifier(spaceDesc)) {
3157 expected ("space identifier");
3158 return;
3159 }
3160 }
3161 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003162 if (! acceptTokenClass(EHTokRightParen)) {
3163 expected(")");
3164 break;
3165 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003166 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003167 } else {
3168 // semantic, in idToken.string
John Kessenich7735b942016-09-05 12:40:06 -06003169 parseContext.handleSemantic(idToken.loc, qualifier, *idToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003170 }
John Kessenicha1e2d492016-09-20 13:22:58 -06003171 } else if (peekTokenClass(EHTokLeftAngle))
3172 acceptAnnotations(qualifier);
3173 else
John Kessenich630dd7d2016-06-12 23:52:12 -06003174 break;
John Kessenich078d7f22016-03-14 10:02:11 -06003175
John Kessenich630dd7d2016-06-12 23:52:12 -06003176 } while (true);
John Kessenich078d7f22016-03-14 10:02:11 -06003177}
3178
John Kesseniche01a9bc2016-03-12 20:11:22 -07003179} // end namespace glslang