blob: 4914f20c14c6df9a56d9d425a60c36311ec69266 [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 Kessenich7a41f962017-03-22 11:38:22 -060078// IDENTIFIER
79// THIS
80// type that can be used as IDENTIFIER
81//
John Kessenichaecd4972016-03-14 10:46:34 -060082// Only process the next token if it is an identifier.
83// Return true if it was an identifier.
84bool HlslGrammar::acceptIdentifier(HlslToken& idToken)
85{
John Kessenich7a41f962017-03-22 11:38:22 -060086 // IDENTIFIER
John Kessenichaecd4972016-03-14 10:46:34 -060087 if (peekTokenClass(EHTokIdentifier)) {
88 idToken = token;
89 advanceToken();
90 return true;
91 }
92
John Kessenich7a41f962017-03-22 11:38:22 -060093 // THIS
94 // -> maps to the IDENTIFIER spelled with the internal special name for 'this'
95 if (peekTokenClass(EHTokThis)) {
96 idToken = token;
97 advanceToken();
98 idToken.tokenClass = EHTokIdentifier;
99 idToken.string = NewPoolTString(intermediate.implicitThisName);
100 return true;
101 }
102
103 // type that can be used as IDENTIFIER
104
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700105 // Even though "sample", "bool", "float", etc keywords (for types, interpolation modifiers),
106 // they ARE still accepted as identifiers. This is not a dense space: e.g, "void" is not a
107 // valid identifier, nor is "linear". This code special cases the known instances of this, so
108 // e.g, "int sample;" or "float float;" is accepted. Other cases can be added here if needed.
John Kessenichecba76f2017-01-06 00:34:48 -0700109
John Kessenich0320d092017-06-13 22:22:52 -0600110 const char* idString = getTypeString(peek());
111 if (idString == nullptr)
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700112 return false;
steve-lunarg75fd2232016-11-16 13:22:11 -0700113
John Kessenich0320d092017-06-13 22:22:52 -0600114 token.string = NewPoolTString(idString);
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700115 token.tokenClass = EHTokIdentifier;
John Kessenich0320d092017-06-13 22:22:52 -0600116 idToken = token;
117 typeIdentifiers = true;
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700118
119 advanceToken();
120
121 return true;
John Kessenichaecd4972016-03-14 10:46:34 -0600122}
123
John Kesseniche01a9bc2016-03-12 20:11:22 -0700124// compilationUnit
John Kessenich8f9fdc92017-03-30 16:22:26 -0600125// : declaration_list EOF
John Kesseniche01a9bc2016-03-12 20:11:22 -0700126//
127bool HlslGrammar::acceptCompilationUnit()
128{
John Kessenichd016be12016-03-13 11:24:20 -0600129 TIntermNode* unitNode = nullptr;
130
John Kessenich8f9fdc92017-03-30 16:22:26 -0600131 if (! acceptDeclarationList(unitNode))
132 return false;
steve-lunargcb88de52016-08-03 07:04:18 -0600133
John Kessenich8f9fdc92017-03-30 16:22:26 -0600134 if (! peekTokenClass(EHTokNone))
135 return false;
John Kesseniche01a9bc2016-03-12 20:11:22 -0700136
John Kessenichd016be12016-03-13 11:24:20 -0600137 // set root of AST
John Kessenichca71d942017-03-07 20:44:09 -0700138 if (unitNode && !unitNode->getAsAggregate())
139 unitNode = intermediate.growAggregate(nullptr, unitNode);
John Kessenich078d7f22016-03-14 10:02:11 -0600140 intermediate.setTreeRoot(unitNode);
John Kessenichd016be12016-03-13 11:24:20 -0600141
John Kesseniche01a9bc2016-03-12 20:11:22 -0700142 return true;
143}
144
John Kessenich8f9fdc92017-03-30 16:22:26 -0600145// Recognize the following, but with the extra condition that it can be
146// successfully terminated by EOF or '}'.
147//
148// declaration_list
149// : list of declaration_or_semicolon followed by EOF or RIGHT_BRACE
150//
151// declaration_or_semicolon
152// : declaration
153// : SEMICOLON
154//
155bool HlslGrammar::acceptDeclarationList(TIntermNode*& nodeList)
156{
157 do {
158 // HLSL allows extra semicolons between global declarations
159 do { } while (acceptTokenClass(EHTokSemicolon));
160
161 // EOF or RIGHT_BRACE
162 if (peekTokenClass(EHTokNone) || peekTokenClass(EHTokRightBrace))
163 return true;
164
165 // declaration
166 if (! acceptDeclaration(nodeList))
167 return false;
168 } while (true);
169
170 return true;
171}
172
LoopDawg4886f692016-06-29 10:58:58 -0600173// sampler_state
John Kessenichecba76f2017-01-06 00:34:48 -0700174// : LEFT_BRACE [sampler_state_assignment ... ] RIGHT_BRACE
LoopDawg4886f692016-06-29 10:58:58 -0600175//
176// sampler_state_assignment
177// : sampler_state_identifier EQUAL value SEMICOLON
178//
179// sampler_state_identifier
180// : ADDRESSU
181// | ADDRESSV
182// | ADDRESSW
183// | BORDERCOLOR
184// | FILTER
185// | MAXANISOTROPY
186// | MAXLOD
187// | MINLOD
188// | MIPLODBIAS
189//
190bool HlslGrammar::acceptSamplerState()
191{
192 // TODO: this should be genericized to accept a list of valid tokens and
193 // return token/value pairs. Presently it is specific to texture values.
194
195 if (! acceptTokenClass(EHTokLeftBrace))
196 return true;
197
198 parseContext.warn(token.loc, "unimplemented", "immediate sampler state", "");
John Kessenichecba76f2017-01-06 00:34:48 -0700199
LoopDawg4886f692016-06-29 10:58:58 -0600200 do {
201 // read state name
202 HlslToken state;
203 if (! acceptIdentifier(state))
204 break; // end of list
205
206 // FXC accepts any case
207 TString stateName = *state.string;
208 std::transform(stateName.begin(), stateName.end(), stateName.begin(), ::tolower);
209
210 if (! acceptTokenClass(EHTokAssign)) {
211 expected("assign");
212 return false;
213 }
214
215 if (stateName == "minlod" || stateName == "maxlod") {
216 if (! peekTokenClass(EHTokIntConstant)) {
217 expected("integer");
218 return false;
219 }
220
221 TIntermTyped* lod = nullptr;
222 if (! acceptLiteral(lod)) // should never fail, since we just looked for an integer
223 return false;
224 } else if (stateName == "maxanisotropy") {
225 if (! peekTokenClass(EHTokIntConstant)) {
226 expected("integer");
227 return false;
228 }
229
230 TIntermTyped* maxAnisotropy = nullptr;
231 if (! acceptLiteral(maxAnisotropy)) // should never fail, since we just looked for an integer
232 return false;
233 } else if (stateName == "filter") {
234 HlslToken filterMode;
235 if (! acceptIdentifier(filterMode)) {
236 expected("filter mode");
237 return false;
238 }
239 } else if (stateName == "addressu" || stateName == "addressv" || stateName == "addressw") {
240 HlslToken addrMode;
241 if (! acceptIdentifier(addrMode)) {
242 expected("texture address mode");
243 return false;
244 }
245 } else if (stateName == "miplodbias") {
246 TIntermTyped* lodBias = nullptr;
247 if (! acceptLiteral(lodBias)) {
248 expected("lod bias");
249 return false;
250 }
251 } else if (stateName == "bordercolor") {
252 return false;
253 } else {
254 expected("texture state");
255 return false;
256 }
257
258 // SEMICOLON
259 if (! acceptTokenClass(EHTokSemicolon)) {
260 expected("semicolon");
261 return false;
262 }
263 } while (true);
264
265 if (! acceptTokenClass(EHTokRightBrace))
266 return false;
267
268 return true;
269}
270
271// sampler_declaration_dx9
272// : SAMPLER identifier EQUAL sampler_type sampler_state
273//
John Kesseniche4821e42016-07-16 10:19:43 -0600274bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/)
LoopDawg4886f692016-06-29 10:58:58 -0600275{
276 if (! acceptTokenClass(EHTokSampler))
277 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700278
LoopDawg4886f692016-06-29 10:58:58 -0600279 // TODO: remove this when DX9 style declarations are implemented.
280 unimplemented("Direct3D 9 sampler declaration");
281
282 // read sampler name
283 HlslToken name;
284 if (! acceptIdentifier(name)) {
285 expected("sampler name");
286 return false;
287 }
288
289 if (! acceptTokenClass(EHTokAssign)) {
290 expected("=");
291 return false;
292 }
293
294 return false;
295}
296
John Kesseniche01a9bc2016-03-12 20:11:22 -0700297// declaration
John Kessenich77ea30b2017-09-30 14:34:50 -0600298// : attributes attributed_declaration
299// | NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
300//
301// attributed_declaration
LoopDawg4886f692016-06-29 10:58:58 -0600302// : sampler_declaration_dx9 post_decls SEMICOLON
John Kessenich054378d2017-06-19 15:13:26 -0600303// | fully_specified_type // for cbuffer/tbuffer
304// | fully_specified_type declarator_list SEMICOLON // for non cbuffer/tbuffer
John Kessenich630dd7d2016-06-12 23:52:12 -0600305// | fully_specified_type identifier function_parameters post_decls compound_statement // function definition
LoopDawg4886f692016-06-29 10:58:58 -0600306// | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition
John Kessenich5e69ec62016-07-05 00:02:40 -0600307// | typedef declaration
John Kessenich87142c72016-03-12 20:24:24 -0700308//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600309// declarator_list
310// : declarator COMMA declarator COMMA declarator... // zero or more declarators
John Kessenich532543c2016-07-01 19:06:44 -0600311//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600312// declarator
John Kessenich532543c2016-07-01 19:06:44 -0600313// : identifier array_specifier post_decls
314// | identifier array_specifier post_decls EQUAL assignment_expression
John Kessenichd5ed0b62016-07-04 17:32:45 -0600315// | identifier function_parameters post_decls // function prototype
John Kessenich532543c2016-07-01 19:06:44 -0600316//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600317// Parsing has to go pretty far in to know whether it's a variable, prototype, or
318// function definition, so the implementation below doesn't perfectly divide up the grammar
John Kessenich532543c2016-07-01 19:06:44 -0600319// as above. (The 'identifier' in the first item in init_declarator list is the
320// same as 'identifier' for function declarations.)
321//
John Kessenichca71d942017-03-07 20:44:09 -0700322// This can generate more than one subtree, one per initializer or a function body.
323// All initializer subtrees are put in their own aggregate node, making one top-level
324// node for all the initializers. Each function created is a top-level node to grow
325// into the passed-in nodeList.
John Kessenichd016be12016-03-13 11:24:20 -0600326//
John Kessenichca71d942017-03-07 20:44:09 -0700327// If 'nodeList' is passed in as non-null, it must an aggregate to extend for
328// each top-level node the declaration creates. Otherwise, if only one top-level
329// node in generated here, that is want is returned in nodeList.
John Kessenich02467d82017-01-19 15:41:47 -0700330//
John Kessenichca71d942017-03-07 20:44:09 -0700331bool HlslGrammar::acceptDeclaration(TIntermNode*& nodeList)
John Kesseniche01a9bc2016-03-12 20:11:22 -0700332{
John Kessenich8f9fdc92017-03-30 16:22:26 -0600333 // NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
334 if (acceptTokenClass(EHTokNamespace)) {
335 HlslToken namespaceToken;
336 if (!acceptIdentifier(namespaceToken)) {
337 expected("namespace name");
338 return false;
339 }
340 parseContext.pushNamespace(*namespaceToken.string);
341 if (!acceptTokenClass(EHTokLeftBrace)) {
342 expected("{");
343 return false;
344 }
345 if (!acceptDeclarationList(nodeList)) {
346 expected("declaration list");
347 return false;
348 }
349 if (!acceptTokenClass(EHTokRightBrace)) {
350 expected("}");
351 return false;
352 }
353 parseContext.popNamespace();
354 return true;
355 }
356
John Kessenich54ee28f2017-03-11 14:13:00 -0700357 bool declarator_list = false; // true when processing comma separation
John Kessenichd016be12016-03-13 11:24:20 -0600358
steve-lunarg1868b142016-10-20 13:07:10 -0600359 // attributes
John Kessenich088d52b2017-03-11 17:55:28 -0700360 TFunctionDeclarator declarator;
361 acceptAttributes(declarator.attributes);
steve-lunarg1868b142016-10-20 13:07:10 -0600362
John Kessenich5e69ec62016-07-05 00:02:40 -0600363 // typedef
364 bool typedefDecl = acceptTokenClass(EHTokTypedef);
365
John Kesseniche82061d2016-09-27 14:38:57 -0600366 TType declaredType;
LoopDawg4886f692016-06-29 10:58:58 -0600367
368 // DX9 sampler declaration use a different syntax
John Kessenich267590d2016-08-05 17:34:34 -0600369 // DX9 shaders need to run through HLSL compiler (fxc) via a back compat mode, it isn't going to
370 // be possible to simultaneously compile D3D10+ style shaders and DX9 shaders. If we want to compile DX9
371 // HLSL shaders, this will have to be a master level switch
372 // As such, the sampler keyword in D3D10+ turns into an automatic sampler type, and is commonly used
John Kessenichecba76f2017-01-06 00:34:48 -0700373 // For that reason, this line is commented out
John Kessenichca71d942017-03-07 20:44:09 -0700374 // if (acceptSamplerDeclarationDX9(declaredType))
375 // return true;
LoopDawg4886f692016-06-29 10:58:58 -0600376
John Kessenich2fcdd642017-06-19 15:41:11 -0600377 bool forbidDeclarators = (peekTokenClass(EHTokCBuffer) || peekTokenClass(EHTokTBuffer));
LoopDawg4886f692016-06-29 10:58:58 -0600378 // fully_specified_type
John Kessenich54ee28f2017-03-11 14:13:00 -0700379 if (! acceptFullySpecifiedType(declaredType, nodeList))
John Kessenich87142c72016-03-12 20:24:24 -0700380 return false;
LoopDawg4886f692016-06-29 10:58:58 -0600381
John Kessenich77ea30b2017-09-30 14:34:50 -0600382 parseContext.transferTypeAttributes(declarator.attributes, declaredType);
383
John Kessenich2fcdd642017-06-19 15:41:11 -0600384 // cbuffer and tbuffer end with the closing '}'.
385 // No semicolon is included.
386 if (forbidDeclarators)
387 return true;
388
John Kessenich054378d2017-06-19 15:13:26 -0600389 // declarator_list
390 // : declarator
391 // : identifier
John Kessenichaecd4972016-03-14 10:46:34 -0600392 HlslToken idToken;
John Kessenichca71d942017-03-07 20:44:09 -0700393 TIntermAggregate* initializers = nullptr;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600394 while (acceptIdentifier(idToken)) {
John Kessenich9855bda2017-09-11 21:48:19 -0600395 TString *fullName = idToken.string;
John Kessenich8f9fdc92017-03-30 16:22:26 -0600396 if (parseContext.symbolTable.atGlobalLevel())
397 parseContext.getFullNamespaceName(fullName);
John Kessenich78388722017-03-08 18:53:51 -0700398 if (peekTokenClass(EHTokLeftParen)) {
399 // looks like function parameters
steve-lunargf1e0c872016-10-31 15:13:43 -0600400
John Kessenich78388722017-03-08 18:53:51 -0700401 // Potentially rename shader entry point function. No-op most of the time.
John Kessenich8f9fdc92017-03-30 16:22:26 -0600402 parseContext.renameShaderFunction(fullName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600403
John Kessenich78388722017-03-08 18:53:51 -0700404 // function_parameters
John Kessenich8f9fdc92017-03-30 16:22:26 -0600405 declarator.function = new TFunction(fullName, declaredType);
John Kessenich088d52b2017-03-11 17:55:28 -0700406 if (!acceptFunctionParameters(*declarator.function)) {
John Kessenich78388722017-03-08 18:53:51 -0700407 expected("function parameter list");
408 return false;
409 }
410
John Kessenich630dd7d2016-06-12 23:52:12 -0600411 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -0700412 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich078d7f22016-03-14 10:02:11 -0600413
John Kessenichd5ed0b62016-07-04 17:32:45 -0600414 // compound_statement (function body definition) or just a prototype?
John Kessenich088d52b2017-03-11 17:55:28 -0700415 declarator.loc = token.loc;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600416 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich54ee28f2017-03-11 14:13:00 -0700417 if (declarator_list)
John Kessenichd5ed0b62016-07-04 17:32:45 -0600418 parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", "");
John Kessenich5e69ec62016-07-05 00:02:40 -0600419 if (typedefDecl)
420 parseContext.error(idToken.loc, "function body can't be in a typedef", "{", "");
John Kessenichb16f7e62017-03-11 19:32:47 -0700421 return acceptFunctionDefinition(declarator, nodeList, nullptr);
John Kessenich5e69ec62016-07-05 00:02:40 -0600422 } else {
423 if (typedefDecl)
424 parseContext.error(idToken.loc, "function typedefs not implemented", "{", "");
John Kessenich088d52b2017-03-11 17:55:28 -0700425 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, true);
John Kessenich5e69ec62016-07-05 00:02:40 -0600426 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600427 } else {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600428 // A variable declaration. Fix the storage qualifier if it's a global.
429 if (declaredType.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel())
430 declaredType.getQualifier().storage = EvqUniform;
431
John Kessenichecba76f2017-01-06 00:34:48 -0700432 // We can handle multiple variables per type declaration, so
John Kesseniche82061d2016-09-27 14:38:57 -0600433 // the number of types can expand when arrayness is different.
434 TType variableType;
435 variableType.shallowCopy(declaredType);
John Kessenich5f934b02016-03-13 17:58:25 -0600436
John Kesseniche82061d2016-09-27 14:38:57 -0600437 // recognize array_specifier
John Kessenichd5ed0b62016-07-04 17:32:45 -0600438 TArraySizes* arraySizes = nullptr;
439 acceptArraySpecifier(arraySizes);
John Kessenich5f934b02016-03-13 17:58:25 -0600440
John Kesseniche82061d2016-09-27 14:38:57 -0600441 // Fix arrayness in the variableType
442 if (declaredType.isImplicitlySizedArray()) {
443 // Because "int[] a = int[2](...), b = int[3](...)" makes two arrays a and b
444 // of different sizes, for this case sharing the shallow copy of arrayness
445 // with the parseType oversubscribes it, so get a deep copy of the arrayness.
446 variableType.newArraySizes(declaredType.getArraySizes());
447 }
448 if (arraySizes || variableType.isArray()) {
449 // In the most general case, arrayness is potentially coming both from the
450 // declared type and from the variable: "int[] a[];" or just one or the other.
451 // Merge it all to the variableType, so all arrayness is part of the variableType.
452 parseContext.arrayDimMerge(variableType, arraySizes);
453 }
454
LoopDawg4886f692016-06-29 10:58:58 -0600455 // samplers accept immediate sampler state
John Kesseniche82061d2016-09-27 14:38:57 -0600456 if (variableType.getBasicType() == EbtSampler) {
LoopDawg4886f692016-06-29 10:58:58 -0600457 if (! acceptSamplerState())
458 return false;
459 }
460
John Kessenichd5ed0b62016-07-04 17:32:45 -0600461 // post_decls
John Kesseniche82061d2016-09-27 14:38:57 -0600462 acceptPostDecls(variableType.getQualifier());
John Kessenichd5ed0b62016-07-04 17:32:45 -0600463
464 // EQUAL assignment_expression
465 TIntermTyped* expressionNode = nullptr;
466 if (acceptTokenClass(EHTokAssign)) {
John Kessenich5e69ec62016-07-05 00:02:40 -0600467 if (typedefDecl)
468 parseContext.error(idToken.loc, "can't have an initializer", "typedef", "");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600469 if (! acceptAssignmentExpression(expressionNode)) {
470 expected("initializer");
471 return false;
472 }
473 }
474
John Kessenich6dbc0a72016-09-27 19:13:05 -0600475 // TODO: things scoped within an annotation need their own name space;
476 // TODO: strings are not yet handled.
477 if (variableType.getBasicType() != EbtString && parseContext.getAnnotationNestingLevel() == 0) {
478 if (typedefDecl)
John Kessenich8f9fdc92017-03-30 16:22:26 -0600479 parseContext.declareTypedef(idToken.loc, *fullName, variableType);
steve-lunarg8e26feb2017-04-10 08:19:21 -0600480 else if (variableType.getBasicType() == EbtBlock) {
steve-lunarga766b832017-04-25 09:30:28 -0600481 parseContext.declareBlock(idToken.loc, variableType, fullName,
482 variableType.isArray() ? &variableType.getArraySizes() : nullptr);
steve-lunarg8e26feb2017-04-10 08:19:21 -0600483 parseContext.declareStructBufferCounter(idToken.loc, variableType, *fullName);
484 } else {
steve-lunarga2b01a02016-11-28 17:09:54 -0700485 if (variableType.getQualifier().storage == EvqUniform && ! variableType.containsOpaque()) {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600486 // this isn't really an individual variable, but a member of the $Global buffer
John Kessenich8f9fdc92017-03-30 16:22:26 -0600487 parseContext.growGlobalUniformBlock(idToken.loc, variableType, *fullName);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600488 } else {
489 // Declare the variable and add any initializer code to the AST.
490 // The top-level node is always made into an aggregate, as that's
491 // historically how the AST has been.
John Kessenichca71d942017-03-07 20:44:09 -0700492 initializers = intermediate.growAggregate(initializers,
John Kessenich8f9fdc92017-03-30 16:22:26 -0600493 parseContext.declareVariable(idToken.loc, *fullName, variableType, expressionNode),
John Kessenichca71d942017-03-07 20:44:09 -0700494 idToken.loc);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600495 }
496 }
John Kessenich5e69ec62016-07-05 00:02:40 -0600497 }
John Kessenich5f934b02016-03-13 17:58:25 -0600498 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600499
John Kessenich054378d2017-06-19 15:13:26 -0600500 // COMMA
501 if (acceptTokenClass(EHTokComma))
John Kessenich54ee28f2017-03-11 14:13:00 -0700502 declarator_list = true;
John Kessenich2fcdd642017-06-19 15:41:11 -0600503 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600504
John Kessenichca71d942017-03-07 20:44:09 -0700505 // The top-level initializer node is a sequence.
506 if (initializers != nullptr)
507 initializers->setOperator(EOpSequence);
508
509 // Add the initializers' aggregate to the nodeList we were handed.
510 if (nodeList)
511 nodeList = intermediate.growAggregate(nodeList, initializers);
512 else
513 nodeList = initializers;
John Kessenich87142c72016-03-12 20:24:24 -0700514
John Kessenich2fcdd642017-06-19 15:41:11 -0600515 // SEMICOLON
John Kessenichd5ed0b62016-07-04 17:32:45 -0600516 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich2fcdd642017-06-19 15:41:11 -0600517 // This may have been a false detection of what appeared to be a declaration, but
518 // was actually an assignment such as "float = 4", where "float" is an identifier.
519 // We put the token back to let further parsing happen for cases where that may
520 // happen. This errors on the side of caution, and mostly triggers the error.
John Kessenich13075c62017-04-11 09:51:32 -0600521 if (peek() == EHTokAssign || peek() == EHTokLeftBracket || peek() == EHTokDot || peek() == EHTokComma) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700522 recedeToken();
John Kessenich13075c62017-04-11 09:51:32 -0600523 return false;
John Kessenich13075c62017-04-11 09:51:32 -0600524 } else {
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700525 expected(";");
John Kessenich13075c62017-04-11 09:51:32 -0600526 return false;
527 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600528 }
John Kessenichecba76f2017-01-06 00:34:48 -0700529
John Kesseniche01a9bc2016-03-12 20:11:22 -0700530 return true;
531}
532
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600533// control_declaration
534// : fully_specified_type identifier EQUAL expression
535//
536bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node)
537{
538 node = nullptr;
539
540 // fully_specified_type
541 TType type;
542 if (! acceptFullySpecifiedType(type))
543 return false;
544
John Kessenich057df292017-03-06 18:18:37 -0700545 // filter out type casts
546 if (peekTokenClass(EHTokLeftParen)) {
547 recedeToken();
548 return false;
549 }
550
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600551 // identifier
552 HlslToken idToken;
553 if (! acceptIdentifier(idToken)) {
554 expected("identifier");
555 return false;
556 }
557
558 // EQUAL
559 TIntermTyped* expressionNode = nullptr;
560 if (! acceptTokenClass(EHTokAssign)) {
561 expected("=");
562 return false;
563 }
564
565 // expression
566 if (! acceptExpression(expressionNode)) {
567 expected("initializer");
568 return false;
569 }
570
John Kesseniche82061d2016-09-27 14:38:57 -0600571 node = parseContext.declareVariable(idToken.loc, *idToken.string, type, expressionNode);
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600572
573 return true;
574}
575
John Kessenich87142c72016-03-12 20:24:24 -0700576// fully_specified_type
577// : type_specifier
578// | type_qualifier type_specifier
579//
580bool HlslGrammar::acceptFullySpecifiedType(TType& type)
581{
John Kessenich54ee28f2017-03-11 14:13:00 -0700582 TIntermNode* nodeList = nullptr;
583 return acceptFullySpecifiedType(type, nodeList);
584}
585bool HlslGrammar::acceptFullySpecifiedType(TType& type, TIntermNode*& nodeList)
586{
John Kessenich87142c72016-03-12 20:24:24 -0700587 // type_qualifier
588 TQualifier qualifier;
589 qualifier.clear();
John Kessenichb9e39122016-08-17 10:22:08 -0600590 if (! acceptQualifier(qualifier))
591 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600592 TSourceLoc loc = token.loc;
John Kessenich87142c72016-03-12 20:24:24 -0700593
594 // type_specifier
John Kessenich54ee28f2017-03-11 14:13:00 -0700595 if (! acceptType(type, nodeList)) {
steve-lunarga64ed3e2016-12-18 17:51:14 -0700596 // If this is not a type, we may have inadvertently gone down a wrong path
steve-lunarg132d3312016-12-19 15:48:01 -0700597 // by parsing "sample", which can be treated like either an identifier or a
steve-lunarga64ed3e2016-12-18 17:51:14 -0700598 // qualifier. Back it out, if we did.
599 if (qualifier.sample)
600 recedeToken();
601
John Kessenich87142c72016-03-12 20:24:24 -0700602 return false;
steve-lunarga64ed3e2016-12-18 17:51:14 -0700603 }
John Kessenich3d157c52016-07-25 16:05:33 -0600604 if (type.getBasicType() == EbtBlock) {
605 // the type was a block, which set some parts of the qualifier
John Kessenich34e7ee72016-09-16 17:10:39 -0600606 parseContext.mergeQualifiers(type.getQualifier(), qualifier);
John Kessenich3d157c52016-07-25 16:05:33 -0600607 // further, it can create an anonymous instance of the block
John Kessenich13075c62017-04-11 09:51:32 -0600608 if (peek() != EHTokIdentifier)
John Kessenich3d157c52016-07-25 16:05:33 -0600609 parseContext.declareBlock(loc, type);
steve-lunargbb0183f2016-10-04 16:58:14 -0600610 } else {
611 // Some qualifiers are set when parsing the type. Merge those with
612 // whatever comes from acceptQualifier.
613 assert(qualifier.layoutFormat == ElfNone);
steve-lunargf49cdf42016-11-17 15:04:20 -0700614
steve-lunargbb0183f2016-10-04 16:58:14 -0600615 qualifier.layoutFormat = type.getQualifier().layoutFormat;
steve-lunarg3226b082016-10-26 19:18:55 -0600616 qualifier.precision = type.getQualifier().precision;
steve-lunargf49cdf42016-11-17 15:04:20 -0700617
steve-lunarg08e0c082017-03-29 20:01:13 -0600618 if (type.getQualifier().storage == EvqOut ||
steve-lunarg5da1f032017-02-12 17:50:28 -0700619 type.getQualifier().storage == EvqBuffer) {
steve-lunargf49cdf42016-11-17 15:04:20 -0700620 qualifier.storage = type.getQualifier().storage;
steve-lunarg5da1f032017-02-12 17:50:28 -0700621 qualifier.readonly = type.getQualifier().readonly;
622 }
steve-lunargf49cdf42016-11-17 15:04:20 -0700623
John Kessenichecd08bc2017-08-07 23:40:05 -0600624 if (type.isBuiltIn())
steve-lunarg08e0c082017-03-29 20:01:13 -0600625 qualifier.builtIn = type.getQualifier().builtIn;
626
steve-lunargf49cdf42016-11-17 15:04:20 -0700627 type.getQualifier() = qualifier;
steve-lunargbb0183f2016-10-04 16:58:14 -0600628 }
John Kessenich87142c72016-03-12 20:24:24 -0700629
630 return true;
631}
632
John Kessenich630dd7d2016-06-12 23:52:12 -0600633// type_qualifier
634// : qualifier qualifier ...
635//
636// Zero or more of these, so this can't return false.
637//
John Kessenichb9e39122016-08-17 10:22:08 -0600638bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
John Kessenich87142c72016-03-12 20:24:24 -0700639{
John Kessenich630dd7d2016-06-12 23:52:12 -0600640 do {
641 switch (peek()) {
642 case EHTokStatic:
John Kessenich6dbc0a72016-09-27 19:13:05 -0600643 qualifier.storage = parseContext.symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
John Kessenich630dd7d2016-06-12 23:52:12 -0600644 break;
645 case EHTokExtern:
646 // TODO: no meaning in glslang?
647 break;
648 case EHTokShared:
649 // TODO: hint
650 break;
651 case EHTokGroupShared:
652 qualifier.storage = EvqShared;
653 break;
654 case EHTokUniform:
655 qualifier.storage = EvqUniform;
656 break;
657 case EHTokConst:
658 qualifier.storage = EvqConst;
659 break;
660 case EHTokVolatile:
661 qualifier.volatil = true;
662 break;
663 case EHTokLinear:
John Kessenich630dd7d2016-06-12 23:52:12 -0600664 qualifier.smooth = true;
665 break;
666 case EHTokCentroid:
667 qualifier.centroid = true;
668 break;
669 case EHTokNointerpolation:
670 qualifier.flat = true;
671 break;
672 case EHTokNoperspective:
673 qualifier.nopersp = true;
674 break;
675 case EHTokSample:
676 qualifier.sample = true;
677 break;
678 case EHTokRowMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600679 qualifier.layoutMatrix = ElmColumnMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600680 break;
681 case EHTokColumnMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600682 qualifier.layoutMatrix = ElmRowMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600683 break;
684 case EHTokPrecise:
685 qualifier.noContraction = true;
686 break;
LoopDawg9249c702016-07-12 20:44:32 -0600687 case EHTokIn:
xavierb1d97532017-06-20 07:49:22 +0200688 qualifier.storage = (qualifier.storage == EvqOut) ? EvqInOut : EvqIn;
LoopDawg9249c702016-07-12 20:44:32 -0600689 break;
690 case EHTokOut:
xavierb1d97532017-06-20 07:49:22 +0200691 qualifier.storage = (qualifier.storage == EvqIn) ? EvqInOut : EvqOut;
LoopDawg9249c702016-07-12 20:44:32 -0600692 break;
693 case EHTokInOut:
694 qualifier.storage = EvqInOut;
695 break;
John Kessenichb9e39122016-08-17 10:22:08 -0600696 case EHTokLayout:
697 if (! acceptLayoutQualifierList(qualifier))
698 return false;
699 continue;
steve-lunarg5da1f032017-02-12 17:50:28 -0700700 case EHTokGloballyCoherent:
701 qualifier.coherent = true;
702 break;
John Kessenich36b218d2017-03-15 09:05:14 -0600703 case EHTokInline:
704 // TODO: map this to SPIR-V function control
705 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700706
707 // GS geometries: these are specified on stage input variables, and are an error (not verified here)
708 // for output variables.
709 case EHTokPoint:
710 qualifier.storage = EvqIn;
711 if (!parseContext.handleInputGeometry(token.loc, ElgPoints))
712 return false;
713 break;
714 case EHTokLine:
715 qualifier.storage = EvqIn;
716 if (!parseContext.handleInputGeometry(token.loc, ElgLines))
717 return false;
718 break;
719 case EHTokTriangle:
720 qualifier.storage = EvqIn;
721 if (!parseContext.handleInputGeometry(token.loc, ElgTriangles))
722 return false;
723 break;
724 case EHTokLineAdj:
725 qualifier.storage = EvqIn;
726 if (!parseContext.handleInputGeometry(token.loc, ElgLinesAdjacency))
727 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700728 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700729 case EHTokTriangleAdj:
730 qualifier.storage = EvqIn;
731 if (!parseContext.handleInputGeometry(token.loc, ElgTrianglesAdjacency))
732 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700733 break;
734
John Kessenich630dd7d2016-06-12 23:52:12 -0600735 default:
John Kessenichb9e39122016-08-17 10:22:08 -0600736 return true;
John Kessenich630dd7d2016-06-12 23:52:12 -0600737 }
738 advanceToken();
739 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -0700740}
741
John Kessenichb9e39122016-08-17 10:22:08 -0600742// layout_qualifier_list
John Kesseniche3218e22016-09-05 14:37:03 -0600743// : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
John Kessenichb9e39122016-08-17 10:22:08 -0600744//
745// layout_qualifier
746// : identifier
John Kessenich841db352016-09-02 21:12:23 -0600747// | identifier EQUAL expression
John Kessenichb9e39122016-08-17 10:22:08 -0600748//
749// Zero or more of these, so this can't return false.
750//
751bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
752{
753 if (! acceptTokenClass(EHTokLayout))
754 return false;
755
756 // LEFT_PAREN
757 if (! acceptTokenClass(EHTokLeftParen))
758 return false;
759
760 do {
761 // identifier
762 HlslToken idToken;
763 if (! acceptIdentifier(idToken))
764 break;
765
766 // EQUAL expression
767 if (acceptTokenClass(EHTokAssign)) {
768 TIntermTyped* expr;
769 if (! acceptConditionalExpression(expr)) {
770 expected("expression");
771 return false;
772 }
773 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
774 } else
775 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
776
777 // COMMA
778 if (! acceptTokenClass(EHTokComma))
779 break;
780 } while (true);
781
782 // RIGHT_PAREN
783 if (! acceptTokenClass(EHTokRightParen)) {
784 expected(")");
785 return false;
786 }
787
788 return true;
789}
790
LoopDawg6daaa4f2016-06-23 19:13:48 -0600791// template_type
792// : FLOAT
793// | DOUBLE
794// | INT
795// | DWORD
796// | UINT
797// | BOOL
798//
steve-lunargf49cdf42016-11-17 15:04:20 -0700799bool HlslGrammar::acceptTemplateVecMatBasicType(TBasicType& basicType)
LoopDawg6daaa4f2016-06-23 19:13:48 -0600800{
801 switch (peek()) {
802 case EHTokFloat:
803 basicType = EbtFloat;
804 break;
805 case EHTokDouble:
806 basicType = EbtDouble;
807 break;
808 case EHTokInt:
809 case EHTokDword:
810 basicType = EbtInt;
811 break;
812 case EHTokUint:
813 basicType = EbtUint;
814 break;
815 case EHTokBool:
816 basicType = EbtBool;
817 break;
818 default:
819 return false;
820 }
821
822 advanceToken();
823
824 return true;
825}
826
827// vector_template_type
828// : VECTOR
829// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
830//
831bool HlslGrammar::acceptVectorTemplateType(TType& type)
832{
833 if (! acceptTokenClass(EHTokVector))
834 return false;
835
836 if (! acceptTokenClass(EHTokLeftAngle)) {
837 // in HLSL, 'vector' alone means float4.
838 new(&type) TType(EbtFloat, EvqTemporary, 4);
839 return true;
840 }
841
842 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700843 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600844 expected("scalar type");
845 return false;
846 }
847
848 // COMMA
849 if (! acceptTokenClass(EHTokComma)) {
850 expected(",");
851 return false;
852 }
853
854 // integer
855 if (! peekTokenClass(EHTokIntConstant)) {
856 expected("literal integer");
857 return false;
858 }
859
860 TIntermTyped* vecSize;
861 if (! acceptLiteral(vecSize))
862 return false;
863
864 const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
865
866 new(&type) TType(basicType, EvqTemporary, vecSizeI);
867
868 if (vecSizeI == 1)
869 type.makeVector();
870
871 if (!acceptTokenClass(EHTokRightAngle)) {
872 expected("right angle bracket");
873 return false;
874 }
875
876 return true;
877}
878
879// matrix_template_type
880// : MATRIX
881// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
882//
883bool HlslGrammar::acceptMatrixTemplateType(TType& type)
884{
885 if (! acceptTokenClass(EHTokMatrix))
886 return false;
887
888 if (! acceptTokenClass(EHTokLeftAngle)) {
889 // in HLSL, 'matrix' alone means float4x4.
890 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
891 return true;
892 }
893
894 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700895 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600896 expected("scalar type");
897 return false;
898 }
899
900 // COMMA
901 if (! acceptTokenClass(EHTokComma)) {
902 expected(",");
903 return false;
904 }
905
906 // integer rows
907 if (! peekTokenClass(EHTokIntConstant)) {
908 expected("literal integer");
909 return false;
910 }
911
912 TIntermTyped* rows;
913 if (! acceptLiteral(rows))
914 return false;
915
916 // COMMA
917 if (! acceptTokenClass(EHTokComma)) {
918 expected(",");
919 return false;
920 }
John Kessenichecba76f2017-01-06 00:34:48 -0700921
LoopDawg6daaa4f2016-06-23 19:13:48 -0600922 // integer cols
923 if (! peekTokenClass(EHTokIntConstant)) {
924 expected("literal integer");
925 return false;
926 }
927
928 TIntermTyped* cols;
929 if (! acceptLiteral(cols))
930 return false;
931
932 new(&type) TType(basicType, EvqTemporary, 0,
steve-lunarg297ae212016-08-24 14:36:13 -0600933 rows->getAsConstantUnion()->getConstArray()[0].getIConst(),
934 cols->getAsConstantUnion()->getConstArray()[0].getIConst());
LoopDawg6daaa4f2016-06-23 19:13:48 -0600935
936 if (!acceptTokenClass(EHTokRightAngle)) {
937 expected("right angle bracket");
938 return false;
939 }
940
941 return true;
942}
943
steve-lunargf49cdf42016-11-17 15:04:20 -0700944// layout_geometry
945// : LINESTREAM
946// | POINTSTREAM
947// | TRIANGLESTREAM
948//
949bool HlslGrammar::acceptOutputPrimitiveGeometry(TLayoutGeometry& geometry)
950{
951 // read geometry type
952 const EHlslTokenClass geometryType = peek();
953
954 switch (geometryType) {
955 case EHTokPointStream: geometry = ElgPoints; break;
956 case EHTokLineStream: geometry = ElgLineStrip; break;
957 case EHTokTriangleStream: geometry = ElgTriangleStrip; break;
958 default:
959 return false; // not a layout geometry
960 }
961
962 advanceToken(); // consume the layout keyword
963 return true;
964}
965
steve-lunarg858c9282017-01-07 08:54:10 -0700966// tessellation_decl_type
967// : INPUTPATCH
968// | OUTPUTPATCH
969//
steve-lunarg067eb9b2017-04-01 15:34:48 -0600970bool HlslGrammar::acceptTessellationDeclType(TBuiltInVariable& patchType)
steve-lunarg858c9282017-01-07 08:54:10 -0700971{
972 // read geometry type
973 const EHlslTokenClass tessType = peek();
974
975 switch (tessType) {
steve-lunarg067eb9b2017-04-01 15:34:48 -0600976 case EHTokInputPatch: patchType = EbvInputPatch; break;
977 case EHTokOutputPatch: patchType = EbvOutputPatch; break;
steve-lunarg858c9282017-01-07 08:54:10 -0700978 default:
979 return false; // not a tessellation decl
980 }
981
982 advanceToken(); // consume the keyword
983 return true;
984}
985
986// tessellation_patch_template_type
987// : tessellation_decl_type LEFT_ANGLE type comma integer_literal RIGHT_ANGLE
988//
989bool HlslGrammar::acceptTessellationPatchTemplateType(TType& type)
990{
steve-lunarg067eb9b2017-04-01 15:34:48 -0600991 TBuiltInVariable patchType;
992
993 if (! acceptTessellationDeclType(patchType))
steve-lunarg858c9282017-01-07 08:54:10 -0700994 return false;
995
996 if (! acceptTokenClass(EHTokLeftAngle))
997 return false;
998
999 if (! acceptType(type)) {
1000 expected("tessellation patch type");
1001 return false;
1002 }
1003
1004 if (! acceptTokenClass(EHTokComma))
1005 return false;
1006
1007 // integer size
1008 if (! peekTokenClass(EHTokIntConstant)) {
1009 expected("literal integer");
1010 return false;
1011 }
1012
1013 TIntermTyped* size;
1014 if (! acceptLiteral(size))
1015 return false;
1016
1017 TArraySizes* arraySizes = new TArraySizes;
1018 arraySizes->addInnerSize(size->getAsConstantUnion()->getConstArray()[0].getIConst());
1019 type.newArraySizes(*arraySizes);
steve-lunarg067eb9b2017-04-01 15:34:48 -06001020 type.getQualifier().builtIn = patchType;
steve-lunarg858c9282017-01-07 08:54:10 -07001021
1022 if (! acceptTokenClass(EHTokRightAngle)) {
1023 expected("right angle bracket");
1024 return false;
1025 }
1026
1027 return true;
1028}
1029
steve-lunargf49cdf42016-11-17 15:04:20 -07001030// stream_out_template_type
1031// : output_primitive_geometry_type LEFT_ANGLE type RIGHT_ANGLE
1032//
1033bool HlslGrammar::acceptStreamOutTemplateType(TType& type, TLayoutGeometry& geometry)
1034{
1035 geometry = ElgNone;
1036
1037 if (! acceptOutputPrimitiveGeometry(geometry))
1038 return false;
1039
1040 if (! acceptTokenClass(EHTokLeftAngle))
1041 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001042
steve-lunargf49cdf42016-11-17 15:04:20 -07001043 if (! acceptType(type)) {
1044 expected("stream output type");
1045 return false;
1046 }
1047
steve-lunarg08e0c082017-03-29 20:01:13 -06001048 type.getQualifier().storage = EvqOut;
1049 type.getQualifier().builtIn = EbvGsOutputStream;
steve-lunargf49cdf42016-11-17 15:04:20 -07001050
1051 if (! acceptTokenClass(EHTokRightAngle)) {
1052 expected("right angle bracket");
1053 return false;
1054 }
1055
1056 return true;
1057}
John Kessenichecba76f2017-01-06 00:34:48 -07001058
John Kessenicha1e2d492016-09-20 13:22:58 -06001059// annotations
1060// : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
John Kessenich86f71382016-09-19 20:23:18 -06001061//
John Kessenicha1e2d492016-09-20 13:22:58 -06001062bool HlslGrammar::acceptAnnotations(TQualifier&)
John Kessenich86f71382016-09-19 20:23:18 -06001063{
John Kessenicha1e2d492016-09-20 13:22:58 -06001064 if (! acceptTokenClass(EHTokLeftAngle))
John Kessenich86f71382016-09-19 20:23:18 -06001065 return false;
1066
John Kessenicha1e2d492016-09-20 13:22:58 -06001067 // note that we are nesting a name space
1068 parseContext.nestAnnotations();
John Kessenich86f71382016-09-19 20:23:18 -06001069
1070 // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
1071 do {
1072 // eat any extra SEMI_COLON; don't know if the grammar calls for this or not
1073 while (acceptTokenClass(EHTokSemicolon))
1074 ;
1075
1076 if (acceptTokenClass(EHTokRightAngle))
John Kessenicha1e2d492016-09-20 13:22:58 -06001077 break;
John Kessenich86f71382016-09-19 20:23:18 -06001078
1079 // declaration
John Kessenichca71d942017-03-07 20:44:09 -07001080 TIntermNode* node = nullptr;
John Kessenich86f71382016-09-19 20:23:18 -06001081 if (! acceptDeclaration(node)) {
John Kessenicha1e2d492016-09-20 13:22:58 -06001082 expected("declaration in annotation");
John Kessenich86f71382016-09-19 20:23:18 -06001083 return false;
1084 }
1085 } while (true);
John Kessenicha1e2d492016-09-20 13:22:58 -06001086
1087 parseContext.unnestAnnotations();
1088 return true;
John Kessenich86f71382016-09-19 20:23:18 -06001089}
LoopDawg6daaa4f2016-06-23 19:13:48 -06001090
LoopDawg4886f692016-06-29 10:58:58 -06001091// sampler_type
1092// : SAMPLER
1093// | SAMPLER1D
1094// | SAMPLER2D
1095// | SAMPLER3D
1096// | SAMPLERCUBE
1097// | SAMPLERSTATE
1098// | SAMPLERCOMPARISONSTATE
1099bool HlslGrammar::acceptSamplerType(TType& type)
1100{
1101 // read sampler type
1102 const EHlslTokenClass samplerType = peek();
1103
LoopDawga78b0292016-07-19 14:28:05 -06001104 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -06001105 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -06001106
LoopDawga78b0292016-07-19 14:28:05 -06001107 bool isShadow = false;
1108
LoopDawg4886f692016-06-29 10:58:58 -06001109 switch (samplerType) {
1110 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -06001111 case EHTokSampler1d: /*dim = Esd1D*/; break;
1112 case EHTokSampler2d: /*dim = Esd2D*/; break;
1113 case EHTokSampler3d: /*dim = Esd3D*/; break;
1114 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -06001115 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -06001116 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001117 default:
1118 return false; // not a sampler declaration
1119 }
1120
1121 advanceToken(); // consume the sampler type keyword
1122
1123 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -06001124
1125 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -06001126 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -06001127
1128 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
1129
1130 return true;
1131}
1132
1133// texture_type
1134// | BUFFER
1135// | TEXTURE1D
1136// | TEXTURE1DARRAY
1137// | TEXTURE2D
1138// | TEXTURE2DARRAY
1139// | TEXTURE3D
1140// | TEXTURECUBE
1141// | TEXTURECUBEARRAY
1142// | TEXTURE2DMS
1143// | TEXTURE2DMSARRAY
steve-lunargbb0183f2016-10-04 16:58:14 -06001144// | RWBUFFER
1145// | RWTEXTURE1D
1146// | RWTEXTURE1DARRAY
1147// | RWTEXTURE2D
1148// | RWTEXTURE2DARRAY
1149// | RWTEXTURE3D
1150
LoopDawg4886f692016-06-29 10:58:58 -06001151bool HlslGrammar::acceptTextureType(TType& type)
1152{
1153 const EHlslTokenClass textureType = peek();
1154
1155 TSamplerDim dim = EsdNone;
1156 bool array = false;
1157 bool ms = false;
steve-lunargbb0183f2016-10-04 16:58:14 -06001158 bool image = false;
steve-lunargbf1537f2017-03-31 17:40:09 -06001159 bool combined = true;
LoopDawg4886f692016-06-29 10:58:58 -06001160
1161 switch (textureType) {
steve-lunargbf1537f2017-03-31 17:40:09 -06001162 case EHTokBuffer: dim = EsdBuffer; combined = false; break;
John Kessenichf36542f2017-03-31 14:39:30 -06001163 case EHTokTexture1d: dim = Esd1D; break;
1164 case EHTokTexture1darray: dim = Esd1D; array = true; break;
1165 case EHTokTexture2d: dim = Esd2D; break;
1166 case EHTokTexture2darray: dim = Esd2D; array = true; break;
1167 case EHTokTexture3d: dim = Esd3D; break;
1168 case EHTokTextureCube: dim = EsdCube; break;
1169 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
1170 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
1171 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
1172 case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
1173 case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
1174 case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
1175 case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
1176 case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
1177 case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001178 default:
1179 return false; // not a texture declaration
1180 }
1181
1182 advanceToken(); // consume the texture object keyword
1183
1184 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
John Kessenichecba76f2017-01-06 00:34:48 -07001185
LoopDawg4886f692016-06-29 10:58:58 -06001186 TIntermTyped* msCount = nullptr;
1187
steve-lunargbb0183f2016-10-04 16:58:14 -06001188 // texture type: required for multisample types and RWBuffer/RWTextures!
LoopDawg4886f692016-06-29 10:58:58 -06001189 if (acceptTokenClass(EHTokLeftAngle)) {
1190 if (! acceptType(txType)) {
1191 expected("scalar or vector type");
1192 return false;
1193 }
1194
1195 const TBasicType basicRetType = txType.getBasicType() ;
1196
LoopDawg5ee05892017-07-31 13:41:42 -06001197 switch (basicRetType) {
1198 case EbtFloat:
1199 case EbtUint:
1200 case EbtInt:
1201 case EbtStruct:
1202 break;
1203 default:
LoopDawg4886f692016-06-29 10:58:58 -06001204 unimplemented("basic type in texture");
1205 return false;
1206 }
1207
steve-lunargd53f7172016-07-27 15:46:48 -06001208 // Buffers can handle small mats if they fit in 4 components
1209 if (dim == EsdBuffer && txType.isMatrix()) {
1210 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
1211 expected("components < 4 in matrix buffer type");
1212 return false;
1213 }
1214
1215 // TODO: except we don't handle it yet...
1216 unimplemented("matrix type in buffer");
1217 return false;
1218 }
1219
LoopDawg5ee05892017-07-31 13:41:42 -06001220 if (!txType.isScalar() && !txType.isVector() && !txType.isStruct()) {
1221 expected("scalar, vector, or struct type");
LoopDawg4886f692016-06-29 10:58:58 -06001222 return false;
1223 }
1224
LoopDawg4886f692016-06-29 10:58:58 -06001225 if (ms && acceptTokenClass(EHTokComma)) {
1226 // read sample count for multisample types, if given
1227 if (! peekTokenClass(EHTokIntConstant)) {
1228 expected("multisample count");
1229 return false;
1230 }
1231
1232 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
1233 return false;
1234 }
1235
1236 if (! acceptTokenClass(EHTokRightAngle)) {
1237 expected("right angle bracket");
1238 return false;
1239 }
1240 } else if (ms) {
1241 expected("texture type for multisample");
1242 return false;
John Kessenichf36542f2017-03-31 14:39:30 -06001243 } else if (image) {
steve-lunargbb0183f2016-10-04 16:58:14 -06001244 expected("type for RWTexture/RWBuffer");
1245 return false;
LoopDawg4886f692016-06-29 10:58:58 -06001246 }
1247
1248 TArraySizes* arraySizes = nullptr;
steve-lunarg4f2da272016-10-10 15:24:57 -06001249 const bool shadow = false; // declared on the sampler
LoopDawg4886f692016-06-29 10:58:58 -06001250
1251 TSampler sampler;
steve-lunargbb0183f2016-10-04 16:58:14 -06001252 TLayoutFormat format = ElfNone;
steve-lunargd53f7172016-07-27 15:46:48 -06001253
steve-lunarg4f2da272016-10-10 15:24:57 -06001254 // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
1255 if (image || dim == EsdBuffer)
1256 format = parseContext.getLayoutFromTxType(token.loc, txType);
steve-lunargbb0183f2016-10-04 16:58:14 -06001257
LoopDawg5ee05892017-07-31 13:41:42 -06001258 const TBasicType txBasicType = txType.isStruct() ? (*txType.getStruct())[0].type->getBasicType()
1259 : txType.getBasicType();
1260
steve-lunargbb0183f2016-10-04 16:58:14 -06001261 // Non-image Buffers are combined
1262 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -06001263 sampler.set(txType.getBasicType(), dim, array);
1264 } else {
1265 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -06001266 if (image) {
LoopDawg5ee05892017-07-31 13:41:42 -06001267 sampler.setImage(txBasicType, dim, array, shadow, ms);
steve-lunargbb0183f2016-10-04 16:58:14 -06001268 } else {
LoopDawg5ee05892017-07-31 13:41:42 -06001269 sampler.setTexture(txBasicType, dim, array, shadow, ms);
steve-lunargbb0183f2016-10-04 16:58:14 -06001270 }
steve-lunargd53f7172016-07-27 15:46:48 -06001271 }
steve-lunarg8b0227c2016-10-14 16:40:32 -06001272
LoopDawg5ee05892017-07-31 13:41:42 -06001273 // Remember the declared return type. Function returns false on error.
1274 if (!parseContext.setTextureReturnType(sampler, txType, token.loc))
1275 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001276
steve-lunargbf1537f2017-03-31 17:40:09 -06001277 // Force uncombined, if necessary
1278 if (!combined)
1279 sampler.combined = false;
1280
LoopDawg4886f692016-06-29 10:58:58 -06001281 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -06001282 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -06001283
1284 return true;
1285}
1286
John Kessenich87142c72016-03-12 20:24:24 -07001287// If token is for a type, update 'type' with the type information,
1288// and return true and advance.
1289// Otherwise, return false, and don't advance
1290bool HlslGrammar::acceptType(TType& type)
1291{
John Kessenich54ee28f2017-03-11 14:13:00 -07001292 TIntermNode* nodeList = nullptr;
1293 return acceptType(type, nodeList);
1294}
1295bool HlslGrammar::acceptType(TType& type, TIntermNode*& nodeList)
1296{
steve-lunarg3226b082016-10-26 19:18:55 -06001297 // Basic types for min* types, broken out here in case of future
1298 // changes, e.g, to use native halfs.
1299 static const TBasicType min16float_bt = EbtFloat;
1300 static const TBasicType min10float_bt = EbtFloat;
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001301 static const TBasicType half_bt = EbtFloat;
steve-lunarg3226b082016-10-26 19:18:55 -06001302 static const TBasicType min16int_bt = EbtInt;
1303 static const TBasicType min12int_bt = EbtInt;
1304 static const TBasicType min16uint_bt = EbtUint;
1305
John Kessenich0320d092017-06-13 22:22:52 -06001306 // Some types might have turned into identifiers. Take the hit for checking
1307 // when this has happened.
1308 if (typeIdentifiers) {
1309 const char* identifierString = getTypeString(peek());
1310 if (identifierString != nullptr) {
1311 TString name = identifierString;
1312 // if it's an identifier, it's not a type
1313 if (parseContext.symbolTable.find(name) != nullptr)
1314 return false;
1315 }
1316 }
1317
John Kessenich9c86c6a2016-05-03 22:49:24 -06001318 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -06001319 case EHTokVector:
1320 return acceptVectorTemplateType(type);
1321 break;
1322
1323 case EHTokMatrix:
1324 return acceptMatrixTemplateType(type);
1325 break;
1326
steve-lunargf49cdf42016-11-17 15:04:20 -07001327 case EHTokPointStream: // fall through
1328 case EHTokLineStream: // ...
1329 case EHTokTriangleStream: // ...
1330 {
1331 TLayoutGeometry geometry;
1332 if (! acceptStreamOutTemplateType(type, geometry))
1333 return false;
1334
1335 if (! parseContext.handleOutputGeometry(token.loc, geometry))
1336 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001337
steve-lunargf49cdf42016-11-17 15:04:20 -07001338 return true;
1339 }
1340
steve-lunarg858c9282017-01-07 08:54:10 -07001341 case EHTokInputPatch: // fall through
1342 case EHTokOutputPatch: // ...
1343 {
1344 if (! acceptTessellationPatchTemplateType(type))
1345 return false;
1346
1347 return true;
1348 }
1349
LoopDawg4886f692016-06-29 10:58:58 -06001350 case EHTokSampler: // fall through
1351 case EHTokSampler1d: // ...
1352 case EHTokSampler2d: // ...
1353 case EHTokSampler3d: // ...
1354 case EHTokSamplerCube: // ...
1355 case EHTokSamplerState: // ...
1356 case EHTokSamplerComparisonState: // ...
1357 return acceptSamplerType(type);
1358 break;
1359
1360 case EHTokBuffer: // fall through
1361 case EHTokTexture1d: // ...
1362 case EHTokTexture1darray: // ...
1363 case EHTokTexture2d: // ...
1364 case EHTokTexture2darray: // ...
1365 case EHTokTexture3d: // ...
1366 case EHTokTextureCube: // ...
1367 case EHTokTextureCubearray: // ...
1368 case EHTokTexture2DMS: // ...
1369 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001370 case EHTokRWTexture1d: // ...
1371 case EHTokRWTexture1darray: // ...
1372 case EHTokRWTexture2d: // ...
1373 case EHTokRWTexture2darray: // ...
1374 case EHTokRWTexture3d: // ...
1375 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001376 return acceptTextureType(type);
1377 break;
1378
steve-lunarg5da1f032017-02-12 17:50:28 -07001379 case EHTokAppendStructuredBuffer:
1380 case EHTokByteAddressBuffer:
1381 case EHTokConsumeStructuredBuffer:
1382 case EHTokRWByteAddressBuffer:
1383 case EHTokRWStructuredBuffer:
1384 case EHTokStructuredBuffer:
1385 return acceptStructBufferType(type);
1386 break;
1387
steve-lunarga766b832017-04-25 09:30:28 -06001388 case EHTokConstantBuffer:
1389 return acceptConstantBufferType(type);
1390
John Kessenich27ffb292017-03-03 17:01:01 -07001391 case EHTokClass:
John Kesseniche6e74942016-06-11 16:43:14 -06001392 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001393 case EHTokCBuffer:
1394 case EHTokTBuffer:
John Kessenich54ee28f2017-03-11 14:13:00 -07001395 return acceptStruct(type, nodeList);
John Kesseniche6e74942016-06-11 16:43:14 -06001396
1397 case EHTokIdentifier:
1398 // An identifier could be for a user-defined type.
1399 // Note we cache the symbol table lookup, to save for a later rule
1400 // when this is not a type.
John Kessenichf4ba25e2017-03-21 18:35:04 -06001401 if (parseContext.lookupUserType(*token.string, type) != nullptr) {
John Kesseniche6e74942016-06-11 16:43:14 -06001402 advanceToken();
1403 return true;
1404 } else
1405 return false;
1406
John Kessenich71351de2016-06-08 12:50:56 -06001407 case EHTokVoid:
1408 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001409 break;
John Kessenich71351de2016-06-08 12:50:56 -06001410
John Kessenicha1e2d492016-09-20 13:22:58 -06001411 case EHTokString:
1412 new(&type) TType(EbtString);
1413 break;
1414
John Kessenich87142c72016-03-12 20:24:24 -07001415 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001416 new(&type) TType(EbtFloat);
1417 break;
John Kessenich87142c72016-03-12 20:24:24 -07001418 case EHTokFloat1:
1419 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001420 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001421 break;
John Kessenich87142c72016-03-12 20:24:24 -07001422 case EHTokFloat2:
1423 new(&type) TType(EbtFloat, EvqTemporary, 2);
1424 break;
1425 case EHTokFloat3:
1426 new(&type) TType(EbtFloat, EvqTemporary, 3);
1427 break;
1428 case EHTokFloat4:
1429 new(&type) TType(EbtFloat, EvqTemporary, 4);
1430 break;
1431
John Kessenich71351de2016-06-08 12:50:56 -06001432 case EHTokDouble:
1433 new(&type) TType(EbtDouble);
1434 break;
1435 case EHTokDouble1:
1436 new(&type) TType(EbtDouble);
1437 type.makeVector();
1438 break;
1439 case EHTokDouble2:
1440 new(&type) TType(EbtDouble, EvqTemporary, 2);
1441 break;
1442 case EHTokDouble3:
1443 new(&type) TType(EbtDouble, EvqTemporary, 3);
1444 break;
1445 case EHTokDouble4:
1446 new(&type) TType(EbtDouble, EvqTemporary, 4);
1447 break;
1448
1449 case EHTokInt:
1450 case EHTokDword:
1451 new(&type) TType(EbtInt);
1452 break;
1453 case EHTokInt1:
1454 new(&type) TType(EbtInt);
1455 type.makeVector();
1456 break;
John Kessenich87142c72016-03-12 20:24:24 -07001457 case EHTokInt2:
1458 new(&type) TType(EbtInt, EvqTemporary, 2);
1459 break;
1460 case EHTokInt3:
1461 new(&type) TType(EbtInt, EvqTemporary, 3);
1462 break;
1463 case EHTokInt4:
1464 new(&type) TType(EbtInt, EvqTemporary, 4);
1465 break;
1466
John Kessenich71351de2016-06-08 12:50:56 -06001467 case EHTokUint:
1468 new(&type) TType(EbtUint);
1469 break;
1470 case EHTokUint1:
1471 new(&type) TType(EbtUint);
1472 type.makeVector();
1473 break;
1474 case EHTokUint2:
1475 new(&type) TType(EbtUint, EvqTemporary, 2);
1476 break;
1477 case EHTokUint3:
1478 new(&type) TType(EbtUint, EvqTemporary, 3);
1479 break;
1480 case EHTokUint4:
1481 new(&type) TType(EbtUint, EvqTemporary, 4);
1482 break;
1483
1484 case EHTokBool:
1485 new(&type) TType(EbtBool);
1486 break;
1487 case EHTokBool1:
1488 new(&type) TType(EbtBool);
1489 type.makeVector();
1490 break;
John Kessenich87142c72016-03-12 20:24:24 -07001491 case EHTokBool2:
1492 new(&type) TType(EbtBool, EvqTemporary, 2);
1493 break;
1494 case EHTokBool3:
1495 new(&type) TType(EbtBool, EvqTemporary, 3);
1496 break;
1497 case EHTokBool4:
1498 new(&type) TType(EbtBool, EvqTemporary, 4);
1499 break;
1500
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001501 case EHTokHalf:
John Kessenich96f65522017-06-06 23:35:25 -06001502 new(&type) TType(half_bt, EvqTemporary);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001503 break;
1504 case EHTokHalf1:
John Kessenich96f65522017-06-06 23:35:25 -06001505 new(&type) TType(half_bt, EvqTemporary);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001506 type.makeVector();
1507 break;
1508 case EHTokHalf2:
John Kessenich96f65522017-06-06 23:35:25 -06001509 new(&type) TType(half_bt, EvqTemporary, 2);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001510 break;
1511 case EHTokHalf3:
John Kessenich96f65522017-06-06 23:35:25 -06001512 new(&type) TType(half_bt, EvqTemporary, 3);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001513 break;
1514 case EHTokHalf4:
John Kessenich96f65522017-06-06 23:35:25 -06001515 new(&type) TType(half_bt, EvqTemporary, 4);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001516 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001517
steve-lunarg3226b082016-10-26 19:18:55 -06001518 case EHTokMin16float:
1519 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1520 break;
1521 case EHTokMin16float1:
1522 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1523 type.makeVector();
1524 break;
1525 case EHTokMin16float2:
1526 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 2);
1527 break;
1528 case EHTokMin16float3:
1529 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 3);
1530 break;
1531 case EHTokMin16float4:
1532 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 4);
1533 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001534
steve-lunarg3226b082016-10-26 19:18:55 -06001535 case EHTokMin10float:
1536 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1537 break;
1538 case EHTokMin10float1:
1539 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1540 type.makeVector();
1541 break;
1542 case EHTokMin10float2:
1543 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 2);
1544 break;
1545 case EHTokMin10float3:
1546 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 3);
1547 break;
1548 case EHTokMin10float4:
1549 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 4);
1550 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001551
steve-lunarg3226b082016-10-26 19:18:55 -06001552 case EHTokMin16int:
1553 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1554 break;
1555 case EHTokMin16int1:
1556 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1557 type.makeVector();
1558 break;
1559 case EHTokMin16int2:
1560 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 2);
1561 break;
1562 case EHTokMin16int3:
1563 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 3);
1564 break;
1565 case EHTokMin16int4:
1566 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 4);
1567 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001568
steve-lunarg3226b082016-10-26 19:18:55 -06001569 case EHTokMin12int:
1570 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1571 break;
1572 case EHTokMin12int1:
1573 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1574 type.makeVector();
1575 break;
1576 case EHTokMin12int2:
1577 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 2);
1578 break;
1579 case EHTokMin12int3:
1580 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 3);
1581 break;
1582 case EHTokMin12int4:
1583 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 4);
1584 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001585
steve-lunarg3226b082016-10-26 19:18:55 -06001586 case EHTokMin16uint:
1587 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1588 break;
1589 case EHTokMin16uint1:
1590 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1591 type.makeVector();
1592 break;
1593 case EHTokMin16uint2:
1594 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 2);
1595 break;
1596 case EHTokMin16uint3:
1597 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 3);
1598 break;
1599 case EHTokMin16uint4:
1600 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 4);
1601 break;
1602
John Kessenich0133c122016-05-20 12:17:26 -06001603 case EHTokInt1x1:
1604 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1605 break;
1606 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001607 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001608 break;
1609 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001610 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001611 break;
1612 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001613 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001614 break;
1615 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001616 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001617 break;
1618 case EHTokInt2x2:
1619 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1620 break;
1621 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001622 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001623 break;
1624 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001625 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001626 break;
1627 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001628 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001629 break;
1630 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001631 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001632 break;
1633 case EHTokInt3x3:
1634 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1635 break;
1636 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001637 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001638 break;
1639 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001640 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001641 break;
1642 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001643 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001644 break;
1645 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001646 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001647 break;
1648 case EHTokInt4x4:
1649 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1650 break;
1651
John Kessenich71351de2016-06-08 12:50:56 -06001652 case EHTokUint1x1:
1653 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1654 break;
1655 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001656 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001657 break;
1658 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001659 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001660 break;
1661 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001662 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001663 break;
1664 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001665 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001666 break;
1667 case EHTokUint2x2:
1668 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1669 break;
1670 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001671 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001672 break;
1673 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001674 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001675 break;
1676 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001677 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001678 break;
1679 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001680 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001681 break;
1682 case EHTokUint3x3:
1683 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1684 break;
1685 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001686 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001687 break;
1688 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001689 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001690 break;
1691 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001692 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001693 break;
1694 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001695 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001696 break;
1697 case EHTokUint4x4:
1698 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1699 break;
1700
1701 case EHTokBool1x1:
1702 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1703 break;
1704 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001705 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001706 break;
1707 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001708 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001709 break;
1710 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001711 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001712 break;
1713 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001714 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001715 break;
1716 case EHTokBool2x2:
1717 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1718 break;
1719 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001720 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001721 break;
1722 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001723 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001724 break;
1725 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001726 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001727 break;
1728 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001729 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001730 break;
1731 case EHTokBool3x3:
1732 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1733 break;
1734 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001735 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001736 break;
1737 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001738 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001739 break;
1740 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001741 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001742 break;
1743 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001744 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001745 break;
1746 case EHTokBool4x4:
1747 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1748 break;
1749
John Kessenich0133c122016-05-20 12:17:26 -06001750 case EHTokFloat1x1:
1751 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1752 break;
1753 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001754 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001755 break;
1756 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001757 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001758 break;
1759 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001760 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001761 break;
1762 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001763 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001764 break;
John Kessenich87142c72016-03-12 20:24:24 -07001765 case EHTokFloat2x2:
1766 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1767 break;
1768 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001769 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001770 break;
1771 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001772 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001773 break;
John Kessenich0133c122016-05-20 12:17:26 -06001774 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001775 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001776 break;
John Kessenich87142c72016-03-12 20:24:24 -07001777 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001778 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001779 break;
1780 case EHTokFloat3x3:
1781 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1782 break;
1783 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001784 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001785 break;
John Kessenich0133c122016-05-20 12:17:26 -06001786 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001787 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001788 break;
John Kessenich87142c72016-03-12 20:24:24 -07001789 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001790 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001791 break;
1792 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001793 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001794 break;
1795 case EHTokFloat4x4:
1796 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1797 break;
1798
John Kessenich96f65522017-06-06 23:35:25 -06001799 case EHTokHalf1x1:
1800 new(&type) TType(half_bt, EvqTemporary, 0, 1, 1);
1801 break;
1802 case EHTokHalf1x2:
1803 new(&type) TType(half_bt, EvqTemporary, 0, 1, 2);
1804 break;
1805 case EHTokHalf1x3:
1806 new(&type) TType(half_bt, EvqTemporary, 0, 1, 3);
1807 break;
1808 case EHTokHalf1x4:
1809 new(&type) TType(half_bt, EvqTemporary, 0, 1, 4);
1810 break;
1811 case EHTokHalf2x1:
1812 new(&type) TType(half_bt, EvqTemporary, 0, 2, 1);
1813 break;
1814 case EHTokHalf2x2:
1815 new(&type) TType(half_bt, EvqTemporary, 0, 2, 2);
1816 break;
1817 case EHTokHalf2x3:
1818 new(&type) TType(half_bt, EvqTemporary, 0, 2, 3);
1819 break;
1820 case EHTokHalf2x4:
1821 new(&type) TType(half_bt, EvqTemporary, 0, 2, 4);
1822 break;
1823 case EHTokHalf3x1:
1824 new(&type) TType(half_bt, EvqTemporary, 0, 3, 1);
1825 break;
1826 case EHTokHalf3x2:
1827 new(&type) TType(half_bt, EvqTemporary, 0, 3, 2);
1828 break;
1829 case EHTokHalf3x3:
1830 new(&type) TType(half_bt, EvqTemporary, 0, 3, 3);
1831 break;
1832 case EHTokHalf3x4:
1833 new(&type) TType(half_bt, EvqTemporary, 0, 3, 4);
1834 break;
1835 case EHTokHalf4x1:
1836 new(&type) TType(half_bt, EvqTemporary, 0, 4, 1);
1837 break;
1838 case EHTokHalf4x2:
1839 new(&type) TType(half_bt, EvqTemporary, 0, 4, 2);
1840 break;
1841 case EHTokHalf4x3:
1842 new(&type) TType(half_bt, EvqTemporary, 0, 4, 3);
1843 break;
1844 case EHTokHalf4x4:
1845 new(&type) TType(half_bt, EvqTemporary, 0, 4, 4);
1846 break;
1847
John Kessenich0133c122016-05-20 12:17:26 -06001848 case EHTokDouble1x1:
1849 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1850 break;
1851 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001852 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001853 break;
1854 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001855 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001856 break;
1857 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001858 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001859 break;
1860 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001861 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001862 break;
1863 case EHTokDouble2x2:
1864 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1865 break;
1866 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001867 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001868 break;
1869 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001870 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001871 break;
1872 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001873 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001874 break;
1875 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001876 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001877 break;
1878 case EHTokDouble3x3:
1879 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1880 break;
1881 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001882 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001883 break;
1884 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001885 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001886 break;
1887 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001888 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001889 break;
1890 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001891 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001892 break;
1893 case EHTokDouble4x4:
1894 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1895 break;
1896
John Kessenich87142c72016-03-12 20:24:24 -07001897 default:
1898 return false;
1899 }
1900
1901 advanceToken();
1902
1903 return true;
1904}
1905
John Kesseniche6e74942016-06-11 16:43:14 -06001906// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001907// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1908// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
John Kessenich854fe242017-03-02 14:30:59 -07001909// | struct_type IDENTIFIER // use of previously declared struct type
John Kessenich3d157c52016-07-25 16:05:33 -06001910//
1911// struct_type
1912// : STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001913// | CLASS
John Kessenich3d157c52016-07-25 16:05:33 -06001914// | CBUFFER
1915// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001916//
John Kessenich54ee28f2017-03-11 14:13:00 -07001917bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
John Kesseniche6e74942016-06-11 16:43:14 -06001918{
John Kessenichb804de62016-09-05 12:19:18 -06001919 // This storage qualifier will tell us whether it's an AST
1920 // block type or just a generic structure type.
1921 TStorageQualifier storageQualifier = EvqTemporary;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001922 bool readonly = false;
John Kessenich3d157c52016-07-25 16:05:33 -06001923
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001924 if (acceptTokenClass(EHTokCBuffer)) {
John Kessenich2fcdd642017-06-19 15:41:11 -06001925 // CBUFFER
John Kessenichb804de62016-09-05 12:19:18 -06001926 storageQualifier = EvqUniform;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001927 } else if (acceptTokenClass(EHTokTBuffer)) {
John Kessenich2fcdd642017-06-19 15:41:11 -06001928 // TBUFFER
John Kessenichb804de62016-09-05 12:19:18 -06001929 storageQualifier = EvqBuffer;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001930 readonly = true;
John Kessenich054378d2017-06-19 15:13:26 -06001931 } else if (! acceptTokenClass(EHTokClass) && ! acceptTokenClass(EHTokStruct)) {
1932 // Neither CLASS nor STRUCT
John Kesseniche6e74942016-06-11 16:43:14 -06001933 return false;
John Kessenich054378d2017-06-19 15:13:26 -06001934 }
1935
1936 // Now known to be one of CBUFFER, TBUFFER, CLASS, or STRUCT
John Kesseniche6e74942016-06-11 16:43:14 -06001937
1938 // IDENTIFIER
1939 TString structName = "";
1940 if (peekTokenClass(EHTokIdentifier)) {
1941 structName = *token.string;
1942 advanceToken();
1943 }
1944
John Kessenich3d157c52016-07-25 16:05:33 -06001945 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001946 TQualifier postDeclQualifier;
1947 postDeclQualifier.clear();
John Kessenich854fe242017-03-02 14:30:59 -07001948 bool postDeclsFound = acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06001949
John Kessenichf3d88bd2017-03-19 12:24:29 -06001950 // LEFT_BRACE, or
John Kessenich854fe242017-03-02 14:30:59 -07001951 // struct_type IDENTIFIER
John Kesseniche6e74942016-06-11 16:43:14 -06001952 if (! acceptTokenClass(EHTokLeftBrace)) {
John Kessenich854fe242017-03-02 14:30:59 -07001953 if (structName.size() > 0 && !postDeclsFound && parseContext.lookupUserType(structName, type) != nullptr) {
1954 // struct_type IDENTIFIER
1955 return true;
1956 } else {
1957 expected("{");
1958 return false;
1959 }
John Kesseniche6e74942016-06-11 16:43:14 -06001960 }
1961
John Kessenichf3d88bd2017-03-19 12:24:29 -06001962
John Kesseniche6e74942016-06-11 16:43:14 -06001963 // struct_declaration_list
1964 TTypeList* typeList;
John Kessenichf3d88bd2017-03-19 12:24:29 -06001965 // Save each member function so they can be processed after we have a fully formed 'this'.
1966 TVector<TFunctionDeclarator> functionDeclarators;
1967
1968 parseContext.pushNamespace(structName);
John Kessenichaa3c64c2017-03-28 09:52:38 -06001969 bool acceptedList = acceptStructDeclarationList(typeList, nodeList, functionDeclarators);
John Kessenichf3d88bd2017-03-19 12:24:29 -06001970 parseContext.popNamespace();
1971
1972 if (! acceptedList) {
John Kesseniche6e74942016-06-11 16:43:14 -06001973 expected("struct member declarations");
1974 return false;
1975 }
1976
1977 // RIGHT_BRACE
1978 if (! acceptTokenClass(EHTokRightBrace)) {
1979 expected("}");
1980 return false;
1981 }
1982
1983 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001984 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001985 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001986 else {
John Kessenich7735b942016-09-05 12:40:06 -06001987 postDeclQualifier.storage = storageQualifier;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001988 postDeclQualifier.readonly = readonly;
John Kessenich7735b942016-09-05 12:40:06 -06001989 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001990 }
John Kesseniche6e74942016-06-11 16:43:14 -06001991
John Kessenich727b3742017-02-03 17:57:55 -07001992 parseContext.declareStruct(token.loc, structName, type);
John Kesseniche6e74942016-06-11 16:43:14 -06001993
John Kessenich4960baa2017-03-19 18:09:59 -06001994 // For member functions: now that we know the type of 'this', go back and
1995 // - add their implicit argument with 'this' (not to the mangling, just the argument list)
1996 // - parse the functions, their tokens were saved for deferred parsing (now)
1997 for (int b = 0; b < (int)functionDeclarators.size(); ++b) {
1998 // update signature
1999 if (functionDeclarators[b].function->hasImplicitThis())
John Kessenich37789792017-03-21 23:56:40 -06002000 functionDeclarators[b].function->addThisParameter(type, intermediate.implicitThisName);
John Kessenich4960baa2017-03-19 18:09:59 -06002001 }
2002
John Kessenichf3d88bd2017-03-19 12:24:29 -06002003 // All member functions get parsed inside the class/struct namespace and with the
2004 // class/struct members in a symbol-table level.
2005 parseContext.pushNamespace(structName);
John Kessenich0a2a0cd2017-05-16 23:16:26 -06002006 parseContext.pushThisScope(type, functionDeclarators);
John Kessenichf3d88bd2017-03-19 12:24:29 -06002007 bool deferredSuccess = true;
2008 for (int b = 0; b < (int)functionDeclarators.size() && deferredSuccess; ++b) {
2009 // parse body
2010 pushTokenStream(functionDeclarators[b].body);
2011 if (! acceptFunctionBody(functionDeclarators[b], nodeList))
2012 deferredSuccess = false;
2013 popTokenStream();
2014 }
John Kessenich37789792017-03-21 23:56:40 -06002015 parseContext.popThisScope();
John Kessenichf3d88bd2017-03-19 12:24:29 -06002016 parseContext.popNamespace();
2017
2018 return deferredSuccess;
John Kesseniche6e74942016-06-11 16:43:14 -06002019}
2020
steve-lunarga766b832017-04-25 09:30:28 -06002021// constantbuffer
2022// : CONSTANTBUFFER LEFT_ANGLE type RIGHT_ANGLE
2023bool HlslGrammar::acceptConstantBufferType(TType& type)
2024{
2025 if (! acceptTokenClass(EHTokConstantBuffer))
2026 return false;
2027
2028 if (! acceptTokenClass(EHTokLeftAngle)) {
2029 expected("left angle bracket");
2030 return false;
2031 }
2032
2033 TType templateType;
2034 if (! acceptType(templateType)) {
2035 expected("type");
2036 return false;
2037 }
2038
2039 if (! acceptTokenClass(EHTokRightAngle)) {
2040 expected("right angle bracket");
2041 return false;
2042 }
2043
2044 TQualifier postDeclQualifier;
2045 postDeclQualifier.clear();
2046 postDeclQualifier.storage = EvqUniform;
2047
2048 if (templateType.isStruct()) {
2049 // Make a block from the type parsed as the template argument
2050 TTypeList* typeList = templateType.getWritableStruct();
2051 new(&type) TType(typeList, "", postDeclQualifier); // sets EbtBlock
2052
2053 type.getQualifier().storage = EvqUniform;
2054
2055 return true;
2056 } else {
2057 parseContext.error(token.loc, "non-structure type in ConstantBuffer", "", "");
2058 return false;
2059 }
2060}
2061
steve-lunarg5da1f032017-02-12 17:50:28 -07002062// struct_buffer
2063// : APPENDSTRUCTUREDBUFFER
2064// | BYTEADDRESSBUFFER
2065// | CONSUMESTRUCTUREDBUFFER
2066// | RWBYTEADDRESSBUFFER
2067// | RWSTRUCTUREDBUFFER
2068// | STRUCTUREDBUFFER
2069bool HlslGrammar::acceptStructBufferType(TType& type)
2070{
2071 const EHlslTokenClass structBuffType = peek();
2072
2073 // TODO: globallycoherent
2074 bool hasTemplateType = true;
2075 bool readonly = false;
2076
2077 TStorageQualifier storage = EvqBuffer;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002078 TBuiltInVariable builtinType = EbvNone;
steve-lunarg5da1f032017-02-12 17:50:28 -07002079
2080 switch (structBuffType) {
2081 case EHTokAppendStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002082 builtinType = EbvAppendConsume;
2083 break;
steve-lunarg5da1f032017-02-12 17:50:28 -07002084 case EHTokByteAddressBuffer:
2085 hasTemplateType = false;
2086 readonly = true;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002087 builtinType = EbvByteAddressBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002088 break;
2089 case EHTokConsumeStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002090 builtinType = EbvAppendConsume;
2091 break;
steve-lunarg5da1f032017-02-12 17:50:28 -07002092 case EHTokRWByteAddressBuffer:
2093 hasTemplateType = false;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002094 builtinType = EbvRWByteAddressBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002095 break;
2096 case EHTokRWStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002097 builtinType = EbvRWStructuredBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002098 break;
2099 case EHTokStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002100 builtinType = EbvStructuredBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002101 readonly = true;
2102 break;
2103 default:
2104 return false; // not a structure buffer type
2105 }
2106
2107 advanceToken(); // consume the structure keyword
2108
2109 // type on which this StructedBuffer is templatized. E.g, StructedBuffer<MyStruct> ==> MyStruct
2110 TType* templateType = new TType;
2111
2112 if (hasTemplateType) {
2113 if (! acceptTokenClass(EHTokLeftAngle)) {
2114 expected("left angle bracket");
2115 return false;
2116 }
2117
2118 if (! acceptType(*templateType)) {
2119 expected("type");
2120 return false;
2121 }
2122 if (! acceptTokenClass(EHTokRightAngle)) {
2123 expected("right angle bracket");
2124 return false;
2125 }
2126 } else {
2127 // byte address buffers have no explicit type.
2128 TType uintType(EbtUint, storage);
2129 templateType->shallowCopy(uintType);
2130 }
2131
2132 // Create an unsized array out of that type.
2133 // TODO: does this work if it's already an array type?
2134 TArraySizes unsizedArray;
2135 unsizedArray.addInnerSize(UnsizedArraySize);
2136 templateType->newArraySizes(unsizedArray);
steve-lunarg40efe5c2017-03-06 12:01:44 -07002137 templateType->getQualifier().storage = storage;
steve-lunargdd8287a2017-02-23 18:04:12 -07002138
2139 // field name is canonical for all structbuffers
2140 templateType->setFieldName("@data");
steve-lunarg5da1f032017-02-12 17:50:28 -07002141
steve-lunarg5da1f032017-02-12 17:50:28 -07002142 TTypeList* blockStruct = new TTypeList;
2143 TTypeLoc member = { templateType, token.loc };
2144 blockStruct->push_back(member);
2145
steve-lunargdd8287a2017-02-23 18:04:12 -07002146 // This is the type of the buffer block (SSBO)
steve-lunarg5da1f032017-02-12 17:50:28 -07002147 TType blockType(blockStruct, "", templateType->getQualifier());
2148
steve-lunargdd8287a2017-02-23 18:04:12 -07002149 blockType.getQualifier().storage = storage;
2150 blockType.getQualifier().readonly = readonly;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002151 blockType.getQualifier().builtIn = builtinType;
steve-lunargdd8287a2017-02-23 18:04:12 -07002152
2153 // We may have created an equivalent type before, in which case we should use its
2154 // deep structure.
2155 parseContext.shareStructBufferType(blockType);
2156
steve-lunarg5da1f032017-02-12 17:50:28 -07002157 type.shallowCopy(blockType);
2158
2159 return true;
2160}
2161
John Kesseniche6e74942016-06-11 16:43:14 -06002162// struct_declaration_list
2163// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
2164//
2165// struct_declaration
2166// : fully_specified_type struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002167// | fully_specified_type IDENTIFIER function_parameters post_decls compound_statement // member-function definition
John Kesseniche6e74942016-06-11 16:43:14 -06002168//
2169// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06002170// : IDENTIFIER post_decls
2171// | IDENTIFIER array_specifier post_decls
John Kessenich54ee28f2017-03-11 14:13:00 -07002172// | IDENTIFIER function_parameters post_decls // member-function prototype
John Kesseniche6e74942016-06-11 16:43:14 -06002173//
John Kessenichaa3c64c2017-03-28 09:52:38 -06002174bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList, TIntermNode*& nodeList,
John Kessenichf3d88bd2017-03-19 12:24:29 -06002175 TVector<TFunctionDeclarator>& declarators)
John Kesseniche6e74942016-06-11 16:43:14 -06002176{
2177 typeList = new TTypeList();
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002178 HlslToken idToken;
John Kesseniche6e74942016-06-11 16:43:14 -06002179
2180 do {
2181 // success on seeing the RIGHT_BRACE coming up
2182 if (peekTokenClass(EHTokRightBrace))
John Kessenichb16f7e62017-03-11 19:32:47 -07002183 break;
John Kesseniche6e74942016-06-11 16:43:14 -06002184
2185 // struct_declaration
John Kessenich54ee28f2017-03-11 14:13:00 -07002186
2187 bool declarator_list = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002188
2189 // fully_specified_type
2190 TType memberType;
John Kessenich54ee28f2017-03-11 14:13:00 -07002191 if (! acceptFullySpecifiedType(memberType, nodeList)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002192 expected("member type");
2193 return false;
2194 }
2195
2196 // struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002197 bool functionDefinitionAccepted = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002198 do {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002199 if (! acceptIdentifier(idToken)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002200 expected("member name");
2201 return false;
2202 }
2203
John Kessenich54ee28f2017-03-11 14:13:00 -07002204 if (peekTokenClass(EHTokLeftParen)) {
2205 // function_parameters
2206 if (!declarator_list) {
John Kessenichb16f7e62017-03-11 19:32:47 -07002207 declarators.resize(declarators.size() + 1);
2208 // request a token stream for deferred processing
John Kessenichf3d88bd2017-03-19 12:24:29 -06002209 functionDefinitionAccepted = acceptMemberFunctionDefinition(nodeList, memberType, *idToken.string,
2210 declarators.back());
John Kessenich54ee28f2017-03-11 14:13:00 -07002211 if (functionDefinitionAccepted)
2212 break;
2213 }
2214 expected("member-function definition");
2215 return false;
2216 } else {
2217 // add it to the list of members
2218 TTypeLoc member = { new TType(EbtVoid), token.loc };
2219 member.type->shallowCopy(memberType);
2220 member.type->setFieldName(*idToken.string);
2221 typeList->push_back(member);
John Kesseniche6e74942016-06-11 16:43:14 -06002222
John Kessenich54ee28f2017-03-11 14:13:00 -07002223 // array_specifier
2224 TArraySizes* arraySizes = nullptr;
2225 acceptArraySpecifier(arraySizes);
2226 if (arraySizes)
2227 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06002228
John Kessenich54ee28f2017-03-11 14:13:00 -07002229 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06002230
John Kessenich54ee28f2017-03-11 14:13:00 -07002231 // EQUAL assignment_expression
2232 if (acceptTokenClass(EHTokAssign)) {
2233 parseContext.warn(idToken.loc, "struct-member initializers ignored", "typedef", "");
2234 TIntermTyped* expressionNode = nullptr;
2235 if (! acceptAssignmentExpression(expressionNode)) {
2236 expected("initializer");
2237 return false;
2238 }
John Kessenich18adbdb2017-02-02 15:16:20 -07002239 }
2240 }
John Kesseniche6e74942016-06-11 16:43:14 -06002241 // success on seeing the SEMICOLON coming up
2242 if (peekTokenClass(EHTokSemicolon))
2243 break;
2244
2245 // COMMA
John Kessenich54ee28f2017-03-11 14:13:00 -07002246 if (acceptTokenClass(EHTokComma))
2247 declarator_list = true;
2248 else {
John Kesseniche6e74942016-06-11 16:43:14 -06002249 expected(",");
2250 return false;
2251 }
2252
2253 } while (true);
2254
2255 // SEMI_COLON
John Kessenich54ee28f2017-03-11 14:13:00 -07002256 if (! functionDefinitionAccepted && ! acceptTokenClass(EHTokSemicolon)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002257 expected(";");
2258 return false;
2259 }
2260
2261 } while (true);
John Kessenichb16f7e62017-03-11 19:32:47 -07002262
John Kessenichb16f7e62017-03-11 19:32:47 -07002263 return true;
John Kesseniche6e74942016-06-11 16:43:14 -06002264}
2265
John Kessenich54ee28f2017-03-11 14:13:00 -07002266// member_function_definition
2267// | function_parameters post_decls compound_statement
2268//
2269// Expects type to have EvqGlobal for a static member and
2270// EvqTemporary for non-static member.
John Kessenich9855bda2017-09-11 21:48:19 -06002271bool HlslGrammar::acceptMemberFunctionDefinition(TIntermNode*& nodeList, const TType& type, TString& memberName,
John Kessenichf3d88bd2017-03-19 12:24:29 -06002272 TFunctionDeclarator& declarator)
John Kessenich54ee28f2017-03-11 14:13:00 -07002273{
John Kessenich54ee28f2017-03-11 14:13:00 -07002274 bool accepted = false;
2275
John Kessenich9855bda2017-09-11 21:48:19 -06002276 TString* functionName = &memberName;
John Kessenich4dc835c2017-03-28 23:43:10 -06002277 parseContext.getFullNamespaceName(functionName);
John Kessenich088d52b2017-03-11 17:55:28 -07002278 declarator.function = new TFunction(functionName, type);
John Kessenich4960baa2017-03-19 18:09:59 -06002279 if (type.getQualifier().storage == EvqTemporary)
2280 declarator.function->setImplicitThis();
John Kessenich37789792017-03-21 23:56:40 -06002281 else
2282 declarator.function->setIllegalImplicitThis();
John Kessenich54ee28f2017-03-11 14:13:00 -07002283
2284 // function_parameters
John Kessenich088d52b2017-03-11 17:55:28 -07002285 if (acceptFunctionParameters(*declarator.function)) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002286 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -07002287 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich54ee28f2017-03-11 14:13:00 -07002288
2289 // compound_statement (function body definition)
2290 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich088d52b2017-03-11 17:55:28 -07002291 declarator.loc = token.loc;
John Kessenichf3d88bd2017-03-19 12:24:29 -06002292 declarator.body = new TVector<HlslToken>;
2293 accepted = acceptFunctionDefinition(declarator, nodeList, declarator.body);
John Kessenich54ee28f2017-03-11 14:13:00 -07002294 }
2295 } else
2296 expected("function parameter list");
2297
John Kessenich54ee28f2017-03-11 14:13:00 -07002298 return accepted;
2299}
2300
John Kessenich5f934b02016-03-13 17:58:25 -06002301// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06002302// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06002303// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002304//
2305bool HlslGrammar::acceptFunctionParameters(TFunction& function)
2306{
John Kessenich078d7f22016-03-14 10:02:11 -06002307 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002308 if (! acceptTokenClass(EHTokLeftParen))
2309 return false;
2310
John Kessenich71351de2016-06-08 12:50:56 -06002311 // VOID RIGHT_PAREN
2312 if (! acceptTokenClass(EHTokVoid)) {
2313 do {
2314 // parameter_declaration
2315 if (! acceptParameterDeclaration(function))
2316 break;
John Kessenich5f934b02016-03-13 17:58:25 -06002317
John Kessenich71351de2016-06-08 12:50:56 -06002318 // COMMA
2319 if (! acceptTokenClass(EHTokComma))
2320 break;
2321 } while (true);
2322 }
John Kessenich5f934b02016-03-13 17:58:25 -06002323
John Kessenich078d7f22016-03-14 10:02:11 -06002324 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002325 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002326 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06002327 return false;
2328 }
2329
2330 return true;
2331}
2332
steve-lunarg26d31452016-12-23 18:56:57 -07002333// default_parameter_declaration
2334// : EQUAL conditional_expression
2335// : EQUAL initializer
2336bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTyped*& node)
2337{
2338 node = nullptr;
2339
2340 // Valid not to have a default_parameter_declaration
2341 if (!acceptTokenClass(EHTokAssign))
2342 return true;
2343
2344 if (!acceptConditionalExpression(node)) {
2345 if (!acceptInitializer(node))
2346 return false;
2347
2348 // For initializer lists, we have to const-fold into a constructor for the type, so build
2349 // that.
John Kessenichc633f642017-04-03 21:48:37 -06002350 TFunction* constructor = parseContext.makeConstructorCall(token.loc, type);
steve-lunarg26d31452016-12-23 18:56:57 -07002351 if (constructor == nullptr) // cannot construct
2352 return false;
2353
2354 TIntermTyped* arguments = nullptr;
John Kessenichecba76f2017-01-06 00:34:48 -07002355 for (int i = 0; i < int(node->getAsAggregate()->getSequence().size()); i++)
steve-lunarg26d31452016-12-23 18:56:57 -07002356 parseContext.handleFunctionArgument(constructor, arguments, node->getAsAggregate()->getSequence()[i]->getAsTyped());
John Kessenichecba76f2017-01-06 00:34:48 -07002357
steve-lunarg26d31452016-12-23 18:56:57 -07002358 node = parseContext.handleFunctionCall(token.loc, constructor, node);
2359 }
2360
2361 // If this is simply a constant, we can use it directly.
2362 if (node->getAsConstantUnion())
2363 return true;
2364
2365 // Otherwise, it has to be const-foldable.
2366 TIntermTyped* origNode = node;
2367
2368 node = intermediate.fold(node->getAsAggregate());
2369
2370 if (node != nullptr && origNode != node)
2371 return true;
2372
2373 parseContext.error(token.loc, "invalid default parameter value", "", "");
2374
2375 return false;
2376}
2377
John Kessenich5f934b02016-03-13 17:58:25 -06002378// parameter_declaration
John Kessenich77ea30b2017-09-30 14:34:50 -06002379// : attributes attributed_declaration
2380//
2381// attributed_declaration
steve-lunarg26d31452016-12-23 18:56:57 -07002382// : fully_specified_type post_decls [ = default_parameter_declaration ]
2383// | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
John Kessenich5f934b02016-03-13 17:58:25 -06002384//
2385bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
2386{
John Kessenich77ea30b2017-09-30 14:34:50 -06002387 // attributes
2388 TAttributeMap attributes;
2389 acceptAttributes(attributes);
2390
John Kessenich5f934b02016-03-13 17:58:25 -06002391 // fully_specified_type
2392 TType* type = new TType;
2393 if (! acceptFullySpecifiedType(*type))
2394 return false;
2395
John Kessenich77ea30b2017-09-30 14:34:50 -06002396 parseContext.transferTypeAttributes(attributes, *type);
2397
John Kessenich5f934b02016-03-13 17:58:25 -06002398 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06002399 HlslToken idToken;
2400 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06002401
John Kessenich19b92ff2016-06-19 11:50:34 -06002402 // array_specifier
2403 TArraySizes* arraySizes = nullptr;
2404 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002405 if (arraySizes) {
2406 if (arraySizes->isImplicit()) {
2407 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
2408 return false;
2409 }
2410
John Kessenich19b92ff2016-06-19 11:50:34 -06002411 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002412 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002413
2414 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06002415 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06002416
steve-lunarg26d31452016-12-23 18:56:57 -07002417 TIntermTyped* defaultValue;
2418 if (!acceptDefaultParameterDeclaration(*type, defaultValue))
2419 return false;
2420
John Kessenich5aa59e22016-06-17 15:50:47 -06002421 parseContext.paramFix(*type);
2422
steve-lunarg26d31452016-12-23 18:56:57 -07002423 // If any prior parameters have default values, all the parameters after that must as well.
2424 if (defaultValue == nullptr && function.getDefaultParamCount() > 0) {
2425 parseContext.error(idToken.loc, "invalid parameter after default value parameters", idToken.string->c_str(), "");
2426 return false;
2427 }
2428
2429 TParameter param = { idToken.string, type, defaultValue };
John Kessenich5f934b02016-03-13 17:58:25 -06002430 function.addParameter(param);
2431
2432 return true;
2433}
2434
2435// Do the work to create the function definition in addition to
2436// parsing the body (compound_statement).
John Kessenichb16f7e62017-03-11 19:32:47 -07002437//
2438// If 'deferredTokens' are passed in, just get the token stream,
2439// don't process.
2440//
2441bool HlslGrammar::acceptFunctionDefinition(TFunctionDeclarator& declarator, TIntermNode*& nodeList,
2442 TVector<HlslToken>* deferredTokens)
John Kessenich5f934b02016-03-13 17:58:25 -06002443{
John Kessenich088d52b2017-03-11 17:55:28 -07002444 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, false /* not prototype */);
John Kessenich5f934b02016-03-13 17:58:25 -06002445
John Kessenichb16f7e62017-03-11 19:32:47 -07002446 if (deferredTokens)
2447 return captureBlockTokens(*deferredTokens);
2448 else
John Kessenich4960baa2017-03-19 18:09:59 -06002449 return acceptFunctionBody(declarator, nodeList);
John Kessenich088d52b2017-03-11 17:55:28 -07002450}
2451
2452bool HlslGrammar::acceptFunctionBody(TFunctionDeclarator& declarator, TIntermNode*& nodeList)
2453{
2454 // we might get back an entry-point
John Kessenichca71d942017-03-07 20:44:09 -07002455 TIntermNode* entryPointNode = nullptr;
2456
John Kessenich077e0522016-06-09 02:02:17 -06002457 // This does a pushScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002458 TIntermNode* functionNode = parseContext.handleFunctionDefinition(declarator.loc, *declarator.function,
2459 declarator.attributes, entryPointNode);
John Kessenich5f934b02016-03-13 17:58:25 -06002460
2461 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002462 TIntermNode* functionBody = nullptr;
John Kessenich02467d82017-01-19 15:41:47 -07002463 if (! acceptCompoundStatement(functionBody))
2464 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002465
John Kessenich54ee28f2017-03-11 14:13:00 -07002466 // this does a popScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002467 parseContext.handleFunctionBody(declarator.loc, *declarator.function, functionBody, functionNode);
John Kessenichca71d942017-03-07 20:44:09 -07002468
2469 // Hook up the 1 or 2 function definitions.
2470 nodeList = intermediate.growAggregate(nodeList, functionNode);
2471 nodeList = intermediate.growAggregate(nodeList, entryPointNode);
John Kessenich02467d82017-01-19 15:41:47 -07002472
2473 return true;
John Kessenich5f934b02016-03-13 17:58:25 -06002474}
2475
John Kessenich0d2b6de2016-06-05 11:23:11 -06002476// Accept an expression with parenthesis around it, where
2477// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002478// syntactically required ones like in "if ( expression )".
2479//
2480// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06002481//
2482// Note this one is not set up to be speculative; as it gives
2483// errors if not found.
2484//
2485bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
2486{
2487 // LEFT_PAREN
2488 if (! acceptTokenClass(EHTokLeftParen))
2489 expected("(");
2490
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002491 bool decl = false;
2492 TIntermNode* declNode = nullptr;
2493 decl = acceptControlDeclaration(declNode);
2494 if (decl) {
2495 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
2496 expected("initialized declaration");
2497 return false;
2498 } else
2499 expression = declNode->getAsTyped();
2500 } else {
2501 // no declaration
2502 if (! acceptExpression(expression)) {
2503 expected("expression");
2504 return false;
2505 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06002506 }
2507
2508 // RIGHT_PAREN
2509 if (! acceptTokenClass(EHTokRightParen))
2510 expected(")");
2511
2512 return true;
2513}
2514
John Kessenich34fb0362016-05-03 23:17:20 -06002515// The top-level full expression recognizer.
2516//
John Kessenich87142c72016-03-12 20:24:24 -07002517// expression
John Kessenich34fb0362016-05-03 23:17:20 -06002518// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07002519//
2520bool HlslGrammar::acceptExpression(TIntermTyped*& node)
2521{
LoopDawgef764a22016-06-03 09:17:51 -06002522 node = nullptr;
2523
John Kessenich34fb0362016-05-03 23:17:20 -06002524 // assignment_expression
2525 if (! acceptAssignmentExpression(node))
2526 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002527
John Kessenich34fb0362016-05-03 23:17:20 -06002528 if (! peekTokenClass(EHTokComma))
2529 return true;
2530
2531 do {
2532 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06002533 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06002534 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06002535
John Kessenich34fb0362016-05-03 23:17:20 -06002536 // ... assignment_expression
2537 TIntermTyped* rightNode = nullptr;
2538 if (! acceptAssignmentExpression(rightNode)) {
2539 expected("assignment expression");
2540 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002541 }
2542
John Kessenich34fb0362016-05-03 23:17:20 -06002543 node = intermediate.addComma(node, rightNode, loc);
2544
2545 if (! peekTokenClass(EHTokComma))
2546 return true;
2547 } while (true);
2548}
2549
John Kessenich07354242016-07-01 19:58:06 -06002550// initializer
John Kessenich98ad4852016-11-27 17:39:07 -07002551// : LEFT_BRACE RIGHT_BRACE
2552// | LEFT_BRACE initializer_list RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002553//
2554// initializer_list
2555// : assignment_expression COMMA assignment_expression COMMA ...
2556//
2557bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
2558{
2559 // LEFT_BRACE
2560 if (! acceptTokenClass(EHTokLeftBrace))
2561 return false;
2562
John Kessenich98ad4852016-11-27 17:39:07 -07002563 // RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002564 TSourceLoc loc = token.loc;
John Kessenich98ad4852016-11-27 17:39:07 -07002565 if (acceptTokenClass(EHTokRightBrace)) {
2566 // a zero-length initializer list
2567 node = intermediate.makeAggregate(loc);
2568 return true;
2569 }
2570
2571 // initializer_list
John Kessenich07354242016-07-01 19:58:06 -06002572 node = nullptr;
2573 do {
2574 // assignment_expression
2575 TIntermTyped* expr;
2576 if (! acceptAssignmentExpression(expr)) {
2577 expected("assignment expression in initializer list");
2578 return false;
2579 }
LoopDawg0fca0ba2017-07-10 15:43:40 -06002580
2581 const bool firstNode = (node == nullptr);
2582
John Kessenich07354242016-07-01 19:58:06 -06002583 node = intermediate.growAggregate(node, expr, loc);
2584
LoopDawg0fca0ba2017-07-10 15:43:40 -06002585 // If every sub-node in the list has qualifier EvqConst, the returned node becomes
2586 // EvqConst. Otherwise, it becomes EvqTemporary. That doesn't happen with e.g.
2587 // EvqIn or EvqPosition, since the collection isn't EvqPosition if all the members are.
2588 if (firstNode && expr->getQualifier().storage == EvqConst)
2589 node->getQualifier().storage = EvqConst;
2590 else if (expr->getQualifier().storage != EvqConst)
2591 node->getQualifier().storage = EvqTemporary;
2592
John Kessenich07354242016-07-01 19:58:06 -06002593 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002594 if (acceptTokenClass(EHTokComma)) {
2595 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
2596 return true;
John Kessenich07354242016-07-01 19:58:06 -06002597 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002598 }
John Kessenich07354242016-07-01 19:58:06 -06002599
2600 // RIGHT_BRACE
2601 if (acceptTokenClass(EHTokRightBrace))
2602 return true;
2603
2604 expected(", or }");
2605 return false;
2606 } while (true);
2607}
2608
John Kessenich34fb0362016-05-03 23:17:20 -06002609// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06002610// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06002611//
2612// a op (b op (c op d))
2613//
2614// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06002615// : initializer
2616// | conditional_expression
2617// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06002618//
2619bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
2620{
John Kessenich07354242016-07-01 19:58:06 -06002621 // initializer
2622 if (peekTokenClass(EHTokLeftBrace)) {
2623 if (acceptInitializer(node))
2624 return true;
2625
2626 expected("initializer");
2627 return false;
2628 }
2629
John Kessenich00957f82016-07-27 10:39:57 -06002630 // conditional_expression
2631 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06002632 return false;
2633
John Kessenich07354242016-07-01 19:58:06 -06002634 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06002635 TOperator assignOp = HlslOpMap::assignment(peek());
2636 if (assignOp == EOpNull)
2637 return true;
2638
John Kessenich00957f82016-07-27 10:39:57 -06002639 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06002640 TSourceLoc loc = token.loc;
2641 advanceToken();
2642
John Kessenich00957f82016-07-27 10:39:57 -06002643 // conditional_expression assign_op conditional_expression ...
2644 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06002645 // gets the right-to-left associativity.
2646 TIntermTyped* rightNode = nullptr;
2647 if (! acceptAssignmentExpression(rightNode)) {
2648 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06002649 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002650 }
2651
John Kessenichd21baed2016-09-16 03:05:12 -06002652 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
steve-lunarg90707962016-10-07 19:35:40 -06002653 node = parseContext.handleLvalue(loc, "assign", node);
2654
John Kessenichfea226b2016-07-28 17:53:56 -06002655 if (node == nullptr) {
2656 parseContext.error(loc, "could not create assignment", "", "");
2657 return false;
2658 }
John Kessenich34fb0362016-05-03 23:17:20 -06002659
2660 if (! peekTokenClass(EHTokComma))
2661 return true;
2662
2663 return true;
2664}
2665
John Kessenich00957f82016-07-27 10:39:57 -06002666// Accept a conditional expression, which associates right-to-left,
2667// accomplished by the "true" expression calling down to lower
2668// precedence levels than this level.
2669//
2670// conditional_expression
2671// : binary_expression
2672// | binary_expression QUESTION expression COLON assignment_expression
2673//
2674bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
2675{
2676 // binary_expression
2677 if (! acceptBinaryExpression(node, PlLogicalOr))
2678 return false;
2679
2680 if (! acceptTokenClass(EHTokQuestion))
2681 return true;
2682
John Kessenich636b62d2017-04-11 19:45:00 -06002683 node = parseContext.convertConditionalExpression(token.loc, node, false);
John Kessenich7e997e22017-03-30 22:09:30 -06002684 if (node == nullptr)
2685 return false;
2686
John Kessenichf6deacd2017-06-06 19:52:55 -06002687 ++parseContext.controlFlowNestingLevel; // this only needs to work right if no errors
2688
John Kessenich00957f82016-07-27 10:39:57 -06002689 TIntermTyped* trueNode = nullptr;
2690 if (! acceptExpression(trueNode)) {
2691 expected("expression after ?");
2692 return false;
2693 }
2694 TSourceLoc loc = token.loc;
2695
2696 if (! acceptTokenClass(EHTokColon)) {
2697 expected(":");
2698 return false;
2699 }
2700
2701 TIntermTyped* falseNode = nullptr;
2702 if (! acceptAssignmentExpression(falseNode)) {
2703 expected("expression after :");
2704 return false;
2705 }
2706
John Kessenichf6deacd2017-06-06 19:52:55 -06002707 --parseContext.controlFlowNestingLevel;
2708
John Kessenich00957f82016-07-27 10:39:57 -06002709 node = intermediate.addSelection(node, trueNode, falseNode, loc);
2710
2711 return true;
2712}
2713
John Kessenich34fb0362016-05-03 23:17:20 -06002714// Accept a binary expression, for binary operations that
2715// associate left-to-right. This is, it is implicit, for example
2716//
2717// ((a op b) op c) op d
2718//
2719// binary_expression
2720// : expression op expression op expression ...
2721//
2722// where 'expression' is the next higher level in precedence.
2723//
2724bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
2725{
2726 if (precedenceLevel > PlMul)
2727 return acceptUnaryExpression(node);
2728
2729 // assignment_expression
2730 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
2731 return false;
2732
John Kessenich34fb0362016-05-03 23:17:20 -06002733 do {
John Kessenich64076ed2016-07-28 21:43:17 -06002734 TOperator op = HlslOpMap::binary(peek());
2735 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
2736 if (tokenLevel < precedenceLevel)
2737 return true;
2738
John Kessenich34fb0362016-05-03 23:17:20 -06002739 // ... op
2740 TSourceLoc loc = token.loc;
2741 advanceToken();
2742
2743 // ... expression
2744 TIntermTyped* rightNode = nullptr;
2745 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
2746 expected("expression");
2747 return false;
2748 }
2749
2750 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06002751 if (node == nullptr) {
2752 parseContext.error(loc, "Could not perform requested binary operation", "", "");
2753 return false;
2754 }
John Kessenich34fb0362016-05-03 23:17:20 -06002755 } while (true);
2756}
2757
2758// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06002759// : (type) unary_expression
2760// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06002761// | - unary_expression
2762// | ! unary_expression
2763// | ~ unary_expression
2764// | ++ unary_expression
2765// | -- unary_expression
2766// | postfix_expression
2767//
2768bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
2769{
John Kessenich1cc1a282016-06-03 16:55:49 -06002770 // (type) unary_expression
2771 // Have to look two steps ahead, because this could be, e.g., a
2772 // postfix_expression instead, since that also starts with at "(".
2773 if (acceptTokenClass(EHTokLeftParen)) {
2774 TType castType;
2775 if (acceptType(castType)) {
John Kessenich82ae8c32017-06-13 23:13:10 -06002776 // recognize any array_specifier as part of the type
2777 TArraySizes* arraySizes = nullptr;
2778 acceptArraySpecifier(arraySizes);
2779 if (arraySizes != nullptr)
2780 castType.newArraySizes(*arraySizes);
2781 TSourceLoc loc = token.loc;
steve-lunarg5964c642016-07-30 07:38:55 -06002782 if (acceptTokenClass(EHTokRightParen)) {
2783 // We've matched "(type)" now, get the expression to cast
steve-lunarg5964c642016-07-30 07:38:55 -06002784 if (! acceptUnaryExpression(node))
2785 return false;
2786
2787 // Hook it up like a constructor
John Kessenichc633f642017-04-03 21:48:37 -06002788 TFunction* constructorFunction = parseContext.makeConstructorCall(loc, castType);
steve-lunarg5964c642016-07-30 07:38:55 -06002789 if (constructorFunction == nullptr) {
2790 expected("type that can be constructed");
2791 return false;
2792 }
2793 TIntermTyped* arguments = nullptr;
2794 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
2795 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
2796
2797 return true;
2798 } else {
2799 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
2800 // the '(int' part. We must back up twice.
2801 recedeToken();
2802 recedeToken();
John Kessenich82ae8c32017-06-13 23:13:10 -06002803
2804 // Note, there are no array constructors like
2805 // (float[2](...))
2806 if (arraySizes != nullptr)
2807 parseContext.error(loc, "parenthesized array constructor not allowed", "([]())", "", "");
John Kessenich1cc1a282016-06-03 16:55:49 -06002808 }
John Kessenich1cc1a282016-06-03 16:55:49 -06002809 } else {
2810 // This isn't a type cast, but it still started "(", so if it is a
2811 // unary expression, it can only be a postfix_expression, so try that.
2812 // Back it up first.
2813 recedeToken();
2814 return acceptPostfixExpression(node);
2815 }
2816 }
2817
2818 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06002819 TOperator unaryOp = HlslOpMap::preUnary(peek());
John Kessenichecba76f2017-01-06 00:34:48 -07002820
John Kessenich1cc1a282016-06-03 16:55:49 -06002821 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06002822 if (unaryOp == EOpNull)
2823 return acceptPostfixExpression(node);
2824
2825 // op unary_expression
2826 TSourceLoc loc = token.loc;
2827 advanceToken();
2828 if (! acceptUnaryExpression(node))
2829 return false;
2830
2831 // + is a no-op
2832 if (unaryOp == EOpAdd)
2833 return true;
2834
2835 node = intermediate.addUnaryMath(unaryOp, node, loc);
steve-lunarge5921f12016-10-15 10:29:58 -06002836
2837 // These unary ops require lvalues
2838 if (unaryOp == EOpPreIncrement || unaryOp == EOpPreDecrement)
2839 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002840
2841 return node != nullptr;
2842}
2843
2844// postfix_expression
2845// : LEFT_PAREN expression RIGHT_PAREN
2846// | literal
2847// | constructor
John Kessenich8f9fdc92017-03-30 16:22:26 -06002848// | IDENTIFIER [ COLONCOLON IDENTIFIER [ COLONCOLON IDENTIFIER ... ] ]
John Kessenich34fb0362016-05-03 23:17:20 -06002849// | function_call
2850// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
2851// | postfix_expression DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002852// | postfix_expression DOT IDENTIFIER arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06002853// | postfix_expression arguments
John Kessenich34fb0362016-05-03 23:17:20 -06002854// | postfix_expression INC_OP
2855// | postfix_expression DEC_OP
2856//
2857bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
2858{
2859 // Not implemented as self-recursive:
John Kessenich54ee28f2017-03-11 14:13:00 -07002860 // The logical "right recursion" is done with a loop at the end
John Kessenich34fb0362016-05-03 23:17:20 -06002861
2862 // idToken will pick up either a variable or a function name in a function call
2863 HlslToken idToken;
2864
John Kessenich21472ae2016-06-04 11:46:33 -06002865 // Find something before the postfix operations, as they can't operate
2866 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07002867 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06002868 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002869 if (! acceptExpression(node)) {
2870 expected("expression");
2871 return false;
2872 }
2873 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002874 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002875 return false;
2876 }
John Kessenich34fb0362016-05-03 23:17:20 -06002877 } else if (acceptLiteral(node)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002878 // literal (nothing else to do yet)
John Kessenich34fb0362016-05-03 23:17:20 -06002879 } else if (acceptConstructor(node)) {
2880 // constructor (nothing else to do yet)
2881 } else if (acceptIdentifier(idToken)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002882 // user-type, namespace name, variable, or function name
2883 TString* fullName = idToken.string;
2884 while (acceptTokenClass(EHTokColonColon)) {
2885 // user-type or namespace name
2886 fullName = NewPoolTString(fullName->c_str());
2887 fullName->append(parseContext.scopeMangler);
2888 if (acceptIdentifier(idToken))
2889 fullName->append(*idToken.string);
2890 else {
2891 expected("identifier after ::");
John Kessenich54ee28f2017-03-11 14:13:00 -07002892 return false;
2893 }
John Kessenich8f9fdc92017-03-30 16:22:26 -06002894 }
2895 if (! peekTokenClass(EHTokLeftParen)) {
2896 node = parseContext.handleVariable(idToken.loc, fullName);
2897 } else if (acceptFunctionCall(idToken.loc, *fullName, node, nullptr)) {
John Kessenich34fb0362016-05-03 23:17:20 -06002898 // function_call (nothing else to do yet)
2899 } else {
2900 expected("function call arguments");
2901 return false;
2902 }
John Kessenich21472ae2016-06-04 11:46:33 -06002903 } else {
2904 // nothing found, can't post operate
2905 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002906 }
2907
John Kessenich21472ae2016-06-04 11:46:33 -06002908 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06002909 do {
2910 TSourceLoc loc = token.loc;
2911 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07002912
John Kessenich34fb0362016-05-03 23:17:20 -06002913 // Consume only a valid post-unary operator, otherwise we are done.
2914 switch (postOp) {
2915 case EOpIndexDirectStruct:
2916 case EOpIndexIndirect:
2917 case EOpPostIncrement:
2918 case EOpPostDecrement:
John Kessenich54ee28f2017-03-11 14:13:00 -07002919 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002920 advanceToken();
2921 break;
2922 default:
2923 return true;
2924 }
John Kessenich87142c72016-03-12 20:24:24 -07002925
John Kessenich34fb0362016-05-03 23:17:20 -06002926 // We have a valid post-unary operator, process it.
2927 switch (postOp) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002928 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002929 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06002930 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002931 // DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002932 // includes swizzles, member variables, and member functions
John Kessenich93a162a2016-06-17 17:16:27 -06002933 HlslToken field;
2934 if (! acceptIdentifier(field)) {
2935 expected("swizzle or member");
2936 return false;
2937 }
LoopDawg4886f692016-06-29 10:58:58 -06002938
John Kessenich516d92d2017-03-08 20:09:03 -07002939 if (peekTokenClass(EHTokLeftParen)) {
2940 // member function
2941 TIntermTyped* thisNode = node;
LoopDawg4886f692016-06-29 10:58:58 -06002942
John Kessenich516d92d2017-03-08 20:09:03 -07002943 // arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06002944 if (! acceptFunctionCall(field.loc, *field.string, node, thisNode)) {
LoopDawg4886f692016-06-29 10:58:58 -06002945 expected("function parameters");
2946 return false;
2947 }
John Kessenich516d92d2017-03-08 20:09:03 -07002948 } else
2949 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06002950
John Kessenich34fb0362016-05-03 23:17:20 -06002951 break;
John Kessenich93a162a2016-06-17 17:16:27 -06002952 }
John Kessenich34fb0362016-05-03 23:17:20 -06002953 case EOpIndexIndirect:
2954 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002955 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06002956 TIntermTyped* indexNode = nullptr;
2957 if (! acceptExpression(indexNode) ||
2958 ! peekTokenClass(EHTokRightBracket)) {
2959 expected("expression followed by ']'");
2960 return false;
2961 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002962 advanceToken();
2963 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
steve-lunarg2efd6c62017-04-06 20:22:20 -06002964 if (node == nullptr)
2965 return false;
John Kessenich19b92ff2016-06-19 11:50:34 -06002966 break;
John Kessenich34fb0362016-05-03 23:17:20 -06002967 }
2968 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002969 // INC_OP
2970 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06002971 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002972 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06002973 node = intermediate.addUnaryMath(postOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06002974 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002975 break;
2976 default:
2977 assert(0);
2978 break;
2979 }
2980 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07002981}
2982
John Kessenichd016be12016-03-13 11:24:20 -06002983// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06002984// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06002985//
2986bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
2987{
2988 // type
2989 TType type;
2990 if (acceptType(type)) {
John Kessenichc633f642017-04-03 21:48:37 -06002991 TFunction* constructorFunction = parseContext.makeConstructorCall(token.loc, type);
John Kessenichd016be12016-03-13 11:24:20 -06002992 if (constructorFunction == nullptr)
2993 return false;
2994
2995 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06002996 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06002997 if (! acceptArguments(constructorFunction, arguments)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002998 // It's possible this is a type keyword used as an identifier. Put the token back
2999 // for later use.
3000 recedeToken();
John Kessenichd016be12016-03-13 11:24:20 -06003001 return false;
3002 }
3003
3004 // hook it up
3005 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
3006
3007 return true;
3008 }
3009
3010 return false;
3011}
3012
John Kessenich34fb0362016-05-03 23:17:20 -06003013// The function_call identifier was already recognized, and passed in as idToken.
3014//
3015// function_call
3016// : [idToken] arguments
3017//
John Kessenich8f9fdc92017-03-30 16:22:26 -06003018bool HlslGrammar::acceptFunctionCall(const TSourceLoc& loc, TString& name, TIntermTyped*& node, TIntermTyped* baseObject)
John Kessenich34fb0362016-05-03 23:17:20 -06003019{
John Kessenich54ee28f2017-03-11 14:13:00 -07003020 // name
3021 TString* functionName = nullptr;
John Kessenich8f9fdc92017-03-30 16:22:26 -06003022 if (baseObject == nullptr) {
3023 functionName = &name;
3024 } else if (parseContext.isBuiltInMethod(loc, baseObject, name)) {
John Kessenich4960baa2017-03-19 18:09:59 -06003025 // Built-in methods are not in the symbol table as methods, but as global functions
3026 // taking an explicit 'this' as the first argument.
steve-lunarge7d07522017-03-19 18:12:37 -06003027 functionName = NewPoolTString(BUILTIN_PREFIX);
John Kessenich8f9fdc92017-03-30 16:22:26 -06003028 functionName->append(name);
John Kessenich4960baa2017-03-19 18:09:59 -06003029 } else {
John Kessenich8f9fdc92017-03-30 16:22:26 -06003030 if (! baseObject->getType().isStruct()) {
3031 expected("structure");
3032 return false;
3033 }
John Kessenich54ee28f2017-03-11 14:13:00 -07003034 functionName = NewPoolTString("");
John Kessenich8f9fdc92017-03-30 16:22:26 -06003035 functionName->append(baseObject->getType().getTypeName());
John Kessenichf3d88bd2017-03-19 12:24:29 -06003036 parseContext.addScopeMangler(*functionName);
John Kessenich8f9fdc92017-03-30 16:22:26 -06003037 functionName->append(name);
John Kessenich5f12d2f2017-03-11 09:39:55 -07003038 }
LoopDawg4886f692016-06-29 10:58:58 -06003039
John Kessenich54ee28f2017-03-11 14:13:00 -07003040 // function
3041 TFunction* function = new TFunction(functionName, TType(EbtVoid));
3042
3043 // arguments
John Kessenich54ee28f2017-03-11 14:13:00 -07003044 TIntermTyped* arguments = nullptr;
John Kessenichdfbdd9e2017-03-19 13:10:28 -06003045 if (baseObject != nullptr) {
3046 // Non-static member functions have an implicit first argument of the base object.
John Kessenich54ee28f2017-03-11 14:13:00 -07003047 parseContext.handleFunctionArgument(function, arguments, baseObject);
John Kessenichdfbdd9e2017-03-19 13:10:28 -06003048 }
John Kessenich4678ca92016-05-13 09:33:42 -06003049 if (! acceptArguments(function, arguments))
3050 return false;
3051
John Kessenich54ee28f2017-03-11 14:13:00 -07003052 // call
John Kessenich8f9fdc92017-03-30 16:22:26 -06003053 node = parseContext.handleFunctionCall(loc, function, arguments);
John Kessenich4678ca92016-05-13 09:33:42 -06003054
3055 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06003056}
3057
John Kessenich87142c72016-03-12 20:24:24 -07003058// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06003059// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07003060//
John Kessenichd016be12016-03-13 11:24:20 -06003061// The arguments are pushed onto the 'function' argument list and
3062// onto the 'arguments' aggregate.
3063//
John Kessenich4678ca92016-05-13 09:33:42 -06003064bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07003065{
John Kessenich078d7f22016-03-14 10:02:11 -06003066 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07003067 if (! acceptTokenClass(EHTokLeftParen))
3068 return false;
3069
John Kessenich2aa12b12017-04-18 14:47:33 -06003070 // RIGHT_PAREN
3071 if (acceptTokenClass(EHTokRightParen))
3072 return true;
3073
3074 // must now be at least one expression...
John Kessenich87142c72016-03-12 20:24:24 -07003075 do {
John Kessenichd016be12016-03-13 11:24:20 -06003076 // expression
John Kessenich87142c72016-03-12 20:24:24 -07003077 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06003078 if (! acceptAssignmentExpression(arg))
John Kessenich2aa12b12017-04-18 14:47:33 -06003079 return false;
John Kessenichd016be12016-03-13 11:24:20 -06003080
3081 // hook it up
3082 parseContext.handleFunctionArgument(function, arguments, arg);
3083
John Kessenich078d7f22016-03-14 10:02:11 -06003084 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07003085 if (! acceptTokenClass(EHTokComma))
3086 break;
3087 } while (true);
3088
John Kessenich078d7f22016-03-14 10:02:11 -06003089 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07003090 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06003091 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07003092 return false;
3093 }
3094
3095 return true;
3096}
3097
3098bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
3099{
3100 switch (token.tokenClass) {
3101 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003102 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003103 break;
steve-lunarg2de32912016-07-28 14:49:48 -06003104 case EHTokUintConstant:
3105 node = intermediate.addConstantUnion(token.u, token.loc, true);
3106 break;
John Kessenich87142c72016-03-12 20:24:24 -07003107 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003108 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003109 break;
3110 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003111 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003112 break;
3113 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003114 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003115 break;
John Kessenich86f71382016-09-19 20:23:18 -06003116 case EHTokStringConstant:
steve-lunarg858c9282017-01-07 08:54:10 -07003117 node = intermediate.addConstantUnion(token.string, token.loc, true);
John Kessenich86f71382016-09-19 20:23:18 -06003118 break;
John Kessenich87142c72016-03-12 20:24:24 -07003119
3120 default:
3121 return false;
3122 }
3123
3124 advanceToken();
3125
3126 return true;
3127}
3128
John Kessenich0e071192017-06-06 11:37:33 -06003129// simple_statement
3130// : SEMICOLON
3131// | declaration_statement
3132// | expression SEMICOLON
3133//
3134bool HlslGrammar::acceptSimpleStatement(TIntermNode*& statement)
3135{
3136 // SEMICOLON
3137 if (acceptTokenClass(EHTokSemicolon))
3138 return true;
3139
3140 // declaration
3141 if (acceptDeclaration(statement))
3142 return true;
3143
3144 // expression
3145 TIntermTyped* node;
3146 if (acceptExpression(node))
3147 statement = node;
3148 else
3149 return false;
3150
3151 // SEMICOLON (following an expression)
3152 if (acceptTokenClass(EHTokSemicolon))
3153 return true;
3154 else {
3155 expected(";");
3156 return false;
3157 }
3158}
3159
John Kessenich5f934b02016-03-13 17:58:25 -06003160// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06003161// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06003162//
John Kessenich21472ae2016-06-04 11:46:33 -06003163bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07003164{
John Kessenich21472ae2016-06-04 11:46:33 -06003165 TIntermAggregate* compoundStatement = nullptr;
3166
John Kessenich34fb0362016-05-03 23:17:20 -06003167 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06003168 if (! acceptTokenClass(EHTokLeftBrace))
3169 return false;
3170
3171 // statement statement ...
3172 TIntermNode* statement = nullptr;
3173 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06003174 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
3175 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
3176 branch->getFlowOp() == EOpDefault)) {
3177 // hook up individual subsequences within a switch statement
3178 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
3179 compoundStatement = nullptr;
3180 } else {
3181 // hook it up to the growing compound statement
3182 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
3183 }
John Kessenich5f934b02016-03-13 17:58:25 -06003184 }
John Kessenich34fb0362016-05-03 23:17:20 -06003185 if (compoundStatement)
3186 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06003187
John Kessenich21472ae2016-06-04 11:46:33 -06003188 retStatement = compoundStatement;
3189
John Kessenich34fb0362016-05-03 23:17:20 -06003190 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06003191 return acceptTokenClass(EHTokRightBrace);
3192}
3193
John Kessenich0d2b6de2016-06-05 11:23:11 -06003194bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
3195{
3196 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06003197 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003198 parseContext.popScope();
3199
3200 return result;
3201}
3202
John Kessenich077e0522016-06-09 02:02:17 -06003203bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003204{
John Kessenich077e0522016-06-09 02:02:17 -06003205 parseContext.pushScope();
3206 bool result = acceptCompoundStatement(statement);
3207 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06003208
3209 return result;
3210}
3211
John Kessenich5f934b02016-03-13 17:58:25 -06003212// statement
John Kessenich21472ae2016-06-04 11:46:33 -06003213// : attributes attributed_statement
3214//
3215// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003216// : compound_statement
John Kessenich0e071192017-06-06 11:37:33 -06003217// | simple_statement
John Kessenich21472ae2016-06-04 11:46:33 -06003218// | selection_statement
3219// | switch_statement
3220// | case_label
John Kessenich0e071192017-06-06 11:37:33 -06003221// | default_label
John Kessenich21472ae2016-06-04 11:46:33 -06003222// | iteration_statement
3223// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003224//
3225bool HlslGrammar::acceptStatement(TIntermNode*& statement)
3226{
John Kessenich21472ae2016-06-04 11:46:33 -06003227 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06003228
John Kessenich21472ae2016-06-04 11:46:33 -06003229 // attributes
steve-lunarg1868b142016-10-20 13:07:10 -06003230 TAttributeMap attributes;
3231 acceptAttributes(attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003232
John Kessenich21472ae2016-06-04 11:46:33 -06003233 // attributed_statement
3234 switch (peek()) {
3235 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06003236 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003237
John Kessenich21472ae2016-06-04 11:46:33 -06003238 case EHTokIf:
Rex Xu57e65922017-07-04 23:23:40 +08003239 return acceptSelectionStatement(statement, attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003240
John Kessenich21472ae2016-06-04 11:46:33 -06003241 case EHTokSwitch:
Rex Xu57e65922017-07-04 23:23:40 +08003242 return acceptSwitchStatement(statement, attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003243
John Kessenich21472ae2016-06-04 11:46:33 -06003244 case EHTokFor:
3245 case EHTokDo:
3246 case EHTokWhile:
steve-lunargf1709e72017-05-02 20:14:50 -06003247 return acceptIterationStatement(statement, attributes);
John Kessenich21472ae2016-06-04 11:46:33 -06003248
3249 case EHTokContinue:
3250 case EHTokBreak:
3251 case EHTokDiscard:
3252 case EHTokReturn:
3253 return acceptJumpStatement(statement);
3254
3255 case EHTokCase:
3256 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06003257 case EHTokDefault:
3258 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06003259
John Kessenich21472ae2016-06-04 11:46:33 -06003260 case EHTokRightBrace:
3261 // Performance: not strictly necessary, but stops a bunch of hunting early,
3262 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06003263 return false;
3264
John Kessenich21472ae2016-06-04 11:46:33 -06003265 default:
John Kessenich0e071192017-06-06 11:37:33 -06003266 return acceptSimpleStatement(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06003267 }
3268
John Kessenich5f934b02016-03-13 17:58:25 -06003269 return true;
John Kessenich87142c72016-03-12 20:24:24 -07003270}
3271
John Kessenich21472ae2016-06-04 11:46:33 -06003272// attributes
John Kessenich77ea30b2017-09-30 14:34:50 -06003273// : [zero or more:] bracketed-attribute
3274//
3275// bracketed-attribute:
3276// : LEFT_BRACKET scoped-attribute RIGHT_BRACKET
3277// : LEFT_BRACKET LEFT_BRACKET scoped-attribute RIGHT_BRACKET RIGHT_BRACKET
3278//
3279// scoped-attribute:
3280// : attribute
3281// | namespace COLON COLON attribute
John Kessenich21472ae2016-06-04 11:46:33 -06003282//
3283// attribute:
3284// : UNROLL
3285// | UNROLL LEFT_PAREN literal RIGHT_PAREN
3286// | FASTOPT
3287// | ALLOW_UAV_CONDITION
3288// | BRANCH
3289// | FLATTEN
3290// | FORCECASE
3291// | CALL
steve-lunarg1868b142016-10-20 13:07:10 -06003292// | DOMAIN
3293// | EARLYDEPTHSTENCIL
3294// | INSTANCE
3295// | MAXTESSFACTOR
3296// | OUTPUTCONTROLPOINTS
3297// | OUTPUTTOPOLOGY
3298// | PARTITIONING
3299// | PATCHCONSTANTFUNC
3300// | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
John Kessenich21472ae2016-06-04 11:46:33 -06003301//
steve-lunarg1868b142016-10-20 13:07:10 -06003302void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003303{
steve-lunarg1868b142016-10-20 13:07:10 -06003304 // For now, accept the [ XXX(X) ] syntax, but drop all but
3305 // numthreads, which is used to set the CS local size.
John Kessenich0d2b6de2016-06-05 11:23:11 -06003306 // TODO: subset to correct set? Pass on?
3307 do {
John Kessenich77ea30b2017-09-30 14:34:50 -06003308 HlslToken attributeToken;
steve-lunarg1868b142016-10-20 13:07:10 -06003309
John Kessenich0d2b6de2016-06-05 11:23:11 -06003310 // LEFT_BRACKET?
3311 if (! acceptTokenClass(EHTokLeftBracket))
3312 return;
John Kessenich77ea30b2017-09-30 14:34:50 -06003313 // another LEFT_BRACKET?
3314 bool doubleBrackets = false;
3315 if (acceptTokenClass(EHTokLeftBracket))
3316 doubleBrackets = true;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003317
John Kessenich77ea30b2017-09-30 14:34:50 -06003318 // attribute? (could be namespace; will adjust later)
3319 if (!acceptIdentifier(attributeToken)) {
3320 if (!peekTokenClass(EHTokRightBracket)) {
3321 expected("namespace or attribute identifier");
3322 advanceToken();
3323 }
3324 }
3325
3326 TString nameSpace;
3327 if (acceptTokenClass(EHTokColonColon)) {
3328 // namespace COLON COLON
3329 nameSpace = *attributeToken.string;
3330 // attribute
3331 if (!acceptIdentifier(attributeToken)) {
3332 expected("attribute identifier");
3333 return;
3334 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06003335 }
3336
steve-lunarga22f7db2016-11-11 08:17:44 -07003337 TIntermAggregate* expressions = nullptr;
steve-lunarg1868b142016-10-20 13:07:10 -06003338
3339 // (x, ...)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003340 if (acceptTokenClass(EHTokLeftParen)) {
steve-lunarga22f7db2016-11-11 08:17:44 -07003341 expressions = new TIntermAggregate;
steve-lunarg1868b142016-10-20 13:07:10 -06003342
John Kessenich0d2b6de2016-06-05 11:23:11 -06003343 TIntermTyped* node;
steve-lunarga22f7db2016-11-11 08:17:44 -07003344 bool expectingExpression = false;
John Kessenichecba76f2017-01-06 00:34:48 -07003345
steve-lunarga22f7db2016-11-11 08:17:44 -07003346 while (acceptAssignmentExpression(node)) {
3347 expectingExpression = false;
3348 expressions->getSequence().push_back(node);
steve-lunarg1868b142016-10-20 13:07:10 -06003349 if (acceptTokenClass(EHTokComma))
steve-lunarga22f7db2016-11-11 08:17:44 -07003350 expectingExpression = true;
steve-lunarg1868b142016-10-20 13:07:10 -06003351 }
3352
steve-lunarga22f7db2016-11-11 08:17:44 -07003353 // 'expressions' is an aggregate with the expressions in it
John Kessenich0d2b6de2016-06-05 11:23:11 -06003354 if (! acceptTokenClass(EHTokRightParen))
3355 expected(")");
steve-lunarga22f7db2016-11-11 08:17:44 -07003356
3357 // Error for partial or missing expression
3358 if (expectingExpression || expressions->getSequence().empty())
3359 expected("expression");
John Kessenich0d2b6de2016-06-05 11:23:11 -06003360 }
3361
3362 // RIGHT_BRACKET
steve-lunarg1868b142016-10-20 13:07:10 -06003363 if (!acceptTokenClass(EHTokRightBracket)) {
3364 expected("]");
3365 return;
3366 }
John Kessenich77ea30b2017-09-30 14:34:50 -06003367 // another RIGHT_BRACKET?
3368 if (doubleBrackets && !acceptTokenClass(EHTokRightBracket)) {
3369 expected("]]");
3370 return;
3371 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06003372
steve-lunarg1868b142016-10-20 13:07:10 -06003373 // Add any values we found into the attribute map. This accepts
3374 // (and ignores) values not mapping to a known TAttributeType;
John Kessenich77ea30b2017-09-30 14:34:50 -06003375 attributes.setAttribute(nameSpace, attributeToken.string, expressions);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003376 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06003377}
3378
John Kessenich0d2b6de2016-06-05 11:23:11 -06003379// selection_statement
3380// : IF LEFT_PAREN expression RIGHT_PAREN statement
3381// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
3382//
Rex Xu57e65922017-07-04 23:23:40 +08003383bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement, const TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003384{
John Kessenich0d2b6de2016-06-05 11:23:11 -06003385 TSourceLoc loc = token.loc;
3386
Rex Xu57e65922017-07-04 23:23:40 +08003387 const TSelectionControl control = parseContext.handleSelectionControl(attributes);
3388
John Kessenich0d2b6de2016-06-05 11:23:11 -06003389 // IF
3390 if (! acceptTokenClass(EHTokIf))
3391 return false;
3392
3393 // so that something declared in the condition is scoped to the lifetimes
3394 // of the then-else statements
3395 parseContext.pushScope();
3396
3397 // LEFT_PAREN expression RIGHT_PAREN
3398 TIntermTyped* condition;
3399 if (! acceptParenExpression(condition))
3400 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003401 condition = parseContext.convertConditionalExpression(loc, condition);
3402 if (condition == nullptr)
3403 return false;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003404
3405 // create the child statements
3406 TIntermNodePair thenElse = { nullptr, nullptr };
3407
John Kessenichf6deacd2017-06-06 19:52:55 -06003408 ++parseContext.controlFlowNestingLevel; // this only needs to work right if no errors
3409
John Kessenich0d2b6de2016-06-05 11:23:11 -06003410 // then statement
3411 if (! acceptScopedStatement(thenElse.node1)) {
3412 expected("then statement");
3413 return false;
3414 }
3415
3416 // ELSE
3417 if (acceptTokenClass(EHTokElse)) {
3418 // else statement
3419 if (! acceptScopedStatement(thenElse.node2)) {
3420 expected("else statement");
3421 return false;
3422 }
3423 }
3424
3425 // Put the pieces together
Rex Xu57e65922017-07-04 23:23:40 +08003426 statement = intermediate.addSelection(condition, thenElse, loc, control);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003427 parseContext.popScope();
John Kessenichf6deacd2017-06-06 19:52:55 -06003428 --parseContext.controlFlowNestingLevel;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003429
3430 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003431}
3432
John Kessenichd02dc5d2016-07-01 00:04:11 -06003433// switch_statement
3434// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
3435//
Rex Xu57e65922017-07-04 23:23:40 +08003436bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement, const TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003437{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003438 // SWITCH
3439 TSourceLoc loc = token.loc;
Rex Xu57e65922017-07-04 23:23:40 +08003440
3441 const TSelectionControl control = parseContext.handleSelectionControl(attributes);
3442
John Kessenichd02dc5d2016-07-01 00:04:11 -06003443 if (! acceptTokenClass(EHTokSwitch))
3444 return false;
3445
3446 // LEFT_PAREN expression RIGHT_PAREN
3447 parseContext.pushScope();
3448 TIntermTyped* switchExpression;
3449 if (! acceptParenExpression(switchExpression)) {
3450 parseContext.popScope();
3451 return false;
3452 }
3453
3454 // compound_statement
3455 parseContext.pushSwitchSequence(new TIntermSequence);
John Kessenichf6deacd2017-06-06 19:52:55 -06003456
3457 ++parseContext.controlFlowNestingLevel;
John Kessenichd02dc5d2016-07-01 00:04:11 -06003458 bool statementOkay = acceptCompoundStatement(statement);
John Kessenichf6deacd2017-06-06 19:52:55 -06003459 --parseContext.controlFlowNestingLevel;
3460
John Kessenichd02dc5d2016-07-01 00:04:11 -06003461 if (statementOkay)
Rex Xu57e65922017-07-04 23:23:40 +08003462 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr, control);
John Kessenichd02dc5d2016-07-01 00:04:11 -06003463
3464 parseContext.popSwitchSequence();
3465 parseContext.popScope();
3466
3467 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06003468}
3469
John Kessenich119f8f62016-06-05 15:44:07 -06003470// iteration_statement
3471// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
3472// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
3473// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
3474//
3475// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
steve-lunargf1709e72017-05-02 20:14:50 -06003476bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement, const TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003477{
John Kessenich119f8f62016-06-05 15:44:07 -06003478 TSourceLoc loc = token.loc;
3479 TIntermTyped* condition = nullptr;
3480
3481 EHlslTokenClass loop = peek();
3482 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
3483
3484 // WHILE or DO or FOR
3485 advanceToken();
steve-lunargf1709e72017-05-02 20:14:50 -06003486
3487 const TLoopControl control = parseContext.handleLoopControl(attributes);
John Kessenich119f8f62016-06-05 15:44:07 -06003488
3489 switch (loop) {
3490 case EHTokWhile:
3491 // so that something declared in the condition is scoped to the lifetime
3492 // of the while sub-statement
John Kessenichf6deacd2017-06-06 19:52:55 -06003493 parseContext.pushScope(); // this only needs to work right if no errors
John Kessenich119f8f62016-06-05 15:44:07 -06003494 parseContext.nestLooping();
John Kessenichf6deacd2017-06-06 19:52:55 -06003495 ++parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003496
3497 // LEFT_PAREN condition RIGHT_PAREN
3498 if (! acceptParenExpression(condition))
3499 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003500 condition = parseContext.convertConditionalExpression(loc, condition);
3501 if (condition == nullptr)
3502 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003503
3504 // statement
3505 if (! acceptScopedStatement(statement)) {
3506 expected("while sub-statement");
3507 return false;
3508 }
3509
3510 parseContext.unnestLooping();
3511 parseContext.popScope();
John Kessenichf6deacd2017-06-06 19:52:55 -06003512 --parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003513
steve-lunargf1709e72017-05-02 20:14:50 -06003514 statement = intermediate.addLoop(statement, condition, nullptr, true, loc, control);
John Kessenich119f8f62016-06-05 15:44:07 -06003515
3516 return true;
3517
3518 case EHTokDo:
John Kessenichf6deacd2017-06-06 19:52:55 -06003519 parseContext.nestLooping(); // this only needs to work right if no errors
3520 ++parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003521
John Kessenich119f8f62016-06-05 15:44:07 -06003522 // statement
John Kessenich0c6f9362017-04-20 11:08:24 -06003523 if (! acceptScopedStatement(statement)) {
John Kessenich119f8f62016-06-05 15:44:07 -06003524 expected("do sub-statement");
3525 return false;
3526 }
3527
John Kessenich119f8f62016-06-05 15:44:07 -06003528 // WHILE
3529 if (! acceptTokenClass(EHTokWhile)) {
3530 expected("while");
3531 return false;
3532 }
3533
3534 // LEFT_PAREN condition RIGHT_PAREN
3535 TIntermTyped* condition;
3536 if (! acceptParenExpression(condition))
3537 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003538 condition = parseContext.convertConditionalExpression(loc, condition);
3539 if (condition == nullptr)
3540 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003541
3542 if (! acceptTokenClass(EHTokSemicolon))
3543 expected(";");
3544
3545 parseContext.unnestLooping();
John Kessenichf6deacd2017-06-06 19:52:55 -06003546 --parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003547
steve-lunargf1709e72017-05-02 20:14:50 -06003548 statement = intermediate.addLoop(statement, condition, 0, false, loc, control);
John Kessenich119f8f62016-06-05 15:44:07 -06003549
3550 return true;
3551
3552 case EHTokFor:
3553 {
3554 // LEFT_PAREN
3555 if (! acceptTokenClass(EHTokLeftParen))
3556 expected("(");
3557
3558 // so that something declared in the condition is scoped to the lifetime
3559 // of the for sub-statement
3560 parseContext.pushScope();
3561
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003562 // initializer
3563 TIntermNode* initNode = nullptr;
John Kessenich0e071192017-06-06 11:37:33 -06003564 if (! acceptSimpleStatement(initNode))
3565 expected("for-loop initializer statement");
John Kessenich119f8f62016-06-05 15:44:07 -06003566
John Kessenichf6deacd2017-06-06 19:52:55 -06003567 parseContext.nestLooping(); // this only needs to work right if no errors
3568 ++parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003569
3570 // condition SEMI_COLON
3571 acceptExpression(condition);
3572 if (! acceptTokenClass(EHTokSemicolon))
3573 expected(";");
John Kessenich7e997e22017-03-30 22:09:30 -06003574 if (condition != nullptr) {
3575 condition = parseContext.convertConditionalExpression(loc, condition);
3576 if (condition == nullptr)
3577 return false;
3578 }
John Kessenich119f8f62016-06-05 15:44:07 -06003579
3580 // iterator SEMI_COLON
3581 TIntermTyped* iterator = nullptr;
3582 acceptExpression(iterator);
3583 if (! acceptTokenClass(EHTokRightParen))
3584 expected(")");
3585
3586 // statement
3587 if (! acceptScopedStatement(statement)) {
3588 expected("for sub-statement");
3589 return false;
3590 }
3591
steve-lunargf1709e72017-05-02 20:14:50 -06003592 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc, control);
John Kessenich119f8f62016-06-05 15:44:07 -06003593
3594 parseContext.popScope();
3595 parseContext.unnestLooping();
John Kessenichf6deacd2017-06-06 19:52:55 -06003596 --parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003597
3598 return true;
3599 }
3600
3601 default:
3602 return false;
3603 }
John Kessenich21472ae2016-06-04 11:46:33 -06003604}
3605
3606// jump_statement
3607// : CONTINUE SEMICOLON
3608// | BREAK SEMICOLON
3609// | DISCARD SEMICOLON
3610// | RETURN SEMICOLON
3611// | RETURN expression SEMICOLON
3612//
3613bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
3614{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003615 EHlslTokenClass jump = peek();
3616 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06003617 case EHTokContinue:
3618 case EHTokBreak:
3619 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06003620 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003621 advanceToken();
3622 break;
John Kessenich21472ae2016-06-04 11:46:33 -06003623 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003624 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06003625 return false;
3626 }
John Kessenich21472ae2016-06-04 11:46:33 -06003627
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003628 switch (jump) {
3629 case EHTokContinue:
3630 statement = intermediate.addBranch(EOpContinue, token.loc);
3631 break;
3632 case EHTokBreak:
3633 statement = intermediate.addBranch(EOpBreak, token.loc);
3634 break;
3635 case EHTokDiscard:
3636 statement = intermediate.addBranch(EOpKill, token.loc);
3637 break;
3638
3639 case EHTokReturn:
3640 {
3641 // expression
3642 TIntermTyped* node;
3643 if (acceptExpression(node)) {
3644 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06003645 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003646 } else
3647 statement = intermediate.addBranch(EOpReturn, token.loc);
3648 break;
3649 }
3650
3651 default:
3652 assert(0);
3653 return false;
3654 }
3655
3656 // SEMICOLON
3657 if (! acceptTokenClass(EHTokSemicolon))
3658 expected(";");
John Kessenichecba76f2017-01-06 00:34:48 -07003659
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003660 return true;
3661}
John Kessenich21472ae2016-06-04 11:46:33 -06003662
John Kessenichd02dc5d2016-07-01 00:04:11 -06003663// case_label
3664// : CASE expression COLON
3665//
John Kessenich21472ae2016-06-04 11:46:33 -06003666bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
3667{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003668 TSourceLoc loc = token.loc;
3669 if (! acceptTokenClass(EHTokCase))
3670 return false;
3671
3672 TIntermTyped* expression;
3673 if (! acceptExpression(expression)) {
3674 expected("case expression");
3675 return false;
3676 }
3677
3678 if (! acceptTokenClass(EHTokColon)) {
3679 expected(":");
3680 return false;
3681 }
3682
3683 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
3684
3685 return true;
3686}
3687
3688// default_label
3689// : DEFAULT COLON
3690//
3691bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
3692{
3693 TSourceLoc loc = token.loc;
3694 if (! acceptTokenClass(EHTokDefault))
3695 return false;
3696
3697 if (! acceptTokenClass(EHTokColon)) {
3698 expected(":");
3699 return false;
3700 }
3701
3702 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
3703
3704 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003705}
3706
John Kessenich19b92ff2016-06-19 11:50:34 -06003707// array_specifier
steve-lunarg7b211a32016-10-13 12:26:18 -06003708// : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
3709// : LEFT_BRACKET RGHT_BRACKET // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06003710//
3711void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
3712{
3713 arraySizes = nullptr;
3714
steve-lunarg7b211a32016-10-13 12:26:18 -06003715 // Early-out if there aren't any array dimensions
3716 if (!peekTokenClass(EHTokLeftBracket))
John Kessenich19b92ff2016-06-19 11:50:34 -06003717 return;
3718
steve-lunarg7b211a32016-10-13 12:26:18 -06003719 // 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 -06003720 arraySizes = new TArraySizes;
steve-lunarg7b211a32016-10-13 12:26:18 -06003721
3722 // Collect each array dimension.
3723 while (acceptTokenClass(EHTokLeftBracket)) {
3724 TSourceLoc loc = token.loc;
3725 TIntermTyped* sizeExpr = nullptr;
3726
John Kessenich057df292017-03-06 18:18:37 -07003727 // Array sizing expression is optional. If omitted, array will be later sized by initializer list.
steve-lunarg7b211a32016-10-13 12:26:18 -06003728 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
3729
3730 if (! acceptTokenClass(EHTokRightBracket)) {
3731 expected("]");
3732 return;
3733 }
3734
3735 if (hasArraySize) {
3736 TArraySize arraySize;
3737 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
3738 arraySizes->addInnerSize(arraySize);
3739 } else {
3740 arraySizes->addInnerSize(0); // sized by initializers.
3741 }
steve-lunarg265c0612016-09-27 10:57:35 -06003742 }
John Kessenich19b92ff2016-06-19 11:50:34 -06003743}
3744
John Kessenich630dd7d2016-06-12 23:52:12 -06003745// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06003746// : COLON semantic // optional
3747// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
3748// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06003749// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06003750// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06003751//
John Kessenich854fe242017-03-02 14:30:59 -07003752// Return true if any tokens were accepted. That is,
3753// false can be returned on successfully recognizing nothing,
3754// not necessarily meaning bad syntax.
3755//
3756bool HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06003757{
John Kessenich854fe242017-03-02 14:30:59 -07003758 bool found = false;
3759
John Kessenich630dd7d2016-06-12 23:52:12 -06003760 do {
John Kessenichecba76f2017-01-06 00:34:48 -07003761 // COLON
John Kessenich630dd7d2016-06-12 23:52:12 -06003762 if (acceptTokenClass(EHTokColon)) {
John Kessenich854fe242017-03-02 14:30:59 -07003763 found = true;
John Kessenich630dd7d2016-06-12 23:52:12 -06003764 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06003765 if (peekTokenClass(EHTokLayout))
3766 acceptLayoutQualifierList(qualifier);
3767 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06003768 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003769 if (! acceptTokenClass(EHTokLeftParen)) {
3770 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003771 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003772 }
John Kessenich82d6baf2016-07-29 13:03:05 -06003773 HlslToken locationToken;
3774 if (! acceptIdentifier(locationToken)) {
3775 expected("c[subcomponent][.component]");
John Kessenich854fe242017-03-02 14:30:59 -07003776 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003777 }
3778 HlslToken componentToken;
3779 if (acceptTokenClass(EHTokDot)) {
3780 if (! acceptIdentifier(componentToken)) {
3781 expected("component");
John Kessenich854fe242017-03-02 14:30:59 -07003782 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003783 }
3784 }
John Kessenich630dd7d2016-06-12 23:52:12 -06003785 if (! acceptTokenClass(EHTokRightParen)) {
3786 expected(")");
3787 break;
3788 }
John Kessenich7735b942016-09-05 12:40:06 -06003789 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003790 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06003791 expected("layout, semantic, packoffset, or register");
John Kessenich854fe242017-03-02 14:30:59 -07003792 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003793 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06003794 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
3795 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003796 if (! acceptTokenClass(EHTokLeftParen)) {
3797 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003798 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003799 }
John Kessenichb38f0712016-07-30 10:29:54 -06003800 HlslToken registerDesc; // for Type#
3801 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06003802 if (! acceptIdentifier(registerDesc)) {
3803 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003804 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003805 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003806 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
3807 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06003808 // Then we didn't really see the registerDesc yet, it was
3809 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06003810 profile = registerDesc;
3811 if (! acceptIdentifier(registerDesc)) {
3812 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003813 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003814 }
3815 }
John Kessenichb38f0712016-07-30 10:29:54 -06003816 int subComponent = 0;
3817 if (acceptTokenClass(EHTokLeftBracket)) {
3818 // LEFT_BRACKET subcomponent RIGHT_BRACKET
3819 if (! peekTokenClass(EHTokIntConstant)) {
3820 expected("literal integer");
John Kessenich854fe242017-03-02 14:30:59 -07003821 return false;
John Kessenichb38f0712016-07-30 10:29:54 -06003822 }
3823 subComponent = token.i;
3824 advanceToken();
3825 if (! acceptTokenClass(EHTokRightBracket)) {
3826 expected("]");
3827 break;
3828 }
3829 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003830 // (COMMA SPACEN)opt
3831 HlslToken spaceDesc;
3832 if (acceptTokenClass(EHTokComma)) {
3833 if (! acceptIdentifier(spaceDesc)) {
3834 expected ("space identifier");
John Kessenich854fe242017-03-02 14:30:59 -07003835 return false;
John Kessenichcfd7ce82016-09-05 16:03:12 -06003836 }
3837 }
3838 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003839 if (! acceptTokenClass(EHTokRightParen)) {
3840 expected(")");
3841 break;
3842 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003843 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003844 } else {
3845 // semantic, in idToken.string
John Kessenich2dd643f2017-03-14 21:50:06 -06003846 TString semanticUpperCase = *idToken.string;
3847 std::transform(semanticUpperCase.begin(), semanticUpperCase.end(), semanticUpperCase.begin(), ::toupper);
3848 parseContext.handleSemantic(idToken.loc, qualifier, mapSemantic(semanticUpperCase.c_str()), semanticUpperCase);
John Kessenich630dd7d2016-06-12 23:52:12 -06003849 }
John Kessenich854fe242017-03-02 14:30:59 -07003850 } else if (peekTokenClass(EHTokLeftAngle)) {
3851 found = true;
John Kessenicha1e2d492016-09-20 13:22:58 -06003852 acceptAnnotations(qualifier);
John Kessenich854fe242017-03-02 14:30:59 -07003853 } else
John Kessenich630dd7d2016-06-12 23:52:12 -06003854 break;
John Kessenich078d7f22016-03-14 10:02:11 -06003855
John Kessenich630dd7d2016-06-12 23:52:12 -06003856 } while (true);
John Kessenich854fe242017-03-02 14:30:59 -07003857
3858 return found;
John Kessenich078d7f22016-03-14 10:02:11 -06003859}
3860
John Kessenichb16f7e62017-03-11 19:32:47 -07003861//
3862// Get the stream of tokens from the scanner, but skip all syntactic/semantic
3863// processing.
3864//
3865bool HlslGrammar::captureBlockTokens(TVector<HlslToken>& tokens)
3866{
3867 if (! peekTokenClass(EHTokLeftBrace))
3868 return false;
3869
3870 int braceCount = 0;
3871
3872 do {
3873 switch (peek()) {
3874 case EHTokLeftBrace:
3875 ++braceCount;
3876 break;
3877 case EHTokRightBrace:
3878 --braceCount;
3879 break;
3880 case EHTokNone:
3881 // End of input before balance { } is bad...
3882 return false;
3883 default:
3884 break;
3885 }
3886
3887 tokens.push_back(token);
3888 advanceToken();
3889 } while (braceCount > 0);
3890
3891 return true;
3892}
3893
John Kessenich0320d092017-06-13 22:22:52 -06003894// Return a string for just the types that can also be declared as an identifier.
3895const char* HlslGrammar::getTypeString(EHlslTokenClass tokenClass) const
3896{
3897 switch (tokenClass) {
3898 case EHTokSample: return "sample";
3899 case EHTokHalf: return "half";
3900 case EHTokHalf1x1: return "half1x1";
3901 case EHTokHalf1x2: return "half1x2";
3902 case EHTokHalf1x3: return "half1x3";
3903 case EHTokHalf1x4: return "half1x4";
3904 case EHTokHalf2x1: return "half2x1";
3905 case EHTokHalf2x2: return "half2x2";
3906 case EHTokHalf2x3: return "half2x3";
3907 case EHTokHalf2x4: return "half2x4";
3908 case EHTokHalf3x1: return "half3x1";
3909 case EHTokHalf3x2: return "half3x2";
3910 case EHTokHalf3x3: return "half3x3";
3911 case EHTokHalf3x4: return "half3x4";
3912 case EHTokHalf4x1: return "half4x1";
3913 case EHTokHalf4x2: return "half4x2";
3914 case EHTokHalf4x3: return "half4x3";
3915 case EHTokHalf4x4: return "half4x4";
3916 case EHTokBool: return "bool";
3917 case EHTokFloat: return "float";
3918 case EHTokDouble: return "double";
3919 case EHTokInt: return "int";
3920 case EHTokUint: return "uint";
3921 case EHTokMin16float: return "min16float";
3922 case EHTokMin10float: return "min10float";
3923 case EHTokMin16int: return "min16int";
3924 case EHTokMin12int: return "min12int";
3925 default:
3926 return nullptr;
3927 }
3928}
3929
John Kesseniche01a9bc2016-03-12 20:11:22 -07003930} // end namespace glslang