blob: 3fe7a0dab70dfd23b929b6ffe9c865819282cd84 [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
LoopDawg7f93d562017-09-27 09:04:43 -06001091// subpass input type
1092// : SUBPASSINPUT
1093// | SUBPASSINPUT VECTOR LEFT_ANGLE template_type RIGHT_ANGLE
1094// | SUBPASSINPUTMS
1095// | SUBPASSINPUTMS VECTOR LEFT_ANGLE template_type RIGHT_ANGLE
1096bool HlslGrammar::acceptSubpassInputType(TType& type)
1097{
1098 // read subpass type
1099 const EHlslTokenClass subpassInputType = peek();
1100
1101 bool multisample;
1102
1103 switch (subpassInputType) {
1104 case EHTokSubpassInput: multisample = false; break;
1105 case EHTokSubpassInputMS: multisample = true; break;
1106 default:
1107 return false; // not a subpass input declaration
1108 }
1109
1110 advanceToken(); // consume the sampler type keyword
1111
1112 TType subpassType(EbtFloat, EvqUniform, 4); // default type is float4
1113
1114 if (acceptTokenClass(EHTokLeftAngle)) {
1115 if (! acceptType(subpassType)) {
1116 expected("scalar or vector type");
1117 return false;
1118 }
1119
1120 const TBasicType basicRetType = subpassType.getBasicType() ;
1121
1122 switch (basicRetType) {
1123 case EbtFloat:
1124 case EbtUint:
1125 case EbtInt:
1126 case EbtStruct:
1127 break;
1128 default:
1129 unimplemented("basic type in subpass input");
1130 return false;
1131 }
1132
1133 if (! acceptTokenClass(EHTokRightAngle)) {
1134 expected("right angle bracket");
1135 return false;
1136 }
1137 }
1138
1139 const TBasicType subpassBasicType = subpassType.isStruct() ? (*subpassType.getStruct())[0].type->getBasicType()
1140 : subpassType.getBasicType();
1141
1142 TSampler sampler;
1143 sampler.setSubpass(subpassBasicType, multisample);
1144
1145 // Remember the declared return type. Function returns false on error.
1146 if (!parseContext.setTextureReturnType(sampler, subpassType, token.loc))
1147 return false;
1148
1149 type.shallowCopy(TType(sampler, EvqUniform));
1150
1151 return true;
1152}
1153
LoopDawg4886f692016-06-29 10:58:58 -06001154// sampler_type
1155// : SAMPLER
1156// | SAMPLER1D
1157// | SAMPLER2D
1158// | SAMPLER3D
1159// | SAMPLERCUBE
1160// | SAMPLERSTATE
1161// | SAMPLERCOMPARISONSTATE
1162bool HlslGrammar::acceptSamplerType(TType& type)
1163{
1164 // read sampler type
1165 const EHlslTokenClass samplerType = peek();
1166
LoopDawga78b0292016-07-19 14:28:05 -06001167 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -06001168 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -06001169
LoopDawga78b0292016-07-19 14:28:05 -06001170 bool isShadow = false;
1171
LoopDawg4886f692016-06-29 10:58:58 -06001172 switch (samplerType) {
1173 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -06001174 case EHTokSampler1d: /*dim = Esd1D*/; break;
1175 case EHTokSampler2d: /*dim = Esd2D*/; break;
1176 case EHTokSampler3d: /*dim = Esd3D*/; break;
1177 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -06001178 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -06001179 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001180 default:
1181 return false; // not a sampler declaration
1182 }
1183
1184 advanceToken(); // consume the sampler type keyword
1185
1186 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -06001187
1188 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -06001189 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -06001190
1191 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
1192
1193 return true;
1194}
1195
1196// texture_type
1197// | BUFFER
1198// | TEXTURE1D
1199// | TEXTURE1DARRAY
1200// | TEXTURE2D
1201// | TEXTURE2DARRAY
1202// | TEXTURE3D
1203// | TEXTURECUBE
1204// | TEXTURECUBEARRAY
1205// | TEXTURE2DMS
1206// | TEXTURE2DMSARRAY
steve-lunargbb0183f2016-10-04 16:58:14 -06001207// | RWBUFFER
1208// | RWTEXTURE1D
1209// | RWTEXTURE1DARRAY
1210// | RWTEXTURE2D
1211// | RWTEXTURE2DARRAY
1212// | RWTEXTURE3D
1213
LoopDawg4886f692016-06-29 10:58:58 -06001214bool HlslGrammar::acceptTextureType(TType& type)
1215{
1216 const EHlslTokenClass textureType = peek();
1217
1218 TSamplerDim dim = EsdNone;
1219 bool array = false;
1220 bool ms = false;
steve-lunargbb0183f2016-10-04 16:58:14 -06001221 bool image = false;
steve-lunargbf1537f2017-03-31 17:40:09 -06001222 bool combined = true;
LoopDawg4886f692016-06-29 10:58:58 -06001223
1224 switch (textureType) {
steve-lunargbf1537f2017-03-31 17:40:09 -06001225 case EHTokBuffer: dim = EsdBuffer; combined = false; break;
John Kessenichf36542f2017-03-31 14:39:30 -06001226 case EHTokTexture1d: dim = Esd1D; break;
1227 case EHTokTexture1darray: dim = Esd1D; array = true; break;
1228 case EHTokTexture2d: dim = Esd2D; break;
1229 case EHTokTexture2darray: dim = Esd2D; array = true; break;
1230 case EHTokTexture3d: dim = Esd3D; break;
1231 case EHTokTextureCube: dim = EsdCube; break;
1232 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
1233 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
1234 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
1235 case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
1236 case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
1237 case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
1238 case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
1239 case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
1240 case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001241 default:
1242 return false; // not a texture declaration
1243 }
1244
1245 advanceToken(); // consume the texture object keyword
1246
1247 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
John Kessenichecba76f2017-01-06 00:34:48 -07001248
LoopDawg4886f692016-06-29 10:58:58 -06001249 TIntermTyped* msCount = nullptr;
1250
steve-lunargbb0183f2016-10-04 16:58:14 -06001251 // texture type: required for multisample types and RWBuffer/RWTextures!
LoopDawg4886f692016-06-29 10:58:58 -06001252 if (acceptTokenClass(EHTokLeftAngle)) {
1253 if (! acceptType(txType)) {
1254 expected("scalar or vector type");
1255 return false;
1256 }
1257
1258 const TBasicType basicRetType = txType.getBasicType() ;
1259
LoopDawg5ee05892017-07-31 13:41:42 -06001260 switch (basicRetType) {
1261 case EbtFloat:
1262 case EbtUint:
1263 case EbtInt:
1264 case EbtStruct:
1265 break;
1266 default:
LoopDawg4886f692016-06-29 10:58:58 -06001267 unimplemented("basic type in texture");
1268 return false;
1269 }
1270
steve-lunargd53f7172016-07-27 15:46:48 -06001271 // Buffers can handle small mats if they fit in 4 components
1272 if (dim == EsdBuffer && txType.isMatrix()) {
1273 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
1274 expected("components < 4 in matrix buffer type");
1275 return false;
1276 }
1277
1278 // TODO: except we don't handle it yet...
1279 unimplemented("matrix type in buffer");
1280 return false;
1281 }
1282
LoopDawg5ee05892017-07-31 13:41:42 -06001283 if (!txType.isScalar() && !txType.isVector() && !txType.isStruct()) {
1284 expected("scalar, vector, or struct type");
LoopDawg4886f692016-06-29 10:58:58 -06001285 return false;
1286 }
1287
LoopDawg4886f692016-06-29 10:58:58 -06001288 if (ms && acceptTokenClass(EHTokComma)) {
1289 // read sample count for multisample types, if given
1290 if (! peekTokenClass(EHTokIntConstant)) {
1291 expected("multisample count");
1292 return false;
1293 }
1294
1295 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
1296 return false;
1297 }
1298
1299 if (! acceptTokenClass(EHTokRightAngle)) {
1300 expected("right angle bracket");
1301 return false;
1302 }
1303 } else if (ms) {
1304 expected("texture type for multisample");
1305 return false;
John Kessenichf36542f2017-03-31 14:39:30 -06001306 } else if (image) {
steve-lunargbb0183f2016-10-04 16:58:14 -06001307 expected("type for RWTexture/RWBuffer");
1308 return false;
LoopDawg4886f692016-06-29 10:58:58 -06001309 }
1310
1311 TArraySizes* arraySizes = nullptr;
steve-lunarg4f2da272016-10-10 15:24:57 -06001312 const bool shadow = false; // declared on the sampler
LoopDawg4886f692016-06-29 10:58:58 -06001313
1314 TSampler sampler;
steve-lunargbb0183f2016-10-04 16:58:14 -06001315 TLayoutFormat format = ElfNone;
steve-lunargd53f7172016-07-27 15:46:48 -06001316
steve-lunarg4f2da272016-10-10 15:24:57 -06001317 // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
1318 if (image || dim == EsdBuffer)
1319 format = parseContext.getLayoutFromTxType(token.loc, txType);
steve-lunargbb0183f2016-10-04 16:58:14 -06001320
LoopDawg5ee05892017-07-31 13:41:42 -06001321 const TBasicType txBasicType = txType.isStruct() ? (*txType.getStruct())[0].type->getBasicType()
1322 : txType.getBasicType();
1323
steve-lunargbb0183f2016-10-04 16:58:14 -06001324 // Non-image Buffers are combined
1325 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -06001326 sampler.set(txType.getBasicType(), dim, array);
1327 } else {
1328 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -06001329 if (image) {
LoopDawg5ee05892017-07-31 13:41:42 -06001330 sampler.setImage(txBasicType, dim, array, shadow, ms);
steve-lunargbb0183f2016-10-04 16:58:14 -06001331 } else {
LoopDawg5ee05892017-07-31 13:41:42 -06001332 sampler.setTexture(txBasicType, dim, array, shadow, ms);
steve-lunargbb0183f2016-10-04 16:58:14 -06001333 }
steve-lunargd53f7172016-07-27 15:46:48 -06001334 }
steve-lunarg8b0227c2016-10-14 16:40:32 -06001335
LoopDawg5ee05892017-07-31 13:41:42 -06001336 // Remember the declared return type. Function returns false on error.
1337 if (!parseContext.setTextureReturnType(sampler, txType, token.loc))
1338 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001339
steve-lunargbf1537f2017-03-31 17:40:09 -06001340 // Force uncombined, if necessary
1341 if (!combined)
1342 sampler.combined = false;
1343
LoopDawg4886f692016-06-29 10:58:58 -06001344 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -06001345 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -06001346
1347 return true;
1348}
1349
John Kessenich87142c72016-03-12 20:24:24 -07001350// If token is for a type, update 'type' with the type information,
1351// and return true and advance.
1352// Otherwise, return false, and don't advance
1353bool HlslGrammar::acceptType(TType& type)
1354{
John Kessenich54ee28f2017-03-11 14:13:00 -07001355 TIntermNode* nodeList = nullptr;
1356 return acceptType(type, nodeList);
1357}
1358bool HlslGrammar::acceptType(TType& type, TIntermNode*& nodeList)
1359{
steve-lunarg3226b082016-10-26 19:18:55 -06001360 // Basic types for min* types, broken out here in case of future
1361 // changes, e.g, to use native halfs.
1362 static const TBasicType min16float_bt = EbtFloat;
1363 static const TBasicType min10float_bt = EbtFloat;
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001364 static const TBasicType half_bt = EbtFloat;
steve-lunarg3226b082016-10-26 19:18:55 -06001365 static const TBasicType min16int_bt = EbtInt;
1366 static const TBasicType min12int_bt = EbtInt;
1367 static const TBasicType min16uint_bt = EbtUint;
1368
John Kessenich0320d092017-06-13 22:22:52 -06001369 // Some types might have turned into identifiers. Take the hit for checking
1370 // when this has happened.
1371 if (typeIdentifiers) {
1372 const char* identifierString = getTypeString(peek());
1373 if (identifierString != nullptr) {
1374 TString name = identifierString;
1375 // if it's an identifier, it's not a type
1376 if (parseContext.symbolTable.find(name) != nullptr)
1377 return false;
1378 }
1379 }
1380
John Kessenich9c86c6a2016-05-03 22:49:24 -06001381 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -06001382 case EHTokVector:
1383 return acceptVectorTemplateType(type);
1384 break;
1385
1386 case EHTokMatrix:
1387 return acceptMatrixTemplateType(type);
1388 break;
1389
steve-lunargf49cdf42016-11-17 15:04:20 -07001390 case EHTokPointStream: // fall through
1391 case EHTokLineStream: // ...
1392 case EHTokTriangleStream: // ...
1393 {
1394 TLayoutGeometry geometry;
1395 if (! acceptStreamOutTemplateType(type, geometry))
1396 return false;
1397
1398 if (! parseContext.handleOutputGeometry(token.loc, geometry))
1399 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001400
steve-lunargf49cdf42016-11-17 15:04:20 -07001401 return true;
1402 }
1403
steve-lunarg858c9282017-01-07 08:54:10 -07001404 case EHTokInputPatch: // fall through
1405 case EHTokOutputPatch: // ...
1406 {
1407 if (! acceptTessellationPatchTemplateType(type))
1408 return false;
1409
1410 return true;
1411 }
1412
LoopDawg4886f692016-06-29 10:58:58 -06001413 case EHTokSampler: // fall through
1414 case EHTokSampler1d: // ...
1415 case EHTokSampler2d: // ...
1416 case EHTokSampler3d: // ...
1417 case EHTokSamplerCube: // ...
1418 case EHTokSamplerState: // ...
1419 case EHTokSamplerComparisonState: // ...
1420 return acceptSamplerType(type);
1421 break;
1422
LoopDawg7f93d562017-09-27 09:04:43 -06001423 case EHTokSubpassInput: // fall through
1424 case EHTokSubpassInputMS: // ...
1425 return acceptSubpassInputType(type);
1426 break;
1427
LoopDawg4886f692016-06-29 10:58:58 -06001428 case EHTokBuffer: // fall through
1429 case EHTokTexture1d: // ...
1430 case EHTokTexture1darray: // ...
1431 case EHTokTexture2d: // ...
1432 case EHTokTexture2darray: // ...
1433 case EHTokTexture3d: // ...
1434 case EHTokTextureCube: // ...
1435 case EHTokTextureCubearray: // ...
1436 case EHTokTexture2DMS: // ...
1437 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001438 case EHTokRWTexture1d: // ...
1439 case EHTokRWTexture1darray: // ...
1440 case EHTokRWTexture2d: // ...
1441 case EHTokRWTexture2darray: // ...
1442 case EHTokRWTexture3d: // ...
1443 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001444 return acceptTextureType(type);
1445 break;
1446
steve-lunarg5da1f032017-02-12 17:50:28 -07001447 case EHTokAppendStructuredBuffer:
1448 case EHTokByteAddressBuffer:
1449 case EHTokConsumeStructuredBuffer:
1450 case EHTokRWByteAddressBuffer:
1451 case EHTokRWStructuredBuffer:
1452 case EHTokStructuredBuffer:
1453 return acceptStructBufferType(type);
1454 break;
1455
LoopDawge5530b92017-11-08 19:48:11 -07001456 case EHTokTextureBuffer:
1457 return acceptTextureBufferType(type);
1458 break;
1459
steve-lunarga766b832017-04-25 09:30:28 -06001460 case EHTokConstantBuffer:
1461 return acceptConstantBufferType(type);
1462
John Kessenich27ffb292017-03-03 17:01:01 -07001463 case EHTokClass:
John Kesseniche6e74942016-06-11 16:43:14 -06001464 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001465 case EHTokCBuffer:
1466 case EHTokTBuffer:
John Kessenich54ee28f2017-03-11 14:13:00 -07001467 return acceptStruct(type, nodeList);
John Kesseniche6e74942016-06-11 16:43:14 -06001468
1469 case EHTokIdentifier:
1470 // An identifier could be for a user-defined type.
1471 // Note we cache the symbol table lookup, to save for a later rule
1472 // when this is not a type.
John Kessenichf4ba25e2017-03-21 18:35:04 -06001473 if (parseContext.lookupUserType(*token.string, type) != nullptr) {
John Kesseniche6e74942016-06-11 16:43:14 -06001474 advanceToken();
1475 return true;
1476 } else
1477 return false;
1478
John Kessenich71351de2016-06-08 12:50:56 -06001479 case EHTokVoid:
1480 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001481 break;
John Kessenich71351de2016-06-08 12:50:56 -06001482
John Kessenicha1e2d492016-09-20 13:22:58 -06001483 case EHTokString:
1484 new(&type) TType(EbtString);
1485 break;
1486
John Kessenich87142c72016-03-12 20:24:24 -07001487 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001488 new(&type) TType(EbtFloat);
1489 break;
John Kessenich87142c72016-03-12 20:24:24 -07001490 case EHTokFloat1:
1491 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001492 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001493 break;
John Kessenich87142c72016-03-12 20:24:24 -07001494 case EHTokFloat2:
1495 new(&type) TType(EbtFloat, EvqTemporary, 2);
1496 break;
1497 case EHTokFloat3:
1498 new(&type) TType(EbtFloat, EvqTemporary, 3);
1499 break;
1500 case EHTokFloat4:
1501 new(&type) TType(EbtFloat, EvqTemporary, 4);
1502 break;
1503
John Kessenich71351de2016-06-08 12:50:56 -06001504 case EHTokDouble:
1505 new(&type) TType(EbtDouble);
1506 break;
1507 case EHTokDouble1:
1508 new(&type) TType(EbtDouble);
1509 type.makeVector();
1510 break;
1511 case EHTokDouble2:
1512 new(&type) TType(EbtDouble, EvqTemporary, 2);
1513 break;
1514 case EHTokDouble3:
1515 new(&type) TType(EbtDouble, EvqTemporary, 3);
1516 break;
1517 case EHTokDouble4:
1518 new(&type) TType(EbtDouble, EvqTemporary, 4);
1519 break;
1520
1521 case EHTokInt:
1522 case EHTokDword:
1523 new(&type) TType(EbtInt);
1524 break;
1525 case EHTokInt1:
1526 new(&type) TType(EbtInt);
1527 type.makeVector();
1528 break;
John Kessenich87142c72016-03-12 20:24:24 -07001529 case EHTokInt2:
1530 new(&type) TType(EbtInt, EvqTemporary, 2);
1531 break;
1532 case EHTokInt3:
1533 new(&type) TType(EbtInt, EvqTemporary, 3);
1534 break;
1535 case EHTokInt4:
1536 new(&type) TType(EbtInt, EvqTemporary, 4);
1537 break;
1538
John Kessenich71351de2016-06-08 12:50:56 -06001539 case EHTokUint:
1540 new(&type) TType(EbtUint);
1541 break;
1542 case EHTokUint1:
1543 new(&type) TType(EbtUint);
1544 type.makeVector();
1545 break;
1546 case EHTokUint2:
1547 new(&type) TType(EbtUint, EvqTemporary, 2);
1548 break;
1549 case EHTokUint3:
1550 new(&type) TType(EbtUint, EvqTemporary, 3);
1551 break;
1552 case EHTokUint4:
1553 new(&type) TType(EbtUint, EvqTemporary, 4);
1554 break;
1555
1556 case EHTokBool:
1557 new(&type) TType(EbtBool);
1558 break;
1559 case EHTokBool1:
1560 new(&type) TType(EbtBool);
1561 type.makeVector();
1562 break;
John Kessenich87142c72016-03-12 20:24:24 -07001563 case EHTokBool2:
1564 new(&type) TType(EbtBool, EvqTemporary, 2);
1565 break;
1566 case EHTokBool3:
1567 new(&type) TType(EbtBool, EvqTemporary, 3);
1568 break;
1569 case EHTokBool4:
1570 new(&type) TType(EbtBool, EvqTemporary, 4);
1571 break;
1572
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001573 case EHTokHalf:
John Kessenich96f65522017-06-06 23:35:25 -06001574 new(&type) TType(half_bt, EvqTemporary);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001575 break;
1576 case EHTokHalf1:
John Kessenich96f65522017-06-06 23:35:25 -06001577 new(&type) TType(half_bt, EvqTemporary);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001578 type.makeVector();
1579 break;
1580 case EHTokHalf2:
John Kessenich96f65522017-06-06 23:35:25 -06001581 new(&type) TType(half_bt, EvqTemporary, 2);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001582 break;
1583 case EHTokHalf3:
John Kessenich96f65522017-06-06 23:35:25 -06001584 new(&type) TType(half_bt, EvqTemporary, 3);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001585 break;
1586 case EHTokHalf4:
John Kessenich96f65522017-06-06 23:35:25 -06001587 new(&type) TType(half_bt, EvqTemporary, 4);
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001588 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001589
steve-lunarg3226b082016-10-26 19:18:55 -06001590 case EHTokMin16float:
1591 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1592 break;
1593 case EHTokMin16float1:
1594 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1595 type.makeVector();
1596 break;
1597 case EHTokMin16float2:
1598 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 2);
1599 break;
1600 case EHTokMin16float3:
1601 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 3);
1602 break;
1603 case EHTokMin16float4:
1604 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 4);
1605 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001606
steve-lunarg3226b082016-10-26 19:18:55 -06001607 case EHTokMin10float:
1608 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1609 break;
1610 case EHTokMin10float1:
1611 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1612 type.makeVector();
1613 break;
1614 case EHTokMin10float2:
1615 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 2);
1616 break;
1617 case EHTokMin10float3:
1618 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 3);
1619 break;
1620 case EHTokMin10float4:
1621 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 4);
1622 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001623
steve-lunarg3226b082016-10-26 19:18:55 -06001624 case EHTokMin16int:
1625 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1626 break;
1627 case EHTokMin16int1:
1628 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1629 type.makeVector();
1630 break;
1631 case EHTokMin16int2:
1632 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 2);
1633 break;
1634 case EHTokMin16int3:
1635 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 3);
1636 break;
1637 case EHTokMin16int4:
1638 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 4);
1639 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001640
steve-lunarg3226b082016-10-26 19:18:55 -06001641 case EHTokMin12int:
1642 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1643 break;
1644 case EHTokMin12int1:
1645 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1646 type.makeVector();
1647 break;
1648 case EHTokMin12int2:
1649 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 2);
1650 break;
1651 case EHTokMin12int3:
1652 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 3);
1653 break;
1654 case EHTokMin12int4:
1655 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 4);
1656 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001657
steve-lunarg3226b082016-10-26 19:18:55 -06001658 case EHTokMin16uint:
1659 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1660 break;
1661 case EHTokMin16uint1:
1662 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1663 type.makeVector();
1664 break;
1665 case EHTokMin16uint2:
1666 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 2);
1667 break;
1668 case EHTokMin16uint3:
1669 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 3);
1670 break;
1671 case EHTokMin16uint4:
1672 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 4);
1673 break;
1674
John Kessenich0133c122016-05-20 12:17:26 -06001675 case EHTokInt1x1:
1676 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1677 break;
1678 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001679 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001680 break;
1681 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001682 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001683 break;
1684 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001685 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001686 break;
1687 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001688 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001689 break;
1690 case EHTokInt2x2:
1691 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1692 break;
1693 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001694 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001695 break;
1696 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001697 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001698 break;
1699 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001700 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001701 break;
1702 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001703 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001704 break;
1705 case EHTokInt3x3:
1706 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1707 break;
1708 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001709 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001710 break;
1711 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001712 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001713 break;
1714 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001715 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001716 break;
1717 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001718 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001719 break;
1720 case EHTokInt4x4:
1721 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1722 break;
1723
John Kessenich71351de2016-06-08 12:50:56 -06001724 case EHTokUint1x1:
1725 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1726 break;
1727 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001728 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001729 break;
1730 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001731 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001732 break;
1733 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001734 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001735 break;
1736 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001737 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001738 break;
1739 case EHTokUint2x2:
1740 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1741 break;
1742 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001743 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001744 break;
1745 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001746 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001747 break;
1748 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001749 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001750 break;
1751 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001752 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001753 break;
1754 case EHTokUint3x3:
1755 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1756 break;
1757 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001758 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001759 break;
1760 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001761 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001762 break;
1763 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001764 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001765 break;
1766 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001767 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001768 break;
1769 case EHTokUint4x4:
1770 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1771 break;
1772
1773 case EHTokBool1x1:
1774 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1775 break;
1776 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001777 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001778 break;
1779 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001780 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001781 break;
1782 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001783 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001784 break;
1785 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001786 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001787 break;
1788 case EHTokBool2x2:
1789 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1790 break;
1791 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001792 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001793 break;
1794 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001795 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001796 break;
1797 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001798 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001799 break;
1800 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001801 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001802 break;
1803 case EHTokBool3x3:
1804 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1805 break;
1806 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001807 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001808 break;
1809 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001810 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001811 break;
1812 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001813 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001814 break;
1815 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001816 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001817 break;
1818 case EHTokBool4x4:
1819 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1820 break;
1821
John Kessenich0133c122016-05-20 12:17:26 -06001822 case EHTokFloat1x1:
1823 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1824 break;
1825 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001826 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001827 break;
1828 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001829 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001830 break;
1831 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001832 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001833 break;
1834 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001835 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001836 break;
John Kessenich87142c72016-03-12 20:24:24 -07001837 case EHTokFloat2x2:
1838 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1839 break;
1840 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001841 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001842 break;
1843 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001844 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001845 break;
John Kessenich0133c122016-05-20 12:17:26 -06001846 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001847 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001848 break;
John Kessenich87142c72016-03-12 20:24:24 -07001849 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001850 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001851 break;
1852 case EHTokFloat3x3:
1853 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1854 break;
1855 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001856 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001857 break;
John Kessenich0133c122016-05-20 12:17:26 -06001858 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001859 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001860 break;
John Kessenich87142c72016-03-12 20:24:24 -07001861 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001862 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001863 break;
1864 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001865 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001866 break;
1867 case EHTokFloat4x4:
1868 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1869 break;
1870
John Kessenich96f65522017-06-06 23:35:25 -06001871 case EHTokHalf1x1:
1872 new(&type) TType(half_bt, EvqTemporary, 0, 1, 1);
1873 break;
1874 case EHTokHalf1x2:
1875 new(&type) TType(half_bt, EvqTemporary, 0, 1, 2);
1876 break;
1877 case EHTokHalf1x3:
1878 new(&type) TType(half_bt, EvqTemporary, 0, 1, 3);
1879 break;
1880 case EHTokHalf1x4:
1881 new(&type) TType(half_bt, EvqTemporary, 0, 1, 4);
1882 break;
1883 case EHTokHalf2x1:
1884 new(&type) TType(half_bt, EvqTemporary, 0, 2, 1);
1885 break;
1886 case EHTokHalf2x2:
1887 new(&type) TType(half_bt, EvqTemporary, 0, 2, 2);
1888 break;
1889 case EHTokHalf2x3:
1890 new(&type) TType(half_bt, EvqTemporary, 0, 2, 3);
1891 break;
1892 case EHTokHalf2x4:
1893 new(&type) TType(half_bt, EvqTemporary, 0, 2, 4);
1894 break;
1895 case EHTokHalf3x1:
1896 new(&type) TType(half_bt, EvqTemporary, 0, 3, 1);
1897 break;
1898 case EHTokHalf3x2:
1899 new(&type) TType(half_bt, EvqTemporary, 0, 3, 2);
1900 break;
1901 case EHTokHalf3x3:
1902 new(&type) TType(half_bt, EvqTemporary, 0, 3, 3);
1903 break;
1904 case EHTokHalf3x4:
1905 new(&type) TType(half_bt, EvqTemporary, 0, 3, 4);
1906 break;
1907 case EHTokHalf4x1:
1908 new(&type) TType(half_bt, EvqTemporary, 0, 4, 1);
1909 break;
1910 case EHTokHalf4x2:
1911 new(&type) TType(half_bt, EvqTemporary, 0, 4, 2);
1912 break;
1913 case EHTokHalf4x3:
1914 new(&type) TType(half_bt, EvqTemporary, 0, 4, 3);
1915 break;
1916 case EHTokHalf4x4:
1917 new(&type) TType(half_bt, EvqTemporary, 0, 4, 4);
1918 break;
1919
John Kessenich0133c122016-05-20 12:17:26 -06001920 case EHTokDouble1x1:
1921 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1922 break;
1923 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001924 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001925 break;
1926 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001927 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001928 break;
1929 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001930 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001931 break;
1932 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001933 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001934 break;
1935 case EHTokDouble2x2:
1936 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1937 break;
1938 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001939 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001940 break;
1941 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001942 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001943 break;
1944 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001945 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001946 break;
1947 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001948 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001949 break;
1950 case EHTokDouble3x3:
1951 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1952 break;
1953 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001954 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001955 break;
1956 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001957 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001958 break;
1959 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001960 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001961 break;
1962 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001963 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001964 break;
1965 case EHTokDouble4x4:
1966 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1967 break;
1968
John Kessenich87142c72016-03-12 20:24:24 -07001969 default:
1970 return false;
1971 }
1972
1973 advanceToken();
1974
1975 return true;
1976}
1977
John Kesseniche6e74942016-06-11 16:43:14 -06001978// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001979// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1980// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
John Kessenich854fe242017-03-02 14:30:59 -07001981// | struct_type IDENTIFIER // use of previously declared struct type
John Kessenich3d157c52016-07-25 16:05:33 -06001982//
1983// struct_type
1984// : STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001985// | CLASS
John Kessenich3d157c52016-07-25 16:05:33 -06001986// | CBUFFER
1987// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001988//
John Kessenich54ee28f2017-03-11 14:13:00 -07001989bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
John Kesseniche6e74942016-06-11 16:43:14 -06001990{
John Kessenichb804de62016-09-05 12:19:18 -06001991 // This storage qualifier will tell us whether it's an AST
1992 // block type or just a generic structure type.
1993 TStorageQualifier storageQualifier = EvqTemporary;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001994 bool readonly = false;
John Kessenich3d157c52016-07-25 16:05:33 -06001995
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001996 if (acceptTokenClass(EHTokCBuffer)) {
John Kessenich2fcdd642017-06-19 15:41:11 -06001997 // CBUFFER
John Kessenichb804de62016-09-05 12:19:18 -06001998 storageQualifier = EvqUniform;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06001999 } else if (acceptTokenClass(EHTokTBuffer)) {
John Kessenich2fcdd642017-06-19 15:41:11 -06002000 // TBUFFER
John Kessenichb804de62016-09-05 12:19:18 -06002001 storageQualifier = EvqBuffer;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06002002 readonly = true;
John Kessenich054378d2017-06-19 15:13:26 -06002003 } else if (! acceptTokenClass(EHTokClass) && ! acceptTokenClass(EHTokStruct)) {
2004 // Neither CLASS nor STRUCT
John Kesseniche6e74942016-06-11 16:43:14 -06002005 return false;
John Kessenich054378d2017-06-19 15:13:26 -06002006 }
2007
2008 // Now known to be one of CBUFFER, TBUFFER, CLASS, or STRUCT
John Kesseniche6e74942016-06-11 16:43:14 -06002009
2010 // IDENTIFIER
2011 TString structName = "";
2012 if (peekTokenClass(EHTokIdentifier)) {
2013 structName = *token.string;
2014 advanceToken();
2015 }
2016
John Kessenich3d157c52016-07-25 16:05:33 -06002017 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06002018 TQualifier postDeclQualifier;
2019 postDeclQualifier.clear();
John Kessenich854fe242017-03-02 14:30:59 -07002020 bool postDeclsFound = acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06002021
John Kessenichf3d88bd2017-03-19 12:24:29 -06002022 // LEFT_BRACE, or
John Kessenich854fe242017-03-02 14:30:59 -07002023 // struct_type IDENTIFIER
John Kesseniche6e74942016-06-11 16:43:14 -06002024 if (! acceptTokenClass(EHTokLeftBrace)) {
John Kessenich854fe242017-03-02 14:30:59 -07002025 if (structName.size() > 0 && !postDeclsFound && parseContext.lookupUserType(structName, type) != nullptr) {
2026 // struct_type IDENTIFIER
2027 return true;
2028 } else {
2029 expected("{");
2030 return false;
2031 }
John Kesseniche6e74942016-06-11 16:43:14 -06002032 }
2033
John Kessenichf3d88bd2017-03-19 12:24:29 -06002034
John Kesseniche6e74942016-06-11 16:43:14 -06002035 // struct_declaration_list
2036 TTypeList* typeList;
John Kessenichf3d88bd2017-03-19 12:24:29 -06002037 // Save each member function so they can be processed after we have a fully formed 'this'.
2038 TVector<TFunctionDeclarator> functionDeclarators;
2039
2040 parseContext.pushNamespace(structName);
John Kessenichaa3c64c2017-03-28 09:52:38 -06002041 bool acceptedList = acceptStructDeclarationList(typeList, nodeList, functionDeclarators);
John Kessenichf3d88bd2017-03-19 12:24:29 -06002042 parseContext.popNamespace();
2043
2044 if (! acceptedList) {
John Kesseniche6e74942016-06-11 16:43:14 -06002045 expected("struct member declarations");
2046 return false;
2047 }
2048
2049 // RIGHT_BRACE
2050 if (! acceptTokenClass(EHTokRightBrace)) {
2051 expected("}");
2052 return false;
2053 }
2054
2055 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06002056 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06002057 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06002058 else {
John Kessenich7735b942016-09-05 12:40:06 -06002059 postDeclQualifier.storage = storageQualifier;
steve-lunarg7b1dcd62017-04-20 13:16:23 -06002060 postDeclQualifier.readonly = readonly;
John Kessenich7735b942016-09-05 12:40:06 -06002061 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06002062 }
John Kesseniche6e74942016-06-11 16:43:14 -06002063
John Kessenich727b3742017-02-03 17:57:55 -07002064 parseContext.declareStruct(token.loc, structName, type);
John Kesseniche6e74942016-06-11 16:43:14 -06002065
John Kessenich4960baa2017-03-19 18:09:59 -06002066 // For member functions: now that we know the type of 'this', go back and
2067 // - add their implicit argument with 'this' (not to the mangling, just the argument list)
2068 // - parse the functions, their tokens were saved for deferred parsing (now)
2069 for (int b = 0; b < (int)functionDeclarators.size(); ++b) {
2070 // update signature
2071 if (functionDeclarators[b].function->hasImplicitThis())
John Kessenich37789792017-03-21 23:56:40 -06002072 functionDeclarators[b].function->addThisParameter(type, intermediate.implicitThisName);
John Kessenich4960baa2017-03-19 18:09:59 -06002073 }
2074
John Kessenichf3d88bd2017-03-19 12:24:29 -06002075 // All member functions get parsed inside the class/struct namespace and with the
2076 // class/struct members in a symbol-table level.
2077 parseContext.pushNamespace(structName);
John Kessenich0a2a0cd2017-05-16 23:16:26 -06002078 parseContext.pushThisScope(type, functionDeclarators);
John Kessenichf3d88bd2017-03-19 12:24:29 -06002079 bool deferredSuccess = true;
2080 for (int b = 0; b < (int)functionDeclarators.size() && deferredSuccess; ++b) {
2081 // parse body
2082 pushTokenStream(functionDeclarators[b].body);
2083 if (! acceptFunctionBody(functionDeclarators[b], nodeList))
2084 deferredSuccess = false;
2085 popTokenStream();
2086 }
John Kessenich37789792017-03-21 23:56:40 -06002087 parseContext.popThisScope();
John Kessenichf3d88bd2017-03-19 12:24:29 -06002088 parseContext.popNamespace();
2089
2090 return deferredSuccess;
John Kesseniche6e74942016-06-11 16:43:14 -06002091}
2092
steve-lunarga766b832017-04-25 09:30:28 -06002093// constantbuffer
2094// : CONSTANTBUFFER LEFT_ANGLE type RIGHT_ANGLE
2095bool HlslGrammar::acceptConstantBufferType(TType& type)
2096{
2097 if (! acceptTokenClass(EHTokConstantBuffer))
2098 return false;
2099
2100 if (! acceptTokenClass(EHTokLeftAngle)) {
2101 expected("left angle bracket");
2102 return false;
2103 }
2104
2105 TType templateType;
2106 if (! acceptType(templateType)) {
2107 expected("type");
2108 return false;
2109 }
2110
2111 if (! acceptTokenClass(EHTokRightAngle)) {
2112 expected("right angle bracket");
2113 return false;
2114 }
2115
2116 TQualifier postDeclQualifier;
2117 postDeclQualifier.clear();
2118 postDeclQualifier.storage = EvqUniform;
2119
2120 if (templateType.isStruct()) {
2121 // Make a block from the type parsed as the template argument
2122 TTypeList* typeList = templateType.getWritableStruct();
2123 new(&type) TType(typeList, "", postDeclQualifier); // sets EbtBlock
2124
2125 type.getQualifier().storage = EvqUniform;
2126
2127 return true;
2128 } else {
2129 parseContext.error(token.loc, "non-structure type in ConstantBuffer", "", "");
2130 return false;
2131 }
2132}
2133
LoopDawge5530b92017-11-08 19:48:11 -07002134// texture_buffer
2135// : TEXTUREBUFFER LEFT_ANGLE type RIGHT_ANGLE
2136bool HlslGrammar::acceptTextureBufferType(TType& type)
2137{
2138 if (! acceptTokenClass(EHTokTextureBuffer))
2139 return false;
2140
2141 if (! acceptTokenClass(EHTokLeftAngle)) {
2142 expected("left angle bracket");
2143 return false;
2144 }
2145
2146 TType templateType;
2147 if (! acceptType(templateType)) {
2148 expected("type");
2149 return false;
2150 }
2151
2152 if (! acceptTokenClass(EHTokRightAngle)) {
2153 expected("right angle bracket");
2154 return false;
2155 }
2156
2157 templateType.getQualifier().storage = EvqBuffer;
2158 templateType.getQualifier().readonly = true;
2159
2160 TType blockType(templateType.getWritableStruct(), "", templateType.getQualifier());
2161
2162 blockType.getQualifier().storage = EvqBuffer;
2163 blockType.getQualifier().readonly = true;
2164
2165 type.shallowCopy(blockType);
2166
2167 return true;
2168}
2169
2170
steve-lunarg5da1f032017-02-12 17:50:28 -07002171// struct_buffer
2172// : APPENDSTRUCTUREDBUFFER
2173// | BYTEADDRESSBUFFER
2174// | CONSUMESTRUCTUREDBUFFER
2175// | RWBYTEADDRESSBUFFER
2176// | RWSTRUCTUREDBUFFER
2177// | STRUCTUREDBUFFER
2178bool HlslGrammar::acceptStructBufferType(TType& type)
2179{
2180 const EHlslTokenClass structBuffType = peek();
2181
2182 // TODO: globallycoherent
2183 bool hasTemplateType = true;
2184 bool readonly = false;
2185
2186 TStorageQualifier storage = EvqBuffer;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002187 TBuiltInVariable builtinType = EbvNone;
steve-lunarg5da1f032017-02-12 17:50:28 -07002188
2189 switch (structBuffType) {
2190 case EHTokAppendStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002191 builtinType = EbvAppendConsume;
2192 break;
steve-lunarg5da1f032017-02-12 17:50:28 -07002193 case EHTokByteAddressBuffer:
2194 hasTemplateType = false;
2195 readonly = true;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002196 builtinType = EbvByteAddressBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002197 break;
2198 case EHTokConsumeStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002199 builtinType = EbvAppendConsume;
2200 break;
steve-lunarg5da1f032017-02-12 17:50:28 -07002201 case EHTokRWByteAddressBuffer:
2202 hasTemplateType = false;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002203 builtinType = EbvRWByteAddressBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002204 break;
2205 case EHTokRWStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002206 builtinType = EbvRWStructuredBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002207 break;
2208 case EHTokStructuredBuffer:
steve-lunarg8e26feb2017-04-10 08:19:21 -06002209 builtinType = EbvStructuredBuffer;
steve-lunarg5da1f032017-02-12 17:50:28 -07002210 readonly = true;
2211 break;
2212 default:
2213 return false; // not a structure buffer type
2214 }
2215
2216 advanceToken(); // consume the structure keyword
2217
2218 // type on which this StructedBuffer is templatized. E.g, StructedBuffer<MyStruct> ==> MyStruct
2219 TType* templateType = new TType;
2220
2221 if (hasTemplateType) {
2222 if (! acceptTokenClass(EHTokLeftAngle)) {
2223 expected("left angle bracket");
2224 return false;
2225 }
2226
2227 if (! acceptType(*templateType)) {
2228 expected("type");
2229 return false;
2230 }
2231 if (! acceptTokenClass(EHTokRightAngle)) {
2232 expected("right angle bracket");
2233 return false;
2234 }
2235 } else {
2236 // byte address buffers have no explicit type.
2237 TType uintType(EbtUint, storage);
2238 templateType->shallowCopy(uintType);
2239 }
2240
2241 // Create an unsized array out of that type.
2242 // TODO: does this work if it's already an array type?
2243 TArraySizes unsizedArray;
2244 unsizedArray.addInnerSize(UnsizedArraySize);
2245 templateType->newArraySizes(unsizedArray);
steve-lunarg40efe5c2017-03-06 12:01:44 -07002246 templateType->getQualifier().storage = storage;
steve-lunargdd8287a2017-02-23 18:04:12 -07002247
2248 // field name is canonical for all structbuffers
2249 templateType->setFieldName("@data");
steve-lunarg5da1f032017-02-12 17:50:28 -07002250
steve-lunarg5da1f032017-02-12 17:50:28 -07002251 TTypeList* blockStruct = new TTypeList;
2252 TTypeLoc member = { templateType, token.loc };
2253 blockStruct->push_back(member);
2254
steve-lunargdd8287a2017-02-23 18:04:12 -07002255 // This is the type of the buffer block (SSBO)
steve-lunarg5da1f032017-02-12 17:50:28 -07002256 TType blockType(blockStruct, "", templateType->getQualifier());
2257
steve-lunargdd8287a2017-02-23 18:04:12 -07002258 blockType.getQualifier().storage = storage;
2259 blockType.getQualifier().readonly = readonly;
steve-lunarg8e26feb2017-04-10 08:19:21 -06002260 blockType.getQualifier().builtIn = builtinType;
steve-lunargdd8287a2017-02-23 18:04:12 -07002261
2262 // We may have created an equivalent type before, in which case we should use its
2263 // deep structure.
2264 parseContext.shareStructBufferType(blockType);
2265
steve-lunarg5da1f032017-02-12 17:50:28 -07002266 type.shallowCopy(blockType);
2267
2268 return true;
2269}
2270
John Kesseniche6e74942016-06-11 16:43:14 -06002271// struct_declaration_list
2272// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
2273//
2274// struct_declaration
2275// : fully_specified_type struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002276// | fully_specified_type IDENTIFIER function_parameters post_decls compound_statement // member-function definition
John Kesseniche6e74942016-06-11 16:43:14 -06002277//
2278// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06002279// : IDENTIFIER post_decls
2280// | IDENTIFIER array_specifier post_decls
John Kessenich54ee28f2017-03-11 14:13:00 -07002281// | IDENTIFIER function_parameters post_decls // member-function prototype
John Kesseniche6e74942016-06-11 16:43:14 -06002282//
John Kessenichaa3c64c2017-03-28 09:52:38 -06002283bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList, TIntermNode*& nodeList,
John Kessenichf3d88bd2017-03-19 12:24:29 -06002284 TVector<TFunctionDeclarator>& declarators)
John Kesseniche6e74942016-06-11 16:43:14 -06002285{
2286 typeList = new TTypeList();
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002287 HlslToken idToken;
John Kesseniche6e74942016-06-11 16:43:14 -06002288
2289 do {
2290 // success on seeing the RIGHT_BRACE coming up
2291 if (peekTokenClass(EHTokRightBrace))
John Kessenichb16f7e62017-03-11 19:32:47 -07002292 break;
John Kesseniche6e74942016-06-11 16:43:14 -06002293
2294 // struct_declaration
John Kessenich54ee28f2017-03-11 14:13:00 -07002295
2296 bool declarator_list = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002297
2298 // fully_specified_type
2299 TType memberType;
John Kessenich54ee28f2017-03-11 14:13:00 -07002300 if (! acceptFullySpecifiedType(memberType, nodeList)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002301 expected("member type");
2302 return false;
2303 }
2304
2305 // struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002306 bool functionDefinitionAccepted = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002307 do {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002308 if (! acceptIdentifier(idToken)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002309 expected("member name");
2310 return false;
2311 }
2312
John Kessenich54ee28f2017-03-11 14:13:00 -07002313 if (peekTokenClass(EHTokLeftParen)) {
2314 // function_parameters
2315 if (!declarator_list) {
John Kessenichb16f7e62017-03-11 19:32:47 -07002316 declarators.resize(declarators.size() + 1);
2317 // request a token stream for deferred processing
John Kessenichf3d88bd2017-03-19 12:24:29 -06002318 functionDefinitionAccepted = acceptMemberFunctionDefinition(nodeList, memberType, *idToken.string,
2319 declarators.back());
John Kessenich54ee28f2017-03-11 14:13:00 -07002320 if (functionDefinitionAccepted)
2321 break;
2322 }
2323 expected("member-function definition");
2324 return false;
2325 } else {
2326 // add it to the list of members
2327 TTypeLoc member = { new TType(EbtVoid), token.loc };
2328 member.type->shallowCopy(memberType);
2329 member.type->setFieldName(*idToken.string);
2330 typeList->push_back(member);
John Kesseniche6e74942016-06-11 16:43:14 -06002331
John Kessenich54ee28f2017-03-11 14:13:00 -07002332 // array_specifier
2333 TArraySizes* arraySizes = nullptr;
2334 acceptArraySpecifier(arraySizes);
2335 if (arraySizes)
2336 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06002337
John Kessenich54ee28f2017-03-11 14:13:00 -07002338 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06002339
John Kessenich54ee28f2017-03-11 14:13:00 -07002340 // EQUAL assignment_expression
2341 if (acceptTokenClass(EHTokAssign)) {
2342 parseContext.warn(idToken.loc, "struct-member initializers ignored", "typedef", "");
2343 TIntermTyped* expressionNode = nullptr;
2344 if (! acceptAssignmentExpression(expressionNode)) {
2345 expected("initializer");
2346 return false;
2347 }
John Kessenich18adbdb2017-02-02 15:16:20 -07002348 }
2349 }
John Kesseniche6e74942016-06-11 16:43:14 -06002350 // success on seeing the SEMICOLON coming up
2351 if (peekTokenClass(EHTokSemicolon))
2352 break;
2353
2354 // COMMA
John Kessenich54ee28f2017-03-11 14:13:00 -07002355 if (acceptTokenClass(EHTokComma))
2356 declarator_list = true;
2357 else {
John Kesseniche6e74942016-06-11 16:43:14 -06002358 expected(",");
2359 return false;
2360 }
2361
2362 } while (true);
2363
2364 // SEMI_COLON
John Kessenich54ee28f2017-03-11 14:13:00 -07002365 if (! functionDefinitionAccepted && ! acceptTokenClass(EHTokSemicolon)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002366 expected(";");
2367 return false;
2368 }
2369
2370 } while (true);
John Kessenichb16f7e62017-03-11 19:32:47 -07002371
John Kessenichb16f7e62017-03-11 19:32:47 -07002372 return true;
John Kesseniche6e74942016-06-11 16:43:14 -06002373}
2374
John Kessenich54ee28f2017-03-11 14:13:00 -07002375// member_function_definition
2376// | function_parameters post_decls compound_statement
2377//
2378// Expects type to have EvqGlobal for a static member and
2379// EvqTemporary for non-static member.
John Kessenich9855bda2017-09-11 21:48:19 -06002380bool HlslGrammar::acceptMemberFunctionDefinition(TIntermNode*& nodeList, const TType& type, TString& memberName,
John Kessenichf3d88bd2017-03-19 12:24:29 -06002381 TFunctionDeclarator& declarator)
John Kessenich54ee28f2017-03-11 14:13:00 -07002382{
John Kessenich54ee28f2017-03-11 14:13:00 -07002383 bool accepted = false;
2384
John Kessenich9855bda2017-09-11 21:48:19 -06002385 TString* functionName = &memberName;
John Kessenich4dc835c2017-03-28 23:43:10 -06002386 parseContext.getFullNamespaceName(functionName);
John Kessenich088d52b2017-03-11 17:55:28 -07002387 declarator.function = new TFunction(functionName, type);
John Kessenich4960baa2017-03-19 18:09:59 -06002388 if (type.getQualifier().storage == EvqTemporary)
2389 declarator.function->setImplicitThis();
John Kessenich37789792017-03-21 23:56:40 -06002390 else
2391 declarator.function->setIllegalImplicitThis();
John Kessenich54ee28f2017-03-11 14:13:00 -07002392
2393 // function_parameters
John Kessenich088d52b2017-03-11 17:55:28 -07002394 if (acceptFunctionParameters(*declarator.function)) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002395 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -07002396 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich54ee28f2017-03-11 14:13:00 -07002397
2398 // compound_statement (function body definition)
2399 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich088d52b2017-03-11 17:55:28 -07002400 declarator.loc = token.loc;
John Kessenichf3d88bd2017-03-19 12:24:29 -06002401 declarator.body = new TVector<HlslToken>;
2402 accepted = acceptFunctionDefinition(declarator, nodeList, declarator.body);
John Kessenich54ee28f2017-03-11 14:13:00 -07002403 }
2404 } else
2405 expected("function parameter list");
2406
John Kessenich54ee28f2017-03-11 14:13:00 -07002407 return accepted;
2408}
2409
John Kessenich5f934b02016-03-13 17:58:25 -06002410// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06002411// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06002412// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002413//
2414bool HlslGrammar::acceptFunctionParameters(TFunction& function)
2415{
John Kessenich078d7f22016-03-14 10:02:11 -06002416 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002417 if (! acceptTokenClass(EHTokLeftParen))
2418 return false;
2419
John Kessenich71351de2016-06-08 12:50:56 -06002420 // VOID RIGHT_PAREN
2421 if (! acceptTokenClass(EHTokVoid)) {
2422 do {
2423 // parameter_declaration
2424 if (! acceptParameterDeclaration(function))
2425 break;
John Kessenich5f934b02016-03-13 17:58:25 -06002426
John Kessenich71351de2016-06-08 12:50:56 -06002427 // COMMA
2428 if (! acceptTokenClass(EHTokComma))
2429 break;
2430 } while (true);
2431 }
John Kessenich5f934b02016-03-13 17:58:25 -06002432
John Kessenich078d7f22016-03-14 10:02:11 -06002433 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002434 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002435 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06002436 return false;
2437 }
2438
2439 return true;
2440}
2441
steve-lunarg26d31452016-12-23 18:56:57 -07002442// default_parameter_declaration
2443// : EQUAL conditional_expression
2444// : EQUAL initializer
2445bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTyped*& node)
2446{
2447 node = nullptr;
2448
2449 // Valid not to have a default_parameter_declaration
2450 if (!acceptTokenClass(EHTokAssign))
2451 return true;
2452
2453 if (!acceptConditionalExpression(node)) {
2454 if (!acceptInitializer(node))
2455 return false;
2456
2457 // For initializer lists, we have to const-fold into a constructor for the type, so build
2458 // that.
John Kessenichc633f642017-04-03 21:48:37 -06002459 TFunction* constructor = parseContext.makeConstructorCall(token.loc, type);
steve-lunarg26d31452016-12-23 18:56:57 -07002460 if (constructor == nullptr) // cannot construct
2461 return false;
2462
2463 TIntermTyped* arguments = nullptr;
John Kessenichecba76f2017-01-06 00:34:48 -07002464 for (int i = 0; i < int(node->getAsAggregate()->getSequence().size()); i++)
steve-lunarg26d31452016-12-23 18:56:57 -07002465 parseContext.handleFunctionArgument(constructor, arguments, node->getAsAggregate()->getSequence()[i]->getAsTyped());
John Kessenichecba76f2017-01-06 00:34:48 -07002466
steve-lunarg26d31452016-12-23 18:56:57 -07002467 node = parseContext.handleFunctionCall(token.loc, constructor, node);
2468 }
2469
John Kessenichbb79abc2017-10-07 13:23:09 -06002470 if (node == nullptr)
2471 return false;
2472
steve-lunarg26d31452016-12-23 18:56:57 -07002473 // If this is simply a constant, we can use it directly.
2474 if (node->getAsConstantUnion())
2475 return true;
2476
2477 // Otherwise, it has to be const-foldable.
2478 TIntermTyped* origNode = node;
2479
2480 node = intermediate.fold(node->getAsAggregate());
2481
2482 if (node != nullptr && origNode != node)
2483 return true;
2484
2485 parseContext.error(token.loc, "invalid default parameter value", "", "");
2486
2487 return false;
2488}
2489
John Kessenich5f934b02016-03-13 17:58:25 -06002490// parameter_declaration
John Kessenich77ea30b2017-09-30 14:34:50 -06002491// : attributes attributed_declaration
2492//
2493// attributed_declaration
steve-lunarg26d31452016-12-23 18:56:57 -07002494// : fully_specified_type post_decls [ = default_parameter_declaration ]
2495// | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
John Kessenich5f934b02016-03-13 17:58:25 -06002496//
2497bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
2498{
John Kessenich77ea30b2017-09-30 14:34:50 -06002499 // attributes
2500 TAttributeMap attributes;
2501 acceptAttributes(attributes);
2502
John Kessenich5f934b02016-03-13 17:58:25 -06002503 // fully_specified_type
2504 TType* type = new TType;
2505 if (! acceptFullySpecifiedType(*type))
2506 return false;
2507
John Kessenich77ea30b2017-09-30 14:34:50 -06002508 parseContext.transferTypeAttributes(attributes, *type);
2509
John Kessenich5f934b02016-03-13 17:58:25 -06002510 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06002511 HlslToken idToken;
2512 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06002513
John Kessenich19b92ff2016-06-19 11:50:34 -06002514 // array_specifier
2515 TArraySizes* arraySizes = nullptr;
2516 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002517 if (arraySizes) {
2518 if (arraySizes->isImplicit()) {
2519 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
2520 return false;
2521 }
2522
John Kessenich19b92ff2016-06-19 11:50:34 -06002523 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002524 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002525
2526 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06002527 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06002528
steve-lunarg26d31452016-12-23 18:56:57 -07002529 TIntermTyped* defaultValue;
2530 if (!acceptDefaultParameterDeclaration(*type, defaultValue))
2531 return false;
2532
John Kessenich5aa59e22016-06-17 15:50:47 -06002533 parseContext.paramFix(*type);
2534
steve-lunarg26d31452016-12-23 18:56:57 -07002535 // If any prior parameters have default values, all the parameters after that must as well.
2536 if (defaultValue == nullptr && function.getDefaultParamCount() > 0) {
2537 parseContext.error(idToken.loc, "invalid parameter after default value parameters", idToken.string->c_str(), "");
2538 return false;
2539 }
2540
2541 TParameter param = { idToken.string, type, defaultValue };
John Kessenich5f934b02016-03-13 17:58:25 -06002542 function.addParameter(param);
2543
2544 return true;
2545}
2546
2547// Do the work to create the function definition in addition to
2548// parsing the body (compound_statement).
John Kessenichb16f7e62017-03-11 19:32:47 -07002549//
2550// If 'deferredTokens' are passed in, just get the token stream,
2551// don't process.
2552//
2553bool HlslGrammar::acceptFunctionDefinition(TFunctionDeclarator& declarator, TIntermNode*& nodeList,
2554 TVector<HlslToken>* deferredTokens)
John Kessenich5f934b02016-03-13 17:58:25 -06002555{
John Kessenich088d52b2017-03-11 17:55:28 -07002556 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, false /* not prototype */);
John Kessenich5f934b02016-03-13 17:58:25 -06002557
John Kessenichb16f7e62017-03-11 19:32:47 -07002558 if (deferredTokens)
2559 return captureBlockTokens(*deferredTokens);
2560 else
John Kessenich4960baa2017-03-19 18:09:59 -06002561 return acceptFunctionBody(declarator, nodeList);
John Kessenich088d52b2017-03-11 17:55:28 -07002562}
2563
2564bool HlslGrammar::acceptFunctionBody(TFunctionDeclarator& declarator, TIntermNode*& nodeList)
2565{
2566 // we might get back an entry-point
John Kessenichca71d942017-03-07 20:44:09 -07002567 TIntermNode* entryPointNode = nullptr;
2568
John Kessenich077e0522016-06-09 02:02:17 -06002569 // This does a pushScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002570 TIntermNode* functionNode = parseContext.handleFunctionDefinition(declarator.loc, *declarator.function,
2571 declarator.attributes, entryPointNode);
John Kessenich5f934b02016-03-13 17:58:25 -06002572
2573 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002574 TIntermNode* functionBody = nullptr;
John Kessenich02467d82017-01-19 15:41:47 -07002575 if (! acceptCompoundStatement(functionBody))
2576 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002577
John Kessenich54ee28f2017-03-11 14:13:00 -07002578 // this does a popScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002579 parseContext.handleFunctionBody(declarator.loc, *declarator.function, functionBody, functionNode);
John Kessenichca71d942017-03-07 20:44:09 -07002580
2581 // Hook up the 1 or 2 function definitions.
2582 nodeList = intermediate.growAggregate(nodeList, functionNode);
2583 nodeList = intermediate.growAggregate(nodeList, entryPointNode);
John Kessenich02467d82017-01-19 15:41:47 -07002584
2585 return true;
John Kessenich5f934b02016-03-13 17:58:25 -06002586}
2587
John Kessenich0d2b6de2016-06-05 11:23:11 -06002588// Accept an expression with parenthesis around it, where
2589// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002590// syntactically required ones like in "if ( expression )".
2591//
2592// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06002593//
2594// Note this one is not set up to be speculative; as it gives
2595// errors if not found.
2596//
2597bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
2598{
2599 // LEFT_PAREN
2600 if (! acceptTokenClass(EHTokLeftParen))
2601 expected("(");
2602
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002603 bool decl = false;
2604 TIntermNode* declNode = nullptr;
2605 decl = acceptControlDeclaration(declNode);
2606 if (decl) {
2607 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
2608 expected("initialized declaration");
2609 return false;
2610 } else
2611 expression = declNode->getAsTyped();
2612 } else {
2613 // no declaration
2614 if (! acceptExpression(expression)) {
2615 expected("expression");
2616 return false;
2617 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06002618 }
2619
2620 // RIGHT_PAREN
2621 if (! acceptTokenClass(EHTokRightParen))
2622 expected(")");
2623
2624 return true;
2625}
2626
John Kessenich34fb0362016-05-03 23:17:20 -06002627// The top-level full expression recognizer.
2628//
John Kessenich87142c72016-03-12 20:24:24 -07002629// expression
John Kessenich34fb0362016-05-03 23:17:20 -06002630// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07002631//
2632bool HlslGrammar::acceptExpression(TIntermTyped*& node)
2633{
LoopDawgef764a22016-06-03 09:17:51 -06002634 node = nullptr;
2635
John Kessenich34fb0362016-05-03 23:17:20 -06002636 // assignment_expression
2637 if (! acceptAssignmentExpression(node))
2638 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002639
John Kessenich34fb0362016-05-03 23:17:20 -06002640 if (! peekTokenClass(EHTokComma))
2641 return true;
2642
2643 do {
2644 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06002645 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06002646 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06002647
John Kessenich34fb0362016-05-03 23:17:20 -06002648 // ... assignment_expression
2649 TIntermTyped* rightNode = nullptr;
2650 if (! acceptAssignmentExpression(rightNode)) {
2651 expected("assignment expression");
2652 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002653 }
2654
John Kessenich34fb0362016-05-03 23:17:20 -06002655 node = intermediate.addComma(node, rightNode, loc);
2656
2657 if (! peekTokenClass(EHTokComma))
2658 return true;
2659 } while (true);
2660}
2661
John Kessenich07354242016-07-01 19:58:06 -06002662// initializer
John Kessenich98ad4852016-11-27 17:39:07 -07002663// : LEFT_BRACE RIGHT_BRACE
2664// | LEFT_BRACE initializer_list RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002665//
2666// initializer_list
2667// : assignment_expression COMMA assignment_expression COMMA ...
2668//
2669bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
2670{
2671 // LEFT_BRACE
2672 if (! acceptTokenClass(EHTokLeftBrace))
2673 return false;
2674
John Kessenich98ad4852016-11-27 17:39:07 -07002675 // RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002676 TSourceLoc loc = token.loc;
John Kessenich98ad4852016-11-27 17:39:07 -07002677 if (acceptTokenClass(EHTokRightBrace)) {
2678 // a zero-length initializer list
2679 node = intermediate.makeAggregate(loc);
2680 return true;
2681 }
2682
2683 // initializer_list
John Kessenich07354242016-07-01 19:58:06 -06002684 node = nullptr;
2685 do {
2686 // assignment_expression
2687 TIntermTyped* expr;
2688 if (! acceptAssignmentExpression(expr)) {
2689 expected("assignment expression in initializer list");
2690 return false;
2691 }
LoopDawg0fca0ba2017-07-10 15:43:40 -06002692
2693 const bool firstNode = (node == nullptr);
2694
John Kessenich07354242016-07-01 19:58:06 -06002695 node = intermediate.growAggregate(node, expr, loc);
2696
LoopDawg0fca0ba2017-07-10 15:43:40 -06002697 // If every sub-node in the list has qualifier EvqConst, the returned node becomes
2698 // EvqConst. Otherwise, it becomes EvqTemporary. That doesn't happen with e.g.
2699 // EvqIn or EvqPosition, since the collection isn't EvqPosition if all the members are.
2700 if (firstNode && expr->getQualifier().storage == EvqConst)
2701 node->getQualifier().storage = EvqConst;
2702 else if (expr->getQualifier().storage != EvqConst)
2703 node->getQualifier().storage = EvqTemporary;
2704
John Kessenich07354242016-07-01 19:58:06 -06002705 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002706 if (acceptTokenClass(EHTokComma)) {
2707 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
2708 return true;
John Kessenich07354242016-07-01 19:58:06 -06002709 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002710 }
John Kessenich07354242016-07-01 19:58:06 -06002711
2712 // RIGHT_BRACE
2713 if (acceptTokenClass(EHTokRightBrace))
2714 return true;
2715
2716 expected(", or }");
2717 return false;
2718 } while (true);
2719}
2720
John Kessenich34fb0362016-05-03 23:17:20 -06002721// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06002722// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06002723//
2724// a op (b op (c op d))
2725//
2726// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06002727// : initializer
2728// | conditional_expression
2729// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06002730//
2731bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
2732{
John Kessenich07354242016-07-01 19:58:06 -06002733 // initializer
2734 if (peekTokenClass(EHTokLeftBrace)) {
2735 if (acceptInitializer(node))
2736 return true;
2737
2738 expected("initializer");
2739 return false;
2740 }
2741
John Kessenich00957f82016-07-27 10:39:57 -06002742 // conditional_expression
2743 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06002744 return false;
2745
John Kessenich07354242016-07-01 19:58:06 -06002746 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06002747 TOperator assignOp = HlslOpMap::assignment(peek());
2748 if (assignOp == EOpNull)
2749 return true;
2750
John Kessenich00957f82016-07-27 10:39:57 -06002751 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06002752 TSourceLoc loc = token.loc;
2753 advanceToken();
2754
John Kessenich00957f82016-07-27 10:39:57 -06002755 // conditional_expression assign_op conditional_expression ...
2756 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06002757 // gets the right-to-left associativity.
2758 TIntermTyped* rightNode = nullptr;
2759 if (! acceptAssignmentExpression(rightNode)) {
2760 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06002761 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002762 }
2763
John Kessenichd21baed2016-09-16 03:05:12 -06002764 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
steve-lunarg90707962016-10-07 19:35:40 -06002765 node = parseContext.handleLvalue(loc, "assign", node);
2766
John Kessenichfea226b2016-07-28 17:53:56 -06002767 if (node == nullptr) {
2768 parseContext.error(loc, "could not create assignment", "", "");
2769 return false;
2770 }
John Kessenich34fb0362016-05-03 23:17:20 -06002771
2772 if (! peekTokenClass(EHTokComma))
2773 return true;
2774
2775 return true;
2776}
2777
John Kessenich00957f82016-07-27 10:39:57 -06002778// Accept a conditional expression, which associates right-to-left,
2779// accomplished by the "true" expression calling down to lower
2780// precedence levels than this level.
2781//
2782// conditional_expression
2783// : binary_expression
2784// | binary_expression QUESTION expression COLON assignment_expression
2785//
2786bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
2787{
2788 // binary_expression
2789 if (! acceptBinaryExpression(node, PlLogicalOr))
2790 return false;
2791
2792 if (! acceptTokenClass(EHTokQuestion))
2793 return true;
2794
John Kessenich636b62d2017-04-11 19:45:00 -06002795 node = parseContext.convertConditionalExpression(token.loc, node, false);
John Kessenich7e997e22017-03-30 22:09:30 -06002796 if (node == nullptr)
2797 return false;
2798
John Kessenichf6deacd2017-06-06 19:52:55 -06002799 ++parseContext.controlFlowNestingLevel; // this only needs to work right if no errors
2800
John Kessenich00957f82016-07-27 10:39:57 -06002801 TIntermTyped* trueNode = nullptr;
2802 if (! acceptExpression(trueNode)) {
2803 expected("expression after ?");
2804 return false;
2805 }
2806 TSourceLoc loc = token.loc;
2807
2808 if (! acceptTokenClass(EHTokColon)) {
2809 expected(":");
2810 return false;
2811 }
2812
2813 TIntermTyped* falseNode = nullptr;
2814 if (! acceptAssignmentExpression(falseNode)) {
2815 expected("expression after :");
2816 return false;
2817 }
2818
John Kessenichf6deacd2017-06-06 19:52:55 -06002819 --parseContext.controlFlowNestingLevel;
2820
John Kessenich00957f82016-07-27 10:39:57 -06002821 node = intermediate.addSelection(node, trueNode, falseNode, loc);
2822
2823 return true;
2824}
2825
John Kessenich34fb0362016-05-03 23:17:20 -06002826// Accept a binary expression, for binary operations that
2827// associate left-to-right. This is, it is implicit, for example
2828//
2829// ((a op b) op c) op d
2830//
2831// binary_expression
2832// : expression op expression op expression ...
2833//
2834// where 'expression' is the next higher level in precedence.
2835//
2836bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
2837{
2838 if (precedenceLevel > PlMul)
2839 return acceptUnaryExpression(node);
2840
2841 // assignment_expression
2842 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
2843 return false;
2844
John Kessenich34fb0362016-05-03 23:17:20 -06002845 do {
John Kessenich64076ed2016-07-28 21:43:17 -06002846 TOperator op = HlslOpMap::binary(peek());
2847 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
2848 if (tokenLevel < precedenceLevel)
2849 return true;
2850
John Kessenich34fb0362016-05-03 23:17:20 -06002851 // ... op
2852 TSourceLoc loc = token.loc;
2853 advanceToken();
2854
2855 // ... expression
2856 TIntermTyped* rightNode = nullptr;
2857 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
2858 expected("expression");
2859 return false;
2860 }
2861
2862 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06002863 if (node == nullptr) {
2864 parseContext.error(loc, "Could not perform requested binary operation", "", "");
2865 return false;
2866 }
John Kessenich34fb0362016-05-03 23:17:20 -06002867 } while (true);
2868}
2869
2870// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06002871// : (type) unary_expression
2872// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06002873// | - unary_expression
2874// | ! unary_expression
2875// | ~ unary_expression
2876// | ++ unary_expression
2877// | -- unary_expression
2878// | postfix_expression
2879//
2880bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
2881{
John Kessenich1cc1a282016-06-03 16:55:49 -06002882 // (type) unary_expression
2883 // Have to look two steps ahead, because this could be, e.g., a
2884 // postfix_expression instead, since that also starts with at "(".
2885 if (acceptTokenClass(EHTokLeftParen)) {
2886 TType castType;
2887 if (acceptType(castType)) {
John Kessenich82ae8c32017-06-13 23:13:10 -06002888 // recognize any array_specifier as part of the type
2889 TArraySizes* arraySizes = nullptr;
2890 acceptArraySpecifier(arraySizes);
2891 if (arraySizes != nullptr)
2892 castType.newArraySizes(*arraySizes);
2893 TSourceLoc loc = token.loc;
steve-lunarg5964c642016-07-30 07:38:55 -06002894 if (acceptTokenClass(EHTokRightParen)) {
2895 // We've matched "(type)" now, get the expression to cast
steve-lunarg5964c642016-07-30 07:38:55 -06002896 if (! acceptUnaryExpression(node))
2897 return false;
2898
2899 // Hook it up like a constructor
John Kessenichc633f642017-04-03 21:48:37 -06002900 TFunction* constructorFunction = parseContext.makeConstructorCall(loc, castType);
steve-lunarg5964c642016-07-30 07:38:55 -06002901 if (constructorFunction == nullptr) {
2902 expected("type that can be constructed");
2903 return false;
2904 }
2905 TIntermTyped* arguments = nullptr;
2906 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
2907 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
2908
John Kessenichbb79abc2017-10-07 13:23:09 -06002909 return node != nullptr;
steve-lunarg5964c642016-07-30 07:38:55 -06002910 } else {
2911 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
2912 // the '(int' part. We must back up twice.
2913 recedeToken();
2914 recedeToken();
John Kessenich82ae8c32017-06-13 23:13:10 -06002915
2916 // Note, there are no array constructors like
2917 // (float[2](...))
2918 if (arraySizes != nullptr)
2919 parseContext.error(loc, "parenthesized array constructor not allowed", "([]())", "", "");
John Kessenich1cc1a282016-06-03 16:55:49 -06002920 }
John Kessenich1cc1a282016-06-03 16:55:49 -06002921 } else {
2922 // This isn't a type cast, but it still started "(", so if it is a
2923 // unary expression, it can only be a postfix_expression, so try that.
2924 // Back it up first.
2925 recedeToken();
2926 return acceptPostfixExpression(node);
2927 }
2928 }
2929
2930 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06002931 TOperator unaryOp = HlslOpMap::preUnary(peek());
John Kessenichecba76f2017-01-06 00:34:48 -07002932
John Kessenich1cc1a282016-06-03 16:55:49 -06002933 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06002934 if (unaryOp == EOpNull)
2935 return acceptPostfixExpression(node);
2936
2937 // op unary_expression
2938 TSourceLoc loc = token.loc;
2939 advanceToken();
2940 if (! acceptUnaryExpression(node))
2941 return false;
2942
2943 // + is a no-op
2944 if (unaryOp == EOpAdd)
2945 return true;
2946
2947 node = intermediate.addUnaryMath(unaryOp, node, loc);
steve-lunarge5921f12016-10-15 10:29:58 -06002948
2949 // These unary ops require lvalues
2950 if (unaryOp == EOpPreIncrement || unaryOp == EOpPreDecrement)
2951 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002952
2953 return node != nullptr;
2954}
2955
2956// postfix_expression
2957// : LEFT_PAREN expression RIGHT_PAREN
2958// | literal
2959// | constructor
John Kessenich8f9fdc92017-03-30 16:22:26 -06002960// | IDENTIFIER [ COLONCOLON IDENTIFIER [ COLONCOLON IDENTIFIER ... ] ]
John Kessenich34fb0362016-05-03 23:17:20 -06002961// | function_call
2962// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
2963// | postfix_expression DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002964// | postfix_expression DOT IDENTIFIER arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06002965// | postfix_expression arguments
John Kessenich34fb0362016-05-03 23:17:20 -06002966// | postfix_expression INC_OP
2967// | postfix_expression DEC_OP
2968//
2969bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
2970{
2971 // Not implemented as self-recursive:
John Kessenich54ee28f2017-03-11 14:13:00 -07002972 // The logical "right recursion" is done with a loop at the end
John Kessenich34fb0362016-05-03 23:17:20 -06002973
2974 // idToken will pick up either a variable or a function name in a function call
2975 HlslToken idToken;
2976
John Kessenich21472ae2016-06-04 11:46:33 -06002977 // Find something before the postfix operations, as they can't operate
2978 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07002979 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06002980 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002981 if (! acceptExpression(node)) {
2982 expected("expression");
2983 return false;
2984 }
2985 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002986 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002987 return false;
2988 }
John Kessenich34fb0362016-05-03 23:17:20 -06002989 } else if (acceptLiteral(node)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002990 // literal (nothing else to do yet)
John Kessenich34fb0362016-05-03 23:17:20 -06002991 } else if (acceptConstructor(node)) {
2992 // constructor (nothing else to do yet)
2993 } else if (acceptIdentifier(idToken)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002994 // user-type, namespace name, variable, or function name
2995 TString* fullName = idToken.string;
2996 while (acceptTokenClass(EHTokColonColon)) {
2997 // user-type or namespace name
2998 fullName = NewPoolTString(fullName->c_str());
2999 fullName->append(parseContext.scopeMangler);
3000 if (acceptIdentifier(idToken))
3001 fullName->append(*idToken.string);
3002 else {
3003 expected("identifier after ::");
John Kessenich54ee28f2017-03-11 14:13:00 -07003004 return false;
3005 }
John Kessenich8f9fdc92017-03-30 16:22:26 -06003006 }
3007 if (! peekTokenClass(EHTokLeftParen)) {
3008 node = parseContext.handleVariable(idToken.loc, fullName);
3009 } else if (acceptFunctionCall(idToken.loc, *fullName, node, nullptr)) {
John Kessenich34fb0362016-05-03 23:17:20 -06003010 // function_call (nothing else to do yet)
3011 } else {
3012 expected("function call arguments");
3013 return false;
3014 }
John Kessenich21472ae2016-06-04 11:46:33 -06003015 } else {
3016 // nothing found, can't post operate
3017 return false;
John Kessenich87142c72016-03-12 20:24:24 -07003018 }
3019
John Kessenich21472ae2016-06-04 11:46:33 -06003020 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06003021 do {
3022 TSourceLoc loc = token.loc;
3023 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07003024
John Kessenich34fb0362016-05-03 23:17:20 -06003025 // Consume only a valid post-unary operator, otherwise we are done.
3026 switch (postOp) {
3027 case EOpIndexDirectStruct:
3028 case EOpIndexIndirect:
3029 case EOpPostIncrement:
3030 case EOpPostDecrement:
John Kessenich54ee28f2017-03-11 14:13:00 -07003031 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06003032 advanceToken();
3033 break;
3034 default:
3035 return true;
3036 }
John Kessenich87142c72016-03-12 20:24:24 -07003037
John Kessenich34fb0362016-05-03 23:17:20 -06003038 // We have a valid post-unary operator, process it.
3039 switch (postOp) {
John Kessenich54ee28f2017-03-11 14:13:00 -07003040 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06003041 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06003042 {
John Kessenich19b92ff2016-06-19 11:50:34 -06003043 // DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07003044 // includes swizzles, member variables, and member functions
John Kessenich93a162a2016-06-17 17:16:27 -06003045 HlslToken field;
3046 if (! acceptIdentifier(field)) {
3047 expected("swizzle or member");
3048 return false;
3049 }
LoopDawg4886f692016-06-29 10:58:58 -06003050
John Kessenich516d92d2017-03-08 20:09:03 -07003051 if (peekTokenClass(EHTokLeftParen)) {
3052 // member function
3053 TIntermTyped* thisNode = node;
LoopDawg4886f692016-06-29 10:58:58 -06003054
John Kessenich516d92d2017-03-08 20:09:03 -07003055 // arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06003056 if (! acceptFunctionCall(field.loc, *field.string, node, thisNode)) {
LoopDawg4886f692016-06-29 10:58:58 -06003057 expected("function parameters");
3058 return false;
3059 }
John Kessenich516d92d2017-03-08 20:09:03 -07003060 } else
3061 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06003062
John Kessenich34fb0362016-05-03 23:17:20 -06003063 break;
John Kessenich93a162a2016-06-17 17:16:27 -06003064 }
John Kessenich34fb0362016-05-03 23:17:20 -06003065 case EOpIndexIndirect:
3066 {
John Kessenich19b92ff2016-06-19 11:50:34 -06003067 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06003068 TIntermTyped* indexNode = nullptr;
3069 if (! acceptExpression(indexNode) ||
3070 ! peekTokenClass(EHTokRightBracket)) {
3071 expected("expression followed by ']'");
3072 return false;
3073 }
John Kessenich19b92ff2016-06-19 11:50:34 -06003074 advanceToken();
3075 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
steve-lunarg2efd6c62017-04-06 20:22:20 -06003076 if (node == nullptr)
3077 return false;
John Kessenich19b92ff2016-06-19 11:50:34 -06003078 break;
John Kessenich34fb0362016-05-03 23:17:20 -06003079 }
3080 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06003081 // INC_OP
3082 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06003083 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06003084 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06003085 node = intermediate.addUnaryMath(postOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06003086 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06003087 break;
3088 default:
3089 assert(0);
3090 break;
3091 }
3092 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07003093}
3094
John Kessenichd016be12016-03-13 11:24:20 -06003095// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06003096// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06003097//
3098bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
3099{
3100 // type
3101 TType type;
3102 if (acceptType(type)) {
John Kessenichc633f642017-04-03 21:48:37 -06003103 TFunction* constructorFunction = parseContext.makeConstructorCall(token.loc, type);
John Kessenichd016be12016-03-13 11:24:20 -06003104 if (constructorFunction == nullptr)
3105 return false;
3106
3107 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06003108 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06003109 if (! acceptArguments(constructorFunction, arguments)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07003110 // It's possible this is a type keyword used as an identifier. Put the token back
3111 // for later use.
3112 recedeToken();
John Kessenichd016be12016-03-13 11:24:20 -06003113 return false;
3114 }
3115
3116 // hook it up
3117 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
3118
John Kessenichbb79abc2017-10-07 13:23:09 -06003119 return node != nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06003120 }
3121
3122 return false;
3123}
3124
John Kessenich34fb0362016-05-03 23:17:20 -06003125// The function_call identifier was already recognized, and passed in as idToken.
3126//
3127// function_call
3128// : [idToken] arguments
3129//
John Kessenich8f9fdc92017-03-30 16:22:26 -06003130bool HlslGrammar::acceptFunctionCall(const TSourceLoc& loc, TString& name, TIntermTyped*& node, TIntermTyped* baseObject)
John Kessenich34fb0362016-05-03 23:17:20 -06003131{
John Kessenich54ee28f2017-03-11 14:13:00 -07003132 // name
3133 TString* functionName = nullptr;
John Kessenich8f9fdc92017-03-30 16:22:26 -06003134 if (baseObject == nullptr) {
3135 functionName = &name;
3136 } else if (parseContext.isBuiltInMethod(loc, baseObject, name)) {
John Kessenich4960baa2017-03-19 18:09:59 -06003137 // Built-in methods are not in the symbol table as methods, but as global functions
3138 // taking an explicit 'this' as the first argument.
steve-lunarge7d07522017-03-19 18:12:37 -06003139 functionName = NewPoolTString(BUILTIN_PREFIX);
John Kessenich8f9fdc92017-03-30 16:22:26 -06003140 functionName->append(name);
John Kessenich4960baa2017-03-19 18:09:59 -06003141 } else {
John Kessenich8f9fdc92017-03-30 16:22:26 -06003142 if (! baseObject->getType().isStruct()) {
3143 expected("structure");
3144 return false;
3145 }
John Kessenich54ee28f2017-03-11 14:13:00 -07003146 functionName = NewPoolTString("");
John Kessenich8f9fdc92017-03-30 16:22:26 -06003147 functionName->append(baseObject->getType().getTypeName());
John Kessenichf3d88bd2017-03-19 12:24:29 -06003148 parseContext.addScopeMangler(*functionName);
John Kessenich8f9fdc92017-03-30 16:22:26 -06003149 functionName->append(name);
John Kessenich5f12d2f2017-03-11 09:39:55 -07003150 }
LoopDawg4886f692016-06-29 10:58:58 -06003151
John Kessenich54ee28f2017-03-11 14:13:00 -07003152 // function
3153 TFunction* function = new TFunction(functionName, TType(EbtVoid));
3154
3155 // arguments
John Kessenich54ee28f2017-03-11 14:13:00 -07003156 TIntermTyped* arguments = nullptr;
John Kessenichdfbdd9e2017-03-19 13:10:28 -06003157 if (baseObject != nullptr) {
3158 // Non-static member functions have an implicit first argument of the base object.
John Kessenich54ee28f2017-03-11 14:13:00 -07003159 parseContext.handleFunctionArgument(function, arguments, baseObject);
John Kessenichdfbdd9e2017-03-19 13:10:28 -06003160 }
John Kessenich4678ca92016-05-13 09:33:42 -06003161 if (! acceptArguments(function, arguments))
3162 return false;
3163
John Kessenich54ee28f2017-03-11 14:13:00 -07003164 // call
John Kessenich8f9fdc92017-03-30 16:22:26 -06003165 node = parseContext.handleFunctionCall(loc, function, arguments);
John Kessenich4678ca92016-05-13 09:33:42 -06003166
John Kessenichbb79abc2017-10-07 13:23:09 -06003167 return node != nullptr;
John Kessenich34fb0362016-05-03 23:17:20 -06003168}
3169
John Kessenich87142c72016-03-12 20:24:24 -07003170// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06003171// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07003172//
John Kessenichd016be12016-03-13 11:24:20 -06003173// The arguments are pushed onto the 'function' argument list and
3174// onto the 'arguments' aggregate.
3175//
John Kessenich4678ca92016-05-13 09:33:42 -06003176bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07003177{
John Kessenich078d7f22016-03-14 10:02:11 -06003178 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07003179 if (! acceptTokenClass(EHTokLeftParen))
3180 return false;
3181
John Kessenich2aa12b12017-04-18 14:47:33 -06003182 // RIGHT_PAREN
3183 if (acceptTokenClass(EHTokRightParen))
3184 return true;
3185
3186 // must now be at least one expression...
John Kessenich87142c72016-03-12 20:24:24 -07003187 do {
John Kessenichd016be12016-03-13 11:24:20 -06003188 // expression
John Kessenich87142c72016-03-12 20:24:24 -07003189 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06003190 if (! acceptAssignmentExpression(arg))
John Kessenich2aa12b12017-04-18 14:47:33 -06003191 return false;
John Kessenichd016be12016-03-13 11:24:20 -06003192
3193 // hook it up
3194 parseContext.handleFunctionArgument(function, arguments, arg);
3195
John Kessenich078d7f22016-03-14 10:02:11 -06003196 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07003197 if (! acceptTokenClass(EHTokComma))
3198 break;
3199 } while (true);
3200
John Kessenich078d7f22016-03-14 10:02:11 -06003201 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07003202 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06003203 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07003204 return false;
3205 }
3206
3207 return true;
3208}
3209
3210bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
3211{
3212 switch (token.tokenClass) {
3213 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003214 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003215 break;
steve-lunarg2de32912016-07-28 14:49:48 -06003216 case EHTokUintConstant:
3217 node = intermediate.addConstantUnion(token.u, token.loc, true);
3218 break;
John Kessenich87142c72016-03-12 20:24:24 -07003219 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003220 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003221 break;
3222 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003223 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003224 break;
3225 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06003226 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07003227 break;
John Kessenich86f71382016-09-19 20:23:18 -06003228 case EHTokStringConstant:
steve-lunarg858c9282017-01-07 08:54:10 -07003229 node = intermediate.addConstantUnion(token.string, token.loc, true);
John Kessenich86f71382016-09-19 20:23:18 -06003230 break;
John Kessenich87142c72016-03-12 20:24:24 -07003231
3232 default:
3233 return false;
3234 }
3235
3236 advanceToken();
3237
3238 return true;
3239}
3240
John Kessenich0e071192017-06-06 11:37:33 -06003241// simple_statement
3242// : SEMICOLON
3243// | declaration_statement
3244// | expression SEMICOLON
3245//
3246bool HlslGrammar::acceptSimpleStatement(TIntermNode*& statement)
3247{
3248 // SEMICOLON
3249 if (acceptTokenClass(EHTokSemicolon))
3250 return true;
3251
3252 // declaration
3253 if (acceptDeclaration(statement))
3254 return true;
3255
3256 // expression
3257 TIntermTyped* node;
3258 if (acceptExpression(node))
3259 statement = node;
3260 else
3261 return false;
3262
3263 // SEMICOLON (following an expression)
3264 if (acceptTokenClass(EHTokSemicolon))
3265 return true;
3266 else {
3267 expected(";");
3268 return false;
3269 }
3270}
3271
John Kessenich5f934b02016-03-13 17:58:25 -06003272// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06003273// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06003274//
John Kessenich21472ae2016-06-04 11:46:33 -06003275bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07003276{
John Kessenich21472ae2016-06-04 11:46:33 -06003277 TIntermAggregate* compoundStatement = nullptr;
3278
John Kessenich34fb0362016-05-03 23:17:20 -06003279 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06003280 if (! acceptTokenClass(EHTokLeftBrace))
3281 return false;
3282
3283 // statement statement ...
3284 TIntermNode* statement = nullptr;
3285 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06003286 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
3287 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
3288 branch->getFlowOp() == EOpDefault)) {
3289 // hook up individual subsequences within a switch statement
3290 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
3291 compoundStatement = nullptr;
3292 } else {
3293 // hook it up to the growing compound statement
3294 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
3295 }
John Kessenich5f934b02016-03-13 17:58:25 -06003296 }
John Kessenich34fb0362016-05-03 23:17:20 -06003297 if (compoundStatement)
3298 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06003299
John Kessenich21472ae2016-06-04 11:46:33 -06003300 retStatement = compoundStatement;
3301
John Kessenich34fb0362016-05-03 23:17:20 -06003302 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06003303 return acceptTokenClass(EHTokRightBrace);
3304}
3305
John Kessenich0d2b6de2016-06-05 11:23:11 -06003306bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
3307{
3308 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06003309 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003310 parseContext.popScope();
3311
3312 return result;
3313}
3314
John Kessenich077e0522016-06-09 02:02:17 -06003315bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003316{
John Kessenich077e0522016-06-09 02:02:17 -06003317 parseContext.pushScope();
3318 bool result = acceptCompoundStatement(statement);
3319 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06003320
3321 return result;
3322}
3323
John Kessenich5f934b02016-03-13 17:58:25 -06003324// statement
John Kessenich21472ae2016-06-04 11:46:33 -06003325// : attributes attributed_statement
3326//
3327// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003328// : compound_statement
John Kessenich0e071192017-06-06 11:37:33 -06003329// | simple_statement
John Kessenich21472ae2016-06-04 11:46:33 -06003330// | selection_statement
3331// | switch_statement
3332// | case_label
John Kessenich0e071192017-06-06 11:37:33 -06003333// | default_label
John Kessenich21472ae2016-06-04 11:46:33 -06003334// | iteration_statement
3335// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003336//
3337bool HlslGrammar::acceptStatement(TIntermNode*& statement)
3338{
John Kessenich21472ae2016-06-04 11:46:33 -06003339 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06003340
John Kessenich21472ae2016-06-04 11:46:33 -06003341 // attributes
steve-lunarg1868b142016-10-20 13:07:10 -06003342 TAttributeMap attributes;
3343 acceptAttributes(attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003344
John Kessenich21472ae2016-06-04 11:46:33 -06003345 // attributed_statement
3346 switch (peek()) {
3347 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06003348 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003349
John Kessenich21472ae2016-06-04 11:46:33 -06003350 case EHTokIf:
Rex Xu57e65922017-07-04 23:23:40 +08003351 return acceptSelectionStatement(statement, attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003352
John Kessenich21472ae2016-06-04 11:46:33 -06003353 case EHTokSwitch:
Rex Xu57e65922017-07-04 23:23:40 +08003354 return acceptSwitchStatement(statement, attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003355
John Kessenich21472ae2016-06-04 11:46:33 -06003356 case EHTokFor:
3357 case EHTokDo:
3358 case EHTokWhile:
steve-lunargf1709e72017-05-02 20:14:50 -06003359 return acceptIterationStatement(statement, attributes);
John Kessenich21472ae2016-06-04 11:46:33 -06003360
3361 case EHTokContinue:
3362 case EHTokBreak:
3363 case EHTokDiscard:
3364 case EHTokReturn:
3365 return acceptJumpStatement(statement);
3366
3367 case EHTokCase:
3368 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06003369 case EHTokDefault:
3370 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06003371
John Kessenich21472ae2016-06-04 11:46:33 -06003372 case EHTokRightBrace:
3373 // Performance: not strictly necessary, but stops a bunch of hunting early,
3374 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06003375 return false;
3376
John Kessenich21472ae2016-06-04 11:46:33 -06003377 default:
John Kessenich0e071192017-06-06 11:37:33 -06003378 return acceptSimpleStatement(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06003379 }
3380
John Kessenich5f934b02016-03-13 17:58:25 -06003381 return true;
John Kessenich87142c72016-03-12 20:24:24 -07003382}
3383
John Kessenich21472ae2016-06-04 11:46:33 -06003384// attributes
John Kessenich77ea30b2017-09-30 14:34:50 -06003385// : [zero or more:] bracketed-attribute
3386//
3387// bracketed-attribute:
3388// : LEFT_BRACKET scoped-attribute RIGHT_BRACKET
3389// : LEFT_BRACKET LEFT_BRACKET scoped-attribute RIGHT_BRACKET RIGHT_BRACKET
3390//
3391// scoped-attribute:
3392// : attribute
3393// | namespace COLON COLON attribute
John Kessenich21472ae2016-06-04 11:46:33 -06003394//
3395// attribute:
3396// : UNROLL
3397// | UNROLL LEFT_PAREN literal RIGHT_PAREN
3398// | FASTOPT
3399// | ALLOW_UAV_CONDITION
3400// | BRANCH
3401// | FLATTEN
3402// | FORCECASE
3403// | CALL
steve-lunarg1868b142016-10-20 13:07:10 -06003404// | DOMAIN
3405// | EARLYDEPTHSTENCIL
3406// | INSTANCE
3407// | MAXTESSFACTOR
3408// | OUTPUTCONTROLPOINTS
3409// | OUTPUTTOPOLOGY
3410// | PARTITIONING
3411// | PATCHCONSTANTFUNC
3412// | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
John Kessenich21472ae2016-06-04 11:46:33 -06003413//
steve-lunarg1868b142016-10-20 13:07:10 -06003414void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003415{
steve-lunarg1868b142016-10-20 13:07:10 -06003416 // For now, accept the [ XXX(X) ] syntax, but drop all but
3417 // numthreads, which is used to set the CS local size.
John Kessenich0d2b6de2016-06-05 11:23:11 -06003418 // TODO: subset to correct set? Pass on?
3419 do {
John Kessenich77ea30b2017-09-30 14:34:50 -06003420 HlslToken attributeToken;
steve-lunarg1868b142016-10-20 13:07:10 -06003421
John Kessenich0d2b6de2016-06-05 11:23:11 -06003422 // LEFT_BRACKET?
3423 if (! acceptTokenClass(EHTokLeftBracket))
3424 return;
John Kessenich77ea30b2017-09-30 14:34:50 -06003425 // another LEFT_BRACKET?
3426 bool doubleBrackets = false;
3427 if (acceptTokenClass(EHTokLeftBracket))
3428 doubleBrackets = true;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003429
John Kessenich77ea30b2017-09-30 14:34:50 -06003430 // attribute? (could be namespace; will adjust later)
3431 if (!acceptIdentifier(attributeToken)) {
3432 if (!peekTokenClass(EHTokRightBracket)) {
3433 expected("namespace or attribute identifier");
3434 advanceToken();
3435 }
3436 }
3437
3438 TString nameSpace;
3439 if (acceptTokenClass(EHTokColonColon)) {
3440 // namespace COLON COLON
3441 nameSpace = *attributeToken.string;
3442 // attribute
3443 if (!acceptIdentifier(attributeToken)) {
3444 expected("attribute identifier");
3445 return;
3446 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06003447 }
3448
steve-lunarga22f7db2016-11-11 08:17:44 -07003449 TIntermAggregate* expressions = nullptr;
steve-lunarg1868b142016-10-20 13:07:10 -06003450
3451 // (x, ...)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003452 if (acceptTokenClass(EHTokLeftParen)) {
steve-lunarga22f7db2016-11-11 08:17:44 -07003453 expressions = new TIntermAggregate;
steve-lunarg1868b142016-10-20 13:07:10 -06003454
John Kessenich0d2b6de2016-06-05 11:23:11 -06003455 TIntermTyped* node;
steve-lunarga22f7db2016-11-11 08:17:44 -07003456 bool expectingExpression = false;
John Kessenichecba76f2017-01-06 00:34:48 -07003457
steve-lunarga22f7db2016-11-11 08:17:44 -07003458 while (acceptAssignmentExpression(node)) {
3459 expectingExpression = false;
3460 expressions->getSequence().push_back(node);
steve-lunarg1868b142016-10-20 13:07:10 -06003461 if (acceptTokenClass(EHTokComma))
steve-lunarga22f7db2016-11-11 08:17:44 -07003462 expectingExpression = true;
steve-lunarg1868b142016-10-20 13:07:10 -06003463 }
3464
steve-lunarga22f7db2016-11-11 08:17:44 -07003465 // 'expressions' is an aggregate with the expressions in it
John Kessenich0d2b6de2016-06-05 11:23:11 -06003466 if (! acceptTokenClass(EHTokRightParen))
3467 expected(")");
steve-lunarga22f7db2016-11-11 08:17:44 -07003468
3469 // Error for partial or missing expression
3470 if (expectingExpression || expressions->getSequence().empty())
3471 expected("expression");
John Kessenich0d2b6de2016-06-05 11:23:11 -06003472 }
3473
3474 // RIGHT_BRACKET
steve-lunarg1868b142016-10-20 13:07:10 -06003475 if (!acceptTokenClass(EHTokRightBracket)) {
3476 expected("]");
3477 return;
3478 }
John Kessenich77ea30b2017-09-30 14:34:50 -06003479 // another RIGHT_BRACKET?
3480 if (doubleBrackets && !acceptTokenClass(EHTokRightBracket)) {
3481 expected("]]");
3482 return;
3483 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06003484
steve-lunarg1868b142016-10-20 13:07:10 -06003485 // Add any values we found into the attribute map. This accepts
3486 // (and ignores) values not mapping to a known TAttributeType;
John Kessenich77ea30b2017-09-30 14:34:50 -06003487 attributes.setAttribute(nameSpace, attributeToken.string, expressions);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003488 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06003489}
3490
John Kessenich0d2b6de2016-06-05 11:23:11 -06003491// selection_statement
3492// : IF LEFT_PAREN expression RIGHT_PAREN statement
3493// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
3494//
Rex Xu57e65922017-07-04 23:23:40 +08003495bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement, const TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003496{
John Kessenich0d2b6de2016-06-05 11:23:11 -06003497 TSourceLoc loc = token.loc;
3498
Rex Xu57e65922017-07-04 23:23:40 +08003499 const TSelectionControl control = parseContext.handleSelectionControl(attributes);
3500
John Kessenich0d2b6de2016-06-05 11:23:11 -06003501 // IF
3502 if (! acceptTokenClass(EHTokIf))
3503 return false;
3504
3505 // so that something declared in the condition is scoped to the lifetimes
3506 // of the then-else statements
3507 parseContext.pushScope();
3508
3509 // LEFT_PAREN expression RIGHT_PAREN
3510 TIntermTyped* condition;
3511 if (! acceptParenExpression(condition))
3512 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003513 condition = parseContext.convertConditionalExpression(loc, condition);
3514 if (condition == nullptr)
3515 return false;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003516
3517 // create the child statements
3518 TIntermNodePair thenElse = { nullptr, nullptr };
3519
John Kessenichf6deacd2017-06-06 19:52:55 -06003520 ++parseContext.controlFlowNestingLevel; // this only needs to work right if no errors
3521
John Kessenich0d2b6de2016-06-05 11:23:11 -06003522 // then statement
3523 if (! acceptScopedStatement(thenElse.node1)) {
3524 expected("then statement");
3525 return false;
3526 }
3527
3528 // ELSE
3529 if (acceptTokenClass(EHTokElse)) {
3530 // else statement
3531 if (! acceptScopedStatement(thenElse.node2)) {
3532 expected("else statement");
3533 return false;
3534 }
3535 }
3536
3537 // Put the pieces together
Rex Xu57e65922017-07-04 23:23:40 +08003538 statement = intermediate.addSelection(condition, thenElse, loc, control);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003539 parseContext.popScope();
John Kessenichf6deacd2017-06-06 19:52:55 -06003540 --parseContext.controlFlowNestingLevel;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003541
3542 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003543}
3544
John Kessenichd02dc5d2016-07-01 00:04:11 -06003545// switch_statement
3546// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
3547//
Rex Xu57e65922017-07-04 23:23:40 +08003548bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement, const TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003549{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003550 // SWITCH
3551 TSourceLoc loc = token.loc;
Rex Xu57e65922017-07-04 23:23:40 +08003552
3553 const TSelectionControl control = parseContext.handleSelectionControl(attributes);
3554
John Kessenichd02dc5d2016-07-01 00:04:11 -06003555 if (! acceptTokenClass(EHTokSwitch))
3556 return false;
3557
3558 // LEFT_PAREN expression RIGHT_PAREN
3559 parseContext.pushScope();
3560 TIntermTyped* switchExpression;
3561 if (! acceptParenExpression(switchExpression)) {
3562 parseContext.popScope();
3563 return false;
3564 }
3565
3566 // compound_statement
3567 parseContext.pushSwitchSequence(new TIntermSequence);
John Kessenichf6deacd2017-06-06 19:52:55 -06003568
3569 ++parseContext.controlFlowNestingLevel;
John Kessenichd02dc5d2016-07-01 00:04:11 -06003570 bool statementOkay = acceptCompoundStatement(statement);
John Kessenichf6deacd2017-06-06 19:52:55 -06003571 --parseContext.controlFlowNestingLevel;
3572
John Kessenichd02dc5d2016-07-01 00:04:11 -06003573 if (statementOkay)
Rex Xu57e65922017-07-04 23:23:40 +08003574 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr, control);
John Kessenichd02dc5d2016-07-01 00:04:11 -06003575
3576 parseContext.popSwitchSequence();
3577 parseContext.popScope();
3578
3579 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06003580}
3581
John Kessenich119f8f62016-06-05 15:44:07 -06003582// iteration_statement
3583// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
3584// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
3585// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
3586//
3587// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
steve-lunargf1709e72017-05-02 20:14:50 -06003588bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement, const TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003589{
John Kessenich119f8f62016-06-05 15:44:07 -06003590 TSourceLoc loc = token.loc;
3591 TIntermTyped* condition = nullptr;
3592
3593 EHlslTokenClass loop = peek();
3594 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
3595
3596 // WHILE or DO or FOR
3597 advanceToken();
steve-lunargf1709e72017-05-02 20:14:50 -06003598
3599 const TLoopControl control = parseContext.handleLoopControl(attributes);
John Kessenich119f8f62016-06-05 15:44:07 -06003600
3601 switch (loop) {
3602 case EHTokWhile:
3603 // so that something declared in the condition is scoped to the lifetime
3604 // of the while sub-statement
John Kessenichf6deacd2017-06-06 19:52:55 -06003605 parseContext.pushScope(); // this only needs to work right if no errors
John Kessenich119f8f62016-06-05 15:44:07 -06003606 parseContext.nestLooping();
John Kessenichf6deacd2017-06-06 19:52:55 -06003607 ++parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003608
3609 // LEFT_PAREN condition RIGHT_PAREN
3610 if (! acceptParenExpression(condition))
3611 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003612 condition = parseContext.convertConditionalExpression(loc, condition);
3613 if (condition == nullptr)
3614 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003615
3616 // statement
3617 if (! acceptScopedStatement(statement)) {
3618 expected("while sub-statement");
3619 return false;
3620 }
3621
3622 parseContext.unnestLooping();
3623 parseContext.popScope();
John Kessenichf6deacd2017-06-06 19:52:55 -06003624 --parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003625
steve-lunargf1709e72017-05-02 20:14:50 -06003626 statement = intermediate.addLoop(statement, condition, nullptr, true, loc, control);
John Kessenich119f8f62016-06-05 15:44:07 -06003627
3628 return true;
3629
3630 case EHTokDo:
John Kessenichf6deacd2017-06-06 19:52:55 -06003631 parseContext.nestLooping(); // this only needs to work right if no errors
3632 ++parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003633
John Kessenich119f8f62016-06-05 15:44:07 -06003634 // statement
John Kessenich0c6f9362017-04-20 11:08:24 -06003635 if (! acceptScopedStatement(statement)) {
John Kessenich119f8f62016-06-05 15:44:07 -06003636 expected("do sub-statement");
3637 return false;
3638 }
3639
John Kessenich119f8f62016-06-05 15:44:07 -06003640 // WHILE
3641 if (! acceptTokenClass(EHTokWhile)) {
3642 expected("while");
3643 return false;
3644 }
3645
3646 // LEFT_PAREN condition RIGHT_PAREN
3647 TIntermTyped* condition;
3648 if (! acceptParenExpression(condition))
3649 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003650 condition = parseContext.convertConditionalExpression(loc, condition);
3651 if (condition == nullptr)
3652 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003653
3654 if (! acceptTokenClass(EHTokSemicolon))
3655 expected(";");
3656
3657 parseContext.unnestLooping();
John Kessenichf6deacd2017-06-06 19:52:55 -06003658 --parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003659
steve-lunargf1709e72017-05-02 20:14:50 -06003660 statement = intermediate.addLoop(statement, condition, 0, false, loc, control);
John Kessenich119f8f62016-06-05 15:44:07 -06003661
3662 return true;
3663
3664 case EHTokFor:
3665 {
3666 // LEFT_PAREN
3667 if (! acceptTokenClass(EHTokLeftParen))
3668 expected("(");
3669
3670 // so that something declared in the condition is scoped to the lifetime
3671 // of the for sub-statement
3672 parseContext.pushScope();
3673
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003674 // initializer
3675 TIntermNode* initNode = nullptr;
John Kessenich0e071192017-06-06 11:37:33 -06003676 if (! acceptSimpleStatement(initNode))
3677 expected("for-loop initializer statement");
John Kessenich119f8f62016-06-05 15:44:07 -06003678
John Kessenichf6deacd2017-06-06 19:52:55 -06003679 parseContext.nestLooping(); // this only needs to work right if no errors
3680 ++parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003681
3682 // condition SEMI_COLON
3683 acceptExpression(condition);
3684 if (! acceptTokenClass(EHTokSemicolon))
3685 expected(";");
John Kessenich7e997e22017-03-30 22:09:30 -06003686 if (condition != nullptr) {
3687 condition = parseContext.convertConditionalExpression(loc, condition);
3688 if (condition == nullptr)
3689 return false;
3690 }
John Kessenich119f8f62016-06-05 15:44:07 -06003691
3692 // iterator SEMI_COLON
3693 TIntermTyped* iterator = nullptr;
3694 acceptExpression(iterator);
3695 if (! acceptTokenClass(EHTokRightParen))
3696 expected(")");
3697
3698 // statement
3699 if (! acceptScopedStatement(statement)) {
3700 expected("for sub-statement");
3701 return false;
3702 }
3703
steve-lunargf1709e72017-05-02 20:14:50 -06003704 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc, control);
John Kessenich119f8f62016-06-05 15:44:07 -06003705
3706 parseContext.popScope();
3707 parseContext.unnestLooping();
John Kessenichf6deacd2017-06-06 19:52:55 -06003708 --parseContext.controlFlowNestingLevel;
John Kessenich119f8f62016-06-05 15:44:07 -06003709
3710 return true;
3711 }
3712
3713 default:
3714 return false;
3715 }
John Kessenich21472ae2016-06-04 11:46:33 -06003716}
3717
3718// jump_statement
3719// : CONTINUE SEMICOLON
3720// | BREAK SEMICOLON
3721// | DISCARD SEMICOLON
3722// | RETURN SEMICOLON
3723// | RETURN expression SEMICOLON
3724//
3725bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
3726{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003727 EHlslTokenClass jump = peek();
3728 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06003729 case EHTokContinue:
3730 case EHTokBreak:
3731 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06003732 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003733 advanceToken();
3734 break;
John Kessenich21472ae2016-06-04 11:46:33 -06003735 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003736 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06003737 return false;
3738 }
John Kessenich21472ae2016-06-04 11:46:33 -06003739
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003740 switch (jump) {
3741 case EHTokContinue:
3742 statement = intermediate.addBranch(EOpContinue, token.loc);
3743 break;
3744 case EHTokBreak:
3745 statement = intermediate.addBranch(EOpBreak, token.loc);
3746 break;
3747 case EHTokDiscard:
3748 statement = intermediate.addBranch(EOpKill, token.loc);
3749 break;
3750
3751 case EHTokReturn:
3752 {
3753 // expression
3754 TIntermTyped* node;
3755 if (acceptExpression(node)) {
3756 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06003757 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003758 } else
3759 statement = intermediate.addBranch(EOpReturn, token.loc);
3760 break;
3761 }
3762
3763 default:
3764 assert(0);
3765 return false;
3766 }
3767
3768 // SEMICOLON
3769 if (! acceptTokenClass(EHTokSemicolon))
3770 expected(";");
John Kessenichecba76f2017-01-06 00:34:48 -07003771
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003772 return true;
3773}
John Kessenich21472ae2016-06-04 11:46:33 -06003774
John Kessenichd02dc5d2016-07-01 00:04:11 -06003775// case_label
3776// : CASE expression COLON
3777//
John Kessenich21472ae2016-06-04 11:46:33 -06003778bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
3779{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003780 TSourceLoc loc = token.loc;
3781 if (! acceptTokenClass(EHTokCase))
3782 return false;
3783
3784 TIntermTyped* expression;
3785 if (! acceptExpression(expression)) {
3786 expected("case expression");
3787 return false;
3788 }
3789
3790 if (! acceptTokenClass(EHTokColon)) {
3791 expected(":");
3792 return false;
3793 }
3794
3795 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
3796
3797 return true;
3798}
3799
3800// default_label
3801// : DEFAULT COLON
3802//
3803bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
3804{
3805 TSourceLoc loc = token.loc;
3806 if (! acceptTokenClass(EHTokDefault))
3807 return false;
3808
3809 if (! acceptTokenClass(EHTokColon)) {
3810 expected(":");
3811 return false;
3812 }
3813
3814 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
3815
3816 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003817}
3818
John Kessenich19b92ff2016-06-19 11:50:34 -06003819// array_specifier
steve-lunarg7b211a32016-10-13 12:26:18 -06003820// : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
3821// : LEFT_BRACKET RGHT_BRACKET // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06003822//
3823void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
3824{
3825 arraySizes = nullptr;
3826
steve-lunarg7b211a32016-10-13 12:26:18 -06003827 // Early-out if there aren't any array dimensions
3828 if (!peekTokenClass(EHTokLeftBracket))
John Kessenich19b92ff2016-06-19 11:50:34 -06003829 return;
3830
steve-lunarg7b211a32016-10-13 12:26:18 -06003831 // 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 -06003832 arraySizes = new TArraySizes;
steve-lunarg7b211a32016-10-13 12:26:18 -06003833
3834 // Collect each array dimension.
3835 while (acceptTokenClass(EHTokLeftBracket)) {
3836 TSourceLoc loc = token.loc;
3837 TIntermTyped* sizeExpr = nullptr;
3838
John Kessenich057df292017-03-06 18:18:37 -07003839 // Array sizing expression is optional. If omitted, array will be later sized by initializer list.
steve-lunarg7b211a32016-10-13 12:26:18 -06003840 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
3841
3842 if (! acceptTokenClass(EHTokRightBracket)) {
3843 expected("]");
3844 return;
3845 }
3846
3847 if (hasArraySize) {
3848 TArraySize arraySize;
3849 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
3850 arraySizes->addInnerSize(arraySize);
3851 } else {
3852 arraySizes->addInnerSize(0); // sized by initializers.
3853 }
steve-lunarg265c0612016-09-27 10:57:35 -06003854 }
John Kessenich19b92ff2016-06-19 11:50:34 -06003855}
3856
John Kessenich630dd7d2016-06-12 23:52:12 -06003857// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06003858// : COLON semantic // optional
3859// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
3860// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06003861// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06003862// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06003863//
John Kessenich854fe242017-03-02 14:30:59 -07003864// Return true if any tokens were accepted. That is,
3865// false can be returned on successfully recognizing nothing,
3866// not necessarily meaning bad syntax.
3867//
3868bool HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06003869{
John Kessenich854fe242017-03-02 14:30:59 -07003870 bool found = false;
3871
John Kessenich630dd7d2016-06-12 23:52:12 -06003872 do {
John Kessenichecba76f2017-01-06 00:34:48 -07003873 // COLON
John Kessenich630dd7d2016-06-12 23:52:12 -06003874 if (acceptTokenClass(EHTokColon)) {
John Kessenich854fe242017-03-02 14:30:59 -07003875 found = true;
John Kessenich630dd7d2016-06-12 23:52:12 -06003876 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06003877 if (peekTokenClass(EHTokLayout))
3878 acceptLayoutQualifierList(qualifier);
3879 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06003880 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003881 if (! acceptTokenClass(EHTokLeftParen)) {
3882 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003883 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003884 }
John Kessenich82d6baf2016-07-29 13:03:05 -06003885 HlslToken locationToken;
3886 if (! acceptIdentifier(locationToken)) {
3887 expected("c[subcomponent][.component]");
John Kessenich854fe242017-03-02 14:30:59 -07003888 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003889 }
3890 HlslToken componentToken;
3891 if (acceptTokenClass(EHTokDot)) {
3892 if (! acceptIdentifier(componentToken)) {
3893 expected("component");
John Kessenich854fe242017-03-02 14:30:59 -07003894 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003895 }
3896 }
John Kessenich630dd7d2016-06-12 23:52:12 -06003897 if (! acceptTokenClass(EHTokRightParen)) {
3898 expected(")");
3899 break;
3900 }
John Kessenich7735b942016-09-05 12:40:06 -06003901 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003902 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06003903 expected("layout, semantic, packoffset, or register");
John Kessenich854fe242017-03-02 14:30:59 -07003904 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003905 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06003906 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
3907 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003908 if (! acceptTokenClass(EHTokLeftParen)) {
3909 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003910 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003911 }
John Kessenichb38f0712016-07-30 10:29:54 -06003912 HlslToken registerDesc; // for Type#
3913 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06003914 if (! acceptIdentifier(registerDesc)) {
3915 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003916 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003917 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003918 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
3919 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06003920 // Then we didn't really see the registerDesc yet, it was
3921 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06003922 profile = registerDesc;
3923 if (! acceptIdentifier(registerDesc)) {
3924 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003925 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003926 }
3927 }
John Kessenichb38f0712016-07-30 10:29:54 -06003928 int subComponent = 0;
3929 if (acceptTokenClass(EHTokLeftBracket)) {
3930 // LEFT_BRACKET subcomponent RIGHT_BRACKET
3931 if (! peekTokenClass(EHTokIntConstant)) {
3932 expected("literal integer");
John Kessenich854fe242017-03-02 14:30:59 -07003933 return false;
John Kessenichb38f0712016-07-30 10:29:54 -06003934 }
3935 subComponent = token.i;
3936 advanceToken();
3937 if (! acceptTokenClass(EHTokRightBracket)) {
3938 expected("]");
3939 break;
3940 }
3941 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003942 // (COMMA SPACEN)opt
3943 HlslToken spaceDesc;
3944 if (acceptTokenClass(EHTokComma)) {
3945 if (! acceptIdentifier(spaceDesc)) {
3946 expected ("space identifier");
John Kessenich854fe242017-03-02 14:30:59 -07003947 return false;
John Kessenichcfd7ce82016-09-05 16:03:12 -06003948 }
3949 }
3950 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003951 if (! acceptTokenClass(EHTokRightParen)) {
3952 expected(")");
3953 break;
3954 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003955 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003956 } else {
3957 // semantic, in idToken.string
John Kessenich2dd643f2017-03-14 21:50:06 -06003958 TString semanticUpperCase = *idToken.string;
3959 std::transform(semanticUpperCase.begin(), semanticUpperCase.end(), semanticUpperCase.begin(), ::toupper);
3960 parseContext.handleSemantic(idToken.loc, qualifier, mapSemantic(semanticUpperCase.c_str()), semanticUpperCase);
John Kessenich630dd7d2016-06-12 23:52:12 -06003961 }
John Kessenich854fe242017-03-02 14:30:59 -07003962 } else if (peekTokenClass(EHTokLeftAngle)) {
3963 found = true;
John Kessenicha1e2d492016-09-20 13:22:58 -06003964 acceptAnnotations(qualifier);
John Kessenich854fe242017-03-02 14:30:59 -07003965 } else
John Kessenich630dd7d2016-06-12 23:52:12 -06003966 break;
John Kessenich078d7f22016-03-14 10:02:11 -06003967
John Kessenich630dd7d2016-06-12 23:52:12 -06003968 } while (true);
John Kessenich854fe242017-03-02 14:30:59 -07003969
3970 return found;
John Kessenich078d7f22016-03-14 10:02:11 -06003971}
3972
John Kessenichb16f7e62017-03-11 19:32:47 -07003973//
3974// Get the stream of tokens from the scanner, but skip all syntactic/semantic
3975// processing.
3976//
3977bool HlslGrammar::captureBlockTokens(TVector<HlslToken>& tokens)
3978{
3979 if (! peekTokenClass(EHTokLeftBrace))
3980 return false;
3981
3982 int braceCount = 0;
3983
3984 do {
3985 switch (peek()) {
3986 case EHTokLeftBrace:
3987 ++braceCount;
3988 break;
3989 case EHTokRightBrace:
3990 --braceCount;
3991 break;
3992 case EHTokNone:
3993 // End of input before balance { } is bad...
3994 return false;
3995 default:
3996 break;
3997 }
3998
3999 tokens.push_back(token);
4000 advanceToken();
4001 } while (braceCount > 0);
4002
4003 return true;
4004}
4005
John Kessenich0320d092017-06-13 22:22:52 -06004006// Return a string for just the types that can also be declared as an identifier.
4007const char* HlslGrammar::getTypeString(EHlslTokenClass tokenClass) const
4008{
4009 switch (tokenClass) {
4010 case EHTokSample: return "sample";
4011 case EHTokHalf: return "half";
4012 case EHTokHalf1x1: return "half1x1";
4013 case EHTokHalf1x2: return "half1x2";
4014 case EHTokHalf1x3: return "half1x3";
4015 case EHTokHalf1x4: return "half1x4";
4016 case EHTokHalf2x1: return "half2x1";
4017 case EHTokHalf2x2: return "half2x2";
4018 case EHTokHalf2x3: return "half2x3";
4019 case EHTokHalf2x4: return "half2x4";
4020 case EHTokHalf3x1: return "half3x1";
4021 case EHTokHalf3x2: return "half3x2";
4022 case EHTokHalf3x3: return "half3x3";
4023 case EHTokHalf3x4: return "half3x4";
4024 case EHTokHalf4x1: return "half4x1";
4025 case EHTokHalf4x2: return "half4x2";
4026 case EHTokHalf4x3: return "half4x3";
4027 case EHTokHalf4x4: return "half4x4";
4028 case EHTokBool: return "bool";
4029 case EHTokFloat: return "float";
4030 case EHTokDouble: return "double";
4031 case EHTokInt: return "int";
4032 case EHTokUint: return "uint";
4033 case EHTokMin16float: return "min16float";
4034 case EHTokMin10float: return "min10float";
4035 case EHTokMin16int: return "min16int";
4036 case EHTokMin12int: return "min12int";
4037 default:
4038 return nullptr;
4039 }
4040}
4041
John Kesseniche01a9bc2016-03-12 20:11:22 -07004042} // end namespace glslang