blob: 4c9bdae2ba3621e17e1d2d508dd197a5c64dec97 [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-lunarg5da1f032017-02-12 17:50:28 -0700614 if (type.getQualifier().storage == EvqVaryingOut ||
615 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
620 type.getQualifier() = qualifier;
steve-lunargbb0183f2016-10-04 16:58:14 -0600621 }
John Kessenich87142c72016-03-12 20:24:24 -0700622
623 return true;
624}
625
John Kessenich630dd7d2016-06-12 23:52:12 -0600626// type_qualifier
627// : qualifier qualifier ...
628//
629// Zero or more of these, so this can't return false.
630//
John Kessenichb9e39122016-08-17 10:22:08 -0600631bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
John Kessenich87142c72016-03-12 20:24:24 -0700632{
John Kessenich630dd7d2016-06-12 23:52:12 -0600633 do {
634 switch (peek()) {
635 case EHTokStatic:
John Kessenich6dbc0a72016-09-27 19:13:05 -0600636 qualifier.storage = parseContext.symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
John Kessenich630dd7d2016-06-12 23:52:12 -0600637 break;
638 case EHTokExtern:
639 // TODO: no meaning in glslang?
640 break;
641 case EHTokShared:
642 // TODO: hint
643 break;
644 case EHTokGroupShared:
645 qualifier.storage = EvqShared;
646 break;
647 case EHTokUniform:
648 qualifier.storage = EvqUniform;
649 break;
650 case EHTokConst:
651 qualifier.storage = EvqConst;
652 break;
653 case EHTokVolatile:
654 qualifier.volatil = true;
655 break;
656 case EHTokLinear:
John Kessenich630dd7d2016-06-12 23:52:12 -0600657 qualifier.smooth = true;
658 break;
659 case EHTokCentroid:
660 qualifier.centroid = true;
661 break;
662 case EHTokNointerpolation:
663 qualifier.flat = true;
664 break;
665 case EHTokNoperspective:
666 qualifier.nopersp = true;
667 break;
668 case EHTokSample:
669 qualifier.sample = true;
670 break;
671 case EHTokRowMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600672 qualifier.layoutMatrix = ElmColumnMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600673 break;
674 case EHTokColumnMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600675 qualifier.layoutMatrix = ElmRowMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600676 break;
677 case EHTokPrecise:
678 qualifier.noContraction = true;
679 break;
LoopDawg9249c702016-07-12 20:44:32 -0600680 case EHTokIn:
681 qualifier.storage = EvqIn;
682 break;
683 case EHTokOut:
684 qualifier.storage = EvqOut;
685 break;
686 case EHTokInOut:
687 qualifier.storage = EvqInOut;
688 break;
John Kessenichb9e39122016-08-17 10:22:08 -0600689 case EHTokLayout:
690 if (! acceptLayoutQualifierList(qualifier))
691 return false;
692 continue;
steve-lunarg5da1f032017-02-12 17:50:28 -0700693 case EHTokGloballyCoherent:
694 qualifier.coherent = true;
695 break;
John Kessenich36b218d2017-03-15 09:05:14 -0600696 case EHTokInline:
697 // TODO: map this to SPIR-V function control
698 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700699
700 // GS geometries: these are specified on stage input variables, and are an error (not verified here)
701 // for output variables.
702 case EHTokPoint:
703 qualifier.storage = EvqIn;
704 if (!parseContext.handleInputGeometry(token.loc, ElgPoints))
705 return false;
706 break;
707 case EHTokLine:
708 qualifier.storage = EvqIn;
709 if (!parseContext.handleInputGeometry(token.loc, ElgLines))
710 return false;
711 break;
712 case EHTokTriangle:
713 qualifier.storage = EvqIn;
714 if (!parseContext.handleInputGeometry(token.loc, ElgTriangles))
715 return false;
716 break;
717 case EHTokLineAdj:
718 qualifier.storage = EvqIn;
719 if (!parseContext.handleInputGeometry(token.loc, ElgLinesAdjacency))
720 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700721 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700722 case EHTokTriangleAdj:
723 qualifier.storage = EvqIn;
724 if (!parseContext.handleInputGeometry(token.loc, ElgTrianglesAdjacency))
725 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700726 break;
727
John Kessenich630dd7d2016-06-12 23:52:12 -0600728 default:
John Kessenichb9e39122016-08-17 10:22:08 -0600729 return true;
John Kessenich630dd7d2016-06-12 23:52:12 -0600730 }
731 advanceToken();
732 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -0700733}
734
John Kessenichb9e39122016-08-17 10:22:08 -0600735// layout_qualifier_list
John Kesseniche3218e22016-09-05 14:37:03 -0600736// : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
John Kessenichb9e39122016-08-17 10:22:08 -0600737//
738// layout_qualifier
739// : identifier
John Kessenich841db352016-09-02 21:12:23 -0600740// | identifier EQUAL expression
John Kessenichb9e39122016-08-17 10:22:08 -0600741//
742// Zero or more of these, so this can't return false.
743//
744bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
745{
746 if (! acceptTokenClass(EHTokLayout))
747 return false;
748
749 // LEFT_PAREN
750 if (! acceptTokenClass(EHTokLeftParen))
751 return false;
752
753 do {
754 // identifier
755 HlslToken idToken;
756 if (! acceptIdentifier(idToken))
757 break;
758
759 // EQUAL expression
760 if (acceptTokenClass(EHTokAssign)) {
761 TIntermTyped* expr;
762 if (! acceptConditionalExpression(expr)) {
763 expected("expression");
764 return false;
765 }
766 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
767 } else
768 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
769
770 // COMMA
771 if (! acceptTokenClass(EHTokComma))
772 break;
773 } while (true);
774
775 // RIGHT_PAREN
776 if (! acceptTokenClass(EHTokRightParen)) {
777 expected(")");
778 return false;
779 }
780
781 return true;
782}
783
LoopDawg6daaa4f2016-06-23 19:13:48 -0600784// template_type
785// : FLOAT
786// | DOUBLE
787// | INT
788// | DWORD
789// | UINT
790// | BOOL
791//
steve-lunargf49cdf42016-11-17 15:04:20 -0700792bool HlslGrammar::acceptTemplateVecMatBasicType(TBasicType& basicType)
LoopDawg6daaa4f2016-06-23 19:13:48 -0600793{
794 switch (peek()) {
795 case EHTokFloat:
796 basicType = EbtFloat;
797 break;
798 case EHTokDouble:
799 basicType = EbtDouble;
800 break;
801 case EHTokInt:
802 case EHTokDword:
803 basicType = EbtInt;
804 break;
805 case EHTokUint:
806 basicType = EbtUint;
807 break;
808 case EHTokBool:
809 basicType = EbtBool;
810 break;
811 default:
812 return false;
813 }
814
815 advanceToken();
816
817 return true;
818}
819
820// vector_template_type
821// : VECTOR
822// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
823//
824bool HlslGrammar::acceptVectorTemplateType(TType& type)
825{
826 if (! acceptTokenClass(EHTokVector))
827 return false;
828
829 if (! acceptTokenClass(EHTokLeftAngle)) {
830 // in HLSL, 'vector' alone means float4.
831 new(&type) TType(EbtFloat, EvqTemporary, 4);
832 return true;
833 }
834
835 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700836 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600837 expected("scalar type");
838 return false;
839 }
840
841 // COMMA
842 if (! acceptTokenClass(EHTokComma)) {
843 expected(",");
844 return false;
845 }
846
847 // integer
848 if (! peekTokenClass(EHTokIntConstant)) {
849 expected("literal integer");
850 return false;
851 }
852
853 TIntermTyped* vecSize;
854 if (! acceptLiteral(vecSize))
855 return false;
856
857 const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
858
859 new(&type) TType(basicType, EvqTemporary, vecSizeI);
860
861 if (vecSizeI == 1)
862 type.makeVector();
863
864 if (!acceptTokenClass(EHTokRightAngle)) {
865 expected("right angle bracket");
866 return false;
867 }
868
869 return true;
870}
871
872// matrix_template_type
873// : MATRIX
874// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
875//
876bool HlslGrammar::acceptMatrixTemplateType(TType& type)
877{
878 if (! acceptTokenClass(EHTokMatrix))
879 return false;
880
881 if (! acceptTokenClass(EHTokLeftAngle)) {
882 // in HLSL, 'matrix' alone means float4x4.
883 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
884 return true;
885 }
886
887 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700888 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600889 expected("scalar type");
890 return false;
891 }
892
893 // COMMA
894 if (! acceptTokenClass(EHTokComma)) {
895 expected(",");
896 return false;
897 }
898
899 // integer rows
900 if (! peekTokenClass(EHTokIntConstant)) {
901 expected("literal integer");
902 return false;
903 }
904
905 TIntermTyped* rows;
906 if (! acceptLiteral(rows))
907 return false;
908
909 // COMMA
910 if (! acceptTokenClass(EHTokComma)) {
911 expected(",");
912 return false;
913 }
John Kessenichecba76f2017-01-06 00:34:48 -0700914
LoopDawg6daaa4f2016-06-23 19:13:48 -0600915 // integer cols
916 if (! peekTokenClass(EHTokIntConstant)) {
917 expected("literal integer");
918 return false;
919 }
920
921 TIntermTyped* cols;
922 if (! acceptLiteral(cols))
923 return false;
924
925 new(&type) TType(basicType, EvqTemporary, 0,
steve-lunarg297ae212016-08-24 14:36:13 -0600926 rows->getAsConstantUnion()->getConstArray()[0].getIConst(),
927 cols->getAsConstantUnion()->getConstArray()[0].getIConst());
LoopDawg6daaa4f2016-06-23 19:13:48 -0600928
929 if (!acceptTokenClass(EHTokRightAngle)) {
930 expected("right angle bracket");
931 return false;
932 }
933
934 return true;
935}
936
steve-lunargf49cdf42016-11-17 15:04:20 -0700937// layout_geometry
938// : LINESTREAM
939// | POINTSTREAM
940// | TRIANGLESTREAM
941//
942bool HlslGrammar::acceptOutputPrimitiveGeometry(TLayoutGeometry& geometry)
943{
944 // read geometry type
945 const EHlslTokenClass geometryType = peek();
946
947 switch (geometryType) {
948 case EHTokPointStream: geometry = ElgPoints; break;
949 case EHTokLineStream: geometry = ElgLineStrip; break;
950 case EHTokTriangleStream: geometry = ElgTriangleStrip; break;
951 default:
952 return false; // not a layout geometry
953 }
954
955 advanceToken(); // consume the layout keyword
956 return true;
957}
958
steve-lunarg858c9282017-01-07 08:54:10 -0700959// tessellation_decl_type
960// : INPUTPATCH
961// | OUTPUTPATCH
962//
963bool HlslGrammar::acceptTessellationDeclType()
964{
965 // read geometry type
966 const EHlslTokenClass tessType = peek();
967
968 switch (tessType) {
969 case EHTokInputPatch: break;
970 case EHTokOutputPatch: break;
971 default:
972 return false; // not a tessellation decl
973 }
974
975 advanceToken(); // consume the keyword
976 return true;
977}
978
979// tessellation_patch_template_type
980// : tessellation_decl_type LEFT_ANGLE type comma integer_literal RIGHT_ANGLE
981//
982bool HlslGrammar::acceptTessellationPatchTemplateType(TType& type)
983{
984 if (! acceptTessellationDeclType())
985 return false;
986
987 if (! acceptTokenClass(EHTokLeftAngle))
988 return false;
989
990 if (! acceptType(type)) {
991 expected("tessellation patch type");
992 return false;
993 }
994
995 if (! acceptTokenClass(EHTokComma))
996 return false;
997
998 // integer size
999 if (! peekTokenClass(EHTokIntConstant)) {
1000 expected("literal integer");
1001 return false;
1002 }
1003
1004 TIntermTyped* size;
1005 if (! acceptLiteral(size))
1006 return false;
1007
1008 TArraySizes* arraySizes = new TArraySizes;
1009 arraySizes->addInnerSize(size->getAsConstantUnion()->getConstArray()[0].getIConst());
1010 type.newArraySizes(*arraySizes);
1011
1012 if (! acceptTokenClass(EHTokRightAngle)) {
1013 expected("right angle bracket");
1014 return false;
1015 }
1016
1017 return true;
1018}
1019
steve-lunargf49cdf42016-11-17 15:04:20 -07001020// stream_out_template_type
1021// : output_primitive_geometry_type LEFT_ANGLE type RIGHT_ANGLE
1022//
1023bool HlslGrammar::acceptStreamOutTemplateType(TType& type, TLayoutGeometry& geometry)
1024{
1025 geometry = ElgNone;
1026
1027 if (! acceptOutputPrimitiveGeometry(geometry))
1028 return false;
1029
1030 if (! acceptTokenClass(EHTokLeftAngle))
1031 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001032
steve-lunargf49cdf42016-11-17 15:04:20 -07001033 if (! acceptType(type)) {
1034 expected("stream output type");
1035 return false;
1036 }
1037
1038 type.getQualifier().storage = EvqVaryingOut;
1039
1040 if (! acceptTokenClass(EHTokRightAngle)) {
1041 expected("right angle bracket");
1042 return false;
1043 }
1044
1045 return true;
1046}
John Kessenichecba76f2017-01-06 00:34:48 -07001047
John Kessenicha1e2d492016-09-20 13:22:58 -06001048// annotations
1049// : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
John Kessenich86f71382016-09-19 20:23:18 -06001050//
John Kessenicha1e2d492016-09-20 13:22:58 -06001051bool HlslGrammar::acceptAnnotations(TQualifier&)
John Kessenich86f71382016-09-19 20:23:18 -06001052{
John Kessenicha1e2d492016-09-20 13:22:58 -06001053 if (! acceptTokenClass(EHTokLeftAngle))
John Kessenich86f71382016-09-19 20:23:18 -06001054 return false;
1055
John Kessenicha1e2d492016-09-20 13:22:58 -06001056 // note that we are nesting a name space
1057 parseContext.nestAnnotations();
John Kessenich86f71382016-09-19 20:23:18 -06001058
1059 // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
1060 do {
1061 // eat any extra SEMI_COLON; don't know if the grammar calls for this or not
1062 while (acceptTokenClass(EHTokSemicolon))
1063 ;
1064
1065 if (acceptTokenClass(EHTokRightAngle))
John Kessenicha1e2d492016-09-20 13:22:58 -06001066 break;
John Kessenich86f71382016-09-19 20:23:18 -06001067
1068 // declaration
John Kessenichca71d942017-03-07 20:44:09 -07001069 TIntermNode* node = nullptr;
John Kessenich86f71382016-09-19 20:23:18 -06001070 if (! acceptDeclaration(node)) {
John Kessenicha1e2d492016-09-20 13:22:58 -06001071 expected("declaration in annotation");
John Kessenich86f71382016-09-19 20:23:18 -06001072 return false;
1073 }
1074 } while (true);
John Kessenicha1e2d492016-09-20 13:22:58 -06001075
1076 parseContext.unnestAnnotations();
1077 return true;
John Kessenich86f71382016-09-19 20:23:18 -06001078}
LoopDawg6daaa4f2016-06-23 19:13:48 -06001079
LoopDawg4886f692016-06-29 10:58:58 -06001080// sampler_type
1081// : SAMPLER
1082// | SAMPLER1D
1083// | SAMPLER2D
1084// | SAMPLER3D
1085// | SAMPLERCUBE
1086// | SAMPLERSTATE
1087// | SAMPLERCOMPARISONSTATE
1088bool HlslGrammar::acceptSamplerType(TType& type)
1089{
1090 // read sampler type
1091 const EHlslTokenClass samplerType = peek();
1092
LoopDawga78b0292016-07-19 14:28:05 -06001093 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -06001094 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -06001095
LoopDawga78b0292016-07-19 14:28:05 -06001096 bool isShadow = false;
1097
LoopDawg4886f692016-06-29 10:58:58 -06001098 switch (samplerType) {
1099 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -06001100 case EHTokSampler1d: /*dim = Esd1D*/; break;
1101 case EHTokSampler2d: /*dim = Esd2D*/; break;
1102 case EHTokSampler3d: /*dim = Esd3D*/; break;
1103 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -06001104 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -06001105 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001106 default:
1107 return false; // not a sampler declaration
1108 }
1109
1110 advanceToken(); // consume the sampler type keyword
1111
1112 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -06001113
1114 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -06001115 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -06001116
1117 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
1118
1119 return true;
1120}
1121
1122// texture_type
1123// | BUFFER
1124// | TEXTURE1D
1125// | TEXTURE1DARRAY
1126// | TEXTURE2D
1127// | TEXTURE2DARRAY
1128// | TEXTURE3D
1129// | TEXTURECUBE
1130// | TEXTURECUBEARRAY
1131// | TEXTURE2DMS
1132// | TEXTURE2DMSARRAY
steve-lunargbb0183f2016-10-04 16:58:14 -06001133// | RWBUFFER
1134// | RWTEXTURE1D
1135// | RWTEXTURE1DARRAY
1136// | RWTEXTURE2D
1137// | RWTEXTURE2DARRAY
1138// | RWTEXTURE3D
1139
LoopDawg4886f692016-06-29 10:58:58 -06001140bool HlslGrammar::acceptTextureType(TType& type)
1141{
1142 const EHlslTokenClass textureType = peek();
1143
1144 TSamplerDim dim = EsdNone;
1145 bool array = false;
1146 bool ms = false;
steve-lunargbb0183f2016-10-04 16:58:14 -06001147 bool image = false;
LoopDawg4886f692016-06-29 10:58:58 -06001148
1149 switch (textureType) {
John Kessenichf36542f2017-03-31 14:39:30 -06001150 case EHTokBuffer: dim = EsdBuffer; break;
1151 case EHTokTexture1d: dim = Esd1D; break;
1152 case EHTokTexture1darray: dim = Esd1D; array = true; break;
1153 case EHTokTexture2d: dim = Esd2D; break;
1154 case EHTokTexture2darray: dim = Esd2D; array = true; break;
1155 case EHTokTexture3d: dim = Esd3D; break;
1156 case EHTokTextureCube: dim = EsdCube; break;
1157 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
1158 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
1159 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
1160 case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
1161 case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
1162 case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
1163 case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
1164 case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
1165 case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001166 default:
1167 return false; // not a texture declaration
1168 }
1169
1170 advanceToken(); // consume the texture object keyword
1171
1172 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
John Kessenichecba76f2017-01-06 00:34:48 -07001173
LoopDawg4886f692016-06-29 10:58:58 -06001174 TIntermTyped* msCount = nullptr;
1175
steve-lunargbb0183f2016-10-04 16:58:14 -06001176 // texture type: required for multisample types and RWBuffer/RWTextures!
LoopDawg4886f692016-06-29 10:58:58 -06001177 if (acceptTokenClass(EHTokLeftAngle)) {
1178 if (! acceptType(txType)) {
1179 expected("scalar or vector type");
1180 return false;
1181 }
1182
1183 const TBasicType basicRetType = txType.getBasicType() ;
1184
1185 if (basicRetType != EbtFloat && basicRetType != EbtUint && basicRetType != EbtInt) {
1186 unimplemented("basic type in texture");
1187 return false;
1188 }
1189
steve-lunargd53f7172016-07-27 15:46:48 -06001190 // Buffers can handle small mats if they fit in 4 components
1191 if (dim == EsdBuffer && txType.isMatrix()) {
1192 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
1193 expected("components < 4 in matrix buffer type");
1194 return false;
1195 }
1196
1197 // TODO: except we don't handle it yet...
1198 unimplemented("matrix type in buffer");
1199 return false;
1200 }
1201
LoopDawg4886f692016-06-29 10:58:58 -06001202 if (!txType.isScalar() && !txType.isVector()) {
1203 expected("scalar or vector type");
1204 return false;
1205 }
1206
LoopDawg4886f692016-06-29 10:58:58 -06001207 if (ms && acceptTokenClass(EHTokComma)) {
1208 // read sample count for multisample types, if given
1209 if (! peekTokenClass(EHTokIntConstant)) {
1210 expected("multisample count");
1211 return false;
1212 }
1213
1214 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
1215 return false;
1216 }
1217
1218 if (! acceptTokenClass(EHTokRightAngle)) {
1219 expected("right angle bracket");
1220 return false;
1221 }
1222 } else if (ms) {
1223 expected("texture type for multisample");
1224 return false;
John Kessenichf36542f2017-03-31 14:39:30 -06001225 } else if (image) {
steve-lunargbb0183f2016-10-04 16:58:14 -06001226 expected("type for RWTexture/RWBuffer");
1227 return false;
LoopDawg4886f692016-06-29 10:58:58 -06001228 }
1229
1230 TArraySizes* arraySizes = nullptr;
steve-lunarg4f2da272016-10-10 15:24:57 -06001231 const bool shadow = false; // declared on the sampler
LoopDawg4886f692016-06-29 10:58:58 -06001232
1233 TSampler sampler;
steve-lunargbb0183f2016-10-04 16:58:14 -06001234 TLayoutFormat format = ElfNone;
steve-lunargd53f7172016-07-27 15:46:48 -06001235
steve-lunarg4f2da272016-10-10 15:24:57 -06001236 // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
1237 if (image || dim == EsdBuffer)
1238 format = parseContext.getLayoutFromTxType(token.loc, txType);
steve-lunargbb0183f2016-10-04 16:58:14 -06001239
1240 // Non-image Buffers are combined
1241 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -06001242 sampler.set(txType.getBasicType(), dim, array);
1243 } else {
1244 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -06001245 if (image) {
1246 sampler.setImage(txType.getBasicType(), dim, array, shadow, ms);
1247 } else {
1248 sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms);
1249 }
steve-lunargd53f7172016-07-27 15:46:48 -06001250 }
steve-lunarg8b0227c2016-10-14 16:40:32 -06001251
1252 // Remember the declared vector size.
1253 sampler.vectorSize = txType.getVectorSize();
John Kessenichecba76f2017-01-06 00:34:48 -07001254
LoopDawg4886f692016-06-29 10:58:58 -06001255 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -06001256 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -06001257
1258 return true;
1259}
1260
John Kessenich87142c72016-03-12 20:24:24 -07001261// If token is for a type, update 'type' with the type information,
1262// and return true and advance.
1263// Otherwise, return false, and don't advance
1264bool HlslGrammar::acceptType(TType& type)
1265{
John Kessenich54ee28f2017-03-11 14:13:00 -07001266 TIntermNode* nodeList = nullptr;
1267 return acceptType(type, nodeList);
1268}
1269bool HlslGrammar::acceptType(TType& type, TIntermNode*& nodeList)
1270{
steve-lunarg3226b082016-10-26 19:18:55 -06001271 // Basic types for min* types, broken out here in case of future
1272 // changes, e.g, to use native halfs.
1273 static const TBasicType min16float_bt = EbtFloat;
1274 static const TBasicType min10float_bt = EbtFloat;
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001275 static const TBasicType half_bt = EbtFloat;
steve-lunarg3226b082016-10-26 19:18:55 -06001276 static const TBasicType min16int_bt = EbtInt;
1277 static const TBasicType min12int_bt = EbtInt;
1278 static const TBasicType min16uint_bt = EbtUint;
1279
John Kessenich9c86c6a2016-05-03 22:49:24 -06001280 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -06001281 case EHTokVector:
1282 return acceptVectorTemplateType(type);
1283 break;
1284
1285 case EHTokMatrix:
1286 return acceptMatrixTemplateType(type);
1287 break;
1288
steve-lunargf49cdf42016-11-17 15:04:20 -07001289 case EHTokPointStream: // fall through
1290 case EHTokLineStream: // ...
1291 case EHTokTriangleStream: // ...
1292 {
1293 TLayoutGeometry geometry;
1294 if (! acceptStreamOutTemplateType(type, geometry))
1295 return false;
1296
1297 if (! parseContext.handleOutputGeometry(token.loc, geometry))
1298 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001299
steve-lunargf49cdf42016-11-17 15:04:20 -07001300 return true;
1301 }
1302
steve-lunarg858c9282017-01-07 08:54:10 -07001303 case EHTokInputPatch: // fall through
1304 case EHTokOutputPatch: // ...
1305 {
1306 if (! acceptTessellationPatchTemplateType(type))
1307 return false;
1308
1309 return true;
1310 }
1311
LoopDawg4886f692016-06-29 10:58:58 -06001312 case EHTokSampler: // fall through
1313 case EHTokSampler1d: // ...
1314 case EHTokSampler2d: // ...
1315 case EHTokSampler3d: // ...
1316 case EHTokSamplerCube: // ...
1317 case EHTokSamplerState: // ...
1318 case EHTokSamplerComparisonState: // ...
1319 return acceptSamplerType(type);
1320 break;
1321
1322 case EHTokBuffer: // fall through
1323 case EHTokTexture1d: // ...
1324 case EHTokTexture1darray: // ...
1325 case EHTokTexture2d: // ...
1326 case EHTokTexture2darray: // ...
1327 case EHTokTexture3d: // ...
1328 case EHTokTextureCube: // ...
1329 case EHTokTextureCubearray: // ...
1330 case EHTokTexture2DMS: // ...
1331 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001332 case EHTokRWTexture1d: // ...
1333 case EHTokRWTexture1darray: // ...
1334 case EHTokRWTexture2d: // ...
1335 case EHTokRWTexture2darray: // ...
1336 case EHTokRWTexture3d: // ...
1337 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001338 return acceptTextureType(type);
1339 break;
1340
steve-lunarg5da1f032017-02-12 17:50:28 -07001341 case EHTokAppendStructuredBuffer:
1342 case EHTokByteAddressBuffer:
1343 case EHTokConsumeStructuredBuffer:
1344 case EHTokRWByteAddressBuffer:
1345 case EHTokRWStructuredBuffer:
1346 case EHTokStructuredBuffer:
1347 return acceptStructBufferType(type);
1348 break;
1349
John Kessenich27ffb292017-03-03 17:01:01 -07001350 case EHTokClass:
John Kesseniche6e74942016-06-11 16:43:14 -06001351 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001352 case EHTokCBuffer:
1353 case EHTokTBuffer:
John Kessenich54ee28f2017-03-11 14:13:00 -07001354 return acceptStruct(type, nodeList);
John Kesseniche6e74942016-06-11 16:43:14 -06001355
1356 case EHTokIdentifier:
1357 // An identifier could be for a user-defined type.
1358 // Note we cache the symbol table lookup, to save for a later rule
1359 // when this is not a type.
John Kessenichf4ba25e2017-03-21 18:35:04 -06001360 if (parseContext.lookupUserType(*token.string, type) != nullptr) {
John Kesseniche6e74942016-06-11 16:43:14 -06001361 advanceToken();
1362 return true;
1363 } else
1364 return false;
1365
John Kessenich71351de2016-06-08 12:50:56 -06001366 case EHTokVoid:
1367 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001368 break;
John Kessenich71351de2016-06-08 12:50:56 -06001369
John Kessenicha1e2d492016-09-20 13:22:58 -06001370 case EHTokString:
1371 new(&type) TType(EbtString);
1372 break;
1373
John Kessenich87142c72016-03-12 20:24:24 -07001374 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001375 new(&type) TType(EbtFloat);
1376 break;
John Kessenich87142c72016-03-12 20:24:24 -07001377 case EHTokFloat1:
1378 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001379 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001380 break;
John Kessenich87142c72016-03-12 20:24:24 -07001381 case EHTokFloat2:
1382 new(&type) TType(EbtFloat, EvqTemporary, 2);
1383 break;
1384 case EHTokFloat3:
1385 new(&type) TType(EbtFloat, EvqTemporary, 3);
1386 break;
1387 case EHTokFloat4:
1388 new(&type) TType(EbtFloat, EvqTemporary, 4);
1389 break;
1390
John Kessenich71351de2016-06-08 12:50:56 -06001391 case EHTokDouble:
1392 new(&type) TType(EbtDouble);
1393 break;
1394 case EHTokDouble1:
1395 new(&type) TType(EbtDouble);
1396 type.makeVector();
1397 break;
1398 case EHTokDouble2:
1399 new(&type) TType(EbtDouble, EvqTemporary, 2);
1400 break;
1401 case EHTokDouble3:
1402 new(&type) TType(EbtDouble, EvqTemporary, 3);
1403 break;
1404 case EHTokDouble4:
1405 new(&type) TType(EbtDouble, EvqTemporary, 4);
1406 break;
1407
1408 case EHTokInt:
1409 case EHTokDword:
1410 new(&type) TType(EbtInt);
1411 break;
1412 case EHTokInt1:
1413 new(&type) TType(EbtInt);
1414 type.makeVector();
1415 break;
John Kessenich87142c72016-03-12 20:24:24 -07001416 case EHTokInt2:
1417 new(&type) TType(EbtInt, EvqTemporary, 2);
1418 break;
1419 case EHTokInt3:
1420 new(&type) TType(EbtInt, EvqTemporary, 3);
1421 break;
1422 case EHTokInt4:
1423 new(&type) TType(EbtInt, EvqTemporary, 4);
1424 break;
1425
John Kessenich71351de2016-06-08 12:50:56 -06001426 case EHTokUint:
1427 new(&type) TType(EbtUint);
1428 break;
1429 case EHTokUint1:
1430 new(&type) TType(EbtUint);
1431 type.makeVector();
1432 break;
1433 case EHTokUint2:
1434 new(&type) TType(EbtUint, EvqTemporary, 2);
1435 break;
1436 case EHTokUint3:
1437 new(&type) TType(EbtUint, EvqTemporary, 3);
1438 break;
1439 case EHTokUint4:
1440 new(&type) TType(EbtUint, EvqTemporary, 4);
1441 break;
1442
1443 case EHTokBool:
1444 new(&type) TType(EbtBool);
1445 break;
1446 case EHTokBool1:
1447 new(&type) TType(EbtBool);
1448 type.makeVector();
1449 break;
John Kessenich87142c72016-03-12 20:24:24 -07001450 case EHTokBool2:
1451 new(&type) TType(EbtBool, EvqTemporary, 2);
1452 break;
1453 case EHTokBool3:
1454 new(&type) TType(EbtBool, EvqTemporary, 3);
1455 break;
1456 case EHTokBool4:
1457 new(&type) TType(EbtBool, EvqTemporary, 4);
1458 break;
1459
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001460 case EHTokHalf:
1461 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1462 break;
1463 case EHTokHalf1:
1464 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1465 type.makeVector();
1466 break;
1467 case EHTokHalf2:
1468 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 2);
1469 break;
1470 case EHTokHalf3:
1471 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 3);
1472 break;
1473 case EHTokHalf4:
1474 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 4);
1475 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001476
steve-lunarg3226b082016-10-26 19:18:55 -06001477 case EHTokMin16float:
1478 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1479 break;
1480 case EHTokMin16float1:
1481 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1482 type.makeVector();
1483 break;
1484 case EHTokMin16float2:
1485 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 2);
1486 break;
1487 case EHTokMin16float3:
1488 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 3);
1489 break;
1490 case EHTokMin16float4:
1491 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 4);
1492 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001493
steve-lunarg3226b082016-10-26 19:18:55 -06001494 case EHTokMin10float:
1495 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1496 break;
1497 case EHTokMin10float1:
1498 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1499 type.makeVector();
1500 break;
1501 case EHTokMin10float2:
1502 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 2);
1503 break;
1504 case EHTokMin10float3:
1505 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 3);
1506 break;
1507 case EHTokMin10float4:
1508 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 4);
1509 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001510
steve-lunarg3226b082016-10-26 19:18:55 -06001511 case EHTokMin16int:
1512 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1513 break;
1514 case EHTokMin16int1:
1515 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1516 type.makeVector();
1517 break;
1518 case EHTokMin16int2:
1519 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 2);
1520 break;
1521 case EHTokMin16int3:
1522 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 3);
1523 break;
1524 case EHTokMin16int4:
1525 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 4);
1526 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001527
steve-lunarg3226b082016-10-26 19:18:55 -06001528 case EHTokMin12int:
1529 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1530 break;
1531 case EHTokMin12int1:
1532 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1533 type.makeVector();
1534 break;
1535 case EHTokMin12int2:
1536 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 2);
1537 break;
1538 case EHTokMin12int3:
1539 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 3);
1540 break;
1541 case EHTokMin12int4:
1542 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 4);
1543 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001544
steve-lunarg3226b082016-10-26 19:18:55 -06001545 case EHTokMin16uint:
1546 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1547 break;
1548 case EHTokMin16uint1:
1549 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1550 type.makeVector();
1551 break;
1552 case EHTokMin16uint2:
1553 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 2);
1554 break;
1555 case EHTokMin16uint3:
1556 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 3);
1557 break;
1558 case EHTokMin16uint4:
1559 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 4);
1560 break;
1561
John Kessenich0133c122016-05-20 12:17:26 -06001562 case EHTokInt1x1:
1563 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1564 break;
1565 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001566 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001567 break;
1568 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001569 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001570 break;
1571 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001572 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001573 break;
1574 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001575 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001576 break;
1577 case EHTokInt2x2:
1578 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1579 break;
1580 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001581 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001582 break;
1583 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001584 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001585 break;
1586 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001587 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001588 break;
1589 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001590 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001591 break;
1592 case EHTokInt3x3:
1593 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1594 break;
1595 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001596 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001597 break;
1598 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001599 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001600 break;
1601 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001602 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001603 break;
1604 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001605 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001606 break;
1607 case EHTokInt4x4:
1608 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1609 break;
1610
John Kessenich71351de2016-06-08 12:50:56 -06001611 case EHTokUint1x1:
1612 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1613 break;
1614 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001615 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001616 break;
1617 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001618 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001619 break;
1620 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001621 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001622 break;
1623 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001624 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001625 break;
1626 case EHTokUint2x2:
1627 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1628 break;
1629 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001630 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001631 break;
1632 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001633 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001634 break;
1635 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001636 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001637 break;
1638 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001639 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001640 break;
1641 case EHTokUint3x3:
1642 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1643 break;
1644 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001645 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001646 break;
1647 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001648 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001649 break;
1650 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001651 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001652 break;
1653 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001654 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001655 break;
1656 case EHTokUint4x4:
1657 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1658 break;
1659
1660 case EHTokBool1x1:
1661 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1662 break;
1663 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001664 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001665 break;
1666 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001667 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001668 break;
1669 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001670 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001671 break;
1672 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001673 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001674 break;
1675 case EHTokBool2x2:
1676 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1677 break;
1678 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001679 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001680 break;
1681 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001682 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001683 break;
1684 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001685 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001686 break;
1687 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001688 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001689 break;
1690 case EHTokBool3x3:
1691 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1692 break;
1693 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001694 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001695 break;
1696 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001697 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001698 break;
1699 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001700 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001701 break;
1702 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001703 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001704 break;
1705 case EHTokBool4x4:
1706 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1707 break;
1708
John Kessenich0133c122016-05-20 12:17:26 -06001709 case EHTokFloat1x1:
1710 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1711 break;
1712 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001713 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001714 break;
1715 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001716 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001717 break;
1718 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001719 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001720 break;
1721 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001722 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001723 break;
John Kessenich87142c72016-03-12 20:24:24 -07001724 case EHTokFloat2x2:
1725 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1726 break;
1727 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001728 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001729 break;
1730 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001731 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001732 break;
John Kessenich0133c122016-05-20 12:17:26 -06001733 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001734 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001735 break;
John Kessenich87142c72016-03-12 20:24:24 -07001736 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001737 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001738 break;
1739 case EHTokFloat3x3:
1740 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1741 break;
1742 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001743 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001744 break;
John Kessenich0133c122016-05-20 12:17:26 -06001745 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001746 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001747 break;
John Kessenich87142c72016-03-12 20:24:24 -07001748 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001749 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001750 break;
1751 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001752 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001753 break;
1754 case EHTokFloat4x4:
1755 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1756 break;
1757
John Kessenich0133c122016-05-20 12:17:26 -06001758 case EHTokDouble1x1:
1759 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1760 break;
1761 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001762 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001763 break;
1764 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001765 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001766 break;
1767 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001768 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001769 break;
1770 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001771 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001772 break;
1773 case EHTokDouble2x2:
1774 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1775 break;
1776 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001777 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001778 break;
1779 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001780 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001781 break;
1782 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001783 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001784 break;
1785 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001786 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001787 break;
1788 case EHTokDouble3x3:
1789 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1790 break;
1791 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001792 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001793 break;
1794 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001795 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001796 break;
1797 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001798 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001799 break;
1800 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001801 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001802 break;
1803 case EHTokDouble4x4:
1804 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1805 break;
1806
John Kessenich87142c72016-03-12 20:24:24 -07001807 default:
1808 return false;
1809 }
1810
1811 advanceToken();
1812
1813 return true;
1814}
1815
John Kesseniche6e74942016-06-11 16:43:14 -06001816// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001817// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1818// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
John Kessenich854fe242017-03-02 14:30:59 -07001819// | struct_type IDENTIFIER // use of previously declared struct type
John Kessenich3d157c52016-07-25 16:05:33 -06001820//
1821// struct_type
1822// : STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001823// | CLASS
John Kessenich3d157c52016-07-25 16:05:33 -06001824// | CBUFFER
1825// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001826//
John Kessenich54ee28f2017-03-11 14:13:00 -07001827bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
John Kesseniche6e74942016-06-11 16:43:14 -06001828{
John Kessenichb804de62016-09-05 12:19:18 -06001829 // This storage qualifier will tell us whether it's an AST
1830 // block type or just a generic structure type.
1831 TStorageQualifier storageQualifier = EvqTemporary;
John Kessenich3d157c52016-07-25 16:05:33 -06001832
1833 // CBUFFER
1834 if (acceptTokenClass(EHTokCBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001835 storageQualifier = EvqUniform;
John Kessenich3d157c52016-07-25 16:05:33 -06001836 // TBUFFER
1837 else if (acceptTokenClass(EHTokTBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001838 storageQualifier = EvqBuffer;
John Kessenich27ffb292017-03-03 17:01:01 -07001839 // CLASS
John Kesseniche6e74942016-06-11 16:43:14 -06001840 // STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001841 else if (! acceptTokenClass(EHTokClass) && ! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001842 return false;
1843
1844 // IDENTIFIER
1845 TString structName = "";
1846 if (peekTokenClass(EHTokIdentifier)) {
1847 structName = *token.string;
1848 advanceToken();
1849 }
1850
John Kessenich3d157c52016-07-25 16:05:33 -06001851 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001852 TQualifier postDeclQualifier;
1853 postDeclQualifier.clear();
John Kessenich854fe242017-03-02 14:30:59 -07001854 bool postDeclsFound = acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06001855
John Kessenichf3d88bd2017-03-19 12:24:29 -06001856 // LEFT_BRACE, or
John Kessenich854fe242017-03-02 14:30:59 -07001857 // struct_type IDENTIFIER
John Kesseniche6e74942016-06-11 16:43:14 -06001858 if (! acceptTokenClass(EHTokLeftBrace)) {
John Kessenich854fe242017-03-02 14:30:59 -07001859 if (structName.size() > 0 && !postDeclsFound && parseContext.lookupUserType(structName, type) != nullptr) {
1860 // struct_type IDENTIFIER
1861 return true;
1862 } else {
1863 expected("{");
1864 return false;
1865 }
John Kesseniche6e74942016-06-11 16:43:14 -06001866 }
1867
John Kessenichf3d88bd2017-03-19 12:24:29 -06001868
John Kesseniche6e74942016-06-11 16:43:14 -06001869 // struct_declaration_list
1870 TTypeList* typeList;
John Kessenichf3d88bd2017-03-19 12:24:29 -06001871 // Save each member function so they can be processed after we have a fully formed 'this'.
1872 TVector<TFunctionDeclarator> functionDeclarators;
1873
1874 parseContext.pushNamespace(structName);
John Kessenichaa3c64c2017-03-28 09:52:38 -06001875 bool acceptedList = acceptStructDeclarationList(typeList, nodeList, functionDeclarators);
John Kessenichf3d88bd2017-03-19 12:24:29 -06001876 parseContext.popNamespace();
1877
1878 if (! acceptedList) {
John Kesseniche6e74942016-06-11 16:43:14 -06001879 expected("struct member declarations");
1880 return false;
1881 }
1882
1883 // RIGHT_BRACE
1884 if (! acceptTokenClass(EHTokRightBrace)) {
1885 expected("}");
1886 return false;
1887 }
1888
1889 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001890 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001891 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001892 else {
John Kessenich7735b942016-09-05 12:40:06 -06001893 postDeclQualifier.storage = storageQualifier;
1894 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001895 }
John Kesseniche6e74942016-06-11 16:43:14 -06001896
John Kessenich727b3742017-02-03 17:57:55 -07001897 parseContext.declareStruct(token.loc, structName, type);
John Kesseniche6e74942016-06-11 16:43:14 -06001898
John Kessenich4960baa2017-03-19 18:09:59 -06001899 // For member functions: now that we know the type of 'this', go back and
1900 // - add their implicit argument with 'this' (not to the mangling, just the argument list)
1901 // - parse the functions, their tokens were saved for deferred parsing (now)
1902 for (int b = 0; b < (int)functionDeclarators.size(); ++b) {
1903 // update signature
1904 if (functionDeclarators[b].function->hasImplicitThis())
John Kessenich37789792017-03-21 23:56:40 -06001905 functionDeclarators[b].function->addThisParameter(type, intermediate.implicitThisName);
John Kessenich4960baa2017-03-19 18:09:59 -06001906 }
1907
John Kessenichf3d88bd2017-03-19 12:24:29 -06001908 // All member functions get parsed inside the class/struct namespace and with the
1909 // class/struct members in a symbol-table level.
1910 parseContext.pushNamespace(structName);
John Kessenich37789792017-03-21 23:56:40 -06001911 parseContext.pushThisScope(type);
John Kessenichf3d88bd2017-03-19 12:24:29 -06001912 bool deferredSuccess = true;
1913 for (int b = 0; b < (int)functionDeclarators.size() && deferredSuccess; ++b) {
1914 // parse body
1915 pushTokenStream(functionDeclarators[b].body);
1916 if (! acceptFunctionBody(functionDeclarators[b], nodeList))
1917 deferredSuccess = false;
1918 popTokenStream();
1919 }
John Kessenich37789792017-03-21 23:56:40 -06001920 parseContext.popThisScope();
John Kessenichf3d88bd2017-03-19 12:24:29 -06001921 parseContext.popNamespace();
1922
1923 return deferredSuccess;
John Kesseniche6e74942016-06-11 16:43:14 -06001924}
1925
steve-lunarg5da1f032017-02-12 17:50:28 -07001926// struct_buffer
1927// : APPENDSTRUCTUREDBUFFER
1928// | BYTEADDRESSBUFFER
1929// | CONSUMESTRUCTUREDBUFFER
1930// | RWBYTEADDRESSBUFFER
1931// | RWSTRUCTUREDBUFFER
1932// | STRUCTUREDBUFFER
1933bool HlslGrammar::acceptStructBufferType(TType& type)
1934{
1935 const EHlslTokenClass structBuffType = peek();
1936
1937 // TODO: globallycoherent
1938 bool hasTemplateType = true;
1939 bool readonly = false;
1940
1941 TStorageQualifier storage = EvqBuffer;
1942
1943 switch (structBuffType) {
1944 case EHTokAppendStructuredBuffer:
1945 unimplemented("AppendStructuredBuffer");
1946 return false;
1947 case EHTokByteAddressBuffer:
1948 hasTemplateType = false;
1949 readonly = true;
1950 break;
1951 case EHTokConsumeStructuredBuffer:
1952 unimplemented("ConsumeStructuredBuffer");
1953 return false;
1954 case EHTokRWByteAddressBuffer:
1955 hasTemplateType = false;
1956 break;
1957 case EHTokRWStructuredBuffer:
1958 break;
1959 case EHTokStructuredBuffer:
1960 readonly = true;
1961 break;
1962 default:
1963 return false; // not a structure buffer type
1964 }
1965
1966 advanceToken(); // consume the structure keyword
1967
1968 // type on which this StructedBuffer is templatized. E.g, StructedBuffer<MyStruct> ==> MyStruct
1969 TType* templateType = new TType;
1970
1971 if (hasTemplateType) {
1972 if (! acceptTokenClass(EHTokLeftAngle)) {
1973 expected("left angle bracket");
1974 return false;
1975 }
1976
1977 if (! acceptType(*templateType)) {
1978 expected("type");
1979 return false;
1980 }
1981 if (! acceptTokenClass(EHTokRightAngle)) {
1982 expected("right angle bracket");
1983 return false;
1984 }
1985 } else {
1986 // byte address buffers have no explicit type.
1987 TType uintType(EbtUint, storage);
1988 templateType->shallowCopy(uintType);
1989 }
1990
1991 // Create an unsized array out of that type.
1992 // TODO: does this work if it's already an array type?
1993 TArraySizes unsizedArray;
1994 unsizedArray.addInnerSize(UnsizedArraySize);
1995 templateType->newArraySizes(unsizedArray);
steve-lunarg40efe5c2017-03-06 12:01:44 -07001996 templateType->getQualifier().storage = storage;
steve-lunargdd8287a2017-02-23 18:04:12 -07001997
1998 // field name is canonical for all structbuffers
1999 templateType->setFieldName("@data");
steve-lunarg5da1f032017-02-12 17:50:28 -07002000
2001 // Create block type. TODO: hidden internal uint member when needed
steve-lunargdd8287a2017-02-23 18:04:12 -07002002
steve-lunarg5da1f032017-02-12 17:50:28 -07002003 TTypeList* blockStruct = new TTypeList;
2004 TTypeLoc member = { templateType, token.loc };
2005 blockStruct->push_back(member);
2006
steve-lunargdd8287a2017-02-23 18:04:12 -07002007 // This is the type of the buffer block (SSBO)
steve-lunarg5da1f032017-02-12 17:50:28 -07002008 TType blockType(blockStruct, "", templateType->getQualifier());
2009
steve-lunargdd8287a2017-02-23 18:04:12 -07002010 blockType.getQualifier().storage = storage;
2011 blockType.getQualifier().readonly = readonly;
2012
2013 // We may have created an equivalent type before, in which case we should use its
2014 // deep structure.
2015 parseContext.shareStructBufferType(blockType);
2016
steve-lunarg5da1f032017-02-12 17:50:28 -07002017 type.shallowCopy(blockType);
2018
2019 return true;
2020}
2021
John Kesseniche6e74942016-06-11 16:43:14 -06002022// struct_declaration_list
2023// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
2024//
2025// struct_declaration
2026// : fully_specified_type struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002027// | fully_specified_type IDENTIFIER function_parameters post_decls compound_statement // member-function definition
John Kesseniche6e74942016-06-11 16:43:14 -06002028//
2029// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06002030// : IDENTIFIER post_decls
2031// | IDENTIFIER array_specifier post_decls
John Kessenich54ee28f2017-03-11 14:13:00 -07002032// | IDENTIFIER function_parameters post_decls // member-function prototype
John Kesseniche6e74942016-06-11 16:43:14 -06002033//
John Kessenichaa3c64c2017-03-28 09:52:38 -06002034bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList, TIntermNode*& nodeList,
John Kessenichf3d88bd2017-03-19 12:24:29 -06002035 TVector<TFunctionDeclarator>& declarators)
John Kesseniche6e74942016-06-11 16:43:14 -06002036{
2037 typeList = new TTypeList();
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002038 HlslToken idToken;
John Kesseniche6e74942016-06-11 16:43:14 -06002039
2040 do {
2041 // success on seeing the RIGHT_BRACE coming up
2042 if (peekTokenClass(EHTokRightBrace))
John Kessenichb16f7e62017-03-11 19:32:47 -07002043 break;
John Kesseniche6e74942016-06-11 16:43:14 -06002044
2045 // struct_declaration
John Kessenich54ee28f2017-03-11 14:13:00 -07002046
2047 bool declarator_list = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002048
2049 // fully_specified_type
2050 TType memberType;
John Kessenich54ee28f2017-03-11 14:13:00 -07002051 if (! acceptFullySpecifiedType(memberType, nodeList)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002052 expected("member type");
2053 return false;
2054 }
2055
2056 // struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07002057 bool functionDefinitionAccepted = false;
John Kesseniche6e74942016-06-11 16:43:14 -06002058 do {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002059 if (! acceptIdentifier(idToken)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002060 expected("member name");
2061 return false;
2062 }
2063
John Kessenich54ee28f2017-03-11 14:13:00 -07002064 if (peekTokenClass(EHTokLeftParen)) {
2065 // function_parameters
2066 if (!declarator_list) {
John Kessenichb16f7e62017-03-11 19:32:47 -07002067 declarators.resize(declarators.size() + 1);
2068 // request a token stream for deferred processing
John Kessenichf3d88bd2017-03-19 12:24:29 -06002069 functionDefinitionAccepted = acceptMemberFunctionDefinition(nodeList, memberType, *idToken.string,
2070 declarators.back());
John Kessenich54ee28f2017-03-11 14:13:00 -07002071 if (functionDefinitionAccepted)
2072 break;
2073 }
2074 expected("member-function definition");
2075 return false;
2076 } else {
2077 // add it to the list of members
2078 TTypeLoc member = { new TType(EbtVoid), token.loc };
2079 member.type->shallowCopy(memberType);
2080 member.type->setFieldName(*idToken.string);
2081 typeList->push_back(member);
John Kesseniche6e74942016-06-11 16:43:14 -06002082
John Kessenich54ee28f2017-03-11 14:13:00 -07002083 // array_specifier
2084 TArraySizes* arraySizes = nullptr;
2085 acceptArraySpecifier(arraySizes);
2086 if (arraySizes)
2087 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06002088
John Kessenich54ee28f2017-03-11 14:13:00 -07002089 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06002090
John Kessenich54ee28f2017-03-11 14:13:00 -07002091 // EQUAL assignment_expression
2092 if (acceptTokenClass(EHTokAssign)) {
2093 parseContext.warn(idToken.loc, "struct-member initializers ignored", "typedef", "");
2094 TIntermTyped* expressionNode = nullptr;
2095 if (! acceptAssignmentExpression(expressionNode)) {
2096 expected("initializer");
2097 return false;
2098 }
John Kessenich18adbdb2017-02-02 15:16:20 -07002099 }
2100 }
John Kesseniche6e74942016-06-11 16:43:14 -06002101 // success on seeing the SEMICOLON coming up
2102 if (peekTokenClass(EHTokSemicolon))
2103 break;
2104
2105 // COMMA
John Kessenich54ee28f2017-03-11 14:13:00 -07002106 if (acceptTokenClass(EHTokComma))
2107 declarator_list = true;
2108 else {
John Kesseniche6e74942016-06-11 16:43:14 -06002109 expected(",");
2110 return false;
2111 }
2112
2113 } while (true);
2114
2115 // SEMI_COLON
John Kessenich54ee28f2017-03-11 14:13:00 -07002116 if (! functionDefinitionAccepted && ! acceptTokenClass(EHTokSemicolon)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002117 expected(";");
2118 return false;
2119 }
2120
2121 } while (true);
John Kessenichb16f7e62017-03-11 19:32:47 -07002122
John Kessenichb16f7e62017-03-11 19:32:47 -07002123 return true;
John Kesseniche6e74942016-06-11 16:43:14 -06002124}
2125
John Kessenich54ee28f2017-03-11 14:13:00 -07002126// member_function_definition
2127// | function_parameters post_decls compound_statement
2128//
2129// Expects type to have EvqGlobal for a static member and
2130// EvqTemporary for non-static member.
John Kessenichf3d88bd2017-03-19 12:24:29 -06002131bool HlslGrammar::acceptMemberFunctionDefinition(TIntermNode*& nodeList, const TType& type, const TString& memberName,
2132 TFunctionDeclarator& declarator)
John Kessenich54ee28f2017-03-11 14:13:00 -07002133{
John Kessenich54ee28f2017-03-11 14:13:00 -07002134 bool accepted = false;
2135
John Kessenich4dc835c2017-03-28 23:43:10 -06002136 const TString* functionName = &memberName;
2137 parseContext.getFullNamespaceName(functionName);
John Kessenich088d52b2017-03-11 17:55:28 -07002138 declarator.function = new TFunction(functionName, type);
John Kessenich4960baa2017-03-19 18:09:59 -06002139 if (type.getQualifier().storage == EvqTemporary)
2140 declarator.function->setImplicitThis();
John Kessenich37789792017-03-21 23:56:40 -06002141 else
2142 declarator.function->setIllegalImplicitThis();
John Kessenich54ee28f2017-03-11 14:13:00 -07002143
2144 // function_parameters
John Kessenich088d52b2017-03-11 17:55:28 -07002145 if (acceptFunctionParameters(*declarator.function)) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002146 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -07002147 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich54ee28f2017-03-11 14:13:00 -07002148
2149 // compound_statement (function body definition)
2150 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich088d52b2017-03-11 17:55:28 -07002151 declarator.loc = token.loc;
John Kessenichf3d88bd2017-03-19 12:24:29 -06002152 declarator.body = new TVector<HlslToken>;
2153 accepted = acceptFunctionDefinition(declarator, nodeList, declarator.body);
John Kessenich54ee28f2017-03-11 14:13:00 -07002154 }
2155 } else
2156 expected("function parameter list");
2157
John Kessenich54ee28f2017-03-11 14:13:00 -07002158 return accepted;
2159}
2160
John Kessenich5f934b02016-03-13 17:58:25 -06002161// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06002162// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06002163// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002164//
2165bool HlslGrammar::acceptFunctionParameters(TFunction& function)
2166{
John Kessenich078d7f22016-03-14 10:02:11 -06002167 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002168 if (! acceptTokenClass(EHTokLeftParen))
2169 return false;
2170
John Kessenich71351de2016-06-08 12:50:56 -06002171 // VOID RIGHT_PAREN
2172 if (! acceptTokenClass(EHTokVoid)) {
2173 do {
2174 // parameter_declaration
2175 if (! acceptParameterDeclaration(function))
2176 break;
John Kessenich5f934b02016-03-13 17:58:25 -06002177
John Kessenich71351de2016-06-08 12:50:56 -06002178 // COMMA
2179 if (! acceptTokenClass(EHTokComma))
2180 break;
2181 } while (true);
2182 }
John Kessenich5f934b02016-03-13 17:58:25 -06002183
John Kessenich078d7f22016-03-14 10:02:11 -06002184 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002185 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002186 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06002187 return false;
2188 }
2189
2190 return true;
2191}
2192
steve-lunarg26d31452016-12-23 18:56:57 -07002193// default_parameter_declaration
2194// : EQUAL conditional_expression
2195// : EQUAL initializer
2196bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTyped*& node)
2197{
2198 node = nullptr;
2199
2200 // Valid not to have a default_parameter_declaration
2201 if (!acceptTokenClass(EHTokAssign))
2202 return true;
2203
2204 if (!acceptConditionalExpression(node)) {
2205 if (!acceptInitializer(node))
2206 return false;
2207
2208 // For initializer lists, we have to const-fold into a constructor for the type, so build
2209 // that.
2210 TFunction* constructor = parseContext.handleConstructorCall(token.loc, type);
2211 if (constructor == nullptr) // cannot construct
2212 return false;
2213
2214 TIntermTyped* arguments = nullptr;
John Kessenichecba76f2017-01-06 00:34:48 -07002215 for (int i = 0; i < int(node->getAsAggregate()->getSequence().size()); i++)
steve-lunarg26d31452016-12-23 18:56:57 -07002216 parseContext.handleFunctionArgument(constructor, arguments, node->getAsAggregate()->getSequence()[i]->getAsTyped());
John Kessenichecba76f2017-01-06 00:34:48 -07002217
steve-lunarg26d31452016-12-23 18:56:57 -07002218 node = parseContext.handleFunctionCall(token.loc, constructor, node);
2219 }
2220
2221 // If this is simply a constant, we can use it directly.
2222 if (node->getAsConstantUnion())
2223 return true;
2224
2225 // Otherwise, it has to be const-foldable.
2226 TIntermTyped* origNode = node;
2227
2228 node = intermediate.fold(node->getAsAggregate());
2229
2230 if (node != nullptr && origNode != node)
2231 return true;
2232
2233 parseContext.error(token.loc, "invalid default parameter value", "", "");
2234
2235 return false;
2236}
2237
John Kessenich5f934b02016-03-13 17:58:25 -06002238// parameter_declaration
steve-lunarg26d31452016-12-23 18:56:57 -07002239// : fully_specified_type post_decls [ = default_parameter_declaration ]
2240// | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
John Kessenich5f934b02016-03-13 17:58:25 -06002241//
2242bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
2243{
2244 // fully_specified_type
2245 TType* type = new TType;
2246 if (! acceptFullySpecifiedType(*type))
2247 return false;
2248
2249 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06002250 HlslToken idToken;
2251 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06002252
John Kessenich19b92ff2016-06-19 11:50:34 -06002253 // array_specifier
2254 TArraySizes* arraySizes = nullptr;
2255 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002256 if (arraySizes) {
2257 if (arraySizes->isImplicit()) {
2258 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
2259 return false;
2260 }
2261
John Kessenich19b92ff2016-06-19 11:50:34 -06002262 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002263 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002264
2265 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06002266 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06002267
steve-lunarg26d31452016-12-23 18:56:57 -07002268 TIntermTyped* defaultValue;
2269 if (!acceptDefaultParameterDeclaration(*type, defaultValue))
2270 return false;
2271
John Kessenich5aa59e22016-06-17 15:50:47 -06002272 parseContext.paramFix(*type);
2273
steve-lunarg26d31452016-12-23 18:56:57 -07002274 // If any prior parameters have default values, all the parameters after that must as well.
2275 if (defaultValue == nullptr && function.getDefaultParamCount() > 0) {
2276 parseContext.error(idToken.loc, "invalid parameter after default value parameters", idToken.string->c_str(), "");
2277 return false;
2278 }
2279
2280 TParameter param = { idToken.string, type, defaultValue };
John Kessenich5f934b02016-03-13 17:58:25 -06002281 function.addParameter(param);
2282
2283 return true;
2284}
2285
2286// Do the work to create the function definition in addition to
2287// parsing the body (compound_statement).
John Kessenichb16f7e62017-03-11 19:32:47 -07002288//
2289// If 'deferredTokens' are passed in, just get the token stream,
2290// don't process.
2291//
2292bool HlslGrammar::acceptFunctionDefinition(TFunctionDeclarator& declarator, TIntermNode*& nodeList,
2293 TVector<HlslToken>* deferredTokens)
John Kessenich5f934b02016-03-13 17:58:25 -06002294{
John Kessenich088d52b2017-03-11 17:55:28 -07002295 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, false /* not prototype */);
John Kessenich5f934b02016-03-13 17:58:25 -06002296
John Kessenichb16f7e62017-03-11 19:32:47 -07002297 if (deferredTokens)
2298 return captureBlockTokens(*deferredTokens);
2299 else
John Kessenich4960baa2017-03-19 18:09:59 -06002300 return acceptFunctionBody(declarator, nodeList);
John Kessenich088d52b2017-03-11 17:55:28 -07002301}
2302
2303bool HlslGrammar::acceptFunctionBody(TFunctionDeclarator& declarator, TIntermNode*& nodeList)
2304{
2305 // we might get back an entry-point
John Kessenichca71d942017-03-07 20:44:09 -07002306 TIntermNode* entryPointNode = nullptr;
2307
John Kessenich077e0522016-06-09 02:02:17 -06002308 // This does a pushScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002309 TIntermNode* functionNode = parseContext.handleFunctionDefinition(declarator.loc, *declarator.function,
2310 declarator.attributes, entryPointNode);
John Kessenich5f934b02016-03-13 17:58:25 -06002311
2312 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002313 TIntermNode* functionBody = nullptr;
John Kessenich02467d82017-01-19 15:41:47 -07002314 if (! acceptCompoundStatement(functionBody))
2315 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002316
John Kessenich54ee28f2017-03-11 14:13:00 -07002317 // this does a popScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002318 parseContext.handleFunctionBody(declarator.loc, *declarator.function, functionBody, functionNode);
John Kessenichca71d942017-03-07 20:44:09 -07002319
2320 // Hook up the 1 or 2 function definitions.
2321 nodeList = intermediate.growAggregate(nodeList, functionNode);
2322 nodeList = intermediate.growAggregate(nodeList, entryPointNode);
John Kessenich02467d82017-01-19 15:41:47 -07002323
2324 return true;
John Kessenich5f934b02016-03-13 17:58:25 -06002325}
2326
John Kessenich0d2b6de2016-06-05 11:23:11 -06002327// Accept an expression with parenthesis around it, where
2328// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002329// syntactically required ones like in "if ( expression )".
2330//
2331// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06002332//
2333// Note this one is not set up to be speculative; as it gives
2334// errors if not found.
2335//
2336bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
2337{
2338 // LEFT_PAREN
2339 if (! acceptTokenClass(EHTokLeftParen))
2340 expected("(");
2341
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002342 bool decl = false;
2343 TIntermNode* declNode = nullptr;
2344 decl = acceptControlDeclaration(declNode);
2345 if (decl) {
2346 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
2347 expected("initialized declaration");
2348 return false;
2349 } else
2350 expression = declNode->getAsTyped();
2351 } else {
2352 // no declaration
2353 if (! acceptExpression(expression)) {
2354 expected("expression");
2355 return false;
2356 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06002357 }
2358
2359 // RIGHT_PAREN
2360 if (! acceptTokenClass(EHTokRightParen))
2361 expected(")");
2362
2363 return true;
2364}
2365
John Kessenich34fb0362016-05-03 23:17:20 -06002366// The top-level full expression recognizer.
2367//
John Kessenich87142c72016-03-12 20:24:24 -07002368// expression
John Kessenich34fb0362016-05-03 23:17:20 -06002369// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07002370//
2371bool HlslGrammar::acceptExpression(TIntermTyped*& node)
2372{
LoopDawgef764a22016-06-03 09:17:51 -06002373 node = nullptr;
2374
John Kessenich34fb0362016-05-03 23:17:20 -06002375 // assignment_expression
2376 if (! acceptAssignmentExpression(node))
2377 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002378
John Kessenich34fb0362016-05-03 23:17:20 -06002379 if (! peekTokenClass(EHTokComma))
2380 return true;
2381
2382 do {
2383 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06002384 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06002385 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06002386
John Kessenich34fb0362016-05-03 23:17:20 -06002387 // ... assignment_expression
2388 TIntermTyped* rightNode = nullptr;
2389 if (! acceptAssignmentExpression(rightNode)) {
2390 expected("assignment expression");
2391 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002392 }
2393
John Kessenich34fb0362016-05-03 23:17:20 -06002394 node = intermediate.addComma(node, rightNode, loc);
2395
2396 if (! peekTokenClass(EHTokComma))
2397 return true;
2398 } while (true);
2399}
2400
John Kessenich07354242016-07-01 19:58:06 -06002401// initializer
John Kessenich98ad4852016-11-27 17:39:07 -07002402// : LEFT_BRACE RIGHT_BRACE
2403// | LEFT_BRACE initializer_list RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002404//
2405// initializer_list
2406// : assignment_expression COMMA assignment_expression COMMA ...
2407//
2408bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
2409{
2410 // LEFT_BRACE
2411 if (! acceptTokenClass(EHTokLeftBrace))
2412 return false;
2413
John Kessenich98ad4852016-11-27 17:39:07 -07002414 // RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002415 TSourceLoc loc = token.loc;
John Kessenich98ad4852016-11-27 17:39:07 -07002416 if (acceptTokenClass(EHTokRightBrace)) {
2417 // a zero-length initializer list
2418 node = intermediate.makeAggregate(loc);
2419 return true;
2420 }
2421
2422 // initializer_list
John Kessenich07354242016-07-01 19:58:06 -06002423 node = nullptr;
2424 do {
2425 // assignment_expression
2426 TIntermTyped* expr;
2427 if (! acceptAssignmentExpression(expr)) {
2428 expected("assignment expression in initializer list");
2429 return false;
2430 }
2431 node = intermediate.growAggregate(node, expr, loc);
2432
2433 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002434 if (acceptTokenClass(EHTokComma)) {
2435 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
2436 return true;
John Kessenich07354242016-07-01 19:58:06 -06002437 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002438 }
John Kessenich07354242016-07-01 19:58:06 -06002439
2440 // RIGHT_BRACE
2441 if (acceptTokenClass(EHTokRightBrace))
2442 return true;
2443
2444 expected(", or }");
2445 return false;
2446 } while (true);
2447}
2448
John Kessenich34fb0362016-05-03 23:17:20 -06002449// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06002450// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06002451//
2452// a op (b op (c op d))
2453//
2454// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06002455// : initializer
2456// | conditional_expression
2457// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06002458//
2459bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
2460{
John Kessenich07354242016-07-01 19:58:06 -06002461 // initializer
2462 if (peekTokenClass(EHTokLeftBrace)) {
2463 if (acceptInitializer(node))
2464 return true;
2465
2466 expected("initializer");
2467 return false;
2468 }
2469
John Kessenich00957f82016-07-27 10:39:57 -06002470 // conditional_expression
2471 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06002472 return false;
2473
John Kessenich07354242016-07-01 19:58:06 -06002474 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06002475 TOperator assignOp = HlslOpMap::assignment(peek());
2476 if (assignOp == EOpNull)
2477 return true;
2478
John Kessenich00957f82016-07-27 10:39:57 -06002479 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06002480 TSourceLoc loc = token.loc;
2481 advanceToken();
2482
John Kessenich00957f82016-07-27 10:39:57 -06002483 // conditional_expression assign_op conditional_expression ...
2484 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06002485 // gets the right-to-left associativity.
2486 TIntermTyped* rightNode = nullptr;
2487 if (! acceptAssignmentExpression(rightNode)) {
2488 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06002489 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002490 }
2491
John Kessenichd21baed2016-09-16 03:05:12 -06002492 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
steve-lunarg90707962016-10-07 19:35:40 -06002493 node = parseContext.handleLvalue(loc, "assign", node);
2494
John Kessenichfea226b2016-07-28 17:53:56 -06002495 if (node == nullptr) {
2496 parseContext.error(loc, "could not create assignment", "", "");
2497 return false;
2498 }
John Kessenich34fb0362016-05-03 23:17:20 -06002499
2500 if (! peekTokenClass(EHTokComma))
2501 return true;
2502
2503 return true;
2504}
2505
John Kessenich00957f82016-07-27 10:39:57 -06002506// Accept a conditional expression, which associates right-to-left,
2507// accomplished by the "true" expression calling down to lower
2508// precedence levels than this level.
2509//
2510// conditional_expression
2511// : binary_expression
2512// | binary_expression QUESTION expression COLON assignment_expression
2513//
2514bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
2515{
2516 // binary_expression
2517 if (! acceptBinaryExpression(node, PlLogicalOr))
2518 return false;
2519
2520 if (! acceptTokenClass(EHTokQuestion))
2521 return true;
2522
John Kessenich7e997e22017-03-30 22:09:30 -06002523 node = parseContext.convertConditionalExpression(token.loc, node);
2524 if (node == nullptr)
2525 return false;
2526
John Kessenich00957f82016-07-27 10:39:57 -06002527 TIntermTyped* trueNode = nullptr;
2528 if (! acceptExpression(trueNode)) {
2529 expected("expression after ?");
2530 return false;
2531 }
2532 TSourceLoc loc = token.loc;
2533
2534 if (! acceptTokenClass(EHTokColon)) {
2535 expected(":");
2536 return false;
2537 }
2538
2539 TIntermTyped* falseNode = nullptr;
2540 if (! acceptAssignmentExpression(falseNode)) {
2541 expected("expression after :");
2542 return false;
2543 }
2544
2545 node = intermediate.addSelection(node, trueNode, falseNode, loc);
2546
2547 return true;
2548}
2549
John Kessenich34fb0362016-05-03 23:17:20 -06002550// Accept a binary expression, for binary operations that
2551// associate left-to-right. This is, it is implicit, for example
2552//
2553// ((a op b) op c) op d
2554//
2555// binary_expression
2556// : expression op expression op expression ...
2557//
2558// where 'expression' is the next higher level in precedence.
2559//
2560bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
2561{
2562 if (precedenceLevel > PlMul)
2563 return acceptUnaryExpression(node);
2564
2565 // assignment_expression
2566 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
2567 return false;
2568
John Kessenich34fb0362016-05-03 23:17:20 -06002569 do {
John Kessenich64076ed2016-07-28 21:43:17 -06002570 TOperator op = HlslOpMap::binary(peek());
2571 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
2572 if (tokenLevel < precedenceLevel)
2573 return true;
2574
John Kessenich34fb0362016-05-03 23:17:20 -06002575 // ... op
2576 TSourceLoc loc = token.loc;
2577 advanceToken();
2578
2579 // ... expression
2580 TIntermTyped* rightNode = nullptr;
2581 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
2582 expected("expression");
2583 return false;
2584 }
2585
2586 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06002587 if (node == nullptr) {
2588 parseContext.error(loc, "Could not perform requested binary operation", "", "");
2589 return false;
2590 }
John Kessenich34fb0362016-05-03 23:17:20 -06002591 } while (true);
2592}
2593
2594// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06002595// : (type) unary_expression
2596// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06002597// | - unary_expression
2598// | ! unary_expression
2599// | ~ unary_expression
2600// | ++ unary_expression
2601// | -- unary_expression
2602// | postfix_expression
2603//
2604bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
2605{
John Kessenich1cc1a282016-06-03 16:55:49 -06002606 // (type) unary_expression
2607 // Have to look two steps ahead, because this could be, e.g., a
2608 // postfix_expression instead, since that also starts with at "(".
2609 if (acceptTokenClass(EHTokLeftParen)) {
2610 TType castType;
2611 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06002612 if (acceptTokenClass(EHTokRightParen)) {
2613 // We've matched "(type)" now, get the expression to cast
2614 TSourceLoc loc = token.loc;
2615 if (! acceptUnaryExpression(node))
2616 return false;
2617
2618 // Hook it up like a constructor
2619 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
2620 if (constructorFunction == nullptr) {
2621 expected("type that can be constructed");
2622 return false;
2623 }
2624 TIntermTyped* arguments = nullptr;
2625 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
2626 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
2627
2628 return true;
2629 } else {
2630 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
2631 // the '(int' part. We must back up twice.
2632 recedeToken();
2633 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06002634 }
John Kessenich1cc1a282016-06-03 16:55:49 -06002635 } else {
2636 // This isn't a type cast, but it still started "(", so if it is a
2637 // unary expression, it can only be a postfix_expression, so try that.
2638 // Back it up first.
2639 recedeToken();
2640 return acceptPostfixExpression(node);
2641 }
2642 }
2643
2644 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06002645 TOperator unaryOp = HlslOpMap::preUnary(peek());
John Kessenichecba76f2017-01-06 00:34:48 -07002646
John Kessenich1cc1a282016-06-03 16:55:49 -06002647 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06002648 if (unaryOp == EOpNull)
2649 return acceptPostfixExpression(node);
2650
2651 // op unary_expression
2652 TSourceLoc loc = token.loc;
2653 advanceToken();
2654 if (! acceptUnaryExpression(node))
2655 return false;
2656
2657 // + is a no-op
2658 if (unaryOp == EOpAdd)
2659 return true;
2660
2661 node = intermediate.addUnaryMath(unaryOp, node, loc);
steve-lunarge5921f12016-10-15 10:29:58 -06002662
2663 // These unary ops require lvalues
2664 if (unaryOp == EOpPreIncrement || unaryOp == EOpPreDecrement)
2665 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002666
2667 return node != nullptr;
2668}
2669
2670// postfix_expression
2671// : LEFT_PAREN expression RIGHT_PAREN
2672// | literal
2673// | constructor
John Kessenich8f9fdc92017-03-30 16:22:26 -06002674// | IDENTIFIER [ COLONCOLON IDENTIFIER [ COLONCOLON IDENTIFIER ... ] ]
John Kessenich34fb0362016-05-03 23:17:20 -06002675// | function_call
2676// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
2677// | postfix_expression DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002678// | postfix_expression DOT IDENTIFIER arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06002679// | postfix_expression arguments
John Kessenich34fb0362016-05-03 23:17:20 -06002680// | postfix_expression INC_OP
2681// | postfix_expression DEC_OP
2682//
2683bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
2684{
2685 // Not implemented as self-recursive:
John Kessenich54ee28f2017-03-11 14:13:00 -07002686 // The logical "right recursion" is done with a loop at the end
John Kessenich34fb0362016-05-03 23:17:20 -06002687
2688 // idToken will pick up either a variable or a function name in a function call
2689 HlslToken idToken;
2690
John Kessenich21472ae2016-06-04 11:46:33 -06002691 // Find something before the postfix operations, as they can't operate
2692 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07002693 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06002694 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002695 if (! acceptExpression(node)) {
2696 expected("expression");
2697 return false;
2698 }
2699 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002700 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002701 return false;
2702 }
John Kessenich34fb0362016-05-03 23:17:20 -06002703 } else if (acceptLiteral(node)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002704 // literal (nothing else to do yet)
John Kessenich34fb0362016-05-03 23:17:20 -06002705 } else if (acceptConstructor(node)) {
2706 // constructor (nothing else to do yet)
2707 } else if (acceptIdentifier(idToken)) {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002708 // user-type, namespace name, variable, or function name
2709 TString* fullName = idToken.string;
2710 while (acceptTokenClass(EHTokColonColon)) {
2711 // user-type or namespace name
2712 fullName = NewPoolTString(fullName->c_str());
2713 fullName->append(parseContext.scopeMangler);
2714 if (acceptIdentifier(idToken))
2715 fullName->append(*idToken.string);
2716 else {
2717 expected("identifier after ::");
John Kessenich54ee28f2017-03-11 14:13:00 -07002718 return false;
2719 }
John Kessenich8f9fdc92017-03-30 16:22:26 -06002720 }
2721 if (! peekTokenClass(EHTokLeftParen)) {
2722 node = parseContext.handleVariable(idToken.loc, fullName);
2723 } else if (acceptFunctionCall(idToken.loc, *fullName, node, nullptr)) {
John Kessenich34fb0362016-05-03 23:17:20 -06002724 // function_call (nothing else to do yet)
2725 } else {
2726 expected("function call arguments");
2727 return false;
2728 }
John Kessenich21472ae2016-06-04 11:46:33 -06002729 } else {
2730 // nothing found, can't post operate
2731 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002732 }
2733
steve-lunarga2b01a02016-11-28 17:09:54 -07002734 // This is to guarantee we do this no matter how we get out of the stack frame.
2735 // This way there's no bug if an early return forgets to do it.
2736 struct tFinalize {
2737 tFinalize(HlslParseContext& p) : parseContext(p) { }
2738 ~tFinalize() { parseContext.finalizeFlattening(); }
John Kessenichf8d0d8c2017-02-08 17:31:03 -07002739 HlslParseContext& parseContext;
John Kessenich32fd5d22017-02-02 14:55:02 -07002740 private:
John Kessenichca71d942017-03-07 20:44:09 -07002741 const tFinalize& operator=(const tFinalize&) { return *this; }
John Kessenichefeefd92017-03-01 13:12:26 -07002742 tFinalize(const tFinalize& f) : parseContext(f.parseContext) { }
steve-lunarga2b01a02016-11-28 17:09:54 -07002743 } finalize(parseContext);
2744
2745 // Initialize the flattening accumulation data, so we can track data across multiple bracket or
2746 // dot operators. This can also be nested, e.g, for [], so we have to track each nesting
2747 // level: hence the init and finalize. Even though in practice these must be
2748 // constants, they are parsed no matter what.
2749 parseContext.initFlattening();
2750
John Kessenich21472ae2016-06-04 11:46:33 -06002751 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06002752 do {
2753 TSourceLoc loc = token.loc;
2754 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07002755
John Kessenich34fb0362016-05-03 23:17:20 -06002756 // Consume only a valid post-unary operator, otherwise we are done.
2757 switch (postOp) {
2758 case EOpIndexDirectStruct:
2759 case EOpIndexIndirect:
2760 case EOpPostIncrement:
2761 case EOpPostDecrement:
John Kessenich54ee28f2017-03-11 14:13:00 -07002762 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002763 advanceToken();
2764 break;
2765 default:
2766 return true;
2767 }
John Kessenich87142c72016-03-12 20:24:24 -07002768
John Kessenich34fb0362016-05-03 23:17:20 -06002769 // We have a valid post-unary operator, process it.
2770 switch (postOp) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002771 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002772 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06002773 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002774 // DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002775 // includes swizzles, member variables, and member functions
John Kessenich93a162a2016-06-17 17:16:27 -06002776 HlslToken field;
2777 if (! acceptIdentifier(field)) {
2778 expected("swizzle or member");
2779 return false;
2780 }
LoopDawg4886f692016-06-29 10:58:58 -06002781
John Kessenich516d92d2017-03-08 20:09:03 -07002782 if (peekTokenClass(EHTokLeftParen)) {
2783 // member function
2784 TIntermTyped* thisNode = node;
LoopDawg4886f692016-06-29 10:58:58 -06002785
John Kessenich516d92d2017-03-08 20:09:03 -07002786 // arguments
John Kessenich8f9fdc92017-03-30 16:22:26 -06002787 if (! acceptFunctionCall(field.loc, *field.string, node, thisNode)) {
LoopDawg4886f692016-06-29 10:58:58 -06002788 expected("function parameters");
2789 return false;
2790 }
John Kessenich516d92d2017-03-08 20:09:03 -07002791 } else
2792 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06002793
John Kessenich34fb0362016-05-03 23:17:20 -06002794 break;
John Kessenich93a162a2016-06-17 17:16:27 -06002795 }
John Kessenich34fb0362016-05-03 23:17:20 -06002796 case EOpIndexIndirect:
2797 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002798 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06002799 TIntermTyped* indexNode = nullptr;
2800 if (! acceptExpression(indexNode) ||
2801 ! peekTokenClass(EHTokRightBracket)) {
2802 expected("expression followed by ']'");
2803 return false;
2804 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002805 advanceToken();
2806 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
2807 break;
John Kessenich34fb0362016-05-03 23:17:20 -06002808 }
2809 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002810 // INC_OP
2811 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06002812 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002813 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06002814 node = intermediate.addUnaryMath(postOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06002815 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002816 break;
2817 default:
2818 assert(0);
2819 break;
2820 }
2821 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07002822}
2823
John Kessenichd016be12016-03-13 11:24:20 -06002824// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06002825// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06002826//
2827bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
2828{
2829 // type
2830 TType type;
2831 if (acceptType(type)) {
2832 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
2833 if (constructorFunction == nullptr)
2834 return false;
2835
2836 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06002837 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06002838 if (! acceptArguments(constructorFunction, arguments)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002839 // It's possible this is a type keyword used as an identifier. Put the token back
2840 // for later use.
2841 recedeToken();
John Kessenichd016be12016-03-13 11:24:20 -06002842 return false;
2843 }
2844
2845 // hook it up
2846 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
2847
2848 return true;
2849 }
2850
2851 return false;
2852}
2853
John Kessenich34fb0362016-05-03 23:17:20 -06002854// The function_call identifier was already recognized, and passed in as idToken.
2855//
2856// function_call
2857// : [idToken] arguments
2858//
John Kessenich8f9fdc92017-03-30 16:22:26 -06002859bool HlslGrammar::acceptFunctionCall(const TSourceLoc& loc, TString& name, TIntermTyped*& node, TIntermTyped* baseObject)
John Kessenich34fb0362016-05-03 23:17:20 -06002860{
John Kessenich54ee28f2017-03-11 14:13:00 -07002861 // name
2862 TString* functionName = nullptr;
John Kessenich8f9fdc92017-03-30 16:22:26 -06002863 if (baseObject == nullptr) {
2864 functionName = &name;
2865 } else if (parseContext.isBuiltInMethod(loc, baseObject, name)) {
John Kessenich4960baa2017-03-19 18:09:59 -06002866 // Built-in methods are not in the symbol table as methods, but as global functions
2867 // taking an explicit 'this' as the first argument.
steve-lunarge7d07522017-03-19 18:12:37 -06002868 functionName = NewPoolTString(BUILTIN_PREFIX);
John Kessenich8f9fdc92017-03-30 16:22:26 -06002869 functionName->append(name);
John Kessenich4960baa2017-03-19 18:09:59 -06002870 } else {
John Kessenich8f9fdc92017-03-30 16:22:26 -06002871 if (! baseObject->getType().isStruct()) {
2872 expected("structure");
2873 return false;
2874 }
John Kessenich54ee28f2017-03-11 14:13:00 -07002875 functionName = NewPoolTString("");
John Kessenich8f9fdc92017-03-30 16:22:26 -06002876 functionName->append(baseObject->getType().getTypeName());
John Kessenichf3d88bd2017-03-19 12:24:29 -06002877 parseContext.addScopeMangler(*functionName);
John Kessenich8f9fdc92017-03-30 16:22:26 -06002878 functionName->append(name);
John Kessenich5f12d2f2017-03-11 09:39:55 -07002879 }
LoopDawg4886f692016-06-29 10:58:58 -06002880
John Kessenich54ee28f2017-03-11 14:13:00 -07002881 // function
2882 TFunction* function = new TFunction(functionName, TType(EbtVoid));
2883
2884 // arguments
John Kessenich54ee28f2017-03-11 14:13:00 -07002885 TIntermTyped* arguments = nullptr;
John Kessenichdfbdd9e2017-03-19 13:10:28 -06002886 if (baseObject != nullptr) {
2887 // Non-static member functions have an implicit first argument of the base object.
John Kessenich54ee28f2017-03-11 14:13:00 -07002888 parseContext.handleFunctionArgument(function, arguments, baseObject);
John Kessenichdfbdd9e2017-03-19 13:10:28 -06002889 }
John Kessenich4678ca92016-05-13 09:33:42 -06002890 if (! acceptArguments(function, arguments))
2891 return false;
2892
John Kessenich54ee28f2017-03-11 14:13:00 -07002893 // call
John Kessenich8f9fdc92017-03-30 16:22:26 -06002894 node = parseContext.handleFunctionCall(loc, function, arguments);
John Kessenich4678ca92016-05-13 09:33:42 -06002895
2896 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002897}
2898
John Kessenich87142c72016-03-12 20:24:24 -07002899// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002900// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002901//
John Kessenichd016be12016-03-13 11:24:20 -06002902// The arguments are pushed onto the 'function' argument list and
2903// onto the 'arguments' aggregate.
2904//
John Kessenich4678ca92016-05-13 09:33:42 -06002905bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002906{
John Kessenich078d7f22016-03-14 10:02:11 -06002907 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002908 if (! acceptTokenClass(EHTokLeftParen))
2909 return false;
2910
2911 do {
John Kessenichd016be12016-03-13 11:24:20 -06002912 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002913 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002914 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002915 break;
John Kessenichd016be12016-03-13 11:24:20 -06002916
2917 // hook it up
2918 parseContext.handleFunctionArgument(function, arguments, arg);
2919
John Kessenich078d7f22016-03-14 10:02:11 -06002920 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002921 if (! acceptTokenClass(EHTokComma))
2922 break;
2923 } while (true);
2924
John Kessenich078d7f22016-03-14 10:02:11 -06002925 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002926 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002927 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002928 return false;
2929 }
2930
2931 return true;
2932}
2933
2934bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2935{
2936 switch (token.tokenClass) {
2937 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002938 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002939 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002940 case EHTokUintConstant:
2941 node = intermediate.addConstantUnion(token.u, token.loc, true);
2942 break;
John Kessenich87142c72016-03-12 20:24:24 -07002943 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002944 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002945 break;
2946 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002947 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002948 break;
2949 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002950 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002951 break;
John Kessenich86f71382016-09-19 20:23:18 -06002952 case EHTokStringConstant:
steve-lunarg858c9282017-01-07 08:54:10 -07002953 node = intermediate.addConstantUnion(token.string, token.loc, true);
John Kessenich86f71382016-09-19 20:23:18 -06002954 break;
John Kessenich87142c72016-03-12 20:24:24 -07002955
2956 default:
2957 return false;
2958 }
2959
2960 advanceToken();
2961
2962 return true;
2963}
2964
John Kessenich5f934b02016-03-13 17:58:25 -06002965// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002966// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002967//
John Kessenich21472ae2016-06-04 11:46:33 -06002968bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002969{
John Kessenich21472ae2016-06-04 11:46:33 -06002970 TIntermAggregate* compoundStatement = nullptr;
2971
John Kessenich34fb0362016-05-03 23:17:20 -06002972 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002973 if (! acceptTokenClass(EHTokLeftBrace))
2974 return false;
2975
2976 // statement statement ...
2977 TIntermNode* statement = nullptr;
2978 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002979 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2980 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2981 branch->getFlowOp() == EOpDefault)) {
2982 // hook up individual subsequences within a switch statement
2983 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2984 compoundStatement = nullptr;
2985 } else {
2986 // hook it up to the growing compound statement
2987 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2988 }
John Kessenich5f934b02016-03-13 17:58:25 -06002989 }
John Kessenich34fb0362016-05-03 23:17:20 -06002990 if (compoundStatement)
2991 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06002992
John Kessenich21472ae2016-06-04 11:46:33 -06002993 retStatement = compoundStatement;
2994
John Kessenich34fb0362016-05-03 23:17:20 -06002995 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002996 return acceptTokenClass(EHTokRightBrace);
2997}
2998
John Kessenich0d2b6de2016-06-05 11:23:11 -06002999bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
3000{
3001 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06003002 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003003 parseContext.popScope();
3004
3005 return result;
3006}
3007
John Kessenich077e0522016-06-09 02:02:17 -06003008bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003009{
John Kessenich077e0522016-06-09 02:02:17 -06003010 parseContext.pushScope();
3011 bool result = acceptCompoundStatement(statement);
3012 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06003013
3014 return result;
3015}
3016
John Kessenich5f934b02016-03-13 17:58:25 -06003017// statement
John Kessenich21472ae2016-06-04 11:46:33 -06003018// : attributes attributed_statement
3019//
3020// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003021// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06003022// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06003023// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06003024// | declaration_statement
3025// | selection_statement
3026// | switch_statement
3027// | case_label
3028// | iteration_statement
3029// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06003030//
3031bool HlslGrammar::acceptStatement(TIntermNode*& statement)
3032{
John Kessenich21472ae2016-06-04 11:46:33 -06003033 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06003034
John Kessenich21472ae2016-06-04 11:46:33 -06003035 // attributes
steve-lunarg1868b142016-10-20 13:07:10 -06003036 TAttributeMap attributes;
3037 acceptAttributes(attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06003038
John Kessenich21472ae2016-06-04 11:46:33 -06003039 // attributed_statement
3040 switch (peek()) {
3041 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06003042 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003043
John Kessenich21472ae2016-06-04 11:46:33 -06003044 case EHTokIf:
3045 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003046
John Kessenich21472ae2016-06-04 11:46:33 -06003047 case EHTokSwitch:
3048 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06003049
John Kessenich21472ae2016-06-04 11:46:33 -06003050 case EHTokFor:
3051 case EHTokDo:
3052 case EHTokWhile:
3053 return acceptIterationStatement(statement);
3054
3055 case EHTokContinue:
3056 case EHTokBreak:
3057 case EHTokDiscard:
3058 case EHTokReturn:
3059 return acceptJumpStatement(statement);
3060
3061 case EHTokCase:
3062 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06003063 case EHTokDefault:
3064 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06003065
3066 case EHTokSemicolon:
3067 return acceptTokenClass(EHTokSemicolon);
3068
3069 case EHTokRightBrace:
3070 // Performance: not strictly necessary, but stops a bunch of hunting early,
3071 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06003072 return false;
3073
John Kessenich21472ae2016-06-04 11:46:33 -06003074 default:
3075 {
3076 // declaration
3077 if (acceptDeclaration(statement))
3078 return true;
3079
3080 // expression
3081 TIntermTyped* node;
3082 if (acceptExpression(node))
3083 statement = node;
3084 else
3085 return false;
3086
3087 // SEMICOLON (following an expression)
3088 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06003089 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06003090 return false;
3091 }
3092 }
3093 }
3094
John Kessenich5f934b02016-03-13 17:58:25 -06003095 return true;
John Kessenich87142c72016-03-12 20:24:24 -07003096}
3097
John Kessenich21472ae2016-06-04 11:46:33 -06003098// attributes
3099// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
3100//
3101// attribute:
3102// : UNROLL
3103// | UNROLL LEFT_PAREN literal RIGHT_PAREN
3104// | FASTOPT
3105// | ALLOW_UAV_CONDITION
3106// | BRANCH
3107// | FLATTEN
3108// | FORCECASE
3109// | CALL
steve-lunarg1868b142016-10-20 13:07:10 -06003110// | DOMAIN
3111// | EARLYDEPTHSTENCIL
3112// | INSTANCE
3113// | MAXTESSFACTOR
3114// | OUTPUTCONTROLPOINTS
3115// | OUTPUTTOPOLOGY
3116// | PARTITIONING
3117// | PATCHCONSTANTFUNC
3118// | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
John Kessenich21472ae2016-06-04 11:46:33 -06003119//
steve-lunarg1868b142016-10-20 13:07:10 -06003120void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003121{
steve-lunarg1868b142016-10-20 13:07:10 -06003122 // For now, accept the [ XXX(X) ] syntax, but drop all but
3123 // numthreads, which is used to set the CS local size.
John Kessenich0d2b6de2016-06-05 11:23:11 -06003124 // TODO: subset to correct set? Pass on?
3125 do {
steve-lunarg1868b142016-10-20 13:07:10 -06003126 HlslToken idToken;
3127
John Kessenich0d2b6de2016-06-05 11:23:11 -06003128 // LEFT_BRACKET?
3129 if (! acceptTokenClass(EHTokLeftBracket))
3130 return;
3131
3132 // attribute
steve-lunarg1868b142016-10-20 13:07:10 -06003133 if (acceptIdentifier(idToken)) {
3134 // 'idToken.string' is the attribute
John Kessenich0d2b6de2016-06-05 11:23:11 -06003135 } else if (! peekTokenClass(EHTokRightBracket)) {
3136 expected("identifier");
3137 advanceToken();
3138 }
3139
steve-lunarga22f7db2016-11-11 08:17:44 -07003140 TIntermAggregate* expressions = nullptr;
steve-lunarg1868b142016-10-20 13:07:10 -06003141
3142 // (x, ...)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003143 if (acceptTokenClass(EHTokLeftParen)) {
steve-lunarga22f7db2016-11-11 08:17:44 -07003144 expressions = new TIntermAggregate;
steve-lunarg1868b142016-10-20 13:07:10 -06003145
John Kessenich0d2b6de2016-06-05 11:23:11 -06003146 TIntermTyped* node;
steve-lunarga22f7db2016-11-11 08:17:44 -07003147 bool expectingExpression = false;
John Kessenichecba76f2017-01-06 00:34:48 -07003148
steve-lunarga22f7db2016-11-11 08:17:44 -07003149 while (acceptAssignmentExpression(node)) {
3150 expectingExpression = false;
3151 expressions->getSequence().push_back(node);
steve-lunarg1868b142016-10-20 13:07:10 -06003152 if (acceptTokenClass(EHTokComma))
steve-lunarga22f7db2016-11-11 08:17:44 -07003153 expectingExpression = true;
steve-lunarg1868b142016-10-20 13:07:10 -06003154 }
3155
steve-lunarga22f7db2016-11-11 08:17:44 -07003156 // 'expressions' is an aggregate with the expressions in it
John Kessenich0d2b6de2016-06-05 11:23:11 -06003157 if (! acceptTokenClass(EHTokRightParen))
3158 expected(")");
steve-lunarga22f7db2016-11-11 08:17:44 -07003159
3160 // Error for partial or missing expression
3161 if (expectingExpression || expressions->getSequence().empty())
3162 expected("expression");
John Kessenich0d2b6de2016-06-05 11:23:11 -06003163 }
3164
3165 // RIGHT_BRACKET
steve-lunarg1868b142016-10-20 13:07:10 -06003166 if (!acceptTokenClass(EHTokRightBracket)) {
3167 expected("]");
3168 return;
3169 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06003170
steve-lunarg1868b142016-10-20 13:07:10 -06003171 // Add any values we found into the attribute map. This accepts
3172 // (and ignores) values not mapping to a known TAttributeType;
steve-lunarga22f7db2016-11-11 08:17:44 -07003173 attributes.setAttribute(idToken.string, expressions);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003174 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06003175}
3176
John Kessenich0d2b6de2016-06-05 11:23:11 -06003177// selection_statement
3178// : IF LEFT_PAREN expression RIGHT_PAREN statement
3179// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
3180//
John Kessenich21472ae2016-06-04 11:46:33 -06003181bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
3182{
John Kessenich0d2b6de2016-06-05 11:23:11 -06003183 TSourceLoc loc = token.loc;
3184
3185 // IF
3186 if (! acceptTokenClass(EHTokIf))
3187 return false;
3188
3189 // so that something declared in the condition is scoped to the lifetimes
3190 // of the then-else statements
3191 parseContext.pushScope();
3192
3193 // LEFT_PAREN expression RIGHT_PAREN
3194 TIntermTyped* condition;
3195 if (! acceptParenExpression(condition))
3196 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003197 condition = parseContext.convertConditionalExpression(loc, condition);
3198 if (condition == nullptr)
3199 return false;
John Kessenich0d2b6de2016-06-05 11:23:11 -06003200
3201 // create the child statements
3202 TIntermNodePair thenElse = { nullptr, nullptr };
3203
3204 // then statement
3205 if (! acceptScopedStatement(thenElse.node1)) {
3206 expected("then statement");
3207 return false;
3208 }
3209
3210 // ELSE
3211 if (acceptTokenClass(EHTokElse)) {
3212 // else statement
3213 if (! acceptScopedStatement(thenElse.node2)) {
3214 expected("else statement");
3215 return false;
3216 }
3217 }
3218
3219 // Put the pieces together
3220 statement = intermediate.addSelection(condition, thenElse, loc);
3221 parseContext.popScope();
3222
3223 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003224}
3225
John Kessenichd02dc5d2016-07-01 00:04:11 -06003226// switch_statement
3227// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
3228//
John Kessenich21472ae2016-06-04 11:46:33 -06003229bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
3230{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003231 // SWITCH
3232 TSourceLoc loc = token.loc;
3233 if (! acceptTokenClass(EHTokSwitch))
3234 return false;
3235
3236 // LEFT_PAREN expression RIGHT_PAREN
3237 parseContext.pushScope();
3238 TIntermTyped* switchExpression;
3239 if (! acceptParenExpression(switchExpression)) {
3240 parseContext.popScope();
3241 return false;
3242 }
3243
3244 // compound_statement
3245 parseContext.pushSwitchSequence(new TIntermSequence);
3246 bool statementOkay = acceptCompoundStatement(statement);
3247 if (statementOkay)
3248 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
3249
3250 parseContext.popSwitchSequence();
3251 parseContext.popScope();
3252
3253 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06003254}
3255
John Kessenich119f8f62016-06-05 15:44:07 -06003256// iteration_statement
3257// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
3258// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
3259// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
3260//
3261// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06003262bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
3263{
John Kessenich119f8f62016-06-05 15:44:07 -06003264 TSourceLoc loc = token.loc;
3265 TIntermTyped* condition = nullptr;
3266
3267 EHlslTokenClass loop = peek();
3268 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
3269
3270 // WHILE or DO or FOR
3271 advanceToken();
3272
3273 switch (loop) {
3274 case EHTokWhile:
3275 // so that something declared in the condition is scoped to the lifetime
3276 // of the while sub-statement
3277 parseContext.pushScope();
3278 parseContext.nestLooping();
3279
3280 // LEFT_PAREN condition RIGHT_PAREN
3281 if (! acceptParenExpression(condition))
3282 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003283 condition = parseContext.convertConditionalExpression(loc, condition);
3284 if (condition == nullptr)
3285 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003286
3287 // statement
3288 if (! acceptScopedStatement(statement)) {
3289 expected("while sub-statement");
3290 return false;
3291 }
3292
3293 parseContext.unnestLooping();
3294 parseContext.popScope();
3295
3296 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
3297
3298 return true;
3299
3300 case EHTokDo:
3301 parseContext.nestLooping();
3302
3303 if (! acceptTokenClass(EHTokLeftBrace))
3304 expected("{");
3305
3306 // statement
3307 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
3308 expected("do sub-statement");
3309 return false;
3310 }
3311
3312 if (! acceptTokenClass(EHTokRightBrace))
3313 expected("}");
3314
3315 // WHILE
3316 if (! acceptTokenClass(EHTokWhile)) {
3317 expected("while");
3318 return false;
3319 }
3320
3321 // LEFT_PAREN condition RIGHT_PAREN
3322 TIntermTyped* condition;
3323 if (! acceptParenExpression(condition))
3324 return false;
John Kessenich7e997e22017-03-30 22:09:30 -06003325 condition = parseContext.convertConditionalExpression(loc, condition);
3326 if (condition == nullptr)
3327 return false;
John Kessenich119f8f62016-06-05 15:44:07 -06003328
3329 if (! acceptTokenClass(EHTokSemicolon))
3330 expected(";");
3331
3332 parseContext.unnestLooping();
3333
3334 statement = intermediate.addLoop(statement, condition, 0, false, loc);
3335
3336 return true;
3337
3338 case EHTokFor:
3339 {
3340 // LEFT_PAREN
3341 if (! acceptTokenClass(EHTokLeftParen))
3342 expected("(");
3343
3344 // so that something declared in the condition is scoped to the lifetime
3345 // of the for sub-statement
3346 parseContext.pushScope();
3347
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003348 // initializer
3349 TIntermNode* initNode = nullptr;
3350 if (! acceptControlDeclaration(initNode)) {
3351 TIntermTyped* initExpr = nullptr;
3352 acceptExpression(initExpr);
3353 initNode = initExpr;
3354 }
3355 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06003356 if (! acceptTokenClass(EHTokSemicolon))
3357 expected(";");
3358
3359 parseContext.nestLooping();
3360
3361 // condition SEMI_COLON
3362 acceptExpression(condition);
3363 if (! acceptTokenClass(EHTokSemicolon))
3364 expected(";");
John Kessenich7e997e22017-03-30 22:09:30 -06003365 if (condition != nullptr) {
3366 condition = parseContext.convertConditionalExpression(loc, condition);
3367 if (condition == nullptr)
3368 return false;
3369 }
John Kessenich119f8f62016-06-05 15:44:07 -06003370
3371 // iterator SEMI_COLON
3372 TIntermTyped* iterator = nullptr;
3373 acceptExpression(iterator);
3374 if (! acceptTokenClass(EHTokRightParen))
3375 expected(")");
3376
3377 // statement
3378 if (! acceptScopedStatement(statement)) {
3379 expected("for sub-statement");
3380 return false;
3381 }
3382
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003383 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06003384
3385 parseContext.popScope();
3386 parseContext.unnestLooping();
3387
3388 return true;
3389 }
3390
3391 default:
3392 return false;
3393 }
John Kessenich21472ae2016-06-04 11:46:33 -06003394}
3395
3396// jump_statement
3397// : CONTINUE SEMICOLON
3398// | BREAK SEMICOLON
3399// | DISCARD SEMICOLON
3400// | RETURN SEMICOLON
3401// | RETURN expression SEMICOLON
3402//
3403bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
3404{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003405 EHlslTokenClass jump = peek();
3406 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06003407 case EHTokContinue:
3408 case EHTokBreak:
3409 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06003410 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003411 advanceToken();
3412 break;
John Kessenich21472ae2016-06-04 11:46:33 -06003413 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003414 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06003415 return false;
3416 }
John Kessenich21472ae2016-06-04 11:46:33 -06003417
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003418 switch (jump) {
3419 case EHTokContinue:
3420 statement = intermediate.addBranch(EOpContinue, token.loc);
3421 break;
3422 case EHTokBreak:
3423 statement = intermediate.addBranch(EOpBreak, token.loc);
3424 break;
3425 case EHTokDiscard:
3426 statement = intermediate.addBranch(EOpKill, token.loc);
3427 break;
3428
3429 case EHTokReturn:
3430 {
3431 // expression
3432 TIntermTyped* node;
3433 if (acceptExpression(node)) {
3434 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06003435 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003436 } else
3437 statement = intermediate.addBranch(EOpReturn, token.loc);
3438 break;
3439 }
3440
3441 default:
3442 assert(0);
3443 return false;
3444 }
3445
3446 // SEMICOLON
3447 if (! acceptTokenClass(EHTokSemicolon))
3448 expected(";");
John Kessenichecba76f2017-01-06 00:34:48 -07003449
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003450 return true;
3451}
John Kessenich21472ae2016-06-04 11:46:33 -06003452
John Kessenichd02dc5d2016-07-01 00:04:11 -06003453// case_label
3454// : CASE expression COLON
3455//
John Kessenich21472ae2016-06-04 11:46:33 -06003456bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
3457{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003458 TSourceLoc loc = token.loc;
3459 if (! acceptTokenClass(EHTokCase))
3460 return false;
3461
3462 TIntermTyped* expression;
3463 if (! acceptExpression(expression)) {
3464 expected("case expression");
3465 return false;
3466 }
3467
3468 if (! acceptTokenClass(EHTokColon)) {
3469 expected(":");
3470 return false;
3471 }
3472
3473 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
3474
3475 return true;
3476}
3477
3478// default_label
3479// : DEFAULT COLON
3480//
3481bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
3482{
3483 TSourceLoc loc = token.loc;
3484 if (! acceptTokenClass(EHTokDefault))
3485 return false;
3486
3487 if (! acceptTokenClass(EHTokColon)) {
3488 expected(":");
3489 return false;
3490 }
3491
3492 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
3493
3494 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003495}
3496
John Kessenich19b92ff2016-06-19 11:50:34 -06003497// array_specifier
steve-lunarg7b211a32016-10-13 12:26:18 -06003498// : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
3499// : LEFT_BRACKET RGHT_BRACKET // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06003500//
3501void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
3502{
3503 arraySizes = nullptr;
3504
steve-lunarg7b211a32016-10-13 12:26:18 -06003505 // Early-out if there aren't any array dimensions
3506 if (!peekTokenClass(EHTokLeftBracket))
John Kessenich19b92ff2016-06-19 11:50:34 -06003507 return;
3508
steve-lunarg7b211a32016-10-13 12:26:18 -06003509 // 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 -06003510 arraySizes = new TArraySizes;
steve-lunarg7b211a32016-10-13 12:26:18 -06003511
3512 // Collect each array dimension.
3513 while (acceptTokenClass(EHTokLeftBracket)) {
3514 TSourceLoc loc = token.loc;
3515 TIntermTyped* sizeExpr = nullptr;
3516
John Kessenich057df292017-03-06 18:18:37 -07003517 // Array sizing expression is optional. If omitted, array will be later sized by initializer list.
steve-lunarg7b211a32016-10-13 12:26:18 -06003518 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
3519
3520 if (! acceptTokenClass(EHTokRightBracket)) {
3521 expected("]");
3522 return;
3523 }
3524
3525 if (hasArraySize) {
3526 TArraySize arraySize;
3527 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
3528 arraySizes->addInnerSize(arraySize);
3529 } else {
3530 arraySizes->addInnerSize(0); // sized by initializers.
3531 }
steve-lunarg265c0612016-09-27 10:57:35 -06003532 }
John Kessenich19b92ff2016-06-19 11:50:34 -06003533}
3534
John Kessenich630dd7d2016-06-12 23:52:12 -06003535// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06003536// : COLON semantic // optional
3537// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
3538// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06003539// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06003540// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06003541//
John Kessenich854fe242017-03-02 14:30:59 -07003542// Return true if any tokens were accepted. That is,
3543// false can be returned on successfully recognizing nothing,
3544// not necessarily meaning bad syntax.
3545//
3546bool HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06003547{
John Kessenich854fe242017-03-02 14:30:59 -07003548 bool found = false;
3549
John Kessenich630dd7d2016-06-12 23:52:12 -06003550 do {
John Kessenichecba76f2017-01-06 00:34:48 -07003551 // COLON
John Kessenich630dd7d2016-06-12 23:52:12 -06003552 if (acceptTokenClass(EHTokColon)) {
John Kessenich854fe242017-03-02 14:30:59 -07003553 found = true;
John Kessenich630dd7d2016-06-12 23:52:12 -06003554 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06003555 if (peekTokenClass(EHTokLayout))
3556 acceptLayoutQualifierList(qualifier);
3557 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06003558 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003559 if (! acceptTokenClass(EHTokLeftParen)) {
3560 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003561 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003562 }
John Kessenich82d6baf2016-07-29 13:03:05 -06003563 HlslToken locationToken;
3564 if (! acceptIdentifier(locationToken)) {
3565 expected("c[subcomponent][.component]");
John Kessenich854fe242017-03-02 14:30:59 -07003566 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003567 }
3568 HlslToken componentToken;
3569 if (acceptTokenClass(EHTokDot)) {
3570 if (! acceptIdentifier(componentToken)) {
3571 expected("component");
John Kessenich854fe242017-03-02 14:30:59 -07003572 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003573 }
3574 }
John Kessenich630dd7d2016-06-12 23:52:12 -06003575 if (! acceptTokenClass(EHTokRightParen)) {
3576 expected(")");
3577 break;
3578 }
John Kessenich7735b942016-09-05 12:40:06 -06003579 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003580 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06003581 expected("layout, semantic, packoffset, or register");
John Kessenich854fe242017-03-02 14:30:59 -07003582 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003583 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06003584 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
3585 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003586 if (! acceptTokenClass(EHTokLeftParen)) {
3587 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003588 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003589 }
John Kessenichb38f0712016-07-30 10:29:54 -06003590 HlslToken registerDesc; // for Type#
3591 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06003592 if (! acceptIdentifier(registerDesc)) {
3593 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003594 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003595 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003596 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
3597 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06003598 // Then we didn't really see the registerDesc yet, it was
3599 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06003600 profile = registerDesc;
3601 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 }
3605 }
John Kessenichb38f0712016-07-30 10:29:54 -06003606 int subComponent = 0;
3607 if (acceptTokenClass(EHTokLeftBracket)) {
3608 // LEFT_BRACKET subcomponent RIGHT_BRACKET
3609 if (! peekTokenClass(EHTokIntConstant)) {
3610 expected("literal integer");
John Kessenich854fe242017-03-02 14:30:59 -07003611 return false;
John Kessenichb38f0712016-07-30 10:29:54 -06003612 }
3613 subComponent = token.i;
3614 advanceToken();
3615 if (! acceptTokenClass(EHTokRightBracket)) {
3616 expected("]");
3617 break;
3618 }
3619 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003620 // (COMMA SPACEN)opt
3621 HlslToken spaceDesc;
3622 if (acceptTokenClass(EHTokComma)) {
3623 if (! acceptIdentifier(spaceDesc)) {
3624 expected ("space identifier");
John Kessenich854fe242017-03-02 14:30:59 -07003625 return false;
John Kessenichcfd7ce82016-09-05 16:03:12 -06003626 }
3627 }
3628 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003629 if (! acceptTokenClass(EHTokRightParen)) {
3630 expected(")");
3631 break;
3632 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003633 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003634 } else {
3635 // semantic, in idToken.string
John Kessenich2dd643f2017-03-14 21:50:06 -06003636 TString semanticUpperCase = *idToken.string;
3637 std::transform(semanticUpperCase.begin(), semanticUpperCase.end(), semanticUpperCase.begin(), ::toupper);
3638 parseContext.handleSemantic(idToken.loc, qualifier, mapSemantic(semanticUpperCase.c_str()), semanticUpperCase);
John Kessenich630dd7d2016-06-12 23:52:12 -06003639 }
John Kessenich854fe242017-03-02 14:30:59 -07003640 } else if (peekTokenClass(EHTokLeftAngle)) {
3641 found = true;
John Kessenicha1e2d492016-09-20 13:22:58 -06003642 acceptAnnotations(qualifier);
John Kessenich854fe242017-03-02 14:30:59 -07003643 } else
John Kessenich630dd7d2016-06-12 23:52:12 -06003644 break;
John Kessenich078d7f22016-03-14 10:02:11 -06003645
John Kessenich630dd7d2016-06-12 23:52:12 -06003646 } while (true);
John Kessenich854fe242017-03-02 14:30:59 -07003647
3648 return found;
John Kessenich078d7f22016-03-14 10:02:11 -06003649}
3650
John Kessenichb16f7e62017-03-11 19:32:47 -07003651//
3652// Get the stream of tokens from the scanner, but skip all syntactic/semantic
3653// processing.
3654//
3655bool HlslGrammar::captureBlockTokens(TVector<HlslToken>& tokens)
3656{
3657 if (! peekTokenClass(EHTokLeftBrace))
3658 return false;
3659
3660 int braceCount = 0;
3661
3662 do {
3663 switch (peek()) {
3664 case EHTokLeftBrace:
3665 ++braceCount;
3666 break;
3667 case EHTokRightBrace:
3668 --braceCount;
3669 break;
3670 case EHTokNone:
3671 // End of input before balance { } is bad...
3672 return false;
3673 default:
3674 break;
3675 }
3676
3677 tokens.push_back(token);
3678 advanceToken();
3679 } while (braceCount > 0);
3680
3681 return true;
3682}
3683
John Kesseniche01a9bc2016-03-12 20:11:22 -07003684} // end namespace glslang