blob: b9d9a3a1351bd05d03b79f0120e007eb38baff75 [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
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700110 TString* idString = nullptr;
111 switch (peek()) {
112 case EHTokSample: idString = NewPoolTString("sample"); break;
113 case EHTokHalf: idString = NewPoolTString("half"); break;
114 case EHTokBool: idString = NewPoolTString("bool"); break;
115 case EHTokFloat: idString = NewPoolTString("float"); break;
116 case EHTokDouble: idString = NewPoolTString("double"); break;
117 case EHTokInt: idString = NewPoolTString("int"); break;
118 case EHTokUint: idString = NewPoolTString("uint"); break;
119 case EHTokMin16float: idString = NewPoolTString("min16float"); break;
120 case EHTokMin10float: idString = NewPoolTString("min10float"); break;
121 case EHTokMin16int: idString = NewPoolTString("min16int"); break;
122 case EHTokMin12int: idString = NewPoolTString("min12int"); break;
123 default:
124 return false;
steve-lunarg75fd2232016-11-16 13:22:11 -0700125 }
126
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700127 token.string = idString;
128 token.tokenClass = EHTokIdentifier;
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700129 idToken = token;
130
131 advanceToken();
132
133 return true;
John Kessenichaecd4972016-03-14 10:46:34 -0600134}
135
John Kesseniche01a9bc2016-03-12 20:11:22 -0700136// compilationUnit
John Kessenich8f9fdc92017-03-30 16:22:26 -0600137// : declaration_list EOF
John Kesseniche01a9bc2016-03-12 20:11:22 -0700138//
139bool HlslGrammar::acceptCompilationUnit()
140{
John Kessenichd016be12016-03-13 11:24:20 -0600141 TIntermNode* unitNode = nullptr;
142
John Kessenich8f9fdc92017-03-30 16:22:26 -0600143 if (! acceptDeclarationList(unitNode))
144 return false;
steve-lunargcb88de52016-08-03 07:04:18 -0600145
John Kessenich8f9fdc92017-03-30 16:22:26 -0600146 if (! peekTokenClass(EHTokNone))
147 return false;
John Kesseniche01a9bc2016-03-12 20:11:22 -0700148
John Kessenichd016be12016-03-13 11:24:20 -0600149 // set root of AST
John Kessenichca71d942017-03-07 20:44:09 -0700150 if (unitNode && !unitNode->getAsAggregate())
151 unitNode = intermediate.growAggregate(nullptr, unitNode);
John Kessenich078d7f22016-03-14 10:02:11 -0600152 intermediate.setTreeRoot(unitNode);
John Kessenichd016be12016-03-13 11:24:20 -0600153
John Kesseniche01a9bc2016-03-12 20:11:22 -0700154 return true;
155}
156
John Kessenich8f9fdc92017-03-30 16:22:26 -0600157// Recognize the following, but with the extra condition that it can be
158// successfully terminated by EOF or '}'.
159//
160// declaration_list
161// : list of declaration_or_semicolon followed by EOF or RIGHT_BRACE
162//
163// declaration_or_semicolon
164// : declaration
165// : SEMICOLON
166//
167bool HlslGrammar::acceptDeclarationList(TIntermNode*& nodeList)
168{
169 do {
170 // HLSL allows extra semicolons between global declarations
171 do { } while (acceptTokenClass(EHTokSemicolon));
172
173 // EOF or RIGHT_BRACE
174 if (peekTokenClass(EHTokNone) || peekTokenClass(EHTokRightBrace))
175 return true;
176
177 // declaration
178 if (! acceptDeclaration(nodeList))
179 return false;
180 } while (true);
181
182 return true;
183}
184
LoopDawg4886f692016-06-29 10:58:58 -0600185// sampler_state
John Kessenichecba76f2017-01-06 00:34:48 -0700186// : LEFT_BRACE [sampler_state_assignment ... ] RIGHT_BRACE
LoopDawg4886f692016-06-29 10:58:58 -0600187//
188// sampler_state_assignment
189// : sampler_state_identifier EQUAL value SEMICOLON
190//
191// sampler_state_identifier
192// : ADDRESSU
193// | ADDRESSV
194// | ADDRESSW
195// | BORDERCOLOR
196// | FILTER
197// | MAXANISOTROPY
198// | MAXLOD
199// | MINLOD
200// | MIPLODBIAS
201//
202bool HlslGrammar::acceptSamplerState()
203{
204 // TODO: this should be genericized to accept a list of valid tokens and
205 // return token/value pairs. Presently it is specific to texture values.
206
207 if (! acceptTokenClass(EHTokLeftBrace))
208 return true;
209
210 parseContext.warn(token.loc, "unimplemented", "immediate sampler state", "");
John Kessenichecba76f2017-01-06 00:34:48 -0700211
LoopDawg4886f692016-06-29 10:58:58 -0600212 do {
213 // read state name
214 HlslToken state;
215 if (! acceptIdentifier(state))
216 break; // end of list
217
218 // FXC accepts any case
219 TString stateName = *state.string;
220 std::transform(stateName.begin(), stateName.end(), stateName.begin(), ::tolower);
221
222 if (! acceptTokenClass(EHTokAssign)) {
223 expected("assign");
224 return false;
225 }
226
227 if (stateName == "minlod" || stateName == "maxlod") {
228 if (! peekTokenClass(EHTokIntConstant)) {
229 expected("integer");
230 return false;
231 }
232
233 TIntermTyped* lod = nullptr;
234 if (! acceptLiteral(lod)) // should never fail, since we just looked for an integer
235 return false;
236 } else if (stateName == "maxanisotropy") {
237 if (! peekTokenClass(EHTokIntConstant)) {
238 expected("integer");
239 return false;
240 }
241
242 TIntermTyped* maxAnisotropy = nullptr;
243 if (! acceptLiteral(maxAnisotropy)) // should never fail, since we just looked for an integer
244 return false;
245 } else if (stateName == "filter") {
246 HlslToken filterMode;
247 if (! acceptIdentifier(filterMode)) {
248 expected("filter mode");
249 return false;
250 }
251 } else if (stateName == "addressu" || stateName == "addressv" || stateName == "addressw") {
252 HlslToken addrMode;
253 if (! acceptIdentifier(addrMode)) {
254 expected("texture address mode");
255 return false;
256 }
257 } else if (stateName == "miplodbias") {
258 TIntermTyped* lodBias = nullptr;
259 if (! acceptLiteral(lodBias)) {
260 expected("lod bias");
261 return false;
262 }
263 } else if (stateName == "bordercolor") {
264 return false;
265 } else {
266 expected("texture state");
267 return false;
268 }
269
270 // SEMICOLON
271 if (! acceptTokenClass(EHTokSemicolon)) {
272 expected("semicolon");
273 return false;
274 }
275 } while (true);
276
277 if (! acceptTokenClass(EHTokRightBrace))
278 return false;
279
280 return true;
281}
282
283// sampler_declaration_dx9
284// : SAMPLER identifier EQUAL sampler_type sampler_state
285//
John Kesseniche4821e42016-07-16 10:19:43 -0600286bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/)
LoopDawg4886f692016-06-29 10:58:58 -0600287{
288 if (! acceptTokenClass(EHTokSampler))
289 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700290
LoopDawg4886f692016-06-29 10:58:58 -0600291 // TODO: remove this when DX9 style declarations are implemented.
292 unimplemented("Direct3D 9 sampler declaration");
293
294 // read sampler name
295 HlslToken name;
296 if (! acceptIdentifier(name)) {
297 expected("sampler name");
298 return false;
299 }
300
301 if (! acceptTokenClass(EHTokAssign)) {
302 expected("=");
303 return false;
304 }
305
306 return false;
307}
308
John Kesseniche01a9bc2016-03-12 20:11:22 -0700309// declaration
LoopDawg4886f692016-06-29 10:58:58 -0600310// : sampler_declaration_dx9 post_decls SEMICOLON
311// | fully_specified_type declarator_list SEMICOLON
John Kessenich630dd7d2016-06-12 23:52:12 -0600312// | fully_specified_type identifier function_parameters post_decls compound_statement // function definition
LoopDawg4886f692016-06-29 10:58:58 -0600313// | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition
John Kessenich5e69ec62016-07-05 00:02:40 -0600314// | typedef declaration
John Kessenich8f9fdc92017-03-30 16:22:26 -0600315// | NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
John Kessenich87142c72016-03-12 20:24:24 -0700316//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600317// declarator_list
318// : declarator COMMA declarator COMMA declarator... // zero or more declarators
John Kessenich532543c2016-07-01 19:06:44 -0600319//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600320// declarator
John Kessenich532543c2016-07-01 19:06:44 -0600321// : identifier array_specifier post_decls
322// | identifier array_specifier post_decls EQUAL assignment_expression
John Kessenichd5ed0b62016-07-04 17:32:45 -0600323// | identifier function_parameters post_decls // function prototype
John Kessenich532543c2016-07-01 19:06:44 -0600324//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600325// Parsing has to go pretty far in to know whether it's a variable, prototype, or
326// function definition, so the implementation below doesn't perfectly divide up the grammar
John Kessenich532543c2016-07-01 19:06:44 -0600327// as above. (The 'identifier' in the first item in init_declarator list is the
328// same as 'identifier' for function declarations.)
329//
John Kessenichca71d942017-03-07 20:44:09 -0700330// This can generate more than one subtree, one per initializer or a function body.
331// All initializer subtrees are put in their own aggregate node, making one top-level
332// node for all the initializers. Each function created is a top-level node to grow
333// into the passed-in nodeList.
John Kessenichd016be12016-03-13 11:24:20 -0600334//
John Kessenichca71d942017-03-07 20:44:09 -0700335// If 'nodeList' is passed in as non-null, it must an aggregate to extend for
336// each top-level node the declaration creates. Otherwise, if only one top-level
337// node in generated here, that is want is returned in nodeList.
John Kessenich02467d82017-01-19 15:41:47 -0700338//
John Kessenichca71d942017-03-07 20:44:09 -0700339bool HlslGrammar::acceptDeclaration(TIntermNode*& nodeList)
John Kesseniche01a9bc2016-03-12 20:11:22 -0700340{
John Kessenich8f9fdc92017-03-30 16:22:26 -0600341 // NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
342 if (acceptTokenClass(EHTokNamespace)) {
343 HlslToken namespaceToken;
344 if (!acceptIdentifier(namespaceToken)) {
345 expected("namespace name");
346 return false;
347 }
348 parseContext.pushNamespace(*namespaceToken.string);
349 if (!acceptTokenClass(EHTokLeftBrace)) {
350 expected("{");
351 return false;
352 }
353 if (!acceptDeclarationList(nodeList)) {
354 expected("declaration list");
355 return false;
356 }
357 if (!acceptTokenClass(EHTokRightBrace)) {
358 expected("}");
359 return false;
360 }
361 parseContext.popNamespace();
362 return true;
363 }
364
John Kessenich54ee28f2017-03-11 14:13:00 -0700365 bool declarator_list = false; // true when processing comma separation
John Kessenichd016be12016-03-13 11:24:20 -0600366
steve-lunarg1868b142016-10-20 13:07:10 -0600367 // attributes
John Kessenich088d52b2017-03-11 17:55:28 -0700368 TFunctionDeclarator declarator;
369 acceptAttributes(declarator.attributes);
steve-lunarg1868b142016-10-20 13:07:10 -0600370
John Kessenich5e69ec62016-07-05 00:02:40 -0600371 // typedef
372 bool typedefDecl = acceptTokenClass(EHTokTypedef);
373
John Kesseniche82061d2016-09-27 14:38:57 -0600374 TType declaredType;
LoopDawg4886f692016-06-29 10:58:58 -0600375
376 // DX9 sampler declaration use a different syntax
John Kessenich267590d2016-08-05 17:34:34 -0600377 // DX9 shaders need to run through HLSL compiler (fxc) via a back compat mode, it isn't going to
378 // be possible to simultaneously compile D3D10+ style shaders and DX9 shaders. If we want to compile DX9
379 // HLSL shaders, this will have to be a master level switch
380 // As such, the sampler keyword in D3D10+ turns into an automatic sampler type, and is commonly used
John Kessenichecba76f2017-01-06 00:34:48 -0700381 // For that reason, this line is commented out
John Kessenichca71d942017-03-07 20:44:09 -0700382 // if (acceptSamplerDeclarationDX9(declaredType))
383 // return true;
LoopDawg4886f692016-06-29 10:58:58 -0600384
385 // fully_specified_type
John Kessenich54ee28f2017-03-11 14:13:00 -0700386 if (! acceptFullySpecifiedType(declaredType, nodeList))
John Kessenich87142c72016-03-12 20:24:24 -0700387 return false;
LoopDawg4886f692016-06-29 10:58:58 -0600388
John Kessenich87142c72016-03-12 20:24:24 -0700389 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -0600390 HlslToken idToken;
John Kessenichca71d942017-03-07 20:44:09 -0700391 TIntermAggregate* initializers = nullptr;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600392 while (acceptIdentifier(idToken)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -0600393 const TString *fullName = idToken.string;
394 if (parseContext.symbolTable.atGlobalLevel())
395 parseContext.getFullNamespaceName(fullName);
John Kessenich78388722017-03-08 18:53:51 -0700396 if (peekTokenClass(EHTokLeftParen)) {
397 // looks like function parameters
steve-lunargf1e0c872016-10-31 15:13:43 -0600398
John Kessenich78388722017-03-08 18:53:51 -0700399 // Potentially rename shader entry point function. No-op most of the time.
John Kessenich8f9fdc92017-03-30 16:22:26 -0600400 parseContext.renameShaderFunction(fullName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600401
John Kessenich78388722017-03-08 18:53:51 -0700402 // function_parameters
John Kessenich8f9fdc92017-03-30 16:22:26 -0600403 declarator.function = new TFunction(fullName, declaredType);
John Kessenich088d52b2017-03-11 17:55:28 -0700404 if (!acceptFunctionParameters(*declarator.function)) {
John Kessenich78388722017-03-08 18:53:51 -0700405 expected("function parameter list");
406 return false;
407 }
408
John Kessenich630dd7d2016-06-12 23:52:12 -0600409 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -0700410 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich078d7f22016-03-14 10:02:11 -0600411
John Kessenichd5ed0b62016-07-04 17:32:45 -0600412 // compound_statement (function body definition) or just a prototype?
John Kessenich088d52b2017-03-11 17:55:28 -0700413 declarator.loc = token.loc;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600414 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich54ee28f2017-03-11 14:13:00 -0700415 if (declarator_list)
John Kessenichd5ed0b62016-07-04 17:32:45 -0600416 parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", "");
John Kessenich5e69ec62016-07-05 00:02:40 -0600417 if (typedefDecl)
418 parseContext.error(idToken.loc, "function body can't be in a typedef", "{", "");
John Kessenichb16f7e62017-03-11 19:32:47 -0700419 return acceptFunctionDefinition(declarator, nodeList, nullptr);
John Kessenich5e69ec62016-07-05 00:02:40 -0600420 } else {
421 if (typedefDecl)
422 parseContext.error(idToken.loc, "function typedefs not implemented", "{", "");
John Kessenich088d52b2017-03-11 17:55:28 -0700423 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, true);
John Kessenich5e69ec62016-07-05 00:02:40 -0600424 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600425 } else {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600426 // A variable declaration. Fix the storage qualifier if it's a global.
427 if (declaredType.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel())
428 declaredType.getQualifier().storage = EvqUniform;
429
John Kessenichecba76f2017-01-06 00:34:48 -0700430 // We can handle multiple variables per type declaration, so
John Kesseniche82061d2016-09-27 14:38:57 -0600431 // the number of types can expand when arrayness is different.
432 TType variableType;
433 variableType.shallowCopy(declaredType);
John Kessenich5f934b02016-03-13 17:58:25 -0600434
John Kesseniche82061d2016-09-27 14:38:57 -0600435 // recognize array_specifier
John Kessenichd5ed0b62016-07-04 17:32:45 -0600436 TArraySizes* arraySizes = nullptr;
437 acceptArraySpecifier(arraySizes);
John Kessenich5f934b02016-03-13 17:58:25 -0600438
John Kesseniche82061d2016-09-27 14:38:57 -0600439 // Fix arrayness in the variableType
440 if (declaredType.isImplicitlySizedArray()) {
441 // Because "int[] a = int[2](...), b = int[3](...)" makes two arrays a and b
442 // of different sizes, for this case sharing the shallow copy of arrayness
443 // with the parseType oversubscribes it, so get a deep copy of the arrayness.
444 variableType.newArraySizes(declaredType.getArraySizes());
445 }
446 if (arraySizes || variableType.isArray()) {
447 // In the most general case, arrayness is potentially coming both from the
448 // declared type and from the variable: "int[] a[];" or just one or the other.
449 // Merge it all to the variableType, so all arrayness is part of the variableType.
450 parseContext.arrayDimMerge(variableType, arraySizes);
451 }
452
LoopDawg4886f692016-06-29 10:58:58 -0600453 // samplers accept immediate sampler state
John Kesseniche82061d2016-09-27 14:38:57 -0600454 if (variableType.getBasicType() == EbtSampler) {
LoopDawg4886f692016-06-29 10:58:58 -0600455 if (! acceptSamplerState())
456 return false;
457 }
458
John Kessenichd5ed0b62016-07-04 17:32:45 -0600459 // post_decls
John Kesseniche82061d2016-09-27 14:38:57 -0600460 acceptPostDecls(variableType.getQualifier());
John Kessenichd5ed0b62016-07-04 17:32:45 -0600461
462 // EQUAL assignment_expression
463 TIntermTyped* expressionNode = nullptr;
464 if (acceptTokenClass(EHTokAssign)) {
John Kessenich5e69ec62016-07-05 00:02:40 -0600465 if (typedefDecl)
466 parseContext.error(idToken.loc, "can't have an initializer", "typedef", "");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600467 if (! acceptAssignmentExpression(expressionNode)) {
468 expected("initializer");
469 return false;
470 }
471 }
472
John Kessenich6dbc0a72016-09-27 19:13:05 -0600473 // TODO: things scoped within an annotation need their own name space;
474 // TODO: strings are not yet handled.
475 if (variableType.getBasicType() != EbtString && parseContext.getAnnotationNestingLevel() == 0) {
476 if (typedefDecl)
John Kessenich8f9fdc92017-03-30 16:22:26 -0600477 parseContext.declareTypedef(idToken.loc, *fullName, variableType);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600478 else if (variableType.getBasicType() == EbtBlock)
John Kessenich8f9fdc92017-03-30 16:22:26 -0600479 parseContext.declareBlock(idToken.loc, variableType, fullName);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600480 else {
steve-lunarga2b01a02016-11-28 17:09:54 -0700481 if (variableType.getQualifier().storage == EvqUniform && ! variableType.containsOpaque()) {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600482 // this isn't really an individual variable, but a member of the $Global buffer
John Kessenich8f9fdc92017-03-30 16:22:26 -0600483 parseContext.growGlobalUniformBlock(idToken.loc, variableType, *fullName);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600484 } else {
485 // Declare the variable and add any initializer code to the AST.
486 // The top-level node is always made into an aggregate, as that's
487 // historically how the AST has been.
John Kessenichca71d942017-03-07 20:44:09 -0700488 initializers = intermediate.growAggregate(initializers,
John Kessenich8f9fdc92017-03-30 16:22:26 -0600489 parseContext.declareVariable(idToken.loc, *fullName, variableType, expressionNode),
John Kessenichca71d942017-03-07 20:44:09 -0700490 idToken.loc);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600491 }
492 }
John Kessenich5e69ec62016-07-05 00:02:40 -0600493 }
John Kessenich5f934b02016-03-13 17:58:25 -0600494 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600495
496 if (acceptTokenClass(EHTokComma)) {
John Kessenich54ee28f2017-03-11 14:13:00 -0700497 declarator_list = true;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600498 continue;
499 }
500 };
501
John Kessenichca71d942017-03-07 20:44:09 -0700502 // The top-level initializer node is a sequence.
503 if (initializers != nullptr)
504 initializers->setOperator(EOpSequence);
505
506 // Add the initializers' aggregate to the nodeList we were handed.
507 if (nodeList)
508 nodeList = intermediate.growAggregate(nodeList, initializers);
509 else
510 nodeList = initializers;
John Kessenich87142c72016-03-12 20:24:24 -0700511
John Kessenich078d7f22016-03-14 10:02:11 -0600512 // SEMICOLON
John Kessenichd5ed0b62016-07-04 17:32:45 -0600513 if (! acceptTokenClass(EHTokSemicolon)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700514 // This may have been a false detection of what appeared to be a declaration, but
515 // was actually an assignment such as "float = 4", where "float" is an identifier.
516 // We put the token back to let further parsing happen for cases where that may
517 // happen. This errors on the side of caution, and mostly triggers the error.
518
519 if (peek() == EHTokAssign || peek() == EHTokLeftBracket || peek() == EHTokDot || peek() == EHTokComma)
520 recedeToken();
521 else
522 expected(";");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600523 return false;
524 }
John Kessenichecba76f2017-01-06 00:34:48 -0700525
John Kesseniche01a9bc2016-03-12 20:11:22 -0700526 return true;
527}
528
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600529// control_declaration
530// : fully_specified_type identifier EQUAL expression
531//
532bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node)
533{
534 node = nullptr;
535
536 // fully_specified_type
537 TType type;
538 if (! acceptFullySpecifiedType(type))
539 return false;
540
John Kessenich057df292017-03-06 18:18:37 -0700541 // filter out type casts
542 if (peekTokenClass(EHTokLeftParen)) {
543 recedeToken();
544 return false;
545 }
546
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600547 // identifier
548 HlslToken idToken;
549 if (! acceptIdentifier(idToken)) {
550 expected("identifier");
551 return false;
552 }
553
554 // EQUAL
555 TIntermTyped* expressionNode = nullptr;
556 if (! acceptTokenClass(EHTokAssign)) {
557 expected("=");
558 return false;
559 }
560
561 // expression
562 if (! acceptExpression(expressionNode)) {
563 expected("initializer");
564 return false;
565 }
566
John Kesseniche82061d2016-09-27 14:38:57 -0600567 node = parseContext.declareVariable(idToken.loc, *idToken.string, type, expressionNode);
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600568
569 return true;
570}
571
John Kessenich87142c72016-03-12 20:24:24 -0700572// fully_specified_type
573// : type_specifier
574// | type_qualifier type_specifier
575//
576bool HlslGrammar::acceptFullySpecifiedType(TType& type)
577{
John Kessenich54ee28f2017-03-11 14:13:00 -0700578 TIntermNode* nodeList = nullptr;
579 return acceptFullySpecifiedType(type, nodeList);
580}
581bool HlslGrammar::acceptFullySpecifiedType(TType& type, TIntermNode*& nodeList)
582{
John Kessenich87142c72016-03-12 20:24:24 -0700583 // type_qualifier
584 TQualifier qualifier;
585 qualifier.clear();
John Kessenichb9e39122016-08-17 10:22:08 -0600586 if (! acceptQualifier(qualifier))
587 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600588 TSourceLoc loc = token.loc;
John Kessenich87142c72016-03-12 20:24:24 -0700589
590 // type_specifier
John Kessenich54ee28f2017-03-11 14:13:00 -0700591 if (! acceptType(type, nodeList)) {
steve-lunarga64ed3e2016-12-18 17:51:14 -0700592 // If this is not a type, we may have inadvertently gone down a wrong path
steve-lunarg132d3312016-12-19 15:48:01 -0700593 // by parsing "sample", which can be treated like either an identifier or a
steve-lunarga64ed3e2016-12-18 17:51:14 -0700594 // qualifier. Back it out, if we did.
595 if (qualifier.sample)
596 recedeToken();
597
John Kessenich87142c72016-03-12 20:24:24 -0700598 return false;
steve-lunarga64ed3e2016-12-18 17:51:14 -0700599 }
John Kessenich3d157c52016-07-25 16:05:33 -0600600 if (type.getBasicType() == EbtBlock) {
601 // the type was a block, which set some parts of the qualifier
John Kessenich34e7ee72016-09-16 17:10:39 -0600602 parseContext.mergeQualifiers(type.getQualifier(), qualifier);
John Kessenich3d157c52016-07-25 16:05:33 -0600603 // further, it can create an anonymous instance of the block
604 if (peekTokenClass(EHTokSemicolon))
605 parseContext.declareBlock(loc, type);
steve-lunargbb0183f2016-10-04 16:58:14 -0600606 } else {
607 // Some qualifiers are set when parsing the type. Merge those with
608 // whatever comes from acceptQualifier.
609 assert(qualifier.layoutFormat == ElfNone);
steve-lunargf49cdf42016-11-17 15:04:20 -0700610
steve-lunargbb0183f2016-10-04 16:58:14 -0600611 qualifier.layoutFormat = type.getQualifier().layoutFormat;
steve-lunarg3226b082016-10-26 19:18:55 -0600612 qualifier.precision = type.getQualifier().precision;
steve-lunargf49cdf42016-11-17 15:04:20 -0700613
steve-lunarg08e0c082017-03-29 20:01:13 -0600614 if (type.getQualifier().storage == EvqOut ||
steve-lunarg5da1f032017-02-12 17:50:28 -0700615 type.getQualifier().storage == EvqBuffer) {
steve-lunargf49cdf42016-11-17 15:04:20 -0700616 qualifier.storage = type.getQualifier().storage;
steve-lunarg5da1f032017-02-12 17:50:28 -0700617 qualifier.readonly = type.getQualifier().readonly;
618 }
steve-lunargf49cdf42016-11-17 15:04:20 -0700619
steve-lunarg08e0c082017-03-29 20:01:13 -0600620 if (type.getQualifier().builtIn != EbvNone)
621 qualifier.builtIn = type.getQualifier().builtIn;
622
steve-lunargf49cdf42016-11-17 15:04:20 -0700623 type.getQualifier() = qualifier;
steve-lunargbb0183f2016-10-04 16:58:14 -0600624 }
John Kessenich87142c72016-03-12 20:24:24 -0700625
626 return true;
627}
628
John Kessenich630dd7d2016-06-12 23:52:12 -0600629// type_qualifier
630// : qualifier qualifier ...
631//
632// Zero or more of these, so this can't return false.
633//
John Kessenichb9e39122016-08-17 10:22:08 -0600634bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
John Kessenich87142c72016-03-12 20:24:24 -0700635{
John Kessenich630dd7d2016-06-12 23:52:12 -0600636 do {
637 switch (peek()) {
638 case EHTokStatic:
John Kessenich6dbc0a72016-09-27 19:13:05 -0600639 qualifier.storage = parseContext.symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
John Kessenich630dd7d2016-06-12 23:52:12 -0600640 break;
641 case EHTokExtern:
642 // TODO: no meaning in glslang?
643 break;
644 case EHTokShared:
645 // TODO: hint
646 break;
647 case EHTokGroupShared:
648 qualifier.storage = EvqShared;
649 break;
650 case EHTokUniform:
651 qualifier.storage = EvqUniform;
652 break;
653 case EHTokConst:
654 qualifier.storage = EvqConst;
655 break;
656 case EHTokVolatile:
657 qualifier.volatil = true;
658 break;
659 case EHTokLinear:
John Kessenich630dd7d2016-06-12 23:52:12 -0600660 qualifier.smooth = true;
661 break;
662 case EHTokCentroid:
663 qualifier.centroid = true;
664 break;
665 case EHTokNointerpolation:
666 qualifier.flat = true;
667 break;
668 case EHTokNoperspective:
669 qualifier.nopersp = true;
670 break;
671 case EHTokSample:
672 qualifier.sample = true;
673 break;
674 case EHTokRowMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600675 qualifier.layoutMatrix = ElmColumnMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600676 break;
677 case EHTokColumnMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600678 qualifier.layoutMatrix = ElmRowMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600679 break;
680 case EHTokPrecise:
681 qualifier.noContraction = true;
682 break;
LoopDawg9249c702016-07-12 20:44:32 -0600683 case EHTokIn:
684 qualifier.storage = EvqIn;
685 break;
686 case EHTokOut:
687 qualifier.storage = EvqOut;
688 break;
689 case EHTokInOut:
690 qualifier.storage = EvqInOut;
691 break;
John Kessenichb9e39122016-08-17 10:22:08 -0600692 case EHTokLayout:
693 if (! acceptLayoutQualifierList(qualifier))
694 return false;
695 continue;
steve-lunarg5da1f032017-02-12 17:50:28 -0700696 case EHTokGloballyCoherent:
697 qualifier.coherent = true;
698 break;
John Kessenich36b218d2017-03-15 09:05:14 -0600699 case EHTokInline:
700 // TODO: map this to SPIR-V function control
701 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700702
703 // GS geometries: these are specified on stage input variables, and are an error (not verified here)
704 // for output variables.
705 case EHTokPoint:
706 qualifier.storage = EvqIn;
707 if (!parseContext.handleInputGeometry(token.loc, ElgPoints))
708 return false;
709 break;
710 case EHTokLine:
711 qualifier.storage = EvqIn;
712 if (!parseContext.handleInputGeometry(token.loc, ElgLines))
713 return false;
714 break;
715 case EHTokTriangle:
716 qualifier.storage = EvqIn;
717 if (!parseContext.handleInputGeometry(token.loc, ElgTriangles))
718 return false;
719 break;
720 case EHTokLineAdj:
721 qualifier.storage = EvqIn;
722 if (!parseContext.handleInputGeometry(token.loc, ElgLinesAdjacency))
723 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700724 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700725 case EHTokTriangleAdj:
726 qualifier.storage = EvqIn;
727 if (!parseContext.handleInputGeometry(token.loc, ElgTrianglesAdjacency))
728 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700729 break;
730
John Kessenich630dd7d2016-06-12 23:52:12 -0600731 default:
John Kessenichb9e39122016-08-17 10:22:08 -0600732 return true;
John Kessenich630dd7d2016-06-12 23:52:12 -0600733 }
734 advanceToken();
735 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -0700736}
737
John Kessenichb9e39122016-08-17 10:22:08 -0600738// layout_qualifier_list
John Kesseniche3218e22016-09-05 14:37:03 -0600739// : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
John Kessenichb9e39122016-08-17 10:22:08 -0600740//
741// layout_qualifier
742// : identifier
John Kessenich841db352016-09-02 21:12:23 -0600743// | identifier EQUAL expression
John Kessenichb9e39122016-08-17 10:22:08 -0600744//
745// Zero or more of these, so this can't return false.
746//
747bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
748{
749 if (! acceptTokenClass(EHTokLayout))
750 return false;
751
752 // LEFT_PAREN
753 if (! acceptTokenClass(EHTokLeftParen))
754 return false;
755
756 do {
757 // identifier
758 HlslToken idToken;
759 if (! acceptIdentifier(idToken))
760 break;
761
762 // EQUAL expression
763 if (acceptTokenClass(EHTokAssign)) {
764 TIntermTyped* expr;
765 if (! acceptConditionalExpression(expr)) {
766 expected("expression");
767 return false;
768 }
769 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
770 } else
771 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
772
773 // COMMA
774 if (! acceptTokenClass(EHTokComma))
775 break;
776 } while (true);
777
778 // RIGHT_PAREN
779 if (! acceptTokenClass(EHTokRightParen)) {
780 expected(")");
781 return false;
782 }
783
784 return true;
785}
786
LoopDawg6daaa4f2016-06-23 19:13:48 -0600787// template_type
788// : FLOAT
789// | DOUBLE
790// | INT
791// | DWORD
792// | UINT
793// | BOOL
794//
steve-lunargf49cdf42016-11-17 15:04:20 -0700795bool HlslGrammar::acceptTemplateVecMatBasicType(TBasicType& basicType)
LoopDawg6daaa4f2016-06-23 19:13:48 -0600796{
797 switch (peek()) {
798 case EHTokFloat:
799 basicType = EbtFloat;
800 break;
801 case EHTokDouble:
802 basicType = EbtDouble;
803 break;
804 case EHTokInt:
805 case EHTokDword:
806 basicType = EbtInt;
807 break;
808 case EHTokUint:
809 basicType = EbtUint;
810 break;
811 case EHTokBool:
812 basicType = EbtBool;
813 break;
814 default:
815 return false;
816 }
817
818 advanceToken();
819
820 return true;
821}
822
823// vector_template_type
824// : VECTOR
825// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
826//
827bool HlslGrammar::acceptVectorTemplateType(TType& type)
828{
829 if (! acceptTokenClass(EHTokVector))
830 return false;
831
832 if (! acceptTokenClass(EHTokLeftAngle)) {
833 // in HLSL, 'vector' alone means float4.
834 new(&type) TType(EbtFloat, EvqTemporary, 4);
835 return true;
836 }
837
838 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700839 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600840 expected("scalar type");
841 return false;
842 }
843
844 // COMMA
845 if (! acceptTokenClass(EHTokComma)) {
846 expected(",");
847 return false;
848 }
849
850 // integer
851 if (! peekTokenClass(EHTokIntConstant)) {
852 expected("literal integer");
853 return false;
854 }
855
856 TIntermTyped* vecSize;
857 if (! acceptLiteral(vecSize))
858 return false;
859
860 const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
861
862 new(&type) TType(basicType, EvqTemporary, vecSizeI);
863
864 if (vecSizeI == 1)
865 type.makeVector();
866
867 if (!acceptTokenClass(EHTokRightAngle)) {
868 expected("right angle bracket");
869 return false;
870 }
871
872 return true;
873}
874
875// matrix_template_type
876// : MATRIX
877// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
878//
879bool HlslGrammar::acceptMatrixTemplateType(TType& type)
880{
881 if (! acceptTokenClass(EHTokMatrix))
882 return false;
883
884 if (! acceptTokenClass(EHTokLeftAngle)) {
885 // in HLSL, 'matrix' alone means float4x4.
886 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
887 return true;
888 }
889
890 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700891 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600892 expected("scalar type");
893 return false;
894 }
895
896 // COMMA
897 if (! acceptTokenClass(EHTokComma)) {
898 expected(",");
899 return false;
900 }
901
902 // integer rows
903 if (! peekTokenClass(EHTokIntConstant)) {
904 expected("literal integer");
905 return false;
906 }
907
908 TIntermTyped* rows;
909 if (! acceptLiteral(rows))
910 return false;
911
912 // COMMA
913 if (! acceptTokenClass(EHTokComma)) {
914 expected(",");
915 return false;
916 }
John Kessenichecba76f2017-01-06 00:34:48 -0700917
LoopDawg6daaa4f2016-06-23 19:13:48 -0600918 // integer cols
919 if (! peekTokenClass(EHTokIntConstant)) {
920 expected("literal integer");
921 return false;
922 }
923
924 TIntermTyped* cols;
925 if (! acceptLiteral(cols))
926 return false;
927
928 new(&type) TType(basicType, EvqTemporary, 0,
steve-lunarg297ae212016-08-24 14:36:13 -0600929 rows->getAsConstantUnion()->getConstArray()[0].getIConst(),
930 cols->getAsConstantUnion()->getConstArray()[0].getIConst());
LoopDawg6daaa4f2016-06-23 19:13:48 -0600931
932 if (!acceptTokenClass(EHTokRightAngle)) {
933 expected("right angle bracket");
934 return false;
935 }
936
937 return true;
938}
939
steve-lunargf49cdf42016-11-17 15:04:20 -0700940// layout_geometry
941// : LINESTREAM
942// | POINTSTREAM
943// | TRIANGLESTREAM
944//
945bool HlslGrammar::acceptOutputPrimitiveGeometry(TLayoutGeometry& geometry)
946{
947 // read geometry type
948 const EHlslTokenClass geometryType = peek();
949
950 switch (geometryType) {
951 case EHTokPointStream: geometry = ElgPoints; break;
952 case EHTokLineStream: geometry = ElgLineStrip; break;
953 case EHTokTriangleStream: geometry = ElgTriangleStrip; break;
954 default:
955 return false; // not a layout geometry
956 }
957
958 advanceToken(); // consume the layout keyword
959 return true;
960}
961
steve-lunarg858c9282017-01-07 08:54:10 -0700962// tessellation_decl_type
963// : INPUTPATCH
964// | OUTPUTPATCH
965//
966bool HlslGrammar::acceptTessellationDeclType()
967{
968 // read geometry type
969 const EHlslTokenClass tessType = peek();
970
971 switch (tessType) {
972 case EHTokInputPatch: break;
973 case EHTokOutputPatch: break;
974 default:
975 return false; // not a tessellation decl
976 }
977
978 advanceToken(); // consume the keyword
979 return true;
980}
981
982// tessellation_patch_template_type
983// : tessellation_decl_type LEFT_ANGLE type comma integer_literal RIGHT_ANGLE
984//
985bool HlslGrammar::acceptTessellationPatchTemplateType(TType& type)
986{
987 if (! acceptTessellationDeclType())
988 return false;
989
990 if (! acceptTokenClass(EHTokLeftAngle))
991 return false;
992
993 if (! acceptType(type)) {
994 expected("tessellation patch type");
995 return false;
996 }
997
998 if (! acceptTokenClass(EHTokComma))
999 return false;
1000
1001 // integer size
1002 if (! peekTokenClass(EHTokIntConstant)) {
1003 expected("literal integer");
1004 return false;
1005 }
1006
1007 TIntermTyped* size;
1008 if (! acceptLiteral(size))
1009 return false;
1010
1011 TArraySizes* arraySizes = new TArraySizes;
1012 arraySizes->addInnerSize(size->getAsConstantUnion()->getConstArray()[0].getIConst());
1013 type.newArraySizes(*arraySizes);
1014
1015 if (! acceptTokenClass(EHTokRightAngle)) {
1016 expected("right angle bracket");
1017 return false;
1018 }
1019
1020 return true;
1021}
1022
steve-lunargf49cdf42016-11-17 15:04:20 -07001023// stream_out_template_type
1024// : output_primitive_geometry_type LEFT_ANGLE type RIGHT_ANGLE
1025//
1026bool HlslGrammar::acceptStreamOutTemplateType(TType& type, TLayoutGeometry& geometry)
1027{
1028 geometry = ElgNone;
1029
1030 if (! acceptOutputPrimitiveGeometry(geometry))
1031 return false;
1032
1033 if (! acceptTokenClass(EHTokLeftAngle))
1034 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001035
steve-lunargf49cdf42016-11-17 15:04:20 -07001036 if (! acceptType(type)) {
1037 expected("stream output type");
1038 return false;
1039 }
1040
steve-lunarg08e0c082017-03-29 20:01:13 -06001041 type.getQualifier().storage = EvqOut;
1042 type.getQualifier().builtIn = EbvGsOutputStream;
steve-lunargf49cdf42016-11-17 15:04:20 -07001043
1044 if (! acceptTokenClass(EHTokRightAngle)) {
1045 expected("right angle bracket");
1046 return false;
1047 }
1048
1049 return true;
1050}
John Kessenichecba76f2017-01-06 00:34:48 -07001051
John Kessenicha1e2d492016-09-20 13:22:58 -06001052// annotations
1053// : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
John Kessenich86f71382016-09-19 20:23:18 -06001054//
John Kessenicha1e2d492016-09-20 13:22:58 -06001055bool HlslGrammar::acceptAnnotations(TQualifier&)
John Kessenich86f71382016-09-19 20:23:18 -06001056{
John Kessenicha1e2d492016-09-20 13:22:58 -06001057 if (! acceptTokenClass(EHTokLeftAngle))
John Kessenich86f71382016-09-19 20:23:18 -06001058 return false;
1059
John Kessenicha1e2d492016-09-20 13:22:58 -06001060 // note that we are nesting a name space
1061 parseContext.nestAnnotations();
John Kessenich86f71382016-09-19 20:23:18 -06001062
1063 // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
1064 do {
1065 // eat any extra SEMI_COLON; don't know if the grammar calls for this or not
1066 while (acceptTokenClass(EHTokSemicolon))
1067 ;
1068
1069 if (acceptTokenClass(EHTokRightAngle))
John Kessenicha1e2d492016-09-20 13:22:58 -06001070 break;
John Kessenich86f71382016-09-19 20:23:18 -06001071
1072 // declaration
John Kessenichca71d942017-03-07 20:44:09 -07001073 TIntermNode* node = nullptr;
John Kessenich86f71382016-09-19 20:23:18 -06001074 if (! acceptDeclaration(node)) {
John Kessenicha1e2d492016-09-20 13:22:58 -06001075 expected("declaration in annotation");
John Kessenich86f71382016-09-19 20:23:18 -06001076 return false;
1077 }
1078 } while (true);
John Kessenicha1e2d492016-09-20 13:22:58 -06001079
1080 parseContext.unnestAnnotations();
1081 return true;
John Kessenich86f71382016-09-19 20:23:18 -06001082}
LoopDawg6daaa4f2016-06-23 19:13:48 -06001083
LoopDawg4886f692016-06-29 10:58:58 -06001084// sampler_type
1085// : SAMPLER
1086// | SAMPLER1D
1087// | SAMPLER2D
1088// | SAMPLER3D
1089// | SAMPLERCUBE
1090// | SAMPLERSTATE
1091// | SAMPLERCOMPARISONSTATE
1092bool HlslGrammar::acceptSamplerType(TType& type)
1093{
1094 // read sampler type
1095 const EHlslTokenClass samplerType = peek();
1096
LoopDawga78b0292016-07-19 14:28:05 -06001097 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -06001098 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -06001099
LoopDawga78b0292016-07-19 14:28:05 -06001100 bool isShadow = false;
1101
LoopDawg4886f692016-06-29 10:58:58 -06001102 switch (samplerType) {
1103 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -06001104 case EHTokSampler1d: /*dim = Esd1D*/; break;
1105 case EHTokSampler2d: /*dim = Esd2D*/; break;
1106 case EHTokSampler3d: /*dim = Esd3D*/; break;
1107 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -06001108 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -06001109 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001110 default:
1111 return false; // not a sampler declaration
1112 }
1113
1114 advanceToken(); // consume the sampler type keyword
1115
1116 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -06001117
1118 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -06001119 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -06001120
1121 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
1122
1123 return true;
1124}
1125
1126// texture_type
1127// | BUFFER
1128// | TEXTURE1D
1129// | TEXTURE1DARRAY
1130// | TEXTURE2D
1131// | TEXTURE2DARRAY
1132// | TEXTURE3D
1133// | TEXTURECUBE
1134// | TEXTURECUBEARRAY
1135// | TEXTURE2DMS
1136// | TEXTURE2DMSARRAY
steve-lunargbb0183f2016-10-04 16:58:14 -06001137// | RWBUFFER
1138// | RWTEXTURE1D
1139// | RWTEXTURE1DARRAY
1140// | RWTEXTURE2D
1141// | RWTEXTURE2DARRAY
1142// | RWTEXTURE3D
1143
LoopDawg4886f692016-06-29 10:58:58 -06001144bool HlslGrammar::acceptTextureType(TType& type)
1145{
1146 const EHlslTokenClass textureType = peek();
1147
1148 TSamplerDim dim = EsdNone;
1149 bool array = false;
1150 bool ms = false;
steve-lunargbb0183f2016-10-04 16:58:14 -06001151 bool image = false;
steve-lunargbf1537f2017-03-31 17:40:09 -06001152 bool combined = true;
LoopDawg4886f692016-06-29 10:58:58 -06001153
1154 switch (textureType) {
steve-lunargbf1537f2017-03-31 17:40:09 -06001155 case EHTokBuffer: dim = EsdBuffer; combined = false; break;
John Kessenichf36542f2017-03-31 14:39:30 -06001156 case EHTokTexture1d: dim = Esd1D; break;
1157 case EHTokTexture1darray: dim = Esd1D; array = true; break;
1158 case EHTokTexture2d: dim = Esd2D; break;
1159 case EHTokTexture2darray: dim = Esd2D; array = true; break;
1160 case EHTokTexture3d: dim = Esd3D; break;
1161 case EHTokTextureCube: dim = EsdCube; break;
1162 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
1163 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
1164 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
1165 case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
1166 case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
1167 case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
1168 case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
1169 case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
1170 case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001171 default:
1172 return false; // not a texture declaration
1173 }
1174
1175 advanceToken(); // consume the texture object keyword
1176
1177 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
John Kessenichecba76f2017-01-06 00:34:48 -07001178
LoopDawg4886f692016-06-29 10:58:58 -06001179 TIntermTyped* msCount = nullptr;
1180
steve-lunargbb0183f2016-10-04 16:58:14 -06001181 // texture type: required for multisample types and RWBuffer/RWTextures!
LoopDawg4886f692016-06-29 10:58:58 -06001182 if (acceptTokenClass(EHTokLeftAngle)) {
1183 if (! acceptType(txType)) {
1184 expected("scalar or vector type");
1185 return false;
1186 }
1187
1188 const TBasicType basicRetType = txType.getBasicType() ;
1189
1190 if (basicRetType != EbtFloat && basicRetType != EbtUint && basicRetType != EbtInt) {
1191 unimplemented("basic type in texture");
1192 return false;
1193 }
1194
steve-lunargd53f7172016-07-27 15:46:48 -06001195 // Buffers can handle small mats if they fit in 4 components
1196 if (dim == EsdBuffer && txType.isMatrix()) {
1197 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
1198 expected("components < 4 in matrix buffer type");
1199 return false;
1200 }
1201
1202 // TODO: except we don't handle it yet...
1203 unimplemented("matrix type in buffer");
1204 return false;
1205 }
1206
LoopDawg4886f692016-06-29 10:58:58 -06001207 if (!txType.isScalar() && !txType.isVector()) {
1208 expected("scalar or vector type");
1209 return false;
1210 }
1211
LoopDawg4886f692016-06-29 10:58:58 -06001212 if (ms && acceptTokenClass(EHTokComma)) {
1213 // read sample count for multisample types, if given
1214 if (! peekTokenClass(EHTokIntConstant)) {
1215 expected("multisample count");
1216 return false;
1217 }
1218
1219 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
1220 return false;
1221 }
1222
1223 if (! acceptTokenClass(EHTokRightAngle)) {
1224 expected("right angle bracket");
1225 return false;
1226 }
1227 } else if (ms) {
1228 expected("texture type for multisample");
1229 return false;
John Kessenichf36542f2017-03-31 14:39:30 -06001230 } else if (image) {
steve-lunargbb0183f2016-10-04 16:58:14 -06001231 expected("type for RWTexture/RWBuffer");
1232 return false;
LoopDawg4886f692016-06-29 10:58:58 -06001233 }
1234
1235 TArraySizes* arraySizes = nullptr;
steve-lunarg4f2da272016-10-10 15:24:57 -06001236 const bool shadow = false; // declared on the sampler
LoopDawg4886f692016-06-29 10:58:58 -06001237
1238 TSampler sampler;
steve-lunargbb0183f2016-10-04 16:58:14 -06001239 TLayoutFormat format = ElfNone;
steve-lunargd53f7172016-07-27 15:46:48 -06001240
steve-lunarg4f2da272016-10-10 15:24:57 -06001241 // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
1242 if (image || dim == EsdBuffer)
1243 format = parseContext.getLayoutFromTxType(token.loc, txType);
steve-lunargbb0183f2016-10-04 16:58:14 -06001244
1245 // Non-image Buffers are combined
1246 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -06001247 sampler.set(txType.getBasicType(), dim, array);
1248 } else {
1249 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -06001250 if (image) {
1251 sampler.setImage(txType.getBasicType(), dim, array, shadow, ms);
1252 } else {
1253 sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms);
1254 }
steve-lunargd53f7172016-07-27 15:46:48 -06001255 }
steve-lunarg8b0227c2016-10-14 16:40:32 -06001256
1257 // Remember the declared vector size.
1258 sampler.vectorSize = txType.getVectorSize();
John Kessenichecba76f2017-01-06 00:34:48 -07001259
steve-lunargbf1537f2017-03-31 17:40:09 -06001260 // Force uncombined, if necessary
1261 if (!combined)
1262 sampler.combined = false;
1263
LoopDawg4886f692016-06-29 10:58:58 -06001264 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -06001265 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -06001266
1267 return true;
1268}
1269
John Kessenich87142c72016-03-12 20:24:24 -07001270// If token is for a type, update 'type' with the type information,
1271// and return true and advance.
1272// Otherwise, return false, and don't advance
1273bool HlslGrammar::acceptType(TType& type)
1274{
John Kessenich54ee28f2017-03-11 14:13:00 -07001275 TIntermNode* nodeList = nullptr;
1276 return acceptType(type, nodeList);
1277}
1278bool HlslGrammar::acceptType(TType& type, TIntermNode*& nodeList)
1279{
steve-lunarg3226b082016-10-26 19:18:55 -06001280 // Basic types for min* types, broken out here in case of future
1281 // changes, e.g, to use native halfs.
1282 static const TBasicType min16float_bt = EbtFloat;
1283 static const TBasicType min10float_bt = EbtFloat;
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001284 static const TBasicType half_bt = EbtFloat;
steve-lunarg3226b082016-10-26 19:18:55 -06001285 static const TBasicType min16int_bt = EbtInt;
1286 static const TBasicType min12int_bt = EbtInt;
1287 static const TBasicType min16uint_bt = EbtUint;
1288
John Kessenich9c86c6a2016-05-03 22:49:24 -06001289 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -06001290 case EHTokVector:
1291 return acceptVectorTemplateType(type);
1292 break;
1293
1294 case EHTokMatrix:
1295 return acceptMatrixTemplateType(type);
1296 break;
1297
steve-lunargf49cdf42016-11-17 15:04:20 -07001298 case EHTokPointStream: // fall through
1299 case EHTokLineStream: // ...
1300 case EHTokTriangleStream: // ...
1301 {
1302 TLayoutGeometry geometry;
1303 if (! acceptStreamOutTemplateType(type, geometry))
1304 return false;
1305
1306 if (! parseContext.handleOutputGeometry(token.loc, geometry))
1307 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001308
steve-lunargf49cdf42016-11-17 15:04:20 -07001309 return true;
1310 }
1311
steve-lunarg858c9282017-01-07 08:54:10 -07001312 case EHTokInputPatch: // fall through
1313 case EHTokOutputPatch: // ...
1314 {
1315 if (! acceptTessellationPatchTemplateType(type))
1316 return false;
1317
1318 return true;
1319 }
1320
LoopDawg4886f692016-06-29 10:58:58 -06001321 case EHTokSampler: // fall through
1322 case EHTokSampler1d: // ...
1323 case EHTokSampler2d: // ...
1324 case EHTokSampler3d: // ...
1325 case EHTokSamplerCube: // ...
1326 case EHTokSamplerState: // ...
1327 case EHTokSamplerComparisonState: // ...
1328 return acceptSamplerType(type);
1329 break;
1330
1331 case EHTokBuffer: // fall through
1332 case EHTokTexture1d: // ...
1333 case EHTokTexture1darray: // ...
1334 case EHTokTexture2d: // ...
1335 case EHTokTexture2darray: // ...
1336 case EHTokTexture3d: // ...
1337 case EHTokTextureCube: // ...
1338 case EHTokTextureCubearray: // ...
1339 case EHTokTexture2DMS: // ...
1340 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001341 case EHTokRWTexture1d: // ...
1342 case EHTokRWTexture1darray: // ...
1343 case EHTokRWTexture2d: // ...
1344 case EHTokRWTexture2darray: // ...
1345 case EHTokRWTexture3d: // ...
1346 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001347 return acceptTextureType(type);
1348 break;
1349
steve-lunarg5da1f032017-02-12 17:50:28 -07001350 case EHTokAppendStructuredBuffer:
1351 case EHTokByteAddressBuffer:
1352 case EHTokConsumeStructuredBuffer:
1353 case EHTokRWByteAddressBuffer:
1354 case EHTokRWStructuredBuffer:
1355 case EHTokStructuredBuffer:
1356 return acceptStructBufferType(type);
1357 break;
1358
John Kessenich27ffb292017-03-03 17:01:01 -07001359 case EHTokClass:
John Kesseniche6e74942016-06-11 16:43:14 -06001360 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001361 case EHTokCBuffer:
1362 case EHTokTBuffer:
John Kessenich54ee28f2017-03-11 14:13:00 -07001363 return acceptStruct(type, nodeList);
John Kesseniche6e74942016-06-11 16:43:14 -06001364
1365 case EHTokIdentifier:
1366 // An identifier could be for a user-defined type.
1367 // Note we cache the symbol table lookup, to save for a later rule
1368 // when this is not a type.
John Kessenichf4ba25e2017-03-21 18:35:04 -06001369 if (parseContext.lookupUserType(*token.string, type) != nullptr) {
John Kesseniche6e74942016-06-11 16:43:14 -06001370 advanceToken();
1371 return true;
1372 } else
1373 return false;
1374
John Kessenich71351de2016-06-08 12:50:56 -06001375 case EHTokVoid:
1376 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001377 break;
John Kessenich71351de2016-06-08 12:50:56 -06001378
John Kessenicha1e2d492016-09-20 13:22:58 -06001379 case EHTokString:
1380 new(&type) TType(EbtString);
1381 break;
1382
John Kessenich87142c72016-03-12 20:24:24 -07001383 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001384 new(&type) TType(EbtFloat);
1385 break;
John Kessenich87142c72016-03-12 20:24:24 -07001386 case EHTokFloat1:
1387 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001388 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001389 break;
John Kessenich87142c72016-03-12 20:24:24 -07001390 case EHTokFloat2:
1391 new(&type) TType(EbtFloat, EvqTemporary, 2);
1392 break;
1393 case EHTokFloat3:
1394 new(&type) TType(EbtFloat, EvqTemporary, 3);
1395 break;
1396 case EHTokFloat4:
1397 new(&type) TType(EbtFloat, EvqTemporary, 4);
1398 break;
1399
John Kessenich71351de2016-06-08 12:50:56 -06001400 case EHTokDouble:
1401 new(&type) TType(EbtDouble);
1402 break;
1403 case EHTokDouble1:
1404 new(&type) TType(EbtDouble);
1405 type.makeVector();
1406 break;
1407 case EHTokDouble2:
1408 new(&type) TType(EbtDouble, EvqTemporary, 2);
1409 break;
1410 case EHTokDouble3:
1411 new(&type) TType(EbtDouble, EvqTemporary, 3);
1412 break;
1413 case EHTokDouble4:
1414 new(&type) TType(EbtDouble, EvqTemporary, 4);
1415 break;
1416
1417 case EHTokInt:
1418 case EHTokDword:
1419 new(&type) TType(EbtInt);
1420 break;
1421 case EHTokInt1:
1422 new(&type) TType(EbtInt);
1423 type.makeVector();
1424 break;
John Kessenich87142c72016-03-12 20:24:24 -07001425 case EHTokInt2:
1426 new(&type) TType(EbtInt, EvqTemporary, 2);
1427 break;
1428 case EHTokInt3:
1429 new(&type) TType(EbtInt, EvqTemporary, 3);
1430 break;
1431 case EHTokInt4:
1432 new(&type) TType(EbtInt, EvqTemporary, 4);
1433 break;
1434
John Kessenich71351de2016-06-08 12:50:56 -06001435 case EHTokUint:
1436 new(&type) TType(EbtUint);
1437 break;
1438 case EHTokUint1:
1439 new(&type) TType(EbtUint);
1440 type.makeVector();
1441 break;
1442 case EHTokUint2:
1443 new(&type) TType(EbtUint, EvqTemporary, 2);
1444 break;
1445 case EHTokUint3:
1446 new(&type) TType(EbtUint, EvqTemporary, 3);
1447 break;
1448 case EHTokUint4:
1449 new(&type) TType(EbtUint, EvqTemporary, 4);
1450 break;
1451
1452 case EHTokBool:
1453 new(&type) TType(EbtBool);
1454 break;
1455 case EHTokBool1:
1456 new(&type) TType(EbtBool);
1457 type.makeVector();
1458 break;
John Kessenich87142c72016-03-12 20:24:24 -07001459 case EHTokBool2:
1460 new(&type) TType(EbtBool, EvqTemporary, 2);
1461 break;
1462 case EHTokBool3:
1463 new(&type) TType(EbtBool, EvqTemporary, 3);
1464 break;
1465 case EHTokBool4:
1466 new(&type) TType(EbtBool, EvqTemporary, 4);
1467 break;
1468
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001469 case EHTokHalf:
1470 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1471 break;
1472 case EHTokHalf1:
1473 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1474 type.makeVector();
1475 break;
1476 case EHTokHalf2:
1477 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 2);
1478 break;
1479 case EHTokHalf3:
1480 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 3);
1481 break;
1482 case EHTokHalf4:
1483 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 4);
1484 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001485
steve-lunarg3226b082016-10-26 19:18:55 -06001486 case EHTokMin16float:
1487 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1488 break;
1489 case EHTokMin16float1:
1490 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1491 type.makeVector();
1492 break;
1493 case EHTokMin16float2:
1494 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 2);
1495 break;
1496 case EHTokMin16float3:
1497 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 3);
1498 break;
1499 case EHTokMin16float4:
1500 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 4);
1501 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001502
steve-lunarg3226b082016-10-26 19:18:55 -06001503 case EHTokMin10float:
1504 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1505 break;
1506 case EHTokMin10float1:
1507 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1508 type.makeVector();
1509 break;
1510 case EHTokMin10float2:
1511 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 2);
1512 break;
1513 case EHTokMin10float3:
1514 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 3);
1515 break;
1516 case EHTokMin10float4:
1517 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 4);
1518 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001519
steve-lunarg3226b082016-10-26 19:18:55 -06001520 case EHTokMin16int:
1521 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1522 break;
1523 case EHTokMin16int1:
1524 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1525 type.makeVector();
1526 break;
1527 case EHTokMin16int2:
1528 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 2);
1529 break;
1530 case EHTokMin16int3:
1531 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 3);
1532 break;
1533 case EHTokMin16int4:
1534 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 4);
1535 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001536
steve-lunarg3226b082016-10-26 19:18:55 -06001537 case EHTokMin12int:
1538 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1539 break;
1540 case EHTokMin12int1:
1541 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1542 type.makeVector();
1543 break;
1544 case EHTokMin12int2:
1545 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 2);
1546 break;
1547 case EHTokMin12int3:
1548 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 3);
1549 break;
1550 case EHTokMin12int4:
1551 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 4);
1552 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001553
steve-lunarg3226b082016-10-26 19:18:55 -06001554 case EHTokMin16uint:
1555 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1556 break;
1557 case EHTokMin16uint1:
1558 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1559 type.makeVector();
1560 break;
1561 case EHTokMin16uint2:
1562 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 2);
1563 break;
1564 case EHTokMin16uint3:
1565 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 3);
1566 break;
1567 case EHTokMin16uint4:
1568 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 4);
1569 break;
1570
John Kessenich0133c122016-05-20 12:17:26 -06001571 case EHTokInt1x1:
1572 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1573 break;
1574 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001575 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001576 break;
1577 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001578 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001579 break;
1580 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001581 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001582 break;
1583 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001584 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001585 break;
1586 case EHTokInt2x2:
1587 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1588 break;
1589 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001590 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001591 break;
1592 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001593 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001594 break;
1595 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001596 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001597 break;
1598 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001599 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001600 break;
1601 case EHTokInt3x3:
1602 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1603 break;
1604 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001605 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001606 break;
1607 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001608 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001609 break;
1610 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001611 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001612 break;
1613 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001614 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001615 break;
1616 case EHTokInt4x4:
1617 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1618 break;
1619
John Kessenich71351de2016-06-08 12:50:56 -06001620 case EHTokUint1x1:
1621 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1622 break;
1623 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001624 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001625 break;
1626 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001627 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001628 break;
1629 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001630 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001631 break;
1632 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001633 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001634 break;
1635 case EHTokUint2x2:
1636 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1637 break;
1638 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001639 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001640 break;
1641 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001642 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001643 break;
1644 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001645 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001646 break;
1647 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001648 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001649 break;
1650 case EHTokUint3x3:
1651 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1652 break;
1653 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001654 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001655 break;
1656 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001657 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001658 break;
1659 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001660 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001661 break;
1662 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001663 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001664 break;
1665 case EHTokUint4x4:
1666 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1667 break;
1668
1669 case EHTokBool1x1:
1670 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1671 break;
1672 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001673 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001674 break;
1675 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001676 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001677 break;
1678 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001679 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001680 break;
1681 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001682 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001683 break;
1684 case EHTokBool2x2:
1685 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1686 break;
1687 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001688 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001689 break;
1690 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001691 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001692 break;
1693 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001694 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001695 break;
1696 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001697 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001698 break;
1699 case EHTokBool3x3:
1700 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1701 break;
1702 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001703 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001704 break;
1705 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001706 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001707 break;
1708 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001709 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001710 break;
1711 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001712 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001713 break;
1714 case EHTokBool4x4:
1715 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1716 break;
1717
John Kessenich0133c122016-05-20 12:17:26 -06001718 case EHTokFloat1x1:
1719 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1720 break;
1721 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001722 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001723 break;
1724 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001725 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001726 break;
1727 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001728 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001729 break;
1730 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001731 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001732 break;
John Kessenich87142c72016-03-12 20:24:24 -07001733 case EHTokFloat2x2:
1734 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1735 break;
1736 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001737 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001738 break;
1739 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001740 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001741 break;
John Kessenich0133c122016-05-20 12:17:26 -06001742 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001743 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001744 break;
John Kessenich87142c72016-03-12 20:24:24 -07001745 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001746 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001747 break;
1748 case EHTokFloat3x3:
1749 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1750 break;
1751 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001752 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001753 break;
John Kessenich0133c122016-05-20 12:17:26 -06001754 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001755 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001756 break;
John Kessenich87142c72016-03-12 20:24:24 -07001757 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001758 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001759 break;
1760 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001761 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001762 break;
1763 case EHTokFloat4x4:
1764 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1765 break;
1766
John Kessenich0133c122016-05-20 12:17:26 -06001767 case EHTokDouble1x1:
1768 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1769 break;
1770 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001771 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001772 break;
1773 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001774 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001775 break;
1776 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001777 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001778 break;
1779 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001780 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001781 break;
1782 case EHTokDouble2x2:
1783 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1784 break;
1785 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001786 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001787 break;
1788 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001789 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001790 break;
1791 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001792 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001793 break;
1794 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001795 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001796 break;
1797 case EHTokDouble3x3:
1798 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1799 break;
1800 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001801 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001802 break;
1803 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001804 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001805 break;
1806 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001807 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001808 break;
1809 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001810 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001811 break;
1812 case EHTokDouble4x4:
1813 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1814 break;
1815
John Kessenich87142c72016-03-12 20:24:24 -07001816 default:
1817 return false;
1818 }
1819
1820 advanceToken();
1821
1822 return true;
1823}
1824
John Kesseniche6e74942016-06-11 16:43:14 -06001825// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001826// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1827// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
John Kessenich854fe242017-03-02 14:30:59 -07001828// | struct_type IDENTIFIER // use of previously declared struct type
John Kessenich3d157c52016-07-25 16:05:33 -06001829//
1830// struct_type
1831// : STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001832// | CLASS
John Kessenich3d157c52016-07-25 16:05:33 -06001833// | CBUFFER
1834// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001835//
John Kessenich54ee28f2017-03-11 14:13:00 -07001836bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
John Kesseniche6e74942016-06-11 16:43:14 -06001837{
John Kessenichb804de62016-09-05 12:19:18 -06001838 // This storage qualifier will tell us whether it's an AST
1839 // block type or just a generic structure type.
1840 TStorageQualifier storageQualifier = EvqTemporary;
John Kessenich3d157c52016-07-25 16:05:33 -06001841
1842 // CBUFFER
1843 if (acceptTokenClass(EHTokCBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001844 storageQualifier = EvqUniform;
John Kessenich3d157c52016-07-25 16:05:33 -06001845 // TBUFFER
1846 else if (acceptTokenClass(EHTokTBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001847 storageQualifier = EvqBuffer;
John Kessenich27ffb292017-03-03 17:01:01 -07001848 // CLASS
John Kesseniche6e74942016-06-11 16:43:14 -06001849 // STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001850 else if (! acceptTokenClass(EHTokClass) && ! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001851 return false;
1852
1853 // IDENTIFIER
1854 TString structName = "";
1855 if (peekTokenClass(EHTokIdentifier)) {
1856 structName = *token.string;
1857 advanceToken();
1858 }
1859
John Kessenich3d157c52016-07-25 16:05:33 -06001860 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001861 TQualifier postDeclQualifier;
1862 postDeclQualifier.clear();
John Kessenich854fe242017-03-02 14:30:59 -07001863 bool postDeclsFound = acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06001864
John Kessenichf3d88bd2017-03-19 12:24:29 -06001865 // LEFT_BRACE, or
John Kessenich854fe242017-03-02 14:30:59 -07001866 // struct_type IDENTIFIER
John Kesseniche6e74942016-06-11 16:43:14 -06001867 if (! acceptTokenClass(EHTokLeftBrace)) {
John Kessenich854fe242017-03-02 14:30:59 -07001868 if (structName.size() > 0 && !postDeclsFound && parseContext.lookupUserType(structName, type) != nullptr) {
1869 // struct_type IDENTIFIER
1870 return true;
1871 } else {
1872 expected("{");
1873 return false;
1874 }
John Kesseniche6e74942016-06-11 16:43:14 -06001875 }
1876
John Kessenichf3d88bd2017-03-19 12:24:29 -06001877
John Kesseniche6e74942016-06-11 16:43:14 -06001878 // struct_declaration_list
1879 TTypeList* typeList;
John Kessenichf3d88bd2017-03-19 12:24:29 -06001880 // Save each member function so they can be processed after we have a fully formed 'this'.
1881 TVector<TFunctionDeclarator> functionDeclarators;
1882
1883 parseContext.pushNamespace(structName);
John Kessenichaa3c64c2017-03-28 09:52:38 -06001884 bool acceptedList = acceptStructDeclarationList(typeList, nodeList, functionDeclarators);
John Kessenichf3d88bd2017-03-19 12:24:29 -06001885 parseContext.popNamespace();
1886
1887 if (! acceptedList) {
John Kesseniche6e74942016-06-11 16:43:14 -06001888 expected("struct member declarations");
1889 return false;
1890 }
1891
1892 // RIGHT_BRACE
1893 if (! acceptTokenClass(EHTokRightBrace)) {
1894 expected("}");
1895 return false;
1896 }
1897
1898 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001899 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001900 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001901 else {
John Kessenich7735b942016-09-05 12:40:06 -06001902 postDeclQualifier.storage = storageQualifier;
1903 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001904 }
John Kesseniche6e74942016-06-11 16:43:14 -06001905
John Kessenich727b3742017-02-03 17:57:55 -07001906 parseContext.declareStruct(token.loc, structName, type);
John Kesseniche6e74942016-06-11 16:43:14 -06001907
John Kessenich4960baa2017-03-19 18:09:59 -06001908 // For member functions: now that we know the type of 'this', go back and
1909 // - add their implicit argument with 'this' (not to the mangling, just the argument list)
1910 // - parse the functions, their tokens were saved for deferred parsing (now)
1911 for (int b = 0; b < (int)functionDeclarators.size(); ++b) {
1912 // update signature
1913 if (functionDeclarators[b].function->hasImplicitThis())
John Kessenich37789792017-03-21 23:56:40 -06001914 functionDeclarators[b].function->addThisParameter(type, intermediate.implicitThisName);
John Kessenich4960baa2017-03-19 18:09:59 -06001915 }
1916
John Kessenichf3d88bd2017-03-19 12:24:29 -06001917 // All member functions get parsed inside the class/struct namespace and with the
1918 // class/struct members in a symbol-table level.
1919 parseContext.pushNamespace(structName);
John Kessenich37789792017-03-21 23:56:40 -06001920 parseContext.pushThisScope(type);
John Kessenichf3d88bd2017-03-19 12:24:29 -06001921 bool deferredSuccess = true;
1922 for (int b = 0; b < (int)functionDeclarators.size() && deferredSuccess; ++b) {
1923 // parse body
1924 pushTokenStream(functionDeclarators[b].body);
1925 if (! acceptFunctionBody(functionDeclarators[b], nodeList))
1926 deferredSuccess = false;
1927 popTokenStream();
1928 }
John Kessenich37789792017-03-21 23:56:40 -06001929 parseContext.popThisScope();
John Kessenichf3d88bd2017-03-19 12:24:29 -06001930 parseContext.popNamespace();
1931
1932 return deferredSuccess;
John Kesseniche6e74942016-06-11 16:43:14 -06001933}
1934
steve-lunarg5da1f032017-02-12 17:50:28 -07001935// struct_buffer
1936// : APPENDSTRUCTUREDBUFFER
1937// | BYTEADDRESSBUFFER
1938// | CONSUMESTRUCTUREDBUFFER
1939// | RWBYTEADDRESSBUFFER
1940// | RWSTRUCTUREDBUFFER
1941// | STRUCTUREDBUFFER
1942bool HlslGrammar::acceptStructBufferType(TType& type)
1943{
1944 const EHlslTokenClass structBuffType = peek();
1945
1946 // TODO: globallycoherent
1947 bool hasTemplateType = true;
1948 bool readonly = false;
1949
1950 TStorageQualifier storage = EvqBuffer;
1951
1952 switch (structBuffType) {
1953 case EHTokAppendStructuredBuffer:
1954 unimplemented("AppendStructuredBuffer");
1955 return false;
1956 case EHTokByteAddressBuffer:
1957 hasTemplateType = false;
1958 readonly = true;
1959 break;
1960 case EHTokConsumeStructuredBuffer:
1961 unimplemented("ConsumeStructuredBuffer");
1962 return false;
1963 case EHTokRWByteAddressBuffer:
1964 hasTemplateType = false;
1965 break;
1966 case EHTokRWStructuredBuffer:
1967 break;
1968 case EHTokStructuredBuffer:
1969 readonly = true;
1970 break;
1971 default:
1972 return false; // not a structure buffer type
1973 }
1974
1975 advanceToken(); // consume the structure keyword
1976
1977 // type on which this StructedBuffer is templatized. E.g, StructedBuffer<MyStruct> ==> MyStruct
1978 TType* templateType = new TType;
1979
1980 if (hasTemplateType) {
1981 if (! acceptTokenClass(EHTokLeftAngle)) {
1982 expected("left angle bracket");
1983 return false;
1984 }
1985
1986 if (! acceptType(*templateType)) {
1987 expected("type");
1988 return false;
1989 }
1990 if (! acceptTokenClass(EHTokRightAngle)) {
1991 expected("right angle bracket");
1992 return false;
1993 }
1994 } else {
1995 // byte address buffers have no explicit type.
1996 TType uintType(EbtUint, storage);
1997 templateType->shallowCopy(uintType);
1998 }
1999
2000 // Create an unsized array out of that type.
2001 // TODO: does this work if it's already an array type?
2002 TArraySizes unsizedArray;
2003 unsizedArray.addInnerSize(UnsizedArraySize);
2004 templateType->newArraySizes(unsizedArray);
steve-lunarg40efe5c2017-03-06 12:01:44 -07002005 templateType->getQualifier().storage = storage;
steve-lunargdd8287a2017-02-23 18:04:12 -07002006
2007 // field name is canonical for all structbuffers
2008 templateType->setFieldName("@data");
steve-lunarg5da1f032017-02-12 17:50:28 -07002009
2010 // Create block type. TODO: hidden internal uint member when needed
steve-lunargdd8287a2017-02-23 18:04:12 -07002011
steve-lunarg5da1f032017-02-12 17:50:28 -07002012 TTypeList* blockStruct = new TTypeList;
2013 TTypeLoc member = { templateType, token.loc };
2014 blockStruct->push_back(member);
2015
steve-lunargdd8287a2017-02-23 18:04:12 -07002016 // This is the type of the buffer block (SSBO)
steve-lunarg5da1f032017-02-12 17:50:28 -07002017 TType blockType(blockStruct, "", templateType->getQualifier());
2018
steve-lunargdd8287a2017-02-23 18:04:12 -07002019 blockType.getQualifier().storage = storage;
2020 blockType.getQualifier().readonly = readonly;
2021
2022 // We may have created an equivalent type before, in which case we should use its
2023 // deep structure.
2024 parseContext.shareStructBufferType(blockType);
2025
steve-lunarg5da1f032017-02-12 17:50:28 -07002026 type.shallowCopy(blockType);
2027
2028 return true;
2029}
2030
John Kesseniche6e74942016-06-11 16:43:14 -06002031// struct_declaration_list
2032// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
2033//
2034// struct_declaration
2035// : fully_specified_type struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002036// | fully_specified_type IDENTIFIER function_parameters post_decls compound_statement // member-function definition
John Kesseniche6e74942016-06-11 16:43:14 -06002037//
2038// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06002039// : IDENTIFIER post_decls
2040// | IDENTIFIER array_specifier post_decls
John Kessenich54ee28f2017-03-11 14:13:00 -07002041// | IDENTIFIER function_parameters post_decls // member-function prototype
John Kesseniche6e74942016-06-11 16:43:14 -06002042//
John Kessenichaa3c64c2017-03-28 09:52:38 -06002043bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList, TIntermNode*& nodeList,
John Kessenichf3d88bd2017-03-19 12:24:29 -06002044 TVector<TFunctionDeclarator>& declarators)
John Kesseniche6e74942016-06-11 16:43:14 -06002045{
2046 typeList = new TTypeList();
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002047 HlslToken idToken;
John Kesseniche6e74942016-06-11 16:43:14 -06002048
2049 do {
2050 // success on seeing the RIGHT_BRACE coming up
2051 if (peekTokenClass(EHTokRightBrace))
John Kessenichb16f7e62017-03-11 19:32:47 -07002052 break;
John Kesseniche6e74942016-06-11 16:43:14 -06002053
2054 // struct_declaration
John Kessenich54ee28f2017-03-11 14:13:00 -07002055
2056 bool declarator_list = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002057
2058 // fully_specified_type
2059 TType memberType;
John Kessenich54ee28f2017-03-11 14:13:00 -07002060 if (! acceptFullySpecifiedType(memberType, nodeList)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002061 expected("member type");
2062 return false;
2063 }
2064
2065 // struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002066 bool functionDefinitionAccepted = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002067 do {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002068 if (! acceptIdentifier(idToken)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002069 expected("member name");
2070 return false;
2071 }
2072
John Kessenich54ee28f2017-03-11 14:13:00 -07002073 if (peekTokenClass(EHTokLeftParen)) {
2074 // function_parameters
2075 if (!declarator_list) {
John Kessenichb16f7e62017-03-11 19:32:47 -07002076 declarators.resize(declarators.size() + 1);
2077 // request a token stream for deferred processing
John Kessenichf3d88bd2017-03-19 12:24:29 -06002078 functionDefinitionAccepted = acceptMemberFunctionDefinition(nodeList, memberType, *idToken.string,
2079 declarators.back());
John Kessenich54ee28f2017-03-11 14:13:00 -07002080 if (functionDefinitionAccepted)
2081 break;
2082 }
2083 expected("member-function definition");
2084 return false;
2085 } else {
2086 // add it to the list of members
2087 TTypeLoc member = { new TType(EbtVoid), token.loc };
2088 member.type->shallowCopy(memberType);
2089 member.type->setFieldName(*idToken.string);
2090 typeList->push_back(member);
John Kesseniche6e74942016-06-11 16:43:14 -06002091
John Kessenich54ee28f2017-03-11 14:13:00 -07002092 // array_specifier
2093 TArraySizes* arraySizes = nullptr;
2094 acceptArraySpecifier(arraySizes);
2095 if (arraySizes)
2096 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06002097
John Kessenich54ee28f2017-03-11 14:13:00 -07002098 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06002099
John Kessenich54ee28f2017-03-11 14:13:00 -07002100 // EQUAL assignment_expression
2101 if (acceptTokenClass(EHTokAssign)) {
2102 parseContext.warn(idToken.loc, "struct-member initializers ignored", "typedef", "");
2103 TIntermTyped* expressionNode = nullptr;
2104 if (! acceptAssignmentExpression(expressionNode)) {
2105 expected("initializer");
2106 return false;
2107 }
John Kessenich18adbdb2017-02-02 15:16:20 -07002108 }
2109 }
John Kesseniche6e74942016-06-11 16:43:14 -06002110 // success on seeing the SEMICOLON coming up
2111 if (peekTokenClass(EHTokSemicolon))
2112 break;
2113
2114 // COMMA
John Kessenich54ee28f2017-03-11 14:13:00 -07002115 if (acceptTokenClass(EHTokComma))
2116 declarator_list = true;
2117 else {
John Kesseniche6e74942016-06-11 16:43:14 -06002118 expected(",");
2119 return false;
2120 }
2121
2122 } while (true);
2123
2124 // SEMI_COLON
John Kessenich54ee28f2017-03-11 14:13:00 -07002125 if (! functionDefinitionAccepted && ! acceptTokenClass(EHTokSemicolon)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002126 expected(";");
2127 return false;
2128 }
2129
2130 } while (true);
John Kessenichb16f7e62017-03-11 19:32:47 -07002131
John Kessenichb16f7e62017-03-11 19:32:47 -07002132 return true;
John Kesseniche6e74942016-06-11 16:43:14 -06002133}
2134
John Kessenich54ee28f2017-03-11 14:13:00 -07002135// member_function_definition
2136// | function_parameters post_decls compound_statement
2137//
2138// Expects type to have EvqGlobal for a static member and
2139// EvqTemporary for non-static member.
John Kessenichf3d88bd2017-03-19 12:24:29 -06002140bool HlslGrammar::acceptMemberFunctionDefinition(TIntermNode*& nodeList, const TType& type, const TString& memberName,
2141 TFunctionDeclarator& declarator)
John Kessenich54ee28f2017-03-11 14:13:00 -07002142{
John Kessenich54ee28f2017-03-11 14:13:00 -07002143 bool accepted = false;
2144
John Kessenich4dc835c2017-03-28 23:43:10 -06002145 const TString* functionName = &memberName;
2146 parseContext.getFullNamespaceName(functionName);
John Kessenich088d52b2017-03-11 17:55:28 -07002147 declarator.function = new TFunction(functionName, type);
John Kessenich4960baa2017-03-19 18:09:59 -06002148 if (type.getQualifier().storage == EvqTemporary)
2149 declarator.function->setImplicitThis();
John Kessenich37789792017-03-21 23:56:40 -06002150 else
2151 declarator.function->setIllegalImplicitThis();
John Kessenich54ee28f2017-03-11 14:13:00 -07002152
2153 // function_parameters
John Kessenich088d52b2017-03-11 17:55:28 -07002154 if (acceptFunctionParameters(*declarator.function)) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002155 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -07002156 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich54ee28f2017-03-11 14:13:00 -07002157
2158 // compound_statement (function body definition)
2159 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich088d52b2017-03-11 17:55:28 -07002160 declarator.loc = token.loc;
John Kessenichf3d88bd2017-03-19 12:24:29 -06002161 declarator.body = new TVector<HlslToken>;
2162 accepted = acceptFunctionDefinition(declarator, nodeList, declarator.body);
John Kessenich54ee28f2017-03-11 14:13:00 -07002163 }
2164 } else
2165 expected("function parameter list");
2166
John Kessenich54ee28f2017-03-11 14:13:00 -07002167 return accepted;
2168}
2169
John Kessenich5f934b02016-03-13 17:58:25 -06002170// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06002171// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06002172// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002173//
2174bool HlslGrammar::acceptFunctionParameters(TFunction& function)
2175{
John Kessenich078d7f22016-03-14 10:02:11 -06002176 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002177 if (! acceptTokenClass(EHTokLeftParen))
2178 return false;
2179
John Kessenich71351de2016-06-08 12:50:56 -06002180 // VOID RIGHT_PAREN
2181 if (! acceptTokenClass(EHTokVoid)) {
2182 do {
2183 // parameter_declaration
2184 if (! acceptParameterDeclaration(function))
2185 break;
John Kessenich5f934b02016-03-13 17:58:25 -06002186
John Kessenich71351de2016-06-08 12:50:56 -06002187 // COMMA
2188 if (! acceptTokenClass(EHTokComma))
2189 break;
2190 } while (true);
2191 }
John Kessenich5f934b02016-03-13 17:58:25 -06002192
John Kessenich078d7f22016-03-14 10:02:11 -06002193 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002194 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002195 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06002196 return false;
2197 }
2198
2199 return true;
2200}
2201
steve-lunarg26d31452016-12-23 18:56:57 -07002202// default_parameter_declaration
2203// : EQUAL conditional_expression
2204// : EQUAL initializer
2205bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTyped*& node)
2206{
2207 node = nullptr;
2208
2209 // Valid not to have a default_parameter_declaration
2210 if (!acceptTokenClass(EHTokAssign))
2211 return true;
2212
2213 if (!acceptConditionalExpression(node)) {
2214 if (!acceptInitializer(node))
2215 return false;
2216
2217 // For initializer lists, we have to const-fold into a constructor for the type, so build
2218 // that.
2219 TFunction* constructor = parseContext.handleConstructorCall(token.loc, type);
2220 if (constructor == nullptr) // cannot construct
2221 return false;
2222
2223 TIntermTyped* arguments = nullptr;
John Kessenichecba76f2017-01-06 00:34:48 -07002224 for (int i = 0; i < int(node->getAsAggregate()->getSequence().size()); i++)
steve-lunarg26d31452016-12-23 18:56:57 -07002225 parseContext.handleFunctionArgument(constructor, arguments, node->getAsAggregate()->getSequence()[i]->getAsTyped());
John Kessenichecba76f2017-01-06 00:34:48 -07002226
steve-lunarg26d31452016-12-23 18:56:57 -07002227 node = parseContext.handleFunctionCall(token.loc, constructor, node);
2228 }
2229
2230 // If this is simply a constant, we can use it directly.
2231 if (node->getAsConstantUnion())
2232 return true;
2233
2234 // Otherwise, it has to be const-foldable.
2235 TIntermTyped* origNode = node;
2236
2237 node = intermediate.fold(node->getAsAggregate());
2238
2239 if (node != nullptr && origNode != node)
2240 return true;
2241
2242 parseContext.error(token.loc, "invalid default parameter value", "", "");
2243
2244 return false;
2245}
2246
John Kessenich5f934b02016-03-13 17:58:25 -06002247// parameter_declaration
steve-lunarg26d31452016-12-23 18:56:57 -07002248// : fully_specified_type post_decls [ = default_parameter_declaration ]
2249// | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
John Kessenich5f934b02016-03-13 17:58:25 -06002250//
2251bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
2252{
2253 // fully_specified_type
2254 TType* type = new TType;
2255 if (! acceptFullySpecifiedType(*type))
2256 return false;
2257
2258 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06002259 HlslToken idToken;
2260 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06002261
John Kessenich19b92ff2016-06-19 11:50:34 -06002262 // array_specifier
2263 TArraySizes* arraySizes = nullptr;
2264 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002265 if (arraySizes) {
2266 if (arraySizes->isImplicit()) {
2267 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
2268 return false;
2269 }
2270
John Kessenich19b92ff2016-06-19 11:50:34 -06002271 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002272 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002273
2274 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06002275 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06002276
steve-lunarg26d31452016-12-23 18:56:57 -07002277 TIntermTyped* defaultValue;
2278 if (!acceptDefaultParameterDeclaration(*type, defaultValue))
2279 return false;
2280
John Kessenich5aa59e22016-06-17 15:50:47 -06002281 parseContext.paramFix(*type);
2282
steve-lunarg26d31452016-12-23 18:56:57 -07002283 // If any prior parameters have default values, all the parameters after that must as well.
2284 if (defaultValue == nullptr && function.getDefaultParamCount() > 0) {
2285 parseContext.error(idToken.loc, "invalid parameter after default value parameters", idToken.string->c_str(), "");
2286 return false;
2287 }
2288
2289 TParameter param = { idToken.string, type, defaultValue };
John Kessenich5f934b02016-03-13 17:58:25 -06002290 function.addParameter(param);
2291
2292 return true;
2293}
2294
2295// Do the work to create the function definition in addition to
2296// parsing the body (compound_statement).
John Kessenichb16f7e62017-03-11 19:32:47 -07002297//
2298// If 'deferredTokens' are passed in, just get the token stream,
2299// don't process.
2300//
2301bool HlslGrammar::acceptFunctionDefinition(TFunctionDeclarator& declarator, TIntermNode*& nodeList,
2302 TVector<HlslToken>* deferredTokens)
John Kessenich5f934b02016-03-13 17:58:25 -06002303{
John Kessenich088d52b2017-03-11 17:55:28 -07002304 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, false /* not prototype */);
John Kessenich5f934b02016-03-13 17:58:25 -06002305
John Kessenichb16f7e62017-03-11 19:32:47 -07002306 if (deferredTokens)
2307 return captureBlockTokens(*deferredTokens);
2308 else
John Kessenich4960baa2017-03-19 18:09:59 -06002309 return acceptFunctionBody(declarator, nodeList);
John Kessenich088d52b2017-03-11 17:55:28 -07002310}
2311
2312bool HlslGrammar::acceptFunctionBody(TFunctionDeclarator& declarator, TIntermNode*& nodeList)
2313{
2314 // we might get back an entry-point
John Kessenichca71d942017-03-07 20:44:09 -07002315 TIntermNode* entryPointNode = nullptr;
2316
John Kessenich077e0522016-06-09 02:02:17 -06002317 // This does a pushScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002318 TIntermNode* functionNode = parseContext.handleFunctionDefinition(declarator.loc, *declarator.function,
2319 declarator.attributes, entryPointNode);
John Kessenich5f934b02016-03-13 17:58:25 -06002320
2321 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002322 TIntermNode* functionBody = nullptr;
John Kessenich02467d82017-01-19 15:41:47 -07002323 if (! acceptCompoundStatement(functionBody))
2324 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002325
John Kessenich54ee28f2017-03-11 14:13:00 -07002326 // this does a popScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002327 parseContext.handleFunctionBody(declarator.loc, *declarator.function, functionBody, functionNode);
John Kessenichca71d942017-03-07 20:44:09 -07002328
2329 // Hook up the 1 or 2 function definitions.
2330 nodeList = intermediate.growAggregate(nodeList, functionNode);
2331 nodeList = intermediate.growAggregate(nodeList, entryPointNode);
John Kessenich02467d82017-01-19 15:41:47 -07002332
2333 return true;
John Kessenich5f934b02016-03-13 17:58:25 -06002334}
2335
John Kessenich0d2b6de2016-06-05 11:23:11 -06002336// Accept an expression with parenthesis around it, where
2337// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002338// syntactically required ones like in "if ( expression )".
2339//
2340// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06002341//
2342// Note this one is not set up to be speculative; as it gives
2343// errors if not found.
2344//
2345bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
2346{
2347 // LEFT_PAREN
2348 if (! acceptTokenClass(EHTokLeftParen))
2349 expected("(");
2350
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002351 bool decl = false;
2352 TIntermNode* declNode = nullptr;
2353 decl = acceptControlDeclaration(declNode);
2354 if (decl) {
2355 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
2356 expected("initialized declaration");
2357 return false;
2358 } else
2359 expression = declNode->getAsTyped();
2360 } else {
2361 // no declaration
2362 if (! acceptExpression(expression)) {
2363 expected("expression");
2364 return false;
2365 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06002366 }
2367
2368 // RIGHT_PAREN
2369 if (! acceptTokenClass(EHTokRightParen))
2370 expected(")");
2371
2372 return true;
2373}
2374
John Kessenich34fb0362016-05-03 23:17:20 -06002375// The top-level full expression recognizer.
2376//
John Kessenich87142c72016-03-12 20:24:24 -07002377// expression
John Kessenich34fb0362016-05-03 23:17:20 -06002378// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07002379//
2380bool HlslGrammar::acceptExpression(TIntermTyped*& node)
2381{
LoopDawgef764a22016-06-03 09:17:51 -06002382 node = nullptr;
2383
John Kessenich34fb0362016-05-03 23:17:20 -06002384 // assignment_expression
2385 if (! acceptAssignmentExpression(node))
2386 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002387
John Kessenich34fb0362016-05-03 23:17:20 -06002388 if (! peekTokenClass(EHTokComma))
2389 return true;
2390
2391 do {
2392 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06002393 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06002394 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06002395
John Kessenich34fb0362016-05-03 23:17:20 -06002396 // ... assignment_expression
2397 TIntermTyped* rightNode = nullptr;
2398 if (! acceptAssignmentExpression(rightNode)) {
2399 expected("assignment expression");
2400 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002401 }
2402
John Kessenich34fb0362016-05-03 23:17:20 -06002403 node = intermediate.addComma(node, rightNode, loc);
2404
2405 if (! peekTokenClass(EHTokComma))
2406 return true;
2407 } while (true);
2408}
2409
John Kessenich07354242016-07-01 19:58:06 -06002410// initializer
John Kessenich98ad4852016-11-27 17:39:07 -07002411// : LEFT_BRACE RIGHT_BRACE
2412// | LEFT_BRACE initializer_list RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002413//
2414// initializer_list
2415// : assignment_expression COMMA assignment_expression COMMA ...
2416//
2417bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
2418{
2419 // LEFT_BRACE
2420 if (! acceptTokenClass(EHTokLeftBrace))
2421 return false;
2422
John Kessenich98ad4852016-11-27 17:39:07 -07002423 // RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002424 TSourceLoc loc = token.loc;
John Kessenich98ad4852016-11-27 17:39:07 -07002425 if (acceptTokenClass(EHTokRightBrace)) {
2426 // a zero-length initializer list
2427 node = intermediate.makeAggregate(loc);
2428 return true;
2429 }
2430
2431 // initializer_list
John Kessenich07354242016-07-01 19:58:06 -06002432 node = nullptr;
2433 do {
2434 // assignment_expression
2435 TIntermTyped* expr;
2436 if (! acceptAssignmentExpression(expr)) {
2437 expected("assignment expression in initializer list");
2438 return false;
2439 }
2440 node = intermediate.growAggregate(node, expr, loc);
2441
2442 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002443 if (acceptTokenClass(EHTokComma)) {
2444 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
2445 return true;
John Kessenich07354242016-07-01 19:58:06 -06002446 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002447 }
John Kessenich07354242016-07-01 19:58:06 -06002448
2449 // RIGHT_BRACE
2450 if (acceptTokenClass(EHTokRightBrace))
2451 return true;
2452
2453 expected(", or }");
2454 return false;
2455 } while (true);
2456}
2457
John Kessenich34fb0362016-05-03 23:17:20 -06002458// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06002459// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06002460//
2461// a op (b op (c op d))
2462//
2463// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06002464// : initializer
2465// | conditional_expression
2466// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06002467//
2468bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
2469{
John Kessenich07354242016-07-01 19:58:06 -06002470 // initializer
2471 if (peekTokenClass(EHTokLeftBrace)) {
2472 if (acceptInitializer(node))
2473 return true;
2474
2475 expected("initializer");
2476 return false;
2477 }
2478
John Kessenich00957f82016-07-27 10:39:57 -06002479 // conditional_expression
2480 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06002481 return false;
2482
John Kessenich07354242016-07-01 19:58:06 -06002483 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06002484 TOperator assignOp = HlslOpMap::assignment(peek());
2485 if (assignOp == EOpNull)
2486 return true;
2487
John Kessenich00957f82016-07-27 10:39:57 -06002488 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06002489 TSourceLoc loc = token.loc;
2490 advanceToken();
2491
John Kessenich00957f82016-07-27 10:39:57 -06002492 // conditional_expression assign_op conditional_expression ...
2493 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06002494 // gets the right-to-left associativity.
2495 TIntermTyped* rightNode = nullptr;
2496 if (! acceptAssignmentExpression(rightNode)) {
2497 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06002498 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002499 }
2500
John Kessenichd21baed2016-09-16 03:05:12 -06002501 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
steve-lunarg90707962016-10-07 19:35:40 -06002502 node = parseContext.handleLvalue(loc, "assign", node);
2503
John Kessenichfea226b2016-07-28 17:53:56 -06002504 if (node == nullptr) {
2505 parseContext.error(loc, "could not create assignment", "", "");
2506 return false;
2507 }
John Kessenich34fb0362016-05-03 23:17:20 -06002508
2509 if (! peekTokenClass(EHTokComma))
2510 return true;
2511
2512 return true;
2513}
2514
John Kessenich00957f82016-07-27 10:39:57 -06002515// Accept a conditional expression, which associates right-to-left,
2516// accomplished by the "true" expression calling down to lower
2517// precedence levels than this level.
2518//
2519// conditional_expression
2520// : binary_expression
2521// | binary_expression QUESTION expression COLON assignment_expression
2522//
2523bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
2524{
2525 // binary_expression
2526 if (! acceptBinaryExpression(node, PlLogicalOr))
2527 return false;
2528
2529 if (! acceptTokenClass(EHTokQuestion))
2530 return true;
2531
John Kessenich7e997e22017-03-30 22:09:30 -06002532 node = parseContext.convertConditionalExpression(token.loc, node);
2533 if (node == nullptr)
2534 return false;
2535
John Kessenich00957f82016-07-27 10:39:57 -06002536 TIntermTyped* trueNode = nullptr;
2537 if (! acceptExpression(trueNode)) {
2538 expected("expression after ?");
2539 return false;
2540 }
2541 TSourceLoc loc = token.loc;
2542
2543 if (! acceptTokenClass(EHTokColon)) {
2544 expected(":");
2545 return false;
2546 }
2547
2548 TIntermTyped* falseNode = nullptr;
2549 if (! acceptAssignmentExpression(falseNode)) {
2550 expected("expression after :");
2551 return false;
2552 }
2553
2554 node = intermediate.addSelection(node, trueNode, falseNode, loc);
2555
2556 return true;
2557}
2558
John Kessenich34fb0362016-05-03 23:17:20 -06002559// Accept a binary expression, for binary operations that
2560// associate left-to-right. This is, it is implicit, for example
2561//
2562// ((a op b) op c) op d
2563//
2564// binary_expression
2565// : expression op expression op expression ...
2566//
2567// where 'expression' is the next higher level in precedence.
2568//
2569bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
2570{
2571 if (precedenceLevel > PlMul)
2572 return acceptUnaryExpression(node);
2573
2574 // assignment_expression
2575 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
2576 return false;
2577
John Kessenich34fb0362016-05-03 23:17:20 -06002578 do {
John Kessenich64076ed2016-07-28 21:43:17 -06002579 TOperator op = HlslOpMap::binary(peek());
2580 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
2581 if (tokenLevel < precedenceLevel)
2582 return true;
2583
John Kessenich34fb0362016-05-03 23:17:20 -06002584 // ... op
2585 TSourceLoc loc = token.loc;
2586 advanceToken();
2587
2588 // ... expression
2589 TIntermTyped* rightNode = nullptr;
2590 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
2591 expected("expression");
2592 return false;
2593 }
2594
2595 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06002596 if (node == nullptr) {
2597 parseContext.error(loc, "Could not perform requested binary operation", "", "");
2598 return false;
2599 }
John Kessenich34fb0362016-05-03 23:17:20 -06002600 } while (true);
2601}
2602
2603// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06002604// : (type) unary_expression
2605// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06002606// | - unary_expression
2607// | ! unary_expression
2608// | ~ unary_expression
2609// | ++ unary_expression
2610// | -- unary_expression
2611// | postfix_expression
2612//
2613bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
2614{
John Kessenich1cc1a282016-06-03 16:55:49 -06002615 // (type) unary_expression
2616 // Have to look two steps ahead, because this could be, e.g., a
2617 // postfix_expression instead, since that also starts with at "(".
2618 if (acceptTokenClass(EHTokLeftParen)) {
2619 TType castType;
2620 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06002621 if (acceptTokenClass(EHTokRightParen)) {
2622 // We've matched "(type)" now, get the expression to cast
2623 TSourceLoc loc = token.loc;
2624 if (! acceptUnaryExpression(node))
2625 return false;
2626
2627 // Hook it up like a constructor
2628 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
2629 if (constructorFunction == nullptr) {
2630 expected("type that can be constructed");
2631 return false;
2632 }
2633 TIntermTyped* arguments = nullptr;
2634 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
2635 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
2636
2637 return true;
2638 } else {
2639 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
2640 // the '(int' part. We must back up twice.
2641 recedeToken();
2642 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06002643 }
John Kessenich1cc1a282016-06-03 16:55:49 -06002644 } else {
2645 // This isn't a type cast, but it still started "(", so if it is a
2646 // unary expression, it can only be a postfix_expression, so try that.
2647 // Back it up first.
2648 recedeToken();
2649 return acceptPostfixExpression(node);
2650 }
2651 }
2652
2653 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06002654 TOperator unaryOp = HlslOpMap::preUnary(peek());
John Kessenichecba76f2017-01-06 00:34:48 -07002655
John Kessenich1cc1a282016-06-03 16:55:49 -06002656 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06002657 if (unaryOp == EOpNull)
2658 return acceptPostfixExpression(node);
2659
2660 // op unary_expression
2661 TSourceLoc loc = token.loc;
2662 advanceToken();
2663 if (! acceptUnaryExpression(node))
2664 return false;
2665
2666 // + is a no-op
2667 if (unaryOp == EOpAdd)
2668 return true;
2669
2670 node = intermediate.addUnaryMath(unaryOp, node, loc);
steve-lunarge5921f12016-10-15 10:29:58 -06002671
2672 // These unary ops require lvalues
2673 if (unaryOp == EOpPreIncrement || unaryOp == EOpPreDecrement)
2674 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002675
2676 return node != nullptr;
2677}
2678
2679// postfix_expression
2680// : LEFT_PAREN expression RIGHT_PAREN
2681// | literal
2682// | constructor
John Kessenich8f9fdc92017-03-30 16:22:26 -06002683// | IDENTIFIER [ COLONCOLON IDENTIFIER [ COLONCOLON IDENTIFIER ... ] ]
John Kessenich34fb0362016-05-03 23:17:20 -06002684// | function_call
2685// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
2686// | postfix_expression DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002687// | postfix_expression DOT IDENTIFIER arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06002688// | postfix_expression arguments
John Kessenich34fb0362016-05-03 23:17:20 -06002689// | postfix_expression INC_OP
2690// | postfix_expression DEC_OP
2691//
2692bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
2693{
2694 // Not implemented as self-recursive:
John Kessenich54ee28f2017-03-11 14:13:00 -07002695 // The logical "right recursion" is done with a loop at the end
John Kessenich34fb0362016-05-03 23:17:20 -06002696
2697 // idToken will pick up either a variable or a function name in a function call
2698 HlslToken idToken;
2699
John Kessenich21472ae2016-06-04 11:46:33 -06002700 // Find something before the postfix operations, as they can't operate
2701 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07002702 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06002703 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002704 if (! acceptExpression(node)) {
2705 expected("expression");
2706 return false;
2707 }
2708 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002709 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002710 return false;
2711 }
John Kessenich34fb0362016-05-03 23:17:20 -06002712 } else if (acceptLiteral(node)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002713 // literal (nothing else to do yet)
John Kessenich34fb0362016-05-03 23:17:20 -06002714 } else if (acceptConstructor(node)) {
2715 // constructor (nothing else to do yet)
2716 } else if (acceptIdentifier(idToken)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002717 // user-type, namespace name, variable, or function name
2718 TString* fullName = idToken.string;
2719 while (acceptTokenClass(EHTokColonColon)) {
2720 // user-type or namespace name
2721 fullName = NewPoolTString(fullName->c_str());
2722 fullName->append(parseContext.scopeMangler);
2723 if (acceptIdentifier(idToken))
2724 fullName->append(*idToken.string);
2725 else {
2726 expected("identifier after ::");
John Kessenich54ee28f2017-03-11 14:13:00 -07002727 return false;
2728 }
John Kessenich8f9fdc92017-03-30 16:22:26 -06002729 }
2730 if (! peekTokenClass(EHTokLeftParen)) {
2731 node = parseContext.handleVariable(idToken.loc, fullName);
2732 } else if (acceptFunctionCall(idToken.loc, *fullName, node, nullptr)) {
John Kessenich34fb0362016-05-03 23:17:20 -06002733 // function_call (nothing else to do yet)
2734 } else {
2735 expected("function call arguments");
2736 return false;
2737 }
John Kessenich21472ae2016-06-04 11:46:33 -06002738 } else {
2739 // nothing found, can't post operate
2740 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002741 }
2742
steve-lunarga2b01a02016-11-28 17:09:54 -07002743 // This is to guarantee we do this no matter how we get out of the stack frame.
2744 // This way there's no bug if an early return forgets to do it.
2745 struct tFinalize {
2746 tFinalize(HlslParseContext& p) : parseContext(p) { }
2747 ~tFinalize() { parseContext.finalizeFlattening(); }
John Kessenichf8d0d8c2017-02-08 17:31:03 -07002748 HlslParseContext& parseContext;
John Kessenich32fd5d22017-02-02 14:55:02 -07002749 private:
John Kessenichca71d942017-03-07 20:44:09 -07002750 const tFinalize& operator=(const tFinalize&) { return *this; }
John Kessenichefeefd92017-03-01 13:12:26 -07002751 tFinalize(const tFinalize& f) : parseContext(f.parseContext) { }
steve-lunarga2b01a02016-11-28 17:09:54 -07002752 } finalize(parseContext);
2753
2754 // Initialize the flattening accumulation data, so we can track data across multiple bracket or
2755 // dot operators. This can also be nested, e.g, for [], so we have to track each nesting
2756 // level: hence the init and finalize. Even though in practice these must be
2757 // constants, they are parsed no matter what.
2758 parseContext.initFlattening();
2759
John Kessenich21472ae2016-06-04 11:46:33 -06002760 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06002761 do {
2762 TSourceLoc loc = token.loc;
2763 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07002764
John Kessenich34fb0362016-05-03 23:17:20 -06002765 // Consume only a valid post-unary operator, otherwise we are done.
2766 switch (postOp) {
2767 case EOpIndexDirectStruct:
2768 case EOpIndexIndirect:
2769 case EOpPostIncrement:
2770 case EOpPostDecrement:
John Kessenich54ee28f2017-03-11 14:13:00 -07002771 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002772 advanceToken();
2773 break;
2774 default:
2775 return true;
2776 }
John Kessenich87142c72016-03-12 20:24:24 -07002777
John Kessenich34fb0362016-05-03 23:17:20 -06002778 // We have a valid post-unary operator, process it.
2779 switch (postOp) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002780 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002781 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06002782 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002783 // DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002784 // includes swizzles, member variables, and member functions
John Kessenich93a162a2016-06-17 17:16:27 -06002785 HlslToken field;
2786 if (! acceptIdentifier(field)) {
2787 expected("swizzle or member");
2788 return false;
2789 }
LoopDawg4886f692016-06-29 10:58:58 -06002790
John Kessenich516d92d2017-03-08 20:09:03 -07002791 if (peekTokenClass(EHTokLeftParen)) {
2792 // member function
2793 TIntermTyped* thisNode = node;
LoopDawg4886f692016-06-29 10:58:58 -06002794
John Kessenich516d92d2017-03-08 20:09:03 -07002795 // arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06002796 if (! acceptFunctionCall(field.loc, *field.string, node, thisNode)) {
LoopDawg4886f692016-06-29 10:58:58 -06002797 expected("function parameters");
2798 return false;
2799 }
John Kessenich516d92d2017-03-08 20:09:03 -07002800 } else
2801 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06002802
John Kessenich34fb0362016-05-03 23:17:20 -06002803 break;
John Kessenich93a162a2016-06-17 17:16:27 -06002804 }
John Kessenich34fb0362016-05-03 23:17:20 -06002805 case EOpIndexIndirect:
2806 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002807 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06002808 TIntermTyped* indexNode = nullptr;
2809 if (! acceptExpression(indexNode) ||
2810 ! peekTokenClass(EHTokRightBracket)) {
2811 expected("expression followed by ']'");
2812 return false;
2813 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002814 advanceToken();
2815 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
2816 break;
John Kessenich34fb0362016-05-03 23:17:20 -06002817 }
2818 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002819 // INC_OP
2820 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06002821 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002822 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06002823 node = intermediate.addUnaryMath(postOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06002824 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002825 break;
2826 default:
2827 assert(0);
2828 break;
2829 }
2830 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07002831}
2832
John Kessenichd016be12016-03-13 11:24:20 -06002833// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06002834// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06002835//
2836bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
2837{
2838 // type
2839 TType type;
2840 if (acceptType(type)) {
2841 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
2842 if (constructorFunction == nullptr)
2843 return false;
2844
2845 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06002846 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06002847 if (! acceptArguments(constructorFunction, arguments)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002848 // It's possible this is a type keyword used as an identifier. Put the token back
2849 // for later use.
2850 recedeToken();
John Kessenichd016be12016-03-13 11:24:20 -06002851 return false;
2852 }
2853
2854 // hook it up
2855 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
2856
2857 return true;
2858 }
2859
2860 return false;
2861}
2862
John Kessenich34fb0362016-05-03 23:17:20 -06002863// The function_call identifier was already recognized, and passed in as idToken.
2864//
2865// function_call
2866// : [idToken] arguments
2867//
John Kessenich8f9fdc92017-03-30 16:22:26 -06002868bool HlslGrammar::acceptFunctionCall(const TSourceLoc& loc, TString& name, TIntermTyped*& node, TIntermTyped* baseObject)
John Kessenich34fb0362016-05-03 23:17:20 -06002869{
John Kessenich54ee28f2017-03-11 14:13:00 -07002870 // name
2871 TString* functionName = nullptr;
John Kessenich8f9fdc92017-03-30 16:22:26 -06002872 if (baseObject == nullptr) {
2873 functionName = &name;
2874 } else if (parseContext.isBuiltInMethod(loc, baseObject, name)) {
John Kessenich4960baa2017-03-19 18:09:59 -06002875 // Built-in methods are not in the symbol table as methods, but as global functions
2876 // taking an explicit 'this' as the first argument.
steve-lunarge7d07522017-03-19 18:12:37 -06002877 functionName = NewPoolTString(BUILTIN_PREFIX);
John Kessenich8f9fdc92017-03-30 16:22:26 -06002878 functionName->append(name);
John Kessenich4960baa2017-03-19 18:09:59 -06002879 } else {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002880 if (! baseObject->getType().isStruct()) {
2881 expected("structure");
2882 return false;
2883 }
John Kessenich54ee28f2017-03-11 14:13:00 -07002884 functionName = NewPoolTString("");
John Kessenich8f9fdc92017-03-30 16:22:26 -06002885 functionName->append(baseObject->getType().getTypeName());
John Kessenichf3d88bd2017-03-19 12:24:29 -06002886 parseContext.addScopeMangler(*functionName);
John Kessenich8f9fdc92017-03-30 16:22:26 -06002887 functionName->append(name);
John Kessenich5f12d2f2017-03-11 09:39:55 -07002888 }
LoopDawg4886f692016-06-29 10:58:58 -06002889
John Kessenich54ee28f2017-03-11 14:13:00 -07002890 // function
2891 TFunction* function = new TFunction(functionName, TType(EbtVoid));
2892
2893 // arguments
John Kessenich54ee28f2017-03-11 14:13:00 -07002894 TIntermTyped* arguments = nullptr;
John Kessenichdfbdd9e2017-03-19 13:10:28 -06002895 if (baseObject != nullptr) {
2896 // Non-static member functions have an implicit first argument of the base object.
John Kessenich54ee28f2017-03-11 14:13:00 -07002897 parseContext.handleFunctionArgument(function, arguments, baseObject);
John Kessenichdfbdd9e2017-03-19 13:10:28 -06002898 }
John Kessenich4678ca92016-05-13 09:33:42 -06002899 if (! acceptArguments(function, arguments))
2900 return false;
2901
John Kessenich54ee28f2017-03-11 14:13:00 -07002902 // call
John Kessenich8f9fdc92017-03-30 16:22:26 -06002903 node = parseContext.handleFunctionCall(loc, function, arguments);
John Kessenich4678ca92016-05-13 09:33:42 -06002904
2905 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002906}
2907
John Kessenich87142c72016-03-12 20:24:24 -07002908// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002909// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002910//
John Kessenichd016be12016-03-13 11:24:20 -06002911// The arguments are pushed onto the 'function' argument list and
2912// onto the 'arguments' aggregate.
2913//
John Kessenich4678ca92016-05-13 09:33:42 -06002914bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002915{
John Kessenich078d7f22016-03-14 10:02:11 -06002916 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002917 if (! acceptTokenClass(EHTokLeftParen))
2918 return false;
2919
2920 do {
John Kessenichd016be12016-03-13 11:24:20 -06002921 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002922 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002923 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002924 break;
John Kessenichd016be12016-03-13 11:24:20 -06002925
2926 // hook it up
2927 parseContext.handleFunctionArgument(function, arguments, arg);
2928
John Kessenich078d7f22016-03-14 10:02:11 -06002929 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002930 if (! acceptTokenClass(EHTokComma))
2931 break;
2932 } while (true);
2933
John Kessenich078d7f22016-03-14 10:02:11 -06002934 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002935 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002936 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002937 return false;
2938 }
2939
2940 return true;
2941}
2942
2943bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2944{
2945 switch (token.tokenClass) {
2946 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002947 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002948 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002949 case EHTokUintConstant:
2950 node = intermediate.addConstantUnion(token.u, token.loc, true);
2951 break;
John Kessenich87142c72016-03-12 20:24:24 -07002952 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002953 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002954 break;
2955 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002956 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002957 break;
2958 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002959 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002960 break;
John Kessenich86f71382016-09-19 20:23:18 -06002961 case EHTokStringConstant:
steve-lunarg858c9282017-01-07 08:54:10 -07002962 node = intermediate.addConstantUnion(token.string, token.loc, true);
John Kessenich86f71382016-09-19 20:23:18 -06002963 break;
John Kessenich87142c72016-03-12 20:24:24 -07002964
2965 default:
2966 return false;
2967 }
2968
2969 advanceToken();
2970
2971 return true;
2972}
2973
John Kessenich5f934b02016-03-13 17:58:25 -06002974// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002975// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002976//
John Kessenich21472ae2016-06-04 11:46:33 -06002977bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002978{
John Kessenich21472ae2016-06-04 11:46:33 -06002979 TIntermAggregate* compoundStatement = nullptr;
2980
John Kessenich34fb0362016-05-03 23:17:20 -06002981 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002982 if (! acceptTokenClass(EHTokLeftBrace))
2983 return false;
2984
2985 // statement statement ...
2986 TIntermNode* statement = nullptr;
2987 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002988 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2989 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2990 branch->getFlowOp() == EOpDefault)) {
2991 // hook up individual subsequences within a switch statement
2992 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2993 compoundStatement = nullptr;
2994 } else {
2995 // hook it up to the growing compound statement
2996 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2997 }
John Kessenich5f934b02016-03-13 17:58:25 -06002998 }
John Kessenich34fb0362016-05-03 23:17:20 -06002999 if (compoundStatement)
3000 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06003001
John Kessenich21472ae2016-06-04 11:46:33 -06003002 retStatement = compoundStatement;
3003
John Kessenich34fb0362016-05-03 23:17:20 -06003004 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06003005 return acceptTokenClass(EHTokRightBrace);
3006}
3007
John Kessenich0d2b6de2016-06-05 11:23:11 -06003008bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
3009{
3010 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06003011 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003012 parseContext.popScope();
3013
3014 return result;
3015}
3016
John Kessenich077e0522016-06-09 02:02:17 -06003017bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003018{
John Kessenich077e0522016-06-09 02:02:17 -06003019 parseContext.pushScope();
3020 bool result = acceptCompoundStatement(statement);
3021 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06003022
3023 return result;
3024}
3025
John Kessenich5f934b02016-03-13 17:58:25 -06003026// statement
John Kessenich21472ae2016-06-04 11:46:33 -06003027// : attributes attributed_statement
3028//
3029// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003030// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06003031// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06003032// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06003033// | declaration_statement
3034// | selection_statement
3035// | switch_statement
3036// | case_label
3037// | iteration_statement
3038// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003039//
3040bool HlslGrammar::acceptStatement(TIntermNode*& statement)
3041{
John Kessenich21472ae2016-06-04 11:46:33 -06003042 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06003043
John Kessenich21472ae2016-06-04 11:46:33 -06003044 // attributes
steve-lunarg1868b142016-10-20 13:07:10 -06003045 TAttributeMap attributes;
3046 acceptAttributes(attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003047
John Kessenich21472ae2016-06-04 11:46:33 -06003048 // attributed_statement
3049 switch (peek()) {
3050 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06003051 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003052
John Kessenich21472ae2016-06-04 11:46:33 -06003053 case EHTokIf:
3054 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003055
John Kessenich21472ae2016-06-04 11:46:33 -06003056 case EHTokSwitch:
3057 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003058
John Kessenich21472ae2016-06-04 11:46:33 -06003059 case EHTokFor:
3060 case EHTokDo:
3061 case EHTokWhile:
3062 return acceptIterationStatement(statement);
3063
3064 case EHTokContinue:
3065 case EHTokBreak:
3066 case EHTokDiscard:
3067 case EHTokReturn:
3068 return acceptJumpStatement(statement);
3069
3070 case EHTokCase:
3071 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06003072 case EHTokDefault:
3073 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06003074
3075 case EHTokSemicolon:
3076 return acceptTokenClass(EHTokSemicolon);
3077
3078 case EHTokRightBrace:
3079 // Performance: not strictly necessary, but stops a bunch of hunting early,
3080 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06003081 return false;
3082
John Kessenich21472ae2016-06-04 11:46:33 -06003083 default:
3084 {
3085 // declaration
3086 if (acceptDeclaration(statement))
3087 return true;
3088
3089 // expression
3090 TIntermTyped* node;
3091 if (acceptExpression(node))
3092 statement = node;
3093 else
3094 return false;
3095
3096 // SEMICOLON (following an expression)
3097 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06003098 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06003099 return false;
3100 }
3101 }
3102 }
3103
John Kessenich5f934b02016-03-13 17:58:25 -06003104 return true;
John Kessenich87142c72016-03-12 20:24:24 -07003105}
3106
John Kessenich21472ae2016-06-04 11:46:33 -06003107// attributes
3108// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
3109//
3110// attribute:
3111// : UNROLL
3112// | UNROLL LEFT_PAREN literal RIGHT_PAREN
3113// | FASTOPT
3114// | ALLOW_UAV_CONDITION
3115// | BRANCH
3116// | FLATTEN
3117// | FORCECASE
3118// | CALL
steve-lunarg1868b142016-10-20 13:07:10 -06003119// | DOMAIN
3120// | EARLYDEPTHSTENCIL
3121// | INSTANCE
3122// | MAXTESSFACTOR
3123// | OUTPUTCONTROLPOINTS
3124// | OUTPUTTOPOLOGY
3125// | PARTITIONING
3126// | PATCHCONSTANTFUNC
3127// | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
John Kessenich21472ae2016-06-04 11:46:33 -06003128//
steve-lunarg1868b142016-10-20 13:07:10 -06003129void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003130{
steve-lunarg1868b142016-10-20 13:07:10 -06003131 // For now, accept the [ XXX(X) ] syntax, but drop all but
3132 // numthreads, which is used to set the CS local size.
John Kessenich0d2b6de2016-06-05 11:23:11 -06003133 // TODO: subset to correct set? Pass on?
3134 do {
steve-lunarg1868b142016-10-20 13:07:10 -06003135 HlslToken idToken;
3136
John Kessenich0d2b6de2016-06-05 11:23:11 -06003137 // LEFT_BRACKET?
3138 if (! acceptTokenClass(EHTokLeftBracket))
3139 return;
3140
3141 // attribute
steve-lunarg1868b142016-10-20 13:07:10 -06003142 if (acceptIdentifier(idToken)) {
3143 // 'idToken.string' is the attribute
John Kessenich0d2b6de2016-06-05 11:23:11 -06003144 } else if (! peekTokenClass(EHTokRightBracket)) {
3145 expected("identifier");
3146 advanceToken();
3147 }
3148
steve-lunarga22f7db2016-11-11 08:17:44 -07003149 TIntermAggregate* expressions = nullptr;
steve-lunarg1868b142016-10-20 13:07:10 -06003150
3151 // (x, ...)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003152 if (acceptTokenClass(EHTokLeftParen)) {
steve-lunarga22f7db2016-11-11 08:17:44 -07003153 expressions = new TIntermAggregate;
steve-lunarg1868b142016-10-20 13:07:10 -06003154
John Kessenich0d2b6de2016-06-05 11:23:11 -06003155 TIntermTyped* node;
steve-lunarga22f7db2016-11-11 08:17:44 -07003156 bool expectingExpression = false;
John Kessenichecba76f2017-01-06 00:34:48 -07003157
steve-lunarga22f7db2016-11-11 08:17:44 -07003158 while (acceptAssignmentExpression(node)) {
3159 expectingExpression = false;
3160 expressions->getSequence().push_back(node);
steve-lunarg1868b142016-10-20 13:07:10 -06003161 if (acceptTokenClass(EHTokComma))
steve-lunarga22f7db2016-11-11 08:17:44 -07003162 expectingExpression = true;
steve-lunarg1868b142016-10-20 13:07:10 -06003163 }
3164
steve-lunarga22f7db2016-11-11 08:17:44 -07003165 // 'expressions' is an aggregate with the expressions in it
John Kessenich0d2b6de2016-06-05 11:23:11 -06003166 if (! acceptTokenClass(EHTokRightParen))
3167 expected(")");
steve-lunarga22f7db2016-11-11 08:17:44 -07003168
3169 // Error for partial or missing expression
3170 if (expectingExpression || expressions->getSequence().empty())
3171 expected("expression");
John Kessenich0d2b6de2016-06-05 11:23:11 -06003172 }
3173
3174 // RIGHT_BRACKET
steve-lunarg1868b142016-10-20 13:07:10 -06003175 if (!acceptTokenClass(EHTokRightBracket)) {
3176 expected("]");
3177 return;
3178 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06003179
steve-lunarg1868b142016-10-20 13:07:10 -06003180 // Add any values we found into the attribute map. This accepts
3181 // (and ignores) values not mapping to a known TAttributeType;
steve-lunarga22f7db2016-11-11 08:17:44 -07003182 attributes.setAttribute(idToken.string, expressions);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003183 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06003184}
3185
John Kessenich0d2b6de2016-06-05 11:23:11 -06003186// selection_statement
3187// : IF LEFT_PAREN expression RIGHT_PAREN statement
3188// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
3189//
John Kessenich21472ae2016-06-04 11:46:33 -06003190bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
3191{
John Kessenich0d2b6de2016-06-05 11:23:11 -06003192 TSourceLoc loc = token.loc;
3193
3194 // IF
3195 if (! acceptTokenClass(EHTokIf))
3196 return false;
3197
3198 // so that something declared in the condition is scoped to the lifetimes
3199 // of the then-else statements
3200 parseContext.pushScope();
3201
3202 // LEFT_PAREN expression RIGHT_PAREN
3203 TIntermTyped* condition;
3204 if (! acceptParenExpression(condition))
3205 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003206 condition = parseContext.convertConditionalExpression(loc, condition);
3207 if (condition == nullptr)
3208 return false;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003209
3210 // create the child statements
3211 TIntermNodePair thenElse = { nullptr, nullptr };
3212
3213 // then statement
3214 if (! acceptScopedStatement(thenElse.node1)) {
3215 expected("then statement");
3216 return false;
3217 }
3218
3219 // ELSE
3220 if (acceptTokenClass(EHTokElse)) {
3221 // else statement
3222 if (! acceptScopedStatement(thenElse.node2)) {
3223 expected("else statement");
3224 return false;
3225 }
3226 }
3227
3228 // Put the pieces together
3229 statement = intermediate.addSelection(condition, thenElse, loc);
3230 parseContext.popScope();
3231
3232 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003233}
3234
John Kessenichd02dc5d2016-07-01 00:04:11 -06003235// switch_statement
3236// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
3237//
John Kessenich21472ae2016-06-04 11:46:33 -06003238bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
3239{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003240 // SWITCH
3241 TSourceLoc loc = token.loc;
3242 if (! acceptTokenClass(EHTokSwitch))
3243 return false;
3244
3245 // LEFT_PAREN expression RIGHT_PAREN
3246 parseContext.pushScope();
3247 TIntermTyped* switchExpression;
3248 if (! acceptParenExpression(switchExpression)) {
3249 parseContext.popScope();
3250 return false;
3251 }
3252
3253 // compound_statement
3254 parseContext.pushSwitchSequence(new TIntermSequence);
3255 bool statementOkay = acceptCompoundStatement(statement);
3256 if (statementOkay)
3257 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
3258
3259 parseContext.popSwitchSequence();
3260 parseContext.popScope();
3261
3262 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06003263}
3264
John Kessenich119f8f62016-06-05 15:44:07 -06003265// iteration_statement
3266// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
3267// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
3268// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
3269//
3270// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06003271bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
3272{
John Kessenich119f8f62016-06-05 15:44:07 -06003273 TSourceLoc loc = token.loc;
3274 TIntermTyped* condition = nullptr;
3275
3276 EHlslTokenClass loop = peek();
3277 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
3278
3279 // WHILE or DO or FOR
3280 advanceToken();
3281
3282 switch (loop) {
3283 case EHTokWhile:
3284 // so that something declared in the condition is scoped to the lifetime
3285 // of the while sub-statement
3286 parseContext.pushScope();
3287 parseContext.nestLooping();
3288
3289 // LEFT_PAREN condition RIGHT_PAREN
3290 if (! acceptParenExpression(condition))
3291 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003292 condition = parseContext.convertConditionalExpression(loc, condition);
3293 if (condition == nullptr)
3294 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003295
3296 // statement
3297 if (! acceptScopedStatement(statement)) {
3298 expected("while sub-statement");
3299 return false;
3300 }
3301
3302 parseContext.unnestLooping();
3303 parseContext.popScope();
3304
3305 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
3306
3307 return true;
3308
3309 case EHTokDo:
3310 parseContext.nestLooping();
3311
3312 if (! acceptTokenClass(EHTokLeftBrace))
3313 expected("{");
3314
3315 // statement
3316 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
3317 expected("do sub-statement");
3318 return false;
3319 }
3320
3321 if (! acceptTokenClass(EHTokRightBrace))
3322 expected("}");
3323
3324 // WHILE
3325 if (! acceptTokenClass(EHTokWhile)) {
3326 expected("while");
3327 return false;
3328 }
3329
3330 // LEFT_PAREN condition RIGHT_PAREN
3331 TIntermTyped* condition;
3332 if (! acceptParenExpression(condition))
3333 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003334 condition = parseContext.convertConditionalExpression(loc, condition);
3335 if (condition == nullptr)
3336 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003337
3338 if (! acceptTokenClass(EHTokSemicolon))
3339 expected(";");
3340
3341 parseContext.unnestLooping();
3342
3343 statement = intermediate.addLoop(statement, condition, 0, false, loc);
3344
3345 return true;
3346
3347 case EHTokFor:
3348 {
3349 // LEFT_PAREN
3350 if (! acceptTokenClass(EHTokLeftParen))
3351 expected("(");
3352
3353 // so that something declared in the condition is scoped to the lifetime
3354 // of the for sub-statement
3355 parseContext.pushScope();
3356
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003357 // initializer
3358 TIntermNode* initNode = nullptr;
3359 if (! acceptControlDeclaration(initNode)) {
3360 TIntermTyped* initExpr = nullptr;
3361 acceptExpression(initExpr);
3362 initNode = initExpr;
3363 }
3364 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06003365 if (! acceptTokenClass(EHTokSemicolon))
3366 expected(";");
3367
3368 parseContext.nestLooping();
3369
3370 // condition SEMI_COLON
3371 acceptExpression(condition);
3372 if (! acceptTokenClass(EHTokSemicolon))
3373 expected(";");
John Kessenich7e997e22017-03-30 22:09:30 -06003374 if (condition != nullptr) {
3375 condition = parseContext.convertConditionalExpression(loc, condition);
3376 if (condition == nullptr)
3377 return false;
3378 }
John Kessenich119f8f62016-06-05 15:44:07 -06003379
3380 // iterator SEMI_COLON
3381 TIntermTyped* iterator = nullptr;
3382 acceptExpression(iterator);
3383 if (! acceptTokenClass(EHTokRightParen))
3384 expected(")");
3385
3386 // statement
3387 if (! acceptScopedStatement(statement)) {
3388 expected("for sub-statement");
3389 return false;
3390 }
3391
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003392 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06003393
3394 parseContext.popScope();
3395 parseContext.unnestLooping();
3396
3397 return true;
3398 }
3399
3400 default:
3401 return false;
3402 }
John Kessenich21472ae2016-06-04 11:46:33 -06003403}
3404
3405// jump_statement
3406// : CONTINUE SEMICOLON
3407// | BREAK SEMICOLON
3408// | DISCARD SEMICOLON
3409// | RETURN SEMICOLON
3410// | RETURN expression SEMICOLON
3411//
3412bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
3413{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003414 EHlslTokenClass jump = peek();
3415 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06003416 case EHTokContinue:
3417 case EHTokBreak:
3418 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06003419 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003420 advanceToken();
3421 break;
John Kessenich21472ae2016-06-04 11:46:33 -06003422 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003423 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06003424 return false;
3425 }
John Kessenich21472ae2016-06-04 11:46:33 -06003426
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003427 switch (jump) {
3428 case EHTokContinue:
3429 statement = intermediate.addBranch(EOpContinue, token.loc);
3430 break;
3431 case EHTokBreak:
3432 statement = intermediate.addBranch(EOpBreak, token.loc);
3433 break;
3434 case EHTokDiscard:
3435 statement = intermediate.addBranch(EOpKill, token.loc);
3436 break;
3437
3438 case EHTokReturn:
3439 {
3440 // expression
3441 TIntermTyped* node;
3442 if (acceptExpression(node)) {
3443 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06003444 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003445 } else
3446 statement = intermediate.addBranch(EOpReturn, token.loc);
3447 break;
3448 }
3449
3450 default:
3451 assert(0);
3452 return false;
3453 }
3454
3455 // SEMICOLON
3456 if (! acceptTokenClass(EHTokSemicolon))
3457 expected(";");
John Kessenichecba76f2017-01-06 00:34:48 -07003458
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003459 return true;
3460}
John Kessenich21472ae2016-06-04 11:46:33 -06003461
John Kessenichd02dc5d2016-07-01 00:04:11 -06003462// case_label
3463// : CASE expression COLON
3464//
John Kessenich21472ae2016-06-04 11:46:33 -06003465bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
3466{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003467 TSourceLoc loc = token.loc;
3468 if (! acceptTokenClass(EHTokCase))
3469 return false;
3470
3471 TIntermTyped* expression;
3472 if (! acceptExpression(expression)) {
3473 expected("case expression");
3474 return false;
3475 }
3476
3477 if (! acceptTokenClass(EHTokColon)) {
3478 expected(":");
3479 return false;
3480 }
3481
3482 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
3483
3484 return true;
3485}
3486
3487// default_label
3488// : DEFAULT COLON
3489//
3490bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
3491{
3492 TSourceLoc loc = token.loc;
3493 if (! acceptTokenClass(EHTokDefault))
3494 return false;
3495
3496 if (! acceptTokenClass(EHTokColon)) {
3497 expected(":");
3498 return false;
3499 }
3500
3501 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
3502
3503 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003504}
3505
John Kessenich19b92ff2016-06-19 11:50:34 -06003506// array_specifier
steve-lunarg7b211a32016-10-13 12:26:18 -06003507// : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
3508// : LEFT_BRACKET RGHT_BRACKET // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06003509//
3510void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
3511{
3512 arraySizes = nullptr;
3513
steve-lunarg7b211a32016-10-13 12:26:18 -06003514 // Early-out if there aren't any array dimensions
3515 if (!peekTokenClass(EHTokLeftBracket))
John Kessenich19b92ff2016-06-19 11:50:34 -06003516 return;
3517
steve-lunarg7b211a32016-10-13 12:26:18 -06003518 // 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 -06003519 arraySizes = new TArraySizes;
steve-lunarg7b211a32016-10-13 12:26:18 -06003520
3521 // Collect each array dimension.
3522 while (acceptTokenClass(EHTokLeftBracket)) {
3523 TSourceLoc loc = token.loc;
3524 TIntermTyped* sizeExpr = nullptr;
3525
John Kessenich057df292017-03-06 18:18:37 -07003526 // Array sizing expression is optional. If omitted, array will be later sized by initializer list.
steve-lunarg7b211a32016-10-13 12:26:18 -06003527 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
3528
3529 if (! acceptTokenClass(EHTokRightBracket)) {
3530 expected("]");
3531 return;
3532 }
3533
3534 if (hasArraySize) {
3535 TArraySize arraySize;
3536 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
3537 arraySizes->addInnerSize(arraySize);
3538 } else {
3539 arraySizes->addInnerSize(0); // sized by initializers.
3540 }
steve-lunarg265c0612016-09-27 10:57:35 -06003541 }
John Kessenich19b92ff2016-06-19 11:50:34 -06003542}
3543
John Kessenich630dd7d2016-06-12 23:52:12 -06003544// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06003545// : COLON semantic // optional
3546// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
3547// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06003548// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06003549// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06003550//
John Kessenich854fe242017-03-02 14:30:59 -07003551// Return true if any tokens were accepted. That is,
3552// false can be returned on successfully recognizing nothing,
3553// not necessarily meaning bad syntax.
3554//
3555bool HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06003556{
John Kessenich854fe242017-03-02 14:30:59 -07003557 bool found = false;
3558
John Kessenich630dd7d2016-06-12 23:52:12 -06003559 do {
John Kessenichecba76f2017-01-06 00:34:48 -07003560 // COLON
John Kessenich630dd7d2016-06-12 23:52:12 -06003561 if (acceptTokenClass(EHTokColon)) {
John Kessenich854fe242017-03-02 14:30:59 -07003562 found = true;
John Kessenich630dd7d2016-06-12 23:52:12 -06003563 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06003564 if (peekTokenClass(EHTokLayout))
3565 acceptLayoutQualifierList(qualifier);
3566 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06003567 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003568 if (! acceptTokenClass(EHTokLeftParen)) {
3569 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003570 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003571 }
John Kessenich82d6baf2016-07-29 13:03:05 -06003572 HlslToken locationToken;
3573 if (! acceptIdentifier(locationToken)) {
3574 expected("c[subcomponent][.component]");
John Kessenich854fe242017-03-02 14:30:59 -07003575 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003576 }
3577 HlslToken componentToken;
3578 if (acceptTokenClass(EHTokDot)) {
3579 if (! acceptIdentifier(componentToken)) {
3580 expected("component");
John Kessenich854fe242017-03-02 14:30:59 -07003581 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003582 }
3583 }
John Kessenich630dd7d2016-06-12 23:52:12 -06003584 if (! acceptTokenClass(EHTokRightParen)) {
3585 expected(")");
3586 break;
3587 }
John Kessenich7735b942016-09-05 12:40:06 -06003588 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003589 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06003590 expected("layout, semantic, packoffset, or register");
John Kessenich854fe242017-03-02 14:30:59 -07003591 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003592 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06003593 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
3594 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003595 if (! acceptTokenClass(EHTokLeftParen)) {
3596 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003597 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003598 }
John Kessenichb38f0712016-07-30 10:29:54 -06003599 HlslToken registerDesc; // for Type#
3600 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06003601 if (! acceptIdentifier(registerDesc)) {
3602 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003603 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003604 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003605 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
3606 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06003607 // Then we didn't really see the registerDesc yet, it was
3608 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06003609 profile = registerDesc;
3610 if (! acceptIdentifier(registerDesc)) {
3611 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003612 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003613 }
3614 }
John Kessenichb38f0712016-07-30 10:29:54 -06003615 int subComponent = 0;
3616 if (acceptTokenClass(EHTokLeftBracket)) {
3617 // LEFT_BRACKET subcomponent RIGHT_BRACKET
3618 if (! peekTokenClass(EHTokIntConstant)) {
3619 expected("literal integer");
John Kessenich854fe242017-03-02 14:30:59 -07003620 return false;
John Kessenichb38f0712016-07-30 10:29:54 -06003621 }
3622 subComponent = token.i;
3623 advanceToken();
3624 if (! acceptTokenClass(EHTokRightBracket)) {
3625 expected("]");
3626 break;
3627 }
3628 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003629 // (COMMA SPACEN)opt
3630 HlslToken spaceDesc;
3631 if (acceptTokenClass(EHTokComma)) {
3632 if (! acceptIdentifier(spaceDesc)) {
3633 expected ("space identifier");
John Kessenich854fe242017-03-02 14:30:59 -07003634 return false;
John Kessenichcfd7ce82016-09-05 16:03:12 -06003635 }
3636 }
3637 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003638 if (! acceptTokenClass(EHTokRightParen)) {
3639 expected(")");
3640 break;
3641 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003642 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003643 } else {
3644 // semantic, in idToken.string
John Kessenich2dd643f2017-03-14 21:50:06 -06003645 TString semanticUpperCase = *idToken.string;
3646 std::transform(semanticUpperCase.begin(), semanticUpperCase.end(), semanticUpperCase.begin(), ::toupper);
3647 parseContext.handleSemantic(idToken.loc, qualifier, mapSemantic(semanticUpperCase.c_str()), semanticUpperCase);
John Kessenich630dd7d2016-06-12 23:52:12 -06003648 }
John Kessenich854fe242017-03-02 14:30:59 -07003649 } else if (peekTokenClass(EHTokLeftAngle)) {
3650 found = true;
John Kessenicha1e2d492016-09-20 13:22:58 -06003651 acceptAnnotations(qualifier);
John Kessenich854fe242017-03-02 14:30:59 -07003652 } else
John Kessenich630dd7d2016-06-12 23:52:12 -06003653 break;
John Kessenich078d7f22016-03-14 10:02:11 -06003654
John Kessenich630dd7d2016-06-12 23:52:12 -06003655 } while (true);
John Kessenich854fe242017-03-02 14:30:59 -07003656
3657 return found;
John Kessenich078d7f22016-03-14 10:02:11 -06003658}
3659
John Kessenichb16f7e62017-03-11 19:32:47 -07003660//
3661// Get the stream of tokens from the scanner, but skip all syntactic/semantic
3662// processing.
3663//
3664bool HlslGrammar::captureBlockTokens(TVector<HlslToken>& tokens)
3665{
3666 if (! peekTokenClass(EHTokLeftBrace))
3667 return false;
3668
3669 int braceCount = 0;
3670
3671 do {
3672 switch (peek()) {
3673 case EHTokLeftBrace:
3674 ++braceCount;
3675 break;
3676 case EHTokRightBrace:
3677 --braceCount;
3678 break;
3679 case EHTokNone:
3680 // End of input before balance { } is bad...
3681 return false;
3682 default:
3683 break;
3684 }
3685
3686 tokens.push_back(token);
3687 advanceToken();
3688 } while (braceCount > 0);
3689
3690 return true;
3691}
3692
John Kesseniche01a9bc2016-03-12 20:11:22 -07003693} // end namespace glslang