blob: 67216684b2bc8487e7634c4c83b5393588824d92 [file] [log] [blame]
John Kesseniche01a9bc2016-03-12 20:11:22 -07001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2016 Google, Inc.
3// Copyright (C) 2016 LunarG, Inc.
John Kesseniche01a9bc2016-03-12 20:11:22 -07004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kesseniche01a9bc2016-03-12 20:11:22 -07006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kesseniche01a9bc2016-03-12 20:11:22 -070010//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of Google, Inc., nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
John Kessenich927608b2017-01-06 12:34:14 -070023// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
John Kesseniche01a9bc2016-03-12 20:11:22 -070035//
36
John Kessenichd016be12016-03-13 11:24:20 -060037//
38// This is a set of mutually recursive methods implementing the HLSL grammar.
39// Generally, each returns
40// - through an argument: a type specifically appropriate to which rule it
41// recognized
42// - through the return value: true/false to indicate whether or not it
43// recognized its rule
44//
45// As much as possible, only grammar recognition should happen in this file,
John Kessenich078d7f22016-03-14 10:02:11 -060046// with all other work being farmed out to hlslParseHelper.cpp, which in turn
John Kessenichd016be12016-03-13 11:24:20 -060047// will build the AST.
48//
49// The next token, yet to be "accepted" is always sitting in 'token'.
50// When a method says it accepts a rule, that means all tokens involved
51// in the rule will have been consumed, and none left in 'token'.
52//
53
John Kesseniche01a9bc2016-03-12 20:11:22 -070054#include "hlslTokens.h"
55#include "hlslGrammar.h"
steve-lunarg1868b142016-10-20 13:07:10 -060056#include "hlslAttributes.h"
John Kesseniche01a9bc2016-03-12 20:11:22 -070057
58namespace glslang {
59
60// Root entry point to this recursive decent parser.
61// Return true if compilation unit was successfully accepted.
62bool HlslGrammar::parse()
63{
64 advanceToken();
65 return acceptCompilationUnit();
66}
67
68void HlslGrammar::expected(const char* syntax)
69{
70 parseContext.error(token.loc, "Expected", syntax, "");
71}
72
LoopDawg4886f692016-06-29 10:58:58 -060073void HlslGrammar::unimplemented(const char* error)
74{
75 parseContext.error(token.loc, "Unimplemented", error, "");
76}
77
John Kessenichaecd4972016-03-14 10:46:34 -060078// Only process the next token if it is an identifier.
79// Return true if it was an identifier.
80bool HlslGrammar::acceptIdentifier(HlslToken& idToken)
81{
82 if (peekTokenClass(EHTokIdentifier)) {
83 idToken = token;
84 advanceToken();
85 return true;
86 }
87
steve-lunarg5ca85ad2016-12-26 18:45:52 -070088 // Even though "sample", "bool", "float", etc keywords (for types, interpolation modifiers),
89 // they ARE still accepted as identifiers. This is not a dense space: e.g, "void" is not a
90 // valid identifier, nor is "linear". This code special cases the known instances of this, so
91 // e.g, "int sample;" or "float float;" is accepted. Other cases can be added here if needed.
John Kessenichecba76f2017-01-06 00:34:48 -070092
steve-lunarg5ca85ad2016-12-26 18:45:52 -070093 TString* idString = nullptr;
94 switch (peek()) {
95 case EHTokSample: idString = NewPoolTString("sample"); break;
96 case EHTokHalf: idString = NewPoolTString("half"); break;
97 case EHTokBool: idString = NewPoolTString("bool"); break;
98 case EHTokFloat: idString = NewPoolTString("float"); break;
99 case EHTokDouble: idString = NewPoolTString("double"); break;
100 case EHTokInt: idString = NewPoolTString("int"); break;
101 case EHTokUint: idString = NewPoolTString("uint"); break;
102 case EHTokMin16float: idString = NewPoolTString("min16float"); break;
103 case EHTokMin10float: idString = NewPoolTString("min10float"); break;
104 case EHTokMin16int: idString = NewPoolTString("min16int"); break;
105 case EHTokMin12int: idString = NewPoolTString("min12int"); break;
106 default:
107 return false;
steve-lunarg75fd2232016-11-16 13:22:11 -0700108 }
109
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700110 token.string = idString;
111 token.tokenClass = EHTokIdentifier;
112 token.symbol = nullptr;
113 idToken = token;
114
115 advanceToken();
116
117 return true;
John Kessenichaecd4972016-03-14 10:46:34 -0600118}
119
John Kesseniche01a9bc2016-03-12 20:11:22 -0700120// compilationUnit
121// : list of externalDeclaration
steve-lunargcb88de52016-08-03 07:04:18 -0600122// | SEMICOLONS
John Kesseniche01a9bc2016-03-12 20:11:22 -0700123//
124bool HlslGrammar::acceptCompilationUnit()
125{
John Kessenichd016be12016-03-13 11:24:20 -0600126 TIntermNode* unitNode = nullptr;
127
John Kessenich9c86c6a2016-05-03 22:49:24 -0600128 while (! peekTokenClass(EHTokNone)) {
steve-lunargcb88de52016-08-03 07:04:18 -0600129 // HLSL allows semicolons between global declarations, e.g, between functions.
130 if (acceptTokenClass(EHTokSemicolon))
131 continue;
132
John Kessenichd016be12016-03-13 11:24:20 -0600133 // externalDeclaration
John Kessenichca71d942017-03-07 20:44:09 -0700134 if (! acceptDeclaration(unitNode))
John Kesseniche01a9bc2016-03-12 20:11:22 -0700135 return false;
136 }
137
John Kessenichd016be12016-03-13 11:24:20 -0600138 // set root of AST
John Kessenichca71d942017-03-07 20:44:09 -0700139 if (unitNode && !unitNode->getAsAggregate())
140 unitNode = intermediate.growAggregate(nullptr, unitNode);
John Kessenich078d7f22016-03-14 10:02:11 -0600141 intermediate.setTreeRoot(unitNode);
John Kessenichd016be12016-03-13 11:24:20 -0600142
John Kesseniche01a9bc2016-03-12 20:11:22 -0700143 return true;
144}
145
LoopDawg4886f692016-06-29 10:58:58 -0600146// sampler_state
John Kessenichecba76f2017-01-06 00:34:48 -0700147// : LEFT_BRACE [sampler_state_assignment ... ] RIGHT_BRACE
LoopDawg4886f692016-06-29 10:58:58 -0600148//
149// sampler_state_assignment
150// : sampler_state_identifier EQUAL value SEMICOLON
151//
152// sampler_state_identifier
153// : ADDRESSU
154// | ADDRESSV
155// | ADDRESSW
156// | BORDERCOLOR
157// | FILTER
158// | MAXANISOTROPY
159// | MAXLOD
160// | MINLOD
161// | MIPLODBIAS
162//
163bool HlslGrammar::acceptSamplerState()
164{
165 // TODO: this should be genericized to accept a list of valid tokens and
166 // return token/value pairs. Presently it is specific to texture values.
167
168 if (! acceptTokenClass(EHTokLeftBrace))
169 return true;
170
171 parseContext.warn(token.loc, "unimplemented", "immediate sampler state", "");
John Kessenichecba76f2017-01-06 00:34:48 -0700172
LoopDawg4886f692016-06-29 10:58:58 -0600173 do {
174 // read state name
175 HlslToken state;
176 if (! acceptIdentifier(state))
177 break; // end of list
178
179 // FXC accepts any case
180 TString stateName = *state.string;
181 std::transform(stateName.begin(), stateName.end(), stateName.begin(), ::tolower);
182
183 if (! acceptTokenClass(EHTokAssign)) {
184 expected("assign");
185 return false;
186 }
187
188 if (stateName == "minlod" || stateName == "maxlod") {
189 if (! peekTokenClass(EHTokIntConstant)) {
190 expected("integer");
191 return false;
192 }
193
194 TIntermTyped* lod = nullptr;
195 if (! acceptLiteral(lod)) // should never fail, since we just looked for an integer
196 return false;
197 } else if (stateName == "maxanisotropy") {
198 if (! peekTokenClass(EHTokIntConstant)) {
199 expected("integer");
200 return false;
201 }
202
203 TIntermTyped* maxAnisotropy = nullptr;
204 if (! acceptLiteral(maxAnisotropy)) // should never fail, since we just looked for an integer
205 return false;
206 } else if (stateName == "filter") {
207 HlslToken filterMode;
208 if (! acceptIdentifier(filterMode)) {
209 expected("filter mode");
210 return false;
211 }
212 } else if (stateName == "addressu" || stateName == "addressv" || stateName == "addressw") {
213 HlslToken addrMode;
214 if (! acceptIdentifier(addrMode)) {
215 expected("texture address mode");
216 return false;
217 }
218 } else if (stateName == "miplodbias") {
219 TIntermTyped* lodBias = nullptr;
220 if (! acceptLiteral(lodBias)) {
221 expected("lod bias");
222 return false;
223 }
224 } else if (stateName == "bordercolor") {
225 return false;
226 } else {
227 expected("texture state");
228 return false;
229 }
230
231 // SEMICOLON
232 if (! acceptTokenClass(EHTokSemicolon)) {
233 expected("semicolon");
234 return false;
235 }
236 } while (true);
237
238 if (! acceptTokenClass(EHTokRightBrace))
239 return false;
240
241 return true;
242}
243
244// sampler_declaration_dx9
245// : SAMPLER identifier EQUAL sampler_type sampler_state
246//
John Kesseniche4821e42016-07-16 10:19:43 -0600247bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/)
LoopDawg4886f692016-06-29 10:58:58 -0600248{
249 if (! acceptTokenClass(EHTokSampler))
250 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700251
LoopDawg4886f692016-06-29 10:58:58 -0600252 // TODO: remove this when DX9 style declarations are implemented.
253 unimplemented("Direct3D 9 sampler declaration");
254
255 // read sampler name
256 HlslToken name;
257 if (! acceptIdentifier(name)) {
258 expected("sampler name");
259 return false;
260 }
261
262 if (! acceptTokenClass(EHTokAssign)) {
263 expected("=");
264 return false;
265 }
266
267 return false;
268}
269
John Kesseniche01a9bc2016-03-12 20:11:22 -0700270// declaration
LoopDawg4886f692016-06-29 10:58:58 -0600271// : sampler_declaration_dx9 post_decls SEMICOLON
272// | fully_specified_type declarator_list SEMICOLON
John Kessenich630dd7d2016-06-12 23:52:12 -0600273// | fully_specified_type identifier function_parameters post_decls compound_statement // function definition
LoopDawg4886f692016-06-29 10:58:58 -0600274// | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition
John Kessenich5e69ec62016-07-05 00:02:40 -0600275// | typedef declaration
John Kessenich87142c72016-03-12 20:24:24 -0700276//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600277// declarator_list
278// : declarator COMMA declarator COMMA declarator... // zero or more declarators
John Kessenich532543c2016-07-01 19:06:44 -0600279//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600280// declarator
John Kessenich532543c2016-07-01 19:06:44 -0600281// : identifier array_specifier post_decls
282// | identifier array_specifier post_decls EQUAL assignment_expression
John Kessenichd5ed0b62016-07-04 17:32:45 -0600283// | identifier function_parameters post_decls // function prototype
John Kessenich532543c2016-07-01 19:06:44 -0600284//
John Kessenichd5ed0b62016-07-04 17:32:45 -0600285// Parsing has to go pretty far in to know whether it's a variable, prototype, or
286// function definition, so the implementation below doesn't perfectly divide up the grammar
John Kessenich532543c2016-07-01 19:06:44 -0600287// as above. (The 'identifier' in the first item in init_declarator list is the
288// same as 'identifier' for function declarations.)
289//
John Kessenichca71d942017-03-07 20:44:09 -0700290// This can generate more than one subtree, one per initializer or a function body.
291// All initializer subtrees are put in their own aggregate node, making one top-level
292// node for all the initializers. Each function created is a top-level node to grow
293// into the passed-in nodeList.
John Kessenichd016be12016-03-13 11:24:20 -0600294//
John Kessenichca71d942017-03-07 20:44:09 -0700295// If 'nodeList' is passed in as non-null, it must an aggregate to extend for
296// each top-level node the declaration creates. Otherwise, if only one top-level
297// node in generated here, that is want is returned in nodeList.
John Kessenich02467d82017-01-19 15:41:47 -0700298//
John Kessenichca71d942017-03-07 20:44:09 -0700299bool HlslGrammar::acceptDeclaration(TIntermNode*& nodeList)
John Kesseniche01a9bc2016-03-12 20:11:22 -0700300{
John Kessenich54ee28f2017-03-11 14:13:00 -0700301 bool declarator_list = false; // true when processing comma separation
John Kessenichd016be12016-03-13 11:24:20 -0600302
steve-lunarg1868b142016-10-20 13:07:10 -0600303 // attributes
John Kessenich088d52b2017-03-11 17:55:28 -0700304 TFunctionDeclarator declarator;
305 acceptAttributes(declarator.attributes);
steve-lunarg1868b142016-10-20 13:07:10 -0600306
John Kessenich5e69ec62016-07-05 00:02:40 -0600307 // typedef
308 bool typedefDecl = acceptTokenClass(EHTokTypedef);
309
John Kesseniche82061d2016-09-27 14:38:57 -0600310 TType declaredType;
LoopDawg4886f692016-06-29 10:58:58 -0600311
312 // DX9 sampler declaration use a different syntax
John Kessenich267590d2016-08-05 17:34:34 -0600313 // DX9 shaders need to run through HLSL compiler (fxc) via a back compat mode, it isn't going to
314 // be possible to simultaneously compile D3D10+ style shaders and DX9 shaders. If we want to compile DX9
315 // HLSL shaders, this will have to be a master level switch
316 // As such, the sampler keyword in D3D10+ turns into an automatic sampler type, and is commonly used
John Kessenichecba76f2017-01-06 00:34:48 -0700317 // For that reason, this line is commented out
John Kessenichca71d942017-03-07 20:44:09 -0700318 // if (acceptSamplerDeclarationDX9(declaredType))
319 // return true;
LoopDawg4886f692016-06-29 10:58:58 -0600320
321 // fully_specified_type
John Kessenich54ee28f2017-03-11 14:13:00 -0700322 if (! acceptFullySpecifiedType(declaredType, nodeList))
John Kessenich87142c72016-03-12 20:24:24 -0700323 return false;
LoopDawg4886f692016-06-29 10:58:58 -0600324
John Kessenich87142c72016-03-12 20:24:24 -0700325 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -0600326 HlslToken idToken;
John Kessenichca71d942017-03-07 20:44:09 -0700327 TIntermAggregate* initializers = nullptr;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600328 while (acceptIdentifier(idToken)) {
John Kessenich78388722017-03-08 18:53:51 -0700329 if (peekTokenClass(EHTokLeftParen)) {
330 // looks like function parameters
331 TString* fnName = idToken.string;
steve-lunargf1e0c872016-10-31 15:13:43 -0600332
John Kessenich78388722017-03-08 18:53:51 -0700333 // Potentially rename shader entry point function. No-op most of the time.
334 parseContext.renameShaderFunction(fnName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600335
John Kessenich78388722017-03-08 18:53:51 -0700336 // function_parameters
John Kessenich088d52b2017-03-11 17:55:28 -0700337 declarator.function = new TFunction(fnName, declaredType);
338 if (!acceptFunctionParameters(*declarator.function)) {
John Kessenich78388722017-03-08 18:53:51 -0700339 expected("function parameter list");
340 return false;
341 }
342
John Kessenich630dd7d2016-06-12 23:52:12 -0600343 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -0700344 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich078d7f22016-03-14 10:02:11 -0600345
John Kessenichd5ed0b62016-07-04 17:32:45 -0600346 // compound_statement (function body definition) or just a prototype?
John Kessenich088d52b2017-03-11 17:55:28 -0700347 declarator.loc = token.loc;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600348 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich54ee28f2017-03-11 14:13:00 -0700349 if (declarator_list)
John Kessenichd5ed0b62016-07-04 17:32:45 -0600350 parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", "");
John Kessenich5e69ec62016-07-05 00:02:40 -0600351 if (typedefDecl)
352 parseContext.error(idToken.loc, "function body can't be in a typedef", "{", "");
John Kessenichb16f7e62017-03-11 19:32:47 -0700353 return acceptFunctionDefinition(declarator, nodeList, nullptr);
John Kessenich5e69ec62016-07-05 00:02:40 -0600354 } else {
355 if (typedefDecl)
356 parseContext.error(idToken.loc, "function typedefs not implemented", "{", "");
John Kessenich088d52b2017-03-11 17:55:28 -0700357 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, true);
John Kessenich5e69ec62016-07-05 00:02:40 -0600358 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600359 } else {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600360 // A variable declaration. Fix the storage qualifier if it's a global.
361 if (declaredType.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel())
362 declaredType.getQualifier().storage = EvqUniform;
363
John Kessenichecba76f2017-01-06 00:34:48 -0700364 // We can handle multiple variables per type declaration, so
John Kesseniche82061d2016-09-27 14:38:57 -0600365 // the number of types can expand when arrayness is different.
366 TType variableType;
367 variableType.shallowCopy(declaredType);
John Kessenich5f934b02016-03-13 17:58:25 -0600368
John Kesseniche82061d2016-09-27 14:38:57 -0600369 // recognize array_specifier
John Kessenichd5ed0b62016-07-04 17:32:45 -0600370 TArraySizes* arraySizes = nullptr;
371 acceptArraySpecifier(arraySizes);
John Kessenich5f934b02016-03-13 17:58:25 -0600372
John Kesseniche82061d2016-09-27 14:38:57 -0600373 // Fix arrayness in the variableType
374 if (declaredType.isImplicitlySizedArray()) {
375 // Because "int[] a = int[2](...), b = int[3](...)" makes two arrays a and b
376 // of different sizes, for this case sharing the shallow copy of arrayness
377 // with the parseType oversubscribes it, so get a deep copy of the arrayness.
378 variableType.newArraySizes(declaredType.getArraySizes());
379 }
380 if (arraySizes || variableType.isArray()) {
381 // In the most general case, arrayness is potentially coming both from the
382 // declared type and from the variable: "int[] a[];" or just one or the other.
383 // Merge it all to the variableType, so all arrayness is part of the variableType.
384 parseContext.arrayDimMerge(variableType, arraySizes);
385 }
386
LoopDawg4886f692016-06-29 10:58:58 -0600387 // samplers accept immediate sampler state
John Kesseniche82061d2016-09-27 14:38:57 -0600388 if (variableType.getBasicType() == EbtSampler) {
LoopDawg4886f692016-06-29 10:58:58 -0600389 if (! acceptSamplerState())
390 return false;
391 }
392
John Kessenichd5ed0b62016-07-04 17:32:45 -0600393 // post_decls
John Kesseniche82061d2016-09-27 14:38:57 -0600394 acceptPostDecls(variableType.getQualifier());
John Kessenichd5ed0b62016-07-04 17:32:45 -0600395
396 // EQUAL assignment_expression
397 TIntermTyped* expressionNode = nullptr;
398 if (acceptTokenClass(EHTokAssign)) {
John Kessenich5e69ec62016-07-05 00:02:40 -0600399 if (typedefDecl)
400 parseContext.error(idToken.loc, "can't have an initializer", "typedef", "");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600401 if (! acceptAssignmentExpression(expressionNode)) {
402 expected("initializer");
403 return false;
404 }
405 }
406
John Kessenich6dbc0a72016-09-27 19:13:05 -0600407 // TODO: things scoped within an annotation need their own name space;
408 // TODO: strings are not yet handled.
409 if (variableType.getBasicType() != EbtString && parseContext.getAnnotationNestingLevel() == 0) {
410 if (typedefDecl)
411 parseContext.declareTypedef(idToken.loc, *idToken.string, variableType);
412 else if (variableType.getBasicType() == EbtBlock)
steve-lunargdd8287a2017-02-23 18:04:12 -0700413 parseContext.declareBlock(idToken.loc, variableType, idToken.string);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600414 else {
steve-lunarga2b01a02016-11-28 17:09:54 -0700415 if (variableType.getQualifier().storage == EvqUniform && ! variableType.containsOpaque()) {
John Kessenich6dbc0a72016-09-27 19:13:05 -0600416 // this isn't really an individual variable, but a member of the $Global buffer
417 parseContext.growGlobalUniformBlock(idToken.loc, variableType, *idToken.string);
418 } else {
419 // Declare the variable and add any initializer code to the AST.
420 // The top-level node is always made into an aggregate, as that's
421 // historically how the AST has been.
John Kessenichca71d942017-03-07 20:44:09 -0700422 initializers = intermediate.growAggregate(initializers,
423 parseContext.declareVariable(idToken.loc, *idToken.string, variableType, expressionNode),
424 idToken.loc);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600425 }
426 }
John Kessenich5e69ec62016-07-05 00:02:40 -0600427 }
John Kessenich5f934b02016-03-13 17:58:25 -0600428 }
John Kessenichd5ed0b62016-07-04 17:32:45 -0600429
430 if (acceptTokenClass(EHTokComma)) {
John Kessenich54ee28f2017-03-11 14:13:00 -0700431 declarator_list = true;
John Kessenichd5ed0b62016-07-04 17:32:45 -0600432 continue;
433 }
434 };
435
John Kessenichca71d942017-03-07 20:44:09 -0700436 // The top-level initializer node is a sequence.
437 if (initializers != nullptr)
438 initializers->setOperator(EOpSequence);
439
440 // Add the initializers' aggregate to the nodeList we were handed.
441 if (nodeList)
442 nodeList = intermediate.growAggregate(nodeList, initializers);
443 else
444 nodeList = initializers;
John Kessenich87142c72016-03-12 20:24:24 -0700445
John Kessenich078d7f22016-03-14 10:02:11 -0600446 // SEMICOLON
John Kessenichd5ed0b62016-07-04 17:32:45 -0600447 if (! acceptTokenClass(EHTokSemicolon)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -0700448 // This may have been a false detection of what appeared to be a declaration, but
449 // was actually an assignment such as "float = 4", where "float" is an identifier.
450 // We put the token back to let further parsing happen for cases where that may
451 // happen. This errors on the side of caution, and mostly triggers the error.
452
453 if (peek() == EHTokAssign || peek() == EHTokLeftBracket || peek() == EHTokDot || peek() == EHTokComma)
454 recedeToken();
455 else
456 expected(";");
John Kessenichd5ed0b62016-07-04 17:32:45 -0600457 return false;
458 }
John Kessenichecba76f2017-01-06 00:34:48 -0700459
John Kesseniche01a9bc2016-03-12 20:11:22 -0700460 return true;
461}
462
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600463// control_declaration
464// : fully_specified_type identifier EQUAL expression
465//
466bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node)
467{
468 node = nullptr;
469
470 // fully_specified_type
471 TType type;
472 if (! acceptFullySpecifiedType(type))
473 return false;
474
John Kessenich057df292017-03-06 18:18:37 -0700475 // filter out type casts
476 if (peekTokenClass(EHTokLeftParen)) {
477 recedeToken();
478 return false;
479 }
480
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600481 // identifier
482 HlslToken idToken;
483 if (! acceptIdentifier(idToken)) {
484 expected("identifier");
485 return false;
486 }
487
488 // EQUAL
489 TIntermTyped* expressionNode = nullptr;
490 if (! acceptTokenClass(EHTokAssign)) {
491 expected("=");
492 return false;
493 }
494
495 // expression
496 if (! acceptExpression(expressionNode)) {
497 expected("initializer");
498 return false;
499 }
500
John Kesseniche82061d2016-09-27 14:38:57 -0600501 node = parseContext.declareVariable(idToken.loc, *idToken.string, type, expressionNode);
John Kessenich5bc4d9a2016-06-20 01:22:38 -0600502
503 return true;
504}
505
John Kessenich87142c72016-03-12 20:24:24 -0700506// fully_specified_type
507// : type_specifier
508// | type_qualifier type_specifier
509//
510bool HlslGrammar::acceptFullySpecifiedType(TType& type)
511{
John Kessenich54ee28f2017-03-11 14:13:00 -0700512 TIntermNode* nodeList = nullptr;
513 return acceptFullySpecifiedType(type, nodeList);
514}
515bool HlslGrammar::acceptFullySpecifiedType(TType& type, TIntermNode*& nodeList)
516{
John Kessenich87142c72016-03-12 20:24:24 -0700517 // type_qualifier
518 TQualifier qualifier;
519 qualifier.clear();
John Kessenichb9e39122016-08-17 10:22:08 -0600520 if (! acceptQualifier(qualifier))
521 return false;
John Kessenich3d157c52016-07-25 16:05:33 -0600522 TSourceLoc loc = token.loc;
John Kessenich87142c72016-03-12 20:24:24 -0700523
524 // type_specifier
John Kessenich54ee28f2017-03-11 14:13:00 -0700525 if (! acceptType(type, nodeList)) {
steve-lunarga64ed3e2016-12-18 17:51:14 -0700526 // If this is not a type, we may have inadvertently gone down a wrong path
steve-lunarg132d3312016-12-19 15:48:01 -0700527 // by parsing "sample", which can be treated like either an identifier or a
steve-lunarga64ed3e2016-12-18 17:51:14 -0700528 // qualifier. Back it out, if we did.
529 if (qualifier.sample)
530 recedeToken();
531
John Kessenich87142c72016-03-12 20:24:24 -0700532 return false;
steve-lunarga64ed3e2016-12-18 17:51:14 -0700533 }
John Kessenich3d157c52016-07-25 16:05:33 -0600534 if (type.getBasicType() == EbtBlock) {
535 // the type was a block, which set some parts of the qualifier
John Kessenich34e7ee72016-09-16 17:10:39 -0600536 parseContext.mergeQualifiers(type.getQualifier(), qualifier);
John Kessenich3d157c52016-07-25 16:05:33 -0600537 // further, it can create an anonymous instance of the block
538 if (peekTokenClass(EHTokSemicolon))
539 parseContext.declareBlock(loc, type);
steve-lunargbb0183f2016-10-04 16:58:14 -0600540 } else {
541 // Some qualifiers are set when parsing the type. Merge those with
542 // whatever comes from acceptQualifier.
543 assert(qualifier.layoutFormat == ElfNone);
steve-lunargf49cdf42016-11-17 15:04:20 -0700544
steve-lunargbb0183f2016-10-04 16:58:14 -0600545 qualifier.layoutFormat = type.getQualifier().layoutFormat;
steve-lunarg3226b082016-10-26 19:18:55 -0600546 qualifier.precision = type.getQualifier().precision;
steve-lunargf49cdf42016-11-17 15:04:20 -0700547
steve-lunarg5da1f032017-02-12 17:50:28 -0700548 if (type.getQualifier().storage == EvqVaryingOut ||
549 type.getQualifier().storage == EvqBuffer) {
steve-lunargf49cdf42016-11-17 15:04:20 -0700550 qualifier.storage = type.getQualifier().storage;
steve-lunarg5da1f032017-02-12 17:50:28 -0700551 qualifier.readonly = type.getQualifier().readonly;
552 }
steve-lunargf49cdf42016-11-17 15:04:20 -0700553
554 type.getQualifier() = qualifier;
steve-lunargbb0183f2016-10-04 16:58:14 -0600555 }
John Kessenich87142c72016-03-12 20:24:24 -0700556
557 return true;
558}
559
John Kessenich630dd7d2016-06-12 23:52:12 -0600560// type_qualifier
561// : qualifier qualifier ...
562//
563// Zero or more of these, so this can't return false.
564//
John Kessenichb9e39122016-08-17 10:22:08 -0600565bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
John Kessenich87142c72016-03-12 20:24:24 -0700566{
John Kessenich630dd7d2016-06-12 23:52:12 -0600567 do {
568 switch (peek()) {
569 case EHTokStatic:
John Kessenich6dbc0a72016-09-27 19:13:05 -0600570 qualifier.storage = parseContext.symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
John Kessenich630dd7d2016-06-12 23:52:12 -0600571 break;
572 case EHTokExtern:
573 // TODO: no meaning in glslang?
574 break;
575 case EHTokShared:
576 // TODO: hint
577 break;
578 case EHTokGroupShared:
579 qualifier.storage = EvqShared;
580 break;
581 case EHTokUniform:
582 qualifier.storage = EvqUniform;
583 break;
584 case EHTokConst:
585 qualifier.storage = EvqConst;
586 break;
587 case EHTokVolatile:
588 qualifier.volatil = true;
589 break;
590 case EHTokLinear:
John Kessenich630dd7d2016-06-12 23:52:12 -0600591 qualifier.smooth = true;
592 break;
593 case EHTokCentroid:
594 qualifier.centroid = true;
595 break;
596 case EHTokNointerpolation:
597 qualifier.flat = true;
598 break;
599 case EHTokNoperspective:
600 qualifier.nopersp = true;
601 break;
602 case EHTokSample:
603 qualifier.sample = true;
604 break;
605 case EHTokRowMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600606 qualifier.layoutMatrix = ElmColumnMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600607 break;
608 case EHTokColumnMajor:
John Kessenich10f7fc72016-09-25 20:25:06 -0600609 qualifier.layoutMatrix = ElmRowMajor;
John Kessenich630dd7d2016-06-12 23:52:12 -0600610 break;
611 case EHTokPrecise:
612 qualifier.noContraction = true;
613 break;
LoopDawg9249c702016-07-12 20:44:32 -0600614 case EHTokIn:
615 qualifier.storage = EvqIn;
616 break;
617 case EHTokOut:
618 qualifier.storage = EvqOut;
619 break;
620 case EHTokInOut:
621 qualifier.storage = EvqInOut;
622 break;
John Kessenichb9e39122016-08-17 10:22:08 -0600623 case EHTokLayout:
624 if (! acceptLayoutQualifierList(qualifier))
625 return false;
626 continue;
steve-lunarg5da1f032017-02-12 17:50:28 -0700627 case EHTokGloballyCoherent:
628 qualifier.coherent = true;
629 break;
John Kessenich36b218d2017-03-15 09:05:14 -0600630 case EHTokInline:
631 // TODO: map this to SPIR-V function control
632 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700633
634 // GS geometries: these are specified on stage input variables, and are an error (not verified here)
635 // for output variables.
636 case EHTokPoint:
637 qualifier.storage = EvqIn;
638 if (!parseContext.handleInputGeometry(token.loc, ElgPoints))
639 return false;
640 break;
641 case EHTokLine:
642 qualifier.storage = EvqIn;
643 if (!parseContext.handleInputGeometry(token.loc, ElgLines))
644 return false;
645 break;
646 case EHTokTriangle:
647 qualifier.storage = EvqIn;
648 if (!parseContext.handleInputGeometry(token.loc, ElgTriangles))
649 return false;
650 break;
651 case EHTokLineAdj:
652 qualifier.storage = EvqIn;
653 if (!parseContext.handleInputGeometry(token.loc, ElgLinesAdjacency))
654 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700655 break;
steve-lunargf49cdf42016-11-17 15:04:20 -0700656 case EHTokTriangleAdj:
657 qualifier.storage = EvqIn;
658 if (!parseContext.handleInputGeometry(token.loc, ElgTrianglesAdjacency))
659 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700660 break;
661
John Kessenich630dd7d2016-06-12 23:52:12 -0600662 default:
John Kessenichb9e39122016-08-17 10:22:08 -0600663 return true;
John Kessenich630dd7d2016-06-12 23:52:12 -0600664 }
665 advanceToken();
666 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -0700667}
668
John Kessenichb9e39122016-08-17 10:22:08 -0600669// layout_qualifier_list
John Kesseniche3218e22016-09-05 14:37:03 -0600670// : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
John Kessenichb9e39122016-08-17 10:22:08 -0600671//
672// layout_qualifier
673// : identifier
John Kessenich841db352016-09-02 21:12:23 -0600674// | identifier EQUAL expression
John Kessenichb9e39122016-08-17 10:22:08 -0600675//
676// Zero or more of these, so this can't return false.
677//
678bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
679{
680 if (! acceptTokenClass(EHTokLayout))
681 return false;
682
683 // LEFT_PAREN
684 if (! acceptTokenClass(EHTokLeftParen))
685 return false;
686
687 do {
688 // identifier
689 HlslToken idToken;
690 if (! acceptIdentifier(idToken))
691 break;
692
693 // EQUAL expression
694 if (acceptTokenClass(EHTokAssign)) {
695 TIntermTyped* expr;
696 if (! acceptConditionalExpression(expr)) {
697 expected("expression");
698 return false;
699 }
700 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
701 } else
702 parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
703
704 // COMMA
705 if (! acceptTokenClass(EHTokComma))
706 break;
707 } while (true);
708
709 // RIGHT_PAREN
710 if (! acceptTokenClass(EHTokRightParen)) {
711 expected(")");
712 return false;
713 }
714
715 return true;
716}
717
LoopDawg6daaa4f2016-06-23 19:13:48 -0600718// template_type
719// : FLOAT
720// | DOUBLE
721// | INT
722// | DWORD
723// | UINT
724// | BOOL
725//
steve-lunargf49cdf42016-11-17 15:04:20 -0700726bool HlslGrammar::acceptTemplateVecMatBasicType(TBasicType& basicType)
LoopDawg6daaa4f2016-06-23 19:13:48 -0600727{
728 switch (peek()) {
729 case EHTokFloat:
730 basicType = EbtFloat;
731 break;
732 case EHTokDouble:
733 basicType = EbtDouble;
734 break;
735 case EHTokInt:
736 case EHTokDword:
737 basicType = EbtInt;
738 break;
739 case EHTokUint:
740 basicType = EbtUint;
741 break;
742 case EHTokBool:
743 basicType = EbtBool;
744 break;
745 default:
746 return false;
747 }
748
749 advanceToken();
750
751 return true;
752}
753
754// vector_template_type
755// : VECTOR
756// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
757//
758bool HlslGrammar::acceptVectorTemplateType(TType& type)
759{
760 if (! acceptTokenClass(EHTokVector))
761 return false;
762
763 if (! acceptTokenClass(EHTokLeftAngle)) {
764 // in HLSL, 'vector' alone means float4.
765 new(&type) TType(EbtFloat, EvqTemporary, 4);
766 return true;
767 }
768
769 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700770 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600771 expected("scalar type");
772 return false;
773 }
774
775 // COMMA
776 if (! acceptTokenClass(EHTokComma)) {
777 expected(",");
778 return false;
779 }
780
781 // integer
782 if (! peekTokenClass(EHTokIntConstant)) {
783 expected("literal integer");
784 return false;
785 }
786
787 TIntermTyped* vecSize;
788 if (! acceptLiteral(vecSize))
789 return false;
790
791 const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
792
793 new(&type) TType(basicType, EvqTemporary, vecSizeI);
794
795 if (vecSizeI == 1)
796 type.makeVector();
797
798 if (!acceptTokenClass(EHTokRightAngle)) {
799 expected("right angle bracket");
800 return false;
801 }
802
803 return true;
804}
805
806// matrix_template_type
807// : MATRIX
808// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
809//
810bool HlslGrammar::acceptMatrixTemplateType(TType& type)
811{
812 if (! acceptTokenClass(EHTokMatrix))
813 return false;
814
815 if (! acceptTokenClass(EHTokLeftAngle)) {
816 // in HLSL, 'matrix' alone means float4x4.
817 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
818 return true;
819 }
820
821 TBasicType basicType;
steve-lunargf49cdf42016-11-17 15:04:20 -0700822 if (! acceptTemplateVecMatBasicType(basicType)) {
LoopDawg6daaa4f2016-06-23 19:13:48 -0600823 expected("scalar type");
824 return false;
825 }
826
827 // COMMA
828 if (! acceptTokenClass(EHTokComma)) {
829 expected(",");
830 return false;
831 }
832
833 // integer rows
834 if (! peekTokenClass(EHTokIntConstant)) {
835 expected("literal integer");
836 return false;
837 }
838
839 TIntermTyped* rows;
840 if (! acceptLiteral(rows))
841 return false;
842
843 // COMMA
844 if (! acceptTokenClass(EHTokComma)) {
845 expected(",");
846 return false;
847 }
John Kessenichecba76f2017-01-06 00:34:48 -0700848
LoopDawg6daaa4f2016-06-23 19:13:48 -0600849 // integer cols
850 if (! peekTokenClass(EHTokIntConstant)) {
851 expected("literal integer");
852 return false;
853 }
854
855 TIntermTyped* cols;
856 if (! acceptLiteral(cols))
857 return false;
858
859 new(&type) TType(basicType, EvqTemporary, 0,
steve-lunarg297ae212016-08-24 14:36:13 -0600860 rows->getAsConstantUnion()->getConstArray()[0].getIConst(),
861 cols->getAsConstantUnion()->getConstArray()[0].getIConst());
LoopDawg6daaa4f2016-06-23 19:13:48 -0600862
863 if (!acceptTokenClass(EHTokRightAngle)) {
864 expected("right angle bracket");
865 return false;
866 }
867
868 return true;
869}
870
steve-lunargf49cdf42016-11-17 15:04:20 -0700871// layout_geometry
872// : LINESTREAM
873// | POINTSTREAM
874// | TRIANGLESTREAM
875//
876bool HlslGrammar::acceptOutputPrimitiveGeometry(TLayoutGeometry& geometry)
877{
878 // read geometry type
879 const EHlslTokenClass geometryType = peek();
880
881 switch (geometryType) {
882 case EHTokPointStream: geometry = ElgPoints; break;
883 case EHTokLineStream: geometry = ElgLineStrip; break;
884 case EHTokTriangleStream: geometry = ElgTriangleStrip; break;
885 default:
886 return false; // not a layout geometry
887 }
888
889 advanceToken(); // consume the layout keyword
890 return true;
891}
892
steve-lunarg858c9282017-01-07 08:54:10 -0700893// tessellation_decl_type
894// : INPUTPATCH
895// | OUTPUTPATCH
896//
897bool HlslGrammar::acceptTessellationDeclType()
898{
899 // read geometry type
900 const EHlslTokenClass tessType = peek();
901
902 switch (tessType) {
903 case EHTokInputPatch: break;
904 case EHTokOutputPatch: break;
905 default:
906 return false; // not a tessellation decl
907 }
908
909 advanceToken(); // consume the keyword
910 return true;
911}
912
913// tessellation_patch_template_type
914// : tessellation_decl_type LEFT_ANGLE type comma integer_literal RIGHT_ANGLE
915//
916bool HlslGrammar::acceptTessellationPatchTemplateType(TType& type)
917{
918 if (! acceptTessellationDeclType())
919 return false;
920
921 if (! acceptTokenClass(EHTokLeftAngle))
922 return false;
923
924 if (! acceptType(type)) {
925 expected("tessellation patch type");
926 return false;
927 }
928
929 if (! acceptTokenClass(EHTokComma))
930 return false;
931
932 // integer size
933 if (! peekTokenClass(EHTokIntConstant)) {
934 expected("literal integer");
935 return false;
936 }
937
938 TIntermTyped* size;
939 if (! acceptLiteral(size))
940 return false;
941
942 TArraySizes* arraySizes = new TArraySizes;
943 arraySizes->addInnerSize(size->getAsConstantUnion()->getConstArray()[0].getIConst());
944 type.newArraySizes(*arraySizes);
945
946 if (! acceptTokenClass(EHTokRightAngle)) {
947 expected("right angle bracket");
948 return false;
949 }
950
951 return true;
952}
953
steve-lunargf49cdf42016-11-17 15:04:20 -0700954// stream_out_template_type
955// : output_primitive_geometry_type LEFT_ANGLE type RIGHT_ANGLE
956//
957bool HlslGrammar::acceptStreamOutTemplateType(TType& type, TLayoutGeometry& geometry)
958{
959 geometry = ElgNone;
960
961 if (! acceptOutputPrimitiveGeometry(geometry))
962 return false;
963
964 if (! acceptTokenClass(EHTokLeftAngle))
965 return false;
John Kessenichecba76f2017-01-06 00:34:48 -0700966
steve-lunargf49cdf42016-11-17 15:04:20 -0700967 if (! acceptType(type)) {
968 expected("stream output type");
969 return false;
970 }
971
972 type.getQualifier().storage = EvqVaryingOut;
973
974 if (! acceptTokenClass(EHTokRightAngle)) {
975 expected("right angle bracket");
976 return false;
977 }
978
979 return true;
980}
John Kessenichecba76f2017-01-06 00:34:48 -0700981
John Kessenicha1e2d492016-09-20 13:22:58 -0600982// annotations
983// : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
John Kessenich86f71382016-09-19 20:23:18 -0600984//
John Kessenicha1e2d492016-09-20 13:22:58 -0600985bool HlslGrammar::acceptAnnotations(TQualifier&)
John Kessenich86f71382016-09-19 20:23:18 -0600986{
John Kessenicha1e2d492016-09-20 13:22:58 -0600987 if (! acceptTokenClass(EHTokLeftAngle))
John Kessenich86f71382016-09-19 20:23:18 -0600988 return false;
989
John Kessenicha1e2d492016-09-20 13:22:58 -0600990 // note that we are nesting a name space
991 parseContext.nestAnnotations();
John Kessenich86f71382016-09-19 20:23:18 -0600992
993 // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
994 do {
995 // eat any extra SEMI_COLON; don't know if the grammar calls for this or not
996 while (acceptTokenClass(EHTokSemicolon))
997 ;
998
999 if (acceptTokenClass(EHTokRightAngle))
John Kessenicha1e2d492016-09-20 13:22:58 -06001000 break;
John Kessenich86f71382016-09-19 20:23:18 -06001001
1002 // declaration
John Kessenichca71d942017-03-07 20:44:09 -07001003 TIntermNode* node = nullptr;
John Kessenich86f71382016-09-19 20:23:18 -06001004 if (! acceptDeclaration(node)) {
John Kessenicha1e2d492016-09-20 13:22:58 -06001005 expected("declaration in annotation");
John Kessenich86f71382016-09-19 20:23:18 -06001006 return false;
1007 }
1008 } while (true);
John Kessenicha1e2d492016-09-20 13:22:58 -06001009
1010 parseContext.unnestAnnotations();
1011 return true;
John Kessenich86f71382016-09-19 20:23:18 -06001012}
LoopDawg6daaa4f2016-06-23 19:13:48 -06001013
LoopDawg4886f692016-06-29 10:58:58 -06001014// sampler_type
1015// : SAMPLER
1016// | SAMPLER1D
1017// | SAMPLER2D
1018// | SAMPLER3D
1019// | SAMPLERCUBE
1020// | SAMPLERSTATE
1021// | SAMPLERCOMPARISONSTATE
1022bool HlslGrammar::acceptSamplerType(TType& type)
1023{
1024 // read sampler type
1025 const EHlslTokenClass samplerType = peek();
1026
LoopDawga78b0292016-07-19 14:28:05 -06001027 // TODO: for DX9
LoopDawg5d58fae2016-07-15 11:22:24 -06001028 // TSamplerDim dim = EsdNone;
LoopDawg4886f692016-06-29 10:58:58 -06001029
LoopDawga78b0292016-07-19 14:28:05 -06001030 bool isShadow = false;
1031
LoopDawg4886f692016-06-29 10:58:58 -06001032 switch (samplerType) {
1033 case EHTokSampler: break;
LoopDawg5d58fae2016-07-15 11:22:24 -06001034 case EHTokSampler1d: /*dim = Esd1D*/; break;
1035 case EHTokSampler2d: /*dim = Esd2D*/; break;
1036 case EHTokSampler3d: /*dim = Esd3D*/; break;
1037 case EHTokSamplerCube: /*dim = EsdCube*/; break;
LoopDawg4886f692016-06-29 10:58:58 -06001038 case EHTokSamplerState: break;
LoopDawga78b0292016-07-19 14:28:05 -06001039 case EHTokSamplerComparisonState: isShadow = true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001040 default:
1041 return false; // not a sampler declaration
1042 }
1043
1044 advanceToken(); // consume the sampler type keyword
1045
1046 TArraySizes* arraySizes = nullptr; // TODO: array
LoopDawg4886f692016-06-29 10:58:58 -06001047
1048 TSampler sampler;
LoopDawga78b0292016-07-19 14:28:05 -06001049 sampler.setPureSampler(isShadow);
LoopDawg4886f692016-06-29 10:58:58 -06001050
1051 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
1052
1053 return true;
1054}
1055
1056// texture_type
1057// | BUFFER
1058// | TEXTURE1D
1059// | TEXTURE1DARRAY
1060// | TEXTURE2D
1061// | TEXTURE2DARRAY
1062// | TEXTURE3D
1063// | TEXTURECUBE
1064// | TEXTURECUBEARRAY
1065// | TEXTURE2DMS
1066// | TEXTURE2DMSARRAY
steve-lunargbb0183f2016-10-04 16:58:14 -06001067// | RWBUFFER
1068// | RWTEXTURE1D
1069// | RWTEXTURE1DARRAY
1070// | RWTEXTURE2D
1071// | RWTEXTURE2DARRAY
1072// | RWTEXTURE3D
1073
LoopDawg4886f692016-06-29 10:58:58 -06001074bool HlslGrammar::acceptTextureType(TType& type)
1075{
1076 const EHlslTokenClass textureType = peek();
1077
1078 TSamplerDim dim = EsdNone;
1079 bool array = false;
1080 bool ms = false;
steve-lunargbb0183f2016-10-04 16:58:14 -06001081 bool image = false;
LoopDawg4886f692016-06-29 10:58:58 -06001082
1083 switch (textureType) {
1084 case EHTokBuffer: dim = EsdBuffer; break;
1085 case EHTokTexture1d: dim = Esd1D; break;
1086 case EHTokTexture1darray: dim = Esd1D; array = true; break;
1087 case EHTokTexture2d: dim = Esd2D; break;
1088 case EHTokTexture2darray: dim = Esd2D; array = true; break;
John Kessenichecba76f2017-01-06 00:34:48 -07001089 case EHTokTexture3d: dim = Esd3D; break;
LoopDawg4886f692016-06-29 10:58:58 -06001090 case EHTokTextureCube: dim = EsdCube; break;
1091 case EHTokTextureCubearray: dim = EsdCube; array = true; break;
1092 case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
1093 case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
steve-lunargbb0183f2016-10-04 16:58:14 -06001094 case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
1095 case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
1096 case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
1097 case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
1098 case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
1099 case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
LoopDawg4886f692016-06-29 10:58:58 -06001100 default:
1101 return false; // not a texture declaration
1102 }
1103
1104 advanceToken(); // consume the texture object keyword
1105
1106 TType txType(EbtFloat, EvqUniform, 4); // default type is float4
John Kessenichecba76f2017-01-06 00:34:48 -07001107
LoopDawg4886f692016-06-29 10:58:58 -06001108 TIntermTyped* msCount = nullptr;
1109
steve-lunargbb0183f2016-10-04 16:58:14 -06001110 // texture type: required for multisample types and RWBuffer/RWTextures!
LoopDawg4886f692016-06-29 10:58:58 -06001111 if (acceptTokenClass(EHTokLeftAngle)) {
1112 if (! acceptType(txType)) {
1113 expected("scalar or vector type");
1114 return false;
1115 }
1116
1117 const TBasicType basicRetType = txType.getBasicType() ;
1118
1119 if (basicRetType != EbtFloat && basicRetType != EbtUint && basicRetType != EbtInt) {
1120 unimplemented("basic type in texture");
1121 return false;
1122 }
1123
steve-lunargd53f7172016-07-27 15:46:48 -06001124 // Buffers can handle small mats if they fit in 4 components
1125 if (dim == EsdBuffer && txType.isMatrix()) {
1126 if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
1127 expected("components < 4 in matrix buffer type");
1128 return false;
1129 }
1130
1131 // TODO: except we don't handle it yet...
1132 unimplemented("matrix type in buffer");
1133 return false;
1134 }
1135
LoopDawg4886f692016-06-29 10:58:58 -06001136 if (!txType.isScalar() && !txType.isVector()) {
1137 expected("scalar or vector type");
1138 return false;
1139 }
1140
LoopDawg4886f692016-06-29 10:58:58 -06001141 if (ms && acceptTokenClass(EHTokComma)) {
1142 // read sample count for multisample types, if given
1143 if (! peekTokenClass(EHTokIntConstant)) {
1144 expected("multisample count");
1145 return false;
1146 }
1147
1148 if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
1149 return false;
1150 }
1151
1152 if (! acceptTokenClass(EHTokRightAngle)) {
1153 expected("right angle bracket");
1154 return false;
1155 }
1156 } else if (ms) {
1157 expected("texture type for multisample");
1158 return false;
steve-lunargbb0183f2016-10-04 16:58:14 -06001159 } else if (image) {
1160 expected("type for RWTexture/RWBuffer");
1161 return false;
LoopDawg4886f692016-06-29 10:58:58 -06001162 }
1163
1164 TArraySizes* arraySizes = nullptr;
steve-lunarg4f2da272016-10-10 15:24:57 -06001165 const bool shadow = false; // declared on the sampler
LoopDawg4886f692016-06-29 10:58:58 -06001166
1167 TSampler sampler;
steve-lunargbb0183f2016-10-04 16:58:14 -06001168 TLayoutFormat format = ElfNone;
steve-lunargd53f7172016-07-27 15:46:48 -06001169
steve-lunarg4f2da272016-10-10 15:24:57 -06001170 // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
1171 if (image || dim == EsdBuffer)
1172 format = parseContext.getLayoutFromTxType(token.loc, txType);
steve-lunargbb0183f2016-10-04 16:58:14 -06001173
1174 // Non-image Buffers are combined
1175 if (dim == EsdBuffer && !image) {
steve-lunargd53f7172016-07-27 15:46:48 -06001176 sampler.set(txType.getBasicType(), dim, array);
1177 } else {
1178 // DX10 textures are separated. TODO: DX9.
steve-lunargbb0183f2016-10-04 16:58:14 -06001179 if (image) {
1180 sampler.setImage(txType.getBasicType(), dim, array, shadow, ms);
1181 } else {
1182 sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms);
1183 }
steve-lunargd53f7172016-07-27 15:46:48 -06001184 }
steve-lunarg8b0227c2016-10-14 16:40:32 -06001185
1186 // Remember the declared vector size.
1187 sampler.vectorSize = txType.getVectorSize();
John Kessenichecba76f2017-01-06 00:34:48 -07001188
LoopDawg4886f692016-06-29 10:58:58 -06001189 type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
steve-lunargbb0183f2016-10-04 16:58:14 -06001190 type.getQualifier().layoutFormat = format;
LoopDawg4886f692016-06-29 10:58:58 -06001191
1192 return true;
1193}
1194
John Kessenich87142c72016-03-12 20:24:24 -07001195// If token is for a type, update 'type' with the type information,
1196// and return true and advance.
1197// Otherwise, return false, and don't advance
1198bool HlslGrammar::acceptType(TType& type)
1199{
John Kessenich54ee28f2017-03-11 14:13:00 -07001200 TIntermNode* nodeList = nullptr;
1201 return acceptType(type, nodeList);
1202}
1203bool HlslGrammar::acceptType(TType& type, TIntermNode*& nodeList)
1204{
steve-lunarg3226b082016-10-26 19:18:55 -06001205 // Basic types for min* types, broken out here in case of future
1206 // changes, e.g, to use native halfs.
1207 static const TBasicType min16float_bt = EbtFloat;
1208 static const TBasicType min10float_bt = EbtFloat;
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001209 static const TBasicType half_bt = EbtFloat;
steve-lunarg3226b082016-10-26 19:18:55 -06001210 static const TBasicType min16int_bt = EbtInt;
1211 static const TBasicType min12int_bt = EbtInt;
1212 static const TBasicType min16uint_bt = EbtUint;
1213
John Kessenich9c86c6a2016-05-03 22:49:24 -06001214 switch (peek()) {
LoopDawg6daaa4f2016-06-23 19:13:48 -06001215 case EHTokVector:
1216 return acceptVectorTemplateType(type);
1217 break;
1218
1219 case EHTokMatrix:
1220 return acceptMatrixTemplateType(type);
1221 break;
1222
steve-lunargf49cdf42016-11-17 15:04:20 -07001223 case EHTokPointStream: // fall through
1224 case EHTokLineStream: // ...
1225 case EHTokTriangleStream: // ...
1226 {
1227 TLayoutGeometry geometry;
1228 if (! acceptStreamOutTemplateType(type, geometry))
1229 return false;
1230
1231 if (! parseContext.handleOutputGeometry(token.loc, geometry))
1232 return false;
John Kessenichecba76f2017-01-06 00:34:48 -07001233
steve-lunargf49cdf42016-11-17 15:04:20 -07001234 return true;
1235 }
1236
steve-lunarg858c9282017-01-07 08:54:10 -07001237 case EHTokInputPatch: // fall through
1238 case EHTokOutputPatch: // ...
1239 {
1240 if (! acceptTessellationPatchTemplateType(type))
1241 return false;
1242
1243 return true;
1244 }
1245
LoopDawg4886f692016-06-29 10:58:58 -06001246 case EHTokSampler: // fall through
1247 case EHTokSampler1d: // ...
1248 case EHTokSampler2d: // ...
1249 case EHTokSampler3d: // ...
1250 case EHTokSamplerCube: // ...
1251 case EHTokSamplerState: // ...
1252 case EHTokSamplerComparisonState: // ...
1253 return acceptSamplerType(type);
1254 break;
1255
1256 case EHTokBuffer: // fall through
1257 case EHTokTexture1d: // ...
1258 case EHTokTexture1darray: // ...
1259 case EHTokTexture2d: // ...
1260 case EHTokTexture2darray: // ...
1261 case EHTokTexture3d: // ...
1262 case EHTokTextureCube: // ...
1263 case EHTokTextureCubearray: // ...
1264 case EHTokTexture2DMS: // ...
1265 case EHTokTexture2DMSarray: // ...
steve-lunargbb0183f2016-10-04 16:58:14 -06001266 case EHTokRWTexture1d: // ...
1267 case EHTokRWTexture1darray: // ...
1268 case EHTokRWTexture2d: // ...
1269 case EHTokRWTexture2darray: // ...
1270 case EHTokRWTexture3d: // ...
1271 case EHTokRWBuffer: // ...
LoopDawg4886f692016-06-29 10:58:58 -06001272 return acceptTextureType(type);
1273 break;
1274
steve-lunarg5da1f032017-02-12 17:50:28 -07001275 case EHTokAppendStructuredBuffer:
1276 case EHTokByteAddressBuffer:
1277 case EHTokConsumeStructuredBuffer:
1278 case EHTokRWByteAddressBuffer:
1279 case EHTokRWStructuredBuffer:
1280 case EHTokStructuredBuffer:
1281 return acceptStructBufferType(type);
1282 break;
1283
John Kessenich27ffb292017-03-03 17:01:01 -07001284 case EHTokClass:
John Kesseniche6e74942016-06-11 16:43:14 -06001285 case EHTokStruct:
John Kessenich3d157c52016-07-25 16:05:33 -06001286 case EHTokCBuffer:
1287 case EHTokTBuffer:
John Kessenich54ee28f2017-03-11 14:13:00 -07001288 return acceptStruct(type, nodeList);
John Kesseniche6e74942016-06-11 16:43:14 -06001289
1290 case EHTokIdentifier:
1291 // An identifier could be for a user-defined type.
1292 // Note we cache the symbol table lookup, to save for a later rule
1293 // when this is not a type.
John Kessenich854fe242017-03-02 14:30:59 -07001294 token.symbol = parseContext.lookupUserType(*token.string, type);
1295 if (token.symbol != nullptr) {
John Kesseniche6e74942016-06-11 16:43:14 -06001296 advanceToken();
1297 return true;
1298 } else
1299 return false;
1300
John Kessenich71351de2016-06-08 12:50:56 -06001301 case EHTokVoid:
1302 new(&type) TType(EbtVoid);
John Kessenich87142c72016-03-12 20:24:24 -07001303 break;
John Kessenich71351de2016-06-08 12:50:56 -06001304
John Kessenicha1e2d492016-09-20 13:22:58 -06001305 case EHTokString:
1306 new(&type) TType(EbtString);
1307 break;
1308
John Kessenich87142c72016-03-12 20:24:24 -07001309 case EHTokFloat:
John Kessenich8d72f1a2016-05-20 12:06:03 -06001310 new(&type) TType(EbtFloat);
1311 break;
John Kessenich87142c72016-03-12 20:24:24 -07001312 case EHTokFloat1:
1313 new(&type) TType(EbtFloat);
John Kessenich8d72f1a2016-05-20 12:06:03 -06001314 type.makeVector();
John Kessenich87142c72016-03-12 20:24:24 -07001315 break;
John Kessenich87142c72016-03-12 20:24:24 -07001316 case EHTokFloat2:
1317 new(&type) TType(EbtFloat, EvqTemporary, 2);
1318 break;
1319 case EHTokFloat3:
1320 new(&type) TType(EbtFloat, EvqTemporary, 3);
1321 break;
1322 case EHTokFloat4:
1323 new(&type) TType(EbtFloat, EvqTemporary, 4);
1324 break;
1325
John Kessenich71351de2016-06-08 12:50:56 -06001326 case EHTokDouble:
1327 new(&type) TType(EbtDouble);
1328 break;
1329 case EHTokDouble1:
1330 new(&type) TType(EbtDouble);
1331 type.makeVector();
1332 break;
1333 case EHTokDouble2:
1334 new(&type) TType(EbtDouble, EvqTemporary, 2);
1335 break;
1336 case EHTokDouble3:
1337 new(&type) TType(EbtDouble, EvqTemporary, 3);
1338 break;
1339 case EHTokDouble4:
1340 new(&type) TType(EbtDouble, EvqTemporary, 4);
1341 break;
1342
1343 case EHTokInt:
1344 case EHTokDword:
1345 new(&type) TType(EbtInt);
1346 break;
1347 case EHTokInt1:
1348 new(&type) TType(EbtInt);
1349 type.makeVector();
1350 break;
John Kessenich87142c72016-03-12 20:24:24 -07001351 case EHTokInt2:
1352 new(&type) TType(EbtInt, EvqTemporary, 2);
1353 break;
1354 case EHTokInt3:
1355 new(&type) TType(EbtInt, EvqTemporary, 3);
1356 break;
1357 case EHTokInt4:
1358 new(&type) TType(EbtInt, EvqTemporary, 4);
1359 break;
1360
John Kessenich71351de2016-06-08 12:50:56 -06001361 case EHTokUint:
1362 new(&type) TType(EbtUint);
1363 break;
1364 case EHTokUint1:
1365 new(&type) TType(EbtUint);
1366 type.makeVector();
1367 break;
1368 case EHTokUint2:
1369 new(&type) TType(EbtUint, EvqTemporary, 2);
1370 break;
1371 case EHTokUint3:
1372 new(&type) TType(EbtUint, EvqTemporary, 3);
1373 break;
1374 case EHTokUint4:
1375 new(&type) TType(EbtUint, EvqTemporary, 4);
1376 break;
1377
1378 case EHTokBool:
1379 new(&type) TType(EbtBool);
1380 break;
1381 case EHTokBool1:
1382 new(&type) TType(EbtBool);
1383 type.makeVector();
1384 break;
John Kessenich87142c72016-03-12 20:24:24 -07001385 case EHTokBool2:
1386 new(&type) TType(EbtBool, EvqTemporary, 2);
1387 break;
1388 case EHTokBool3:
1389 new(&type) TType(EbtBool, EvqTemporary, 3);
1390 break;
1391 case EHTokBool4:
1392 new(&type) TType(EbtBool, EvqTemporary, 4);
1393 break;
1394
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001395 case EHTokHalf:
1396 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1397 break;
1398 case EHTokHalf1:
1399 new(&type) TType(half_bt, EvqTemporary, EpqMedium);
1400 type.makeVector();
1401 break;
1402 case EHTokHalf2:
1403 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 2);
1404 break;
1405 case EHTokHalf3:
1406 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 3);
1407 break;
1408 case EHTokHalf4:
1409 new(&type) TType(half_bt, EvqTemporary, EpqMedium, 4);
1410 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001411
steve-lunarg3226b082016-10-26 19:18:55 -06001412 case EHTokMin16float:
1413 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1414 break;
1415 case EHTokMin16float1:
1416 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
1417 type.makeVector();
1418 break;
1419 case EHTokMin16float2:
1420 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 2);
1421 break;
1422 case EHTokMin16float3:
1423 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 3);
1424 break;
1425 case EHTokMin16float4:
1426 new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 4);
1427 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001428
steve-lunarg3226b082016-10-26 19:18:55 -06001429 case EHTokMin10float:
1430 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1431 break;
1432 case EHTokMin10float1:
1433 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
1434 type.makeVector();
1435 break;
1436 case EHTokMin10float2:
1437 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 2);
1438 break;
1439 case EHTokMin10float3:
1440 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 3);
1441 break;
1442 case EHTokMin10float4:
1443 new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 4);
1444 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001445
steve-lunarg3226b082016-10-26 19:18:55 -06001446 case EHTokMin16int:
1447 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1448 break;
1449 case EHTokMin16int1:
1450 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
1451 type.makeVector();
1452 break;
1453 case EHTokMin16int2:
1454 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 2);
1455 break;
1456 case EHTokMin16int3:
1457 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 3);
1458 break;
1459 case EHTokMin16int4:
1460 new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 4);
1461 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001462
steve-lunarg3226b082016-10-26 19:18:55 -06001463 case EHTokMin12int:
1464 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1465 break;
1466 case EHTokMin12int1:
1467 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
1468 type.makeVector();
1469 break;
1470 case EHTokMin12int2:
1471 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 2);
1472 break;
1473 case EHTokMin12int3:
1474 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 3);
1475 break;
1476 case EHTokMin12int4:
1477 new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 4);
1478 break;
John Kessenichecba76f2017-01-06 00:34:48 -07001479
steve-lunarg3226b082016-10-26 19:18:55 -06001480 case EHTokMin16uint:
1481 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1482 break;
1483 case EHTokMin16uint1:
1484 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
1485 type.makeVector();
1486 break;
1487 case EHTokMin16uint2:
1488 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 2);
1489 break;
1490 case EHTokMin16uint3:
1491 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 3);
1492 break;
1493 case EHTokMin16uint4:
1494 new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 4);
1495 break;
1496
John Kessenich0133c122016-05-20 12:17:26 -06001497 case EHTokInt1x1:
1498 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
1499 break;
1500 case EHTokInt1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001501 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001502 break;
1503 case EHTokInt1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001504 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001505 break;
1506 case EHTokInt1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001507 new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001508 break;
1509 case EHTokInt2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001510 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001511 break;
1512 case EHTokInt2x2:
1513 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
1514 break;
1515 case EHTokInt2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001516 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001517 break;
1518 case EHTokInt2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001519 new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001520 break;
1521 case EHTokInt3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001522 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001523 break;
1524 case EHTokInt3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001525 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001526 break;
1527 case EHTokInt3x3:
1528 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
1529 break;
1530 case EHTokInt3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001531 new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001532 break;
1533 case EHTokInt4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001534 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001535 break;
1536 case EHTokInt4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001537 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001538 break;
1539 case EHTokInt4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001540 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001541 break;
1542 case EHTokInt4x4:
1543 new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
1544 break;
1545
John Kessenich71351de2016-06-08 12:50:56 -06001546 case EHTokUint1x1:
1547 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
1548 break;
1549 case EHTokUint1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001550 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001551 break;
1552 case EHTokUint1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001553 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001554 break;
1555 case EHTokUint1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001556 new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001557 break;
1558 case EHTokUint2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001559 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001560 break;
1561 case EHTokUint2x2:
1562 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
1563 break;
1564 case EHTokUint2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001565 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001566 break;
1567 case EHTokUint2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001568 new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001569 break;
1570 case EHTokUint3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001571 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001572 break;
1573 case EHTokUint3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001574 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001575 break;
1576 case EHTokUint3x3:
1577 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
1578 break;
1579 case EHTokUint3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001580 new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001581 break;
1582 case EHTokUint4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001583 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001584 break;
1585 case EHTokUint4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001586 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001587 break;
1588 case EHTokUint4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001589 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001590 break;
1591 case EHTokUint4x4:
1592 new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
1593 break;
1594
1595 case EHTokBool1x1:
1596 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
1597 break;
1598 case EHTokBool1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001599 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001600 break;
1601 case EHTokBool1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001602 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001603 break;
1604 case EHTokBool1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001605 new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001606 break;
1607 case EHTokBool2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001608 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001609 break;
1610 case EHTokBool2x2:
1611 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
1612 break;
1613 case EHTokBool2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001614 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001615 break;
1616 case EHTokBool2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001617 new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001618 break;
1619 case EHTokBool3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001620 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001621 break;
1622 case EHTokBool3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001623 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001624 break;
1625 case EHTokBool3x3:
1626 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
1627 break;
1628 case EHTokBool3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001629 new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
John Kessenich71351de2016-06-08 12:50:56 -06001630 break;
1631 case EHTokBool4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001632 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
John Kessenich71351de2016-06-08 12:50:56 -06001633 break;
1634 case EHTokBool4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001635 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
John Kessenich71351de2016-06-08 12:50:56 -06001636 break;
1637 case EHTokBool4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001638 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
John Kessenich71351de2016-06-08 12:50:56 -06001639 break;
1640 case EHTokBool4x4:
1641 new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
1642 break;
1643
John Kessenich0133c122016-05-20 12:17:26 -06001644 case EHTokFloat1x1:
1645 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
1646 break;
1647 case EHTokFloat1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001648 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001649 break;
1650 case EHTokFloat1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001651 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001652 break;
1653 case EHTokFloat1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001654 new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001655 break;
1656 case EHTokFloat2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001657 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001658 break;
John Kessenich87142c72016-03-12 20:24:24 -07001659 case EHTokFloat2x2:
1660 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
1661 break;
1662 case EHTokFloat2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001663 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001664 break;
1665 case EHTokFloat2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001666 new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001667 break;
John Kessenich0133c122016-05-20 12:17:26 -06001668 case EHTokFloat3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001669 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001670 break;
John Kessenich87142c72016-03-12 20:24:24 -07001671 case EHTokFloat3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001672 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001673 break;
1674 case EHTokFloat3x3:
1675 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
1676 break;
1677 case EHTokFloat3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001678 new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
John Kessenich87142c72016-03-12 20:24:24 -07001679 break;
John Kessenich0133c122016-05-20 12:17:26 -06001680 case EHTokFloat4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001681 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001682 break;
John Kessenich87142c72016-03-12 20:24:24 -07001683 case EHTokFloat4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001684 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
John Kessenich87142c72016-03-12 20:24:24 -07001685 break;
1686 case EHTokFloat4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001687 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
John Kessenich87142c72016-03-12 20:24:24 -07001688 break;
1689 case EHTokFloat4x4:
1690 new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
1691 break;
1692
John Kessenich0133c122016-05-20 12:17:26 -06001693 case EHTokDouble1x1:
1694 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
1695 break;
1696 case EHTokDouble1x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001697 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001698 break;
1699 case EHTokDouble1x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001700 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001701 break;
1702 case EHTokDouble1x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001703 new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001704 break;
1705 case EHTokDouble2x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001706 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001707 break;
1708 case EHTokDouble2x2:
1709 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
1710 break;
1711 case EHTokDouble2x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001712 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001713 break;
1714 case EHTokDouble2x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001715 new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001716 break;
1717 case EHTokDouble3x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001718 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001719 break;
1720 case EHTokDouble3x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001721 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001722 break;
1723 case EHTokDouble3x3:
1724 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
1725 break;
1726 case EHTokDouble3x4:
steve-lunarg297ae212016-08-24 14:36:13 -06001727 new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
John Kessenich0133c122016-05-20 12:17:26 -06001728 break;
1729 case EHTokDouble4x1:
steve-lunarg297ae212016-08-24 14:36:13 -06001730 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
John Kessenich0133c122016-05-20 12:17:26 -06001731 break;
1732 case EHTokDouble4x2:
steve-lunarg297ae212016-08-24 14:36:13 -06001733 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
John Kessenich0133c122016-05-20 12:17:26 -06001734 break;
1735 case EHTokDouble4x3:
steve-lunarg297ae212016-08-24 14:36:13 -06001736 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
John Kessenich0133c122016-05-20 12:17:26 -06001737 break;
1738 case EHTokDouble4x4:
1739 new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
1740 break;
1741
John Kessenich87142c72016-03-12 20:24:24 -07001742 default:
1743 return false;
1744 }
1745
1746 advanceToken();
1747
1748 return true;
1749}
1750
John Kesseniche6e74942016-06-11 16:43:14 -06001751// struct
John Kessenich3d157c52016-07-25 16:05:33 -06001752// : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
1753// | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
John Kessenich854fe242017-03-02 14:30:59 -07001754// | struct_type IDENTIFIER // use of previously declared struct type
John Kessenich3d157c52016-07-25 16:05:33 -06001755//
1756// struct_type
1757// : STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001758// | CLASS
John Kessenich3d157c52016-07-25 16:05:33 -06001759// | CBUFFER
1760// | TBUFFER
John Kesseniche6e74942016-06-11 16:43:14 -06001761//
John Kessenich54ee28f2017-03-11 14:13:00 -07001762bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
John Kesseniche6e74942016-06-11 16:43:14 -06001763{
John Kessenichb804de62016-09-05 12:19:18 -06001764 // This storage qualifier will tell us whether it's an AST
1765 // block type or just a generic structure type.
1766 TStorageQualifier storageQualifier = EvqTemporary;
John Kessenich3d157c52016-07-25 16:05:33 -06001767
1768 // CBUFFER
1769 if (acceptTokenClass(EHTokCBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001770 storageQualifier = EvqUniform;
John Kessenich3d157c52016-07-25 16:05:33 -06001771 // TBUFFER
1772 else if (acceptTokenClass(EHTokTBuffer))
John Kessenichb804de62016-09-05 12:19:18 -06001773 storageQualifier = EvqBuffer;
John Kessenich27ffb292017-03-03 17:01:01 -07001774 // CLASS
John Kesseniche6e74942016-06-11 16:43:14 -06001775 // STRUCT
John Kessenich27ffb292017-03-03 17:01:01 -07001776 else if (! acceptTokenClass(EHTokClass) && ! acceptTokenClass(EHTokStruct))
John Kesseniche6e74942016-06-11 16:43:14 -06001777 return false;
1778
1779 // IDENTIFIER
1780 TString structName = "";
1781 if (peekTokenClass(EHTokIdentifier)) {
1782 structName = *token.string;
1783 advanceToken();
1784 }
1785
John Kessenich3d157c52016-07-25 16:05:33 -06001786 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06001787 TQualifier postDeclQualifier;
1788 postDeclQualifier.clear();
John Kessenich854fe242017-03-02 14:30:59 -07001789 bool postDeclsFound = acceptPostDecls(postDeclQualifier);
John Kessenich3d157c52016-07-25 16:05:33 -06001790
John Kessenichf3d88bd2017-03-19 12:24:29 -06001791 // LEFT_BRACE, or
John Kessenich854fe242017-03-02 14:30:59 -07001792 // struct_type IDENTIFIER
John Kesseniche6e74942016-06-11 16:43:14 -06001793 if (! acceptTokenClass(EHTokLeftBrace)) {
John Kessenich854fe242017-03-02 14:30:59 -07001794 if (structName.size() > 0 && !postDeclsFound && parseContext.lookupUserType(structName, type) != nullptr) {
1795 // struct_type IDENTIFIER
1796 return true;
1797 } else {
1798 expected("{");
1799 return false;
1800 }
John Kesseniche6e74942016-06-11 16:43:14 -06001801 }
1802
John Kessenichf3d88bd2017-03-19 12:24:29 -06001803
John Kesseniche6e74942016-06-11 16:43:14 -06001804 // struct_declaration_list
1805 TTypeList* typeList;
John Kessenichf3d88bd2017-03-19 12:24:29 -06001806 // Save each member function so they can be processed after we have a fully formed 'this'.
1807 TVector<TFunctionDeclarator> functionDeclarators;
1808
1809 parseContext.pushNamespace(structName);
1810 bool acceptedList = acceptStructDeclarationList(typeList, nodeList, structName, functionDeclarators);
1811 parseContext.popNamespace();
1812
1813 if (! acceptedList) {
John Kesseniche6e74942016-06-11 16:43:14 -06001814 expected("struct member declarations");
1815 return false;
1816 }
1817
1818 // RIGHT_BRACE
1819 if (! acceptTokenClass(EHTokRightBrace)) {
1820 expected("}");
1821 return false;
1822 }
1823
1824 // create the user-defined type
John Kessenichb804de62016-09-05 12:19:18 -06001825 if (storageQualifier == EvqTemporary)
John Kessenich3d157c52016-07-25 16:05:33 -06001826 new(&type) TType(typeList, structName);
John Kessenichb804de62016-09-05 12:19:18 -06001827 else {
John Kessenich7735b942016-09-05 12:40:06 -06001828 postDeclQualifier.storage = storageQualifier;
1829 new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
John Kessenichb804de62016-09-05 12:19:18 -06001830 }
John Kesseniche6e74942016-06-11 16:43:14 -06001831
John Kessenich727b3742017-02-03 17:57:55 -07001832 parseContext.declareStruct(token.loc, structName, type);
John Kesseniche6e74942016-06-11 16:43:14 -06001833
John Kessenichf3d88bd2017-03-19 12:24:29 -06001834 // All member functions get parsed inside the class/struct namespace and with the
1835 // class/struct members in a symbol-table level.
1836 parseContext.pushNamespace(structName);
1837 bool deferredSuccess = true;
1838 for (int b = 0; b < (int)functionDeclarators.size() && deferredSuccess; ++b) {
1839 // parse body
1840 pushTokenStream(functionDeclarators[b].body);
1841 if (! acceptFunctionBody(functionDeclarators[b], nodeList))
1842 deferredSuccess = false;
1843 popTokenStream();
1844 }
1845 parseContext.popNamespace();
1846
1847 return deferredSuccess;
John Kesseniche6e74942016-06-11 16:43:14 -06001848}
1849
steve-lunarg5da1f032017-02-12 17:50:28 -07001850// struct_buffer
1851// : APPENDSTRUCTUREDBUFFER
1852// | BYTEADDRESSBUFFER
1853// | CONSUMESTRUCTUREDBUFFER
1854// | RWBYTEADDRESSBUFFER
1855// | RWSTRUCTUREDBUFFER
1856// | STRUCTUREDBUFFER
1857bool HlslGrammar::acceptStructBufferType(TType& type)
1858{
1859 const EHlslTokenClass structBuffType = peek();
1860
1861 // TODO: globallycoherent
1862 bool hasTemplateType = true;
1863 bool readonly = false;
1864
1865 TStorageQualifier storage = EvqBuffer;
1866
1867 switch (structBuffType) {
1868 case EHTokAppendStructuredBuffer:
1869 unimplemented("AppendStructuredBuffer");
1870 return false;
1871 case EHTokByteAddressBuffer:
1872 hasTemplateType = false;
1873 readonly = true;
1874 break;
1875 case EHTokConsumeStructuredBuffer:
1876 unimplemented("ConsumeStructuredBuffer");
1877 return false;
1878 case EHTokRWByteAddressBuffer:
1879 hasTemplateType = false;
1880 break;
1881 case EHTokRWStructuredBuffer:
1882 break;
1883 case EHTokStructuredBuffer:
1884 readonly = true;
1885 break;
1886 default:
1887 return false; // not a structure buffer type
1888 }
1889
1890 advanceToken(); // consume the structure keyword
1891
1892 // type on which this StructedBuffer is templatized. E.g, StructedBuffer<MyStruct> ==> MyStruct
1893 TType* templateType = new TType;
1894
1895 if (hasTemplateType) {
1896 if (! acceptTokenClass(EHTokLeftAngle)) {
1897 expected("left angle bracket");
1898 return false;
1899 }
1900
1901 if (! acceptType(*templateType)) {
1902 expected("type");
1903 return false;
1904 }
1905 if (! acceptTokenClass(EHTokRightAngle)) {
1906 expected("right angle bracket");
1907 return false;
1908 }
1909 } else {
1910 // byte address buffers have no explicit type.
1911 TType uintType(EbtUint, storage);
1912 templateType->shallowCopy(uintType);
1913 }
1914
1915 // Create an unsized array out of that type.
1916 // TODO: does this work if it's already an array type?
1917 TArraySizes unsizedArray;
1918 unsizedArray.addInnerSize(UnsizedArraySize);
1919 templateType->newArraySizes(unsizedArray);
steve-lunarg40efe5c2017-03-06 12:01:44 -07001920 templateType->getQualifier().storage = storage;
steve-lunargdd8287a2017-02-23 18:04:12 -07001921
1922 // field name is canonical for all structbuffers
1923 templateType->setFieldName("@data");
steve-lunarg5da1f032017-02-12 17:50:28 -07001924
1925 // Create block type. TODO: hidden internal uint member when needed
steve-lunargdd8287a2017-02-23 18:04:12 -07001926
steve-lunarg5da1f032017-02-12 17:50:28 -07001927 TTypeList* blockStruct = new TTypeList;
1928 TTypeLoc member = { templateType, token.loc };
1929 blockStruct->push_back(member);
1930
steve-lunargdd8287a2017-02-23 18:04:12 -07001931 // This is the type of the buffer block (SSBO)
steve-lunarg5da1f032017-02-12 17:50:28 -07001932 TType blockType(blockStruct, "", templateType->getQualifier());
1933
steve-lunargdd8287a2017-02-23 18:04:12 -07001934 blockType.getQualifier().storage = storage;
1935 blockType.getQualifier().readonly = readonly;
1936
1937 // We may have created an equivalent type before, in which case we should use its
1938 // deep structure.
1939 parseContext.shareStructBufferType(blockType);
1940
steve-lunarg5da1f032017-02-12 17:50:28 -07001941 type.shallowCopy(blockType);
1942
1943 return true;
1944}
1945
John Kesseniche6e74942016-06-11 16:43:14 -06001946// struct_declaration_list
1947// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
1948//
1949// struct_declaration
1950// : fully_specified_type struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07001951// | fully_specified_type IDENTIFIER function_parameters post_decls compound_statement // member-function definition
John Kesseniche6e74942016-06-11 16:43:14 -06001952//
1953// struct_declarator
John Kessenich630dd7d2016-06-12 23:52:12 -06001954// : IDENTIFIER post_decls
1955// | IDENTIFIER array_specifier post_decls
John Kessenich54ee28f2017-03-11 14:13:00 -07001956// | IDENTIFIER function_parameters post_decls // member-function prototype
John Kesseniche6e74942016-06-11 16:43:14 -06001957//
John Kessenichf3d88bd2017-03-19 12:24:29 -06001958bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList, TIntermNode*& nodeList, const TString& typeName,
1959 TVector<TFunctionDeclarator>& declarators)
John Kesseniche6e74942016-06-11 16:43:14 -06001960{
1961 typeList = new TTypeList();
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001962 HlslToken idToken;
John Kesseniche6e74942016-06-11 16:43:14 -06001963
1964 do {
1965 // success on seeing the RIGHT_BRACE coming up
1966 if (peekTokenClass(EHTokRightBrace))
John Kessenichb16f7e62017-03-11 19:32:47 -07001967 break;
John Kesseniche6e74942016-06-11 16:43:14 -06001968
1969 // struct_declaration
John Kessenich54ee28f2017-03-11 14:13:00 -07001970
1971 bool declarator_list = false;
John Kesseniche6e74942016-06-11 16:43:14 -06001972
1973 // fully_specified_type
1974 TType memberType;
John Kessenich54ee28f2017-03-11 14:13:00 -07001975 if (! acceptFullySpecifiedType(memberType, nodeList)) {
John Kesseniche6e74942016-06-11 16:43:14 -06001976 expected("member type");
1977 return false;
1978 }
1979
1980 // struct_declarator COMMA struct_declarator ...
John Kessenich54ee28f2017-03-11 14:13:00 -07001981 bool functionDefinitionAccepted = false;
John Kesseniche6e74942016-06-11 16:43:14 -06001982 do {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07001983 if (! acceptIdentifier(idToken)) {
John Kesseniche6e74942016-06-11 16:43:14 -06001984 expected("member name");
1985 return false;
1986 }
1987
John Kessenich54ee28f2017-03-11 14:13:00 -07001988 if (peekTokenClass(EHTokLeftParen)) {
1989 // function_parameters
1990 if (!declarator_list) {
John Kessenichb16f7e62017-03-11 19:32:47 -07001991 declarators.resize(declarators.size() + 1);
1992 // request a token stream for deferred processing
John Kessenichf3d88bd2017-03-19 12:24:29 -06001993 functionDefinitionAccepted = acceptMemberFunctionDefinition(nodeList, memberType, *idToken.string,
1994 declarators.back());
John Kessenich54ee28f2017-03-11 14:13:00 -07001995 if (functionDefinitionAccepted)
1996 break;
1997 }
1998 expected("member-function definition");
1999 return false;
2000 } else {
2001 // add it to the list of members
2002 TTypeLoc member = { new TType(EbtVoid), token.loc };
2003 member.type->shallowCopy(memberType);
2004 member.type->setFieldName(*idToken.string);
2005 typeList->push_back(member);
John Kesseniche6e74942016-06-11 16:43:14 -06002006
John Kessenich54ee28f2017-03-11 14:13:00 -07002007 // array_specifier
2008 TArraySizes* arraySizes = nullptr;
2009 acceptArraySpecifier(arraySizes);
2010 if (arraySizes)
2011 typeList->back().type->newArraySizes(*arraySizes);
John Kesseniche6e74942016-06-11 16:43:14 -06002012
John Kessenich54ee28f2017-03-11 14:13:00 -07002013 acceptPostDecls(member.type->getQualifier());
John Kessenich630dd7d2016-06-12 23:52:12 -06002014
John Kessenich54ee28f2017-03-11 14:13:00 -07002015 // EQUAL assignment_expression
2016 if (acceptTokenClass(EHTokAssign)) {
2017 parseContext.warn(idToken.loc, "struct-member initializers ignored", "typedef", "");
2018 TIntermTyped* expressionNode = nullptr;
2019 if (! acceptAssignmentExpression(expressionNode)) {
2020 expected("initializer");
2021 return false;
2022 }
John Kessenich18adbdb2017-02-02 15:16:20 -07002023 }
2024 }
John Kesseniche6e74942016-06-11 16:43:14 -06002025 // success on seeing the SEMICOLON coming up
2026 if (peekTokenClass(EHTokSemicolon))
2027 break;
2028
2029 // COMMA
John Kessenich54ee28f2017-03-11 14:13:00 -07002030 if (acceptTokenClass(EHTokComma))
2031 declarator_list = true;
2032 else {
John Kesseniche6e74942016-06-11 16:43:14 -06002033 expected(",");
2034 return false;
2035 }
2036
2037 } while (true);
2038
2039 // SEMI_COLON
John Kessenich54ee28f2017-03-11 14:13:00 -07002040 if (! functionDefinitionAccepted && ! acceptTokenClass(EHTokSemicolon)) {
John Kesseniche6e74942016-06-11 16:43:14 -06002041 expected(";");
2042 return false;
2043 }
2044
2045 } while (true);
John Kessenichb16f7e62017-03-11 19:32:47 -07002046
John Kessenichb16f7e62017-03-11 19:32:47 -07002047 return true;
John Kesseniche6e74942016-06-11 16:43:14 -06002048}
2049
John Kessenich54ee28f2017-03-11 14:13:00 -07002050// member_function_definition
2051// | function_parameters post_decls compound_statement
2052//
2053// Expects type to have EvqGlobal for a static member and
2054// EvqTemporary for non-static member.
John Kessenichf3d88bd2017-03-19 12:24:29 -06002055bool HlslGrammar::acceptMemberFunctionDefinition(TIntermNode*& nodeList, const TType& type, const TString& memberName,
2056 TFunctionDeclarator& declarator)
John Kessenich54ee28f2017-03-11 14:13:00 -07002057{
John Kessenich54ee28f2017-03-11 14:13:00 -07002058 bool accepted = false;
2059
John Kessenichf3d88bd2017-03-19 12:24:29 -06002060 TString* functionName = parseContext.getFullNamespaceName(memberName);
John Kessenich088d52b2017-03-11 17:55:28 -07002061 declarator.function = new TFunction(functionName, type);
John Kessenich54ee28f2017-03-11 14:13:00 -07002062
2063 // function_parameters
John Kessenich088d52b2017-03-11 17:55:28 -07002064 if (acceptFunctionParameters(*declarator.function)) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002065 // post_decls
John Kessenich088d52b2017-03-11 17:55:28 -07002066 acceptPostDecls(declarator.function->getWritableType().getQualifier());
John Kessenich54ee28f2017-03-11 14:13:00 -07002067
2068 // compound_statement (function body definition)
2069 if (peekTokenClass(EHTokLeftBrace)) {
John Kessenich088d52b2017-03-11 17:55:28 -07002070 if (declarator.function->getType().getQualifier().storage != EvqGlobal) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002071 expected("only static member functions are accepted");
2072 return false;
2073 }
2074
John Kessenich088d52b2017-03-11 17:55:28 -07002075 declarator.loc = token.loc;
John Kessenichf3d88bd2017-03-19 12:24:29 -06002076 declarator.body = new TVector<HlslToken>;
2077 accepted = acceptFunctionDefinition(declarator, nodeList, declarator.body);
John Kessenich54ee28f2017-03-11 14:13:00 -07002078 }
2079 } else
2080 expected("function parameter list");
2081
John Kessenich54ee28f2017-03-11 14:13:00 -07002082 return accepted;
2083}
2084
John Kessenich5f934b02016-03-13 17:58:25 -06002085// function_parameters
John Kessenich078d7f22016-03-14 10:02:11 -06002086// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
John Kessenich71351de2016-06-08 12:50:56 -06002087// | LEFT_PAREN VOID RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002088//
2089bool HlslGrammar::acceptFunctionParameters(TFunction& function)
2090{
John Kessenich078d7f22016-03-14 10:02:11 -06002091 // LEFT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002092 if (! acceptTokenClass(EHTokLeftParen))
2093 return false;
2094
John Kessenich71351de2016-06-08 12:50:56 -06002095 // VOID RIGHT_PAREN
2096 if (! acceptTokenClass(EHTokVoid)) {
2097 do {
2098 // parameter_declaration
2099 if (! acceptParameterDeclaration(function))
2100 break;
John Kessenich5f934b02016-03-13 17:58:25 -06002101
John Kessenich71351de2016-06-08 12:50:56 -06002102 // COMMA
2103 if (! acceptTokenClass(EHTokComma))
2104 break;
2105 } while (true);
2106 }
John Kessenich5f934b02016-03-13 17:58:25 -06002107
John Kessenich078d7f22016-03-14 10:02:11 -06002108 // RIGHT_PAREN
John Kessenich5f934b02016-03-13 17:58:25 -06002109 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002110 expected(")");
John Kessenich5f934b02016-03-13 17:58:25 -06002111 return false;
2112 }
2113
2114 return true;
2115}
2116
steve-lunarg26d31452016-12-23 18:56:57 -07002117// default_parameter_declaration
2118// : EQUAL conditional_expression
2119// : EQUAL initializer
2120bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTyped*& node)
2121{
2122 node = nullptr;
2123
2124 // Valid not to have a default_parameter_declaration
2125 if (!acceptTokenClass(EHTokAssign))
2126 return true;
2127
2128 if (!acceptConditionalExpression(node)) {
2129 if (!acceptInitializer(node))
2130 return false;
2131
2132 // For initializer lists, we have to const-fold into a constructor for the type, so build
2133 // that.
2134 TFunction* constructor = parseContext.handleConstructorCall(token.loc, type);
2135 if (constructor == nullptr) // cannot construct
2136 return false;
2137
2138 TIntermTyped* arguments = nullptr;
John Kessenichecba76f2017-01-06 00:34:48 -07002139 for (int i = 0; i < int(node->getAsAggregate()->getSequence().size()); i++)
steve-lunarg26d31452016-12-23 18:56:57 -07002140 parseContext.handleFunctionArgument(constructor, arguments, node->getAsAggregate()->getSequence()[i]->getAsTyped());
John Kessenichecba76f2017-01-06 00:34:48 -07002141
steve-lunarg26d31452016-12-23 18:56:57 -07002142 node = parseContext.handleFunctionCall(token.loc, constructor, node);
2143 }
2144
2145 // If this is simply a constant, we can use it directly.
2146 if (node->getAsConstantUnion())
2147 return true;
2148
2149 // Otherwise, it has to be const-foldable.
2150 TIntermTyped* origNode = node;
2151
2152 node = intermediate.fold(node->getAsAggregate());
2153
2154 if (node != nullptr && origNode != node)
2155 return true;
2156
2157 parseContext.error(token.loc, "invalid default parameter value", "", "");
2158
2159 return false;
2160}
2161
John Kessenich5f934b02016-03-13 17:58:25 -06002162// parameter_declaration
steve-lunarg26d31452016-12-23 18:56:57 -07002163// : fully_specified_type post_decls [ = default_parameter_declaration ]
2164// | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
John Kessenich5f934b02016-03-13 17:58:25 -06002165//
2166bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
2167{
2168 // fully_specified_type
2169 TType* type = new TType;
2170 if (! acceptFullySpecifiedType(*type))
2171 return false;
2172
2173 // identifier
John Kessenichaecd4972016-03-14 10:46:34 -06002174 HlslToken idToken;
2175 acceptIdentifier(idToken);
John Kessenich5f934b02016-03-13 17:58:25 -06002176
John Kessenich19b92ff2016-06-19 11:50:34 -06002177 // array_specifier
2178 TArraySizes* arraySizes = nullptr;
2179 acceptArraySpecifier(arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002180 if (arraySizes) {
2181 if (arraySizes->isImplicit()) {
2182 parseContext.error(token.loc, "function parameter array cannot be implicitly sized", "", "");
2183 return false;
2184 }
2185
John Kessenich19b92ff2016-06-19 11:50:34 -06002186 type->newArraySizes(*arraySizes);
steve-lunarg265c0612016-09-27 10:57:35 -06002187 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002188
2189 // post_decls
John Kessenich7735b942016-09-05 12:40:06 -06002190 acceptPostDecls(type->getQualifier());
John Kessenichc3387d32016-06-17 14:21:02 -06002191
steve-lunarg26d31452016-12-23 18:56:57 -07002192 TIntermTyped* defaultValue;
2193 if (!acceptDefaultParameterDeclaration(*type, defaultValue))
2194 return false;
2195
John Kessenich5aa59e22016-06-17 15:50:47 -06002196 parseContext.paramFix(*type);
2197
steve-lunarg26d31452016-12-23 18:56:57 -07002198 // If any prior parameters have default values, all the parameters after that must as well.
2199 if (defaultValue == nullptr && function.getDefaultParamCount() > 0) {
2200 parseContext.error(idToken.loc, "invalid parameter after default value parameters", idToken.string->c_str(), "");
2201 return false;
2202 }
2203
2204 TParameter param = { idToken.string, type, defaultValue };
John Kessenich5f934b02016-03-13 17:58:25 -06002205 function.addParameter(param);
2206
2207 return true;
2208}
2209
2210// Do the work to create the function definition in addition to
2211// parsing the body (compound_statement).
John Kessenichb16f7e62017-03-11 19:32:47 -07002212//
2213// If 'deferredTokens' are passed in, just get the token stream,
2214// don't process.
2215//
2216bool HlslGrammar::acceptFunctionDefinition(TFunctionDeclarator& declarator, TIntermNode*& nodeList,
2217 TVector<HlslToken>* deferredTokens)
John Kessenich5f934b02016-03-13 17:58:25 -06002218{
John Kessenich088d52b2017-03-11 17:55:28 -07002219 parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, false /* not prototype */);
John Kessenich5f934b02016-03-13 17:58:25 -06002220
John Kessenichb16f7e62017-03-11 19:32:47 -07002221 if (deferredTokens)
2222 return captureBlockTokens(*deferredTokens);
2223 else
John Kessenich088d52b2017-03-11 17:55:28 -07002224 return acceptFunctionBody(declarator, nodeList);
2225}
2226
2227bool HlslGrammar::acceptFunctionBody(TFunctionDeclarator& declarator, TIntermNode*& nodeList)
2228{
2229 // we might get back an entry-point
John Kessenichca71d942017-03-07 20:44:09 -07002230 TIntermNode* entryPointNode = nullptr;
2231
John Kessenich077e0522016-06-09 02:02:17 -06002232 // This does a pushScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002233 TIntermNode* functionNode = parseContext.handleFunctionDefinition(declarator.loc, *declarator.function,
2234 declarator.attributes, entryPointNode);
John Kessenich5f934b02016-03-13 17:58:25 -06002235
2236 // compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002237 TIntermNode* functionBody = nullptr;
John Kessenich02467d82017-01-19 15:41:47 -07002238 if (! acceptCompoundStatement(functionBody))
2239 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002240
John Kessenich54ee28f2017-03-11 14:13:00 -07002241 // this does a popScope()
John Kessenich088d52b2017-03-11 17:55:28 -07002242 parseContext.handleFunctionBody(declarator.loc, *declarator.function, functionBody, functionNode);
John Kessenichca71d942017-03-07 20:44:09 -07002243
2244 // Hook up the 1 or 2 function definitions.
2245 nodeList = intermediate.growAggregate(nodeList, functionNode);
2246 nodeList = intermediate.growAggregate(nodeList, entryPointNode);
John Kessenich02467d82017-01-19 15:41:47 -07002247
2248 return true;
John Kessenich5f934b02016-03-13 17:58:25 -06002249}
2250
John Kessenich0d2b6de2016-06-05 11:23:11 -06002251// Accept an expression with parenthesis around it, where
2252// the parenthesis ARE NOT expression parenthesis, but the
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002253// syntactically required ones like in "if ( expression )".
2254//
2255// Also accepts a declaration expression; "if (int a = expression)".
John Kessenich0d2b6de2016-06-05 11:23:11 -06002256//
2257// Note this one is not set up to be speculative; as it gives
2258// errors if not found.
2259//
2260bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
2261{
2262 // LEFT_PAREN
2263 if (! acceptTokenClass(EHTokLeftParen))
2264 expected("(");
2265
John Kessenich5bc4d9a2016-06-20 01:22:38 -06002266 bool decl = false;
2267 TIntermNode* declNode = nullptr;
2268 decl = acceptControlDeclaration(declNode);
2269 if (decl) {
2270 if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
2271 expected("initialized declaration");
2272 return false;
2273 } else
2274 expression = declNode->getAsTyped();
2275 } else {
2276 // no declaration
2277 if (! acceptExpression(expression)) {
2278 expected("expression");
2279 return false;
2280 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06002281 }
2282
2283 // RIGHT_PAREN
2284 if (! acceptTokenClass(EHTokRightParen))
2285 expected(")");
2286
2287 return true;
2288}
2289
John Kessenich34fb0362016-05-03 23:17:20 -06002290// The top-level full expression recognizer.
2291//
John Kessenich87142c72016-03-12 20:24:24 -07002292// expression
John Kessenich34fb0362016-05-03 23:17:20 -06002293// : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
John Kessenich87142c72016-03-12 20:24:24 -07002294//
2295bool HlslGrammar::acceptExpression(TIntermTyped*& node)
2296{
LoopDawgef764a22016-06-03 09:17:51 -06002297 node = nullptr;
2298
John Kessenich34fb0362016-05-03 23:17:20 -06002299 // assignment_expression
2300 if (! acceptAssignmentExpression(node))
2301 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002302
John Kessenich34fb0362016-05-03 23:17:20 -06002303 if (! peekTokenClass(EHTokComma))
2304 return true;
2305
2306 do {
2307 // ... COMMA
John Kessenich5f934b02016-03-13 17:58:25 -06002308 TSourceLoc loc = token.loc;
John Kessenich34fb0362016-05-03 23:17:20 -06002309 advanceToken();
John Kessenich5f934b02016-03-13 17:58:25 -06002310
John Kessenich34fb0362016-05-03 23:17:20 -06002311 // ... assignment_expression
2312 TIntermTyped* rightNode = nullptr;
2313 if (! acceptAssignmentExpression(rightNode)) {
2314 expected("assignment expression");
2315 return false;
John Kessenich5f934b02016-03-13 17:58:25 -06002316 }
2317
John Kessenich34fb0362016-05-03 23:17:20 -06002318 node = intermediate.addComma(node, rightNode, loc);
2319
2320 if (! peekTokenClass(EHTokComma))
2321 return true;
2322 } while (true);
2323}
2324
John Kessenich07354242016-07-01 19:58:06 -06002325// initializer
John Kessenich98ad4852016-11-27 17:39:07 -07002326// : LEFT_BRACE RIGHT_BRACE
2327// | LEFT_BRACE initializer_list RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002328//
2329// initializer_list
2330// : assignment_expression COMMA assignment_expression COMMA ...
2331//
2332bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
2333{
2334 // LEFT_BRACE
2335 if (! acceptTokenClass(EHTokLeftBrace))
2336 return false;
2337
John Kessenich98ad4852016-11-27 17:39:07 -07002338 // RIGHT_BRACE
John Kessenich07354242016-07-01 19:58:06 -06002339 TSourceLoc loc = token.loc;
John Kessenich98ad4852016-11-27 17:39:07 -07002340 if (acceptTokenClass(EHTokRightBrace)) {
2341 // a zero-length initializer list
2342 node = intermediate.makeAggregate(loc);
2343 return true;
2344 }
2345
2346 // initializer_list
John Kessenich07354242016-07-01 19:58:06 -06002347 node = nullptr;
2348 do {
2349 // assignment_expression
2350 TIntermTyped* expr;
2351 if (! acceptAssignmentExpression(expr)) {
2352 expected("assignment expression in initializer list");
2353 return false;
2354 }
2355 node = intermediate.growAggregate(node, expr, loc);
2356
2357 // COMMA
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002358 if (acceptTokenClass(EHTokComma)) {
2359 if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
2360 return true;
John Kessenich07354242016-07-01 19:58:06 -06002361 continue;
steve-lunargfe5a3ff2016-07-30 10:36:09 -06002362 }
John Kessenich07354242016-07-01 19:58:06 -06002363
2364 // RIGHT_BRACE
2365 if (acceptTokenClass(EHTokRightBrace))
2366 return true;
2367
2368 expected(", or }");
2369 return false;
2370 } while (true);
2371}
2372
John Kessenich34fb0362016-05-03 23:17:20 -06002373// Accept an assignment expression, where assignment operations
John Kessenich07354242016-07-01 19:58:06 -06002374// associate right-to-left. That is, it is implicit, for example
John Kessenich34fb0362016-05-03 23:17:20 -06002375//
2376// a op (b op (c op d))
2377//
2378// assigment_expression
John Kessenich00957f82016-07-27 10:39:57 -06002379// : initializer
2380// | conditional_expression
2381// | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
John Kessenich34fb0362016-05-03 23:17:20 -06002382//
2383bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
2384{
John Kessenich07354242016-07-01 19:58:06 -06002385 // initializer
2386 if (peekTokenClass(EHTokLeftBrace)) {
2387 if (acceptInitializer(node))
2388 return true;
2389
2390 expected("initializer");
2391 return false;
2392 }
2393
John Kessenich00957f82016-07-27 10:39:57 -06002394 // conditional_expression
2395 if (! acceptConditionalExpression(node))
John Kessenich34fb0362016-05-03 23:17:20 -06002396 return false;
2397
John Kessenich07354242016-07-01 19:58:06 -06002398 // assignment operation?
John Kessenich34fb0362016-05-03 23:17:20 -06002399 TOperator assignOp = HlslOpMap::assignment(peek());
2400 if (assignOp == EOpNull)
2401 return true;
2402
John Kessenich00957f82016-07-27 10:39:57 -06002403 // assign_op
John Kessenich34fb0362016-05-03 23:17:20 -06002404 TSourceLoc loc = token.loc;
2405 advanceToken();
2406
John Kessenich00957f82016-07-27 10:39:57 -06002407 // conditional_expression assign_op conditional_expression ...
2408 // Done by recursing this function, which automatically
John Kessenich34fb0362016-05-03 23:17:20 -06002409 // gets the right-to-left associativity.
2410 TIntermTyped* rightNode = nullptr;
2411 if (! acceptAssignmentExpression(rightNode)) {
2412 expected("assignment expression");
John Kessenich5f934b02016-03-13 17:58:25 -06002413 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002414 }
2415
John Kessenichd21baed2016-09-16 03:05:12 -06002416 node = parseContext.handleAssign(loc, assignOp, node, rightNode);
steve-lunarg90707962016-10-07 19:35:40 -06002417 node = parseContext.handleLvalue(loc, "assign", node);
2418
John Kessenichfea226b2016-07-28 17:53:56 -06002419 if (node == nullptr) {
2420 parseContext.error(loc, "could not create assignment", "", "");
2421 return false;
2422 }
John Kessenich34fb0362016-05-03 23:17:20 -06002423
2424 if (! peekTokenClass(EHTokComma))
2425 return true;
2426
2427 return true;
2428}
2429
John Kessenich00957f82016-07-27 10:39:57 -06002430// Accept a conditional expression, which associates right-to-left,
2431// accomplished by the "true" expression calling down to lower
2432// precedence levels than this level.
2433//
2434// conditional_expression
2435// : binary_expression
2436// | binary_expression QUESTION expression COLON assignment_expression
2437//
2438bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
2439{
2440 // binary_expression
2441 if (! acceptBinaryExpression(node, PlLogicalOr))
2442 return false;
2443
2444 if (! acceptTokenClass(EHTokQuestion))
2445 return true;
2446
2447 TIntermTyped* trueNode = nullptr;
2448 if (! acceptExpression(trueNode)) {
2449 expected("expression after ?");
2450 return false;
2451 }
2452 TSourceLoc loc = token.loc;
2453
2454 if (! acceptTokenClass(EHTokColon)) {
2455 expected(":");
2456 return false;
2457 }
2458
2459 TIntermTyped* falseNode = nullptr;
2460 if (! acceptAssignmentExpression(falseNode)) {
2461 expected("expression after :");
2462 return false;
2463 }
2464
2465 node = intermediate.addSelection(node, trueNode, falseNode, loc);
2466
2467 return true;
2468}
2469
John Kessenich34fb0362016-05-03 23:17:20 -06002470// Accept a binary expression, for binary operations that
2471// associate left-to-right. This is, it is implicit, for example
2472//
2473// ((a op b) op c) op d
2474//
2475// binary_expression
2476// : expression op expression op expression ...
2477//
2478// where 'expression' is the next higher level in precedence.
2479//
2480bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
2481{
2482 if (precedenceLevel > PlMul)
2483 return acceptUnaryExpression(node);
2484
2485 // assignment_expression
2486 if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
2487 return false;
2488
John Kessenich34fb0362016-05-03 23:17:20 -06002489 do {
John Kessenich64076ed2016-07-28 21:43:17 -06002490 TOperator op = HlslOpMap::binary(peek());
2491 PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
2492 if (tokenLevel < precedenceLevel)
2493 return true;
2494
John Kessenich34fb0362016-05-03 23:17:20 -06002495 // ... op
2496 TSourceLoc loc = token.loc;
2497 advanceToken();
2498
2499 // ... expression
2500 TIntermTyped* rightNode = nullptr;
2501 if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
2502 expected("expression");
2503 return false;
2504 }
2505
2506 node = intermediate.addBinaryMath(op, node, rightNode, loc);
John Kessenichfea226b2016-07-28 17:53:56 -06002507 if (node == nullptr) {
2508 parseContext.error(loc, "Could not perform requested binary operation", "", "");
2509 return false;
2510 }
John Kessenich34fb0362016-05-03 23:17:20 -06002511 } while (true);
2512}
2513
2514// unary_expression
John Kessenich1cc1a282016-06-03 16:55:49 -06002515// : (type) unary_expression
2516// | + unary_expression
John Kessenich34fb0362016-05-03 23:17:20 -06002517// | - unary_expression
2518// | ! unary_expression
2519// | ~ unary_expression
2520// | ++ unary_expression
2521// | -- unary_expression
2522// | postfix_expression
2523//
2524bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
2525{
John Kessenich1cc1a282016-06-03 16:55:49 -06002526 // (type) unary_expression
2527 // Have to look two steps ahead, because this could be, e.g., a
2528 // postfix_expression instead, since that also starts with at "(".
2529 if (acceptTokenClass(EHTokLeftParen)) {
2530 TType castType;
2531 if (acceptType(castType)) {
steve-lunarg5964c642016-07-30 07:38:55 -06002532 if (acceptTokenClass(EHTokRightParen)) {
2533 // We've matched "(type)" now, get the expression to cast
2534 TSourceLoc loc = token.loc;
2535 if (! acceptUnaryExpression(node))
2536 return false;
2537
2538 // Hook it up like a constructor
2539 TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType);
2540 if (constructorFunction == nullptr) {
2541 expected("type that can be constructed");
2542 return false;
2543 }
2544 TIntermTyped* arguments = nullptr;
2545 parseContext.handleFunctionArgument(constructorFunction, arguments, node);
2546 node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
2547
2548 return true;
2549 } else {
2550 // This could be a parenthesized constructor, ala (int(3)), and we just accepted
2551 // the '(int' part. We must back up twice.
2552 recedeToken();
2553 recedeToken();
John Kessenich1cc1a282016-06-03 16:55:49 -06002554 }
John Kessenich1cc1a282016-06-03 16:55:49 -06002555 } else {
2556 // This isn't a type cast, but it still started "(", so if it is a
2557 // unary expression, it can only be a postfix_expression, so try that.
2558 // Back it up first.
2559 recedeToken();
2560 return acceptPostfixExpression(node);
2561 }
2562 }
2563
2564 // peek for "op unary_expression"
John Kessenich34fb0362016-05-03 23:17:20 -06002565 TOperator unaryOp = HlslOpMap::preUnary(peek());
John Kessenichecba76f2017-01-06 00:34:48 -07002566
John Kessenich1cc1a282016-06-03 16:55:49 -06002567 // postfix_expression (if no unary operator)
John Kessenich34fb0362016-05-03 23:17:20 -06002568 if (unaryOp == EOpNull)
2569 return acceptPostfixExpression(node);
2570
2571 // op unary_expression
2572 TSourceLoc loc = token.loc;
2573 advanceToken();
2574 if (! acceptUnaryExpression(node))
2575 return false;
2576
2577 // + is a no-op
2578 if (unaryOp == EOpAdd)
2579 return true;
2580
2581 node = intermediate.addUnaryMath(unaryOp, node, loc);
steve-lunarge5921f12016-10-15 10:29:58 -06002582
2583 // These unary ops require lvalues
2584 if (unaryOp == EOpPreIncrement || unaryOp == EOpPreDecrement)
2585 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002586
2587 return node != nullptr;
2588}
2589
2590// postfix_expression
2591// : LEFT_PAREN expression RIGHT_PAREN
2592// | literal
2593// | constructor
2594// | identifier
2595// | function_call
2596// | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
2597// | postfix_expression DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002598// | postfix_expression DOT IDENTIFIER arguments
John Kessenich54ee28f2017-03-11 14:13:00 -07002599// | postfix_expression COLONCOLON IDENTIFIER arguments
John Kessenich34fb0362016-05-03 23:17:20 -06002600// | postfix_expression INC_OP
2601// | postfix_expression DEC_OP
2602//
2603bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
2604{
2605 // Not implemented as self-recursive:
John Kessenich54ee28f2017-03-11 14:13:00 -07002606 // The logical "right recursion" is done with a loop at the end
John Kessenich34fb0362016-05-03 23:17:20 -06002607
2608 // idToken will pick up either a variable or a function name in a function call
2609 HlslToken idToken;
2610
John Kessenich54ee28f2017-03-11 14:13:00 -07002611 // scopeBase will pick up the type symbol on the left of '::'
2612 TSymbol* scopeBase = nullptr;
2613
John Kessenich21472ae2016-06-04 11:46:33 -06002614 // Find something before the postfix operations, as they can't operate
2615 // on nothing. So, no "return true", they fall through, only "return false".
John Kessenich87142c72016-03-12 20:24:24 -07002616 if (acceptTokenClass(EHTokLeftParen)) {
John Kessenich21472ae2016-06-04 11:46:33 -06002617 // LEFT_PAREN expression RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002618 if (! acceptExpression(node)) {
2619 expected("expression");
2620 return false;
2621 }
2622 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002623 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002624 return false;
2625 }
John Kessenich34fb0362016-05-03 23:17:20 -06002626 } else if (acceptLiteral(node)) {
John Kessenichecba76f2017-01-06 00:34:48 -07002627 // literal (nothing else to do yet), go on to the
John Kessenich34fb0362016-05-03 23:17:20 -06002628 } else if (acceptConstructor(node)) {
2629 // constructor (nothing else to do yet)
2630 } else if (acceptIdentifier(idToken)) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002631 // user-type, identifier, or function name
2632 if (peekTokenClass(EHTokColonColon)) {
2633 TType type;
2634 scopeBase = parseContext.lookupUserType(*idToken.string, type);
2635 if (scopeBase == nullptr) {
2636 expected("type left of ::");
2637 return false;
2638 }
2639 } else if (! peekTokenClass(EHTokLeftParen)) {
steve-lunarga64ed3e2016-12-18 17:51:14 -07002640 node = parseContext.handleVariable(idToken.loc, idToken.symbol, idToken.string);
John Kessenich34fb0362016-05-03 23:17:20 -06002641 } else if (acceptFunctionCall(idToken, node)) {
2642 // function_call (nothing else to do yet)
2643 } else {
2644 expected("function call arguments");
2645 return false;
2646 }
John Kessenich21472ae2016-06-04 11:46:33 -06002647 } else {
2648 // nothing found, can't post operate
2649 return false;
John Kessenich87142c72016-03-12 20:24:24 -07002650 }
2651
steve-lunarga2b01a02016-11-28 17:09:54 -07002652 // This is to guarantee we do this no matter how we get out of the stack frame.
2653 // This way there's no bug if an early return forgets to do it.
2654 struct tFinalize {
2655 tFinalize(HlslParseContext& p) : parseContext(p) { }
2656 ~tFinalize() { parseContext.finalizeFlattening(); }
John Kessenichf8d0d8c2017-02-08 17:31:03 -07002657 HlslParseContext& parseContext;
John Kessenich32fd5d22017-02-02 14:55:02 -07002658 private:
John Kessenichca71d942017-03-07 20:44:09 -07002659 const tFinalize& operator=(const tFinalize&) { return *this; }
John Kessenichefeefd92017-03-01 13:12:26 -07002660 tFinalize(const tFinalize& f) : parseContext(f.parseContext) { }
steve-lunarga2b01a02016-11-28 17:09:54 -07002661 } finalize(parseContext);
2662
2663 // Initialize the flattening accumulation data, so we can track data across multiple bracket or
2664 // dot operators. This can also be nested, e.g, for [], so we have to track each nesting
2665 // level: hence the init and finalize. Even though in practice these must be
2666 // constants, they are parsed no matter what.
2667 parseContext.initFlattening();
2668
John Kessenich21472ae2016-06-04 11:46:33 -06002669 // Something was found, chain as many postfix operations as exist.
John Kessenich34fb0362016-05-03 23:17:20 -06002670 do {
2671 TSourceLoc loc = token.loc;
2672 TOperator postOp = HlslOpMap::postUnary(peek());
John Kessenich87142c72016-03-12 20:24:24 -07002673
John Kessenich34fb0362016-05-03 23:17:20 -06002674 // Consume only a valid post-unary operator, otherwise we are done.
2675 switch (postOp) {
2676 case EOpIndexDirectStruct:
2677 case EOpIndexIndirect:
2678 case EOpPostIncrement:
2679 case EOpPostDecrement:
John Kessenich54ee28f2017-03-11 14:13:00 -07002680 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002681 advanceToken();
2682 break;
2683 default:
2684 return true;
2685 }
John Kessenich87142c72016-03-12 20:24:24 -07002686
John Kessenich34fb0362016-05-03 23:17:20 -06002687 // We have a valid post-unary operator, process it.
2688 switch (postOp) {
John Kessenich54ee28f2017-03-11 14:13:00 -07002689 case EOpScoping:
John Kessenich34fb0362016-05-03 23:17:20 -06002690 case EOpIndexDirectStruct:
John Kessenich93a162a2016-06-17 17:16:27 -06002691 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002692 // DOT IDENTIFIER
John Kessenich516d92d2017-03-08 20:09:03 -07002693 // includes swizzles, member variables, and member functions
John Kessenich93a162a2016-06-17 17:16:27 -06002694 HlslToken field;
2695 if (! acceptIdentifier(field)) {
2696 expected("swizzle or member");
2697 return false;
2698 }
LoopDawg4886f692016-06-29 10:58:58 -06002699
John Kessenich516d92d2017-03-08 20:09:03 -07002700 if (peekTokenClass(EHTokLeftParen)) {
2701 // member function
2702 TIntermTyped* thisNode = node;
LoopDawg4886f692016-06-29 10:58:58 -06002703
John Kessenich516d92d2017-03-08 20:09:03 -07002704 // arguments
John Kessenich54ee28f2017-03-11 14:13:00 -07002705 if (! acceptFunctionCall(field, node, thisNode, scopeBase)) {
LoopDawg4886f692016-06-29 10:58:58 -06002706 expected("function parameters");
2707 return false;
2708 }
John Kessenich516d92d2017-03-08 20:09:03 -07002709 } else
2710 node = parseContext.handleDotDereference(field.loc, node, *field.string);
LoopDawg4886f692016-06-29 10:58:58 -06002711
John Kessenich34fb0362016-05-03 23:17:20 -06002712 break;
John Kessenich93a162a2016-06-17 17:16:27 -06002713 }
John Kessenich34fb0362016-05-03 23:17:20 -06002714 case EOpIndexIndirect:
2715 {
John Kessenich19b92ff2016-06-19 11:50:34 -06002716 // LEFT_BRACKET integer_expression RIGHT_BRACKET
John Kessenich34fb0362016-05-03 23:17:20 -06002717 TIntermTyped* indexNode = nullptr;
2718 if (! acceptExpression(indexNode) ||
2719 ! peekTokenClass(EHTokRightBracket)) {
2720 expected("expression followed by ']'");
2721 return false;
2722 }
John Kessenich19b92ff2016-06-19 11:50:34 -06002723 advanceToken();
2724 node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
2725 break;
John Kessenich34fb0362016-05-03 23:17:20 -06002726 }
2727 case EOpPostIncrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002728 // INC_OP
2729 // fall through
John Kessenich34fb0362016-05-03 23:17:20 -06002730 case EOpPostDecrement:
John Kessenich19b92ff2016-06-19 11:50:34 -06002731 // DEC_OP
John Kessenich34fb0362016-05-03 23:17:20 -06002732 node = intermediate.addUnaryMath(postOp, node, loc);
steve-lunarg07830e82016-10-10 10:00:14 -06002733 node = parseContext.handleLvalue(loc, "unary operator", node);
John Kessenich34fb0362016-05-03 23:17:20 -06002734 break;
2735 default:
2736 assert(0);
2737 break;
2738 }
2739 } while (true);
John Kessenich87142c72016-03-12 20:24:24 -07002740}
2741
John Kessenichd016be12016-03-13 11:24:20 -06002742// constructor
John Kessenich078d7f22016-03-14 10:02:11 -06002743// : type argument_list
John Kessenichd016be12016-03-13 11:24:20 -06002744//
2745bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
2746{
2747 // type
2748 TType type;
2749 if (acceptType(type)) {
2750 TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type);
2751 if (constructorFunction == nullptr)
2752 return false;
2753
2754 // arguments
John Kessenich4678ca92016-05-13 09:33:42 -06002755 TIntermTyped* arguments = nullptr;
John Kessenichd016be12016-03-13 11:24:20 -06002756 if (! acceptArguments(constructorFunction, arguments)) {
steve-lunarg5ca85ad2016-12-26 18:45:52 -07002757 // It's possible this is a type keyword used as an identifier. Put the token back
2758 // for later use.
2759 recedeToken();
John Kessenichd016be12016-03-13 11:24:20 -06002760 return false;
2761 }
2762
2763 // hook it up
2764 node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
2765
2766 return true;
2767 }
2768
2769 return false;
2770}
2771
John Kessenich34fb0362016-05-03 23:17:20 -06002772// The function_call identifier was already recognized, and passed in as idToken.
2773//
2774// function_call
2775// : [idToken] arguments
2776//
John Kessenich54ee28f2017-03-11 14:13:00 -07002777bool HlslGrammar::acceptFunctionCall(HlslToken callToken, TIntermTyped*& node, TIntermTyped* baseObject,
2778 const TSymbol* baseType)
John Kessenich34fb0362016-05-03 23:17:20 -06002779{
John Kessenich54ee28f2017-03-11 14:13:00 -07002780 // name
2781 TString* functionName = nullptr;
2782 if ((baseObject == nullptr && baseType == nullptr)
2783 || parseContext.isBuiltInMethod(callToken.loc, baseObject, *callToken.string))
2784 functionName = callToken.string;
2785 else {
2786 functionName = NewPoolTString("");
John Kessenichf3d88bd2017-03-19 12:24:29 -06002787 functionName->append(baseType->getType().getTypeName());
2788 parseContext.addScopeMangler(*functionName);
John Kessenich54ee28f2017-03-11 14:13:00 -07002789 functionName->append(*callToken.string);
John Kessenich5f12d2f2017-03-11 09:39:55 -07002790 }
LoopDawg4886f692016-06-29 10:58:58 -06002791
John Kessenich54ee28f2017-03-11 14:13:00 -07002792 // function
2793 TFunction* function = new TFunction(functionName, TType(EbtVoid));
2794
2795 // arguments
John Kessenich54ee28f2017-03-11 14:13:00 -07002796 TIntermTyped* arguments = nullptr;
John Kessenichdfbdd9e2017-03-19 13:10:28 -06002797 if (baseObject != nullptr) {
2798 // Non-static member functions have an implicit first argument of the base object.
John Kessenich54ee28f2017-03-11 14:13:00 -07002799 parseContext.handleFunctionArgument(function, arguments, baseObject);
John Kessenichdfbdd9e2017-03-19 13:10:28 -06002800 }
John Kessenich4678ca92016-05-13 09:33:42 -06002801 if (! acceptArguments(function, arguments))
2802 return false;
2803
John Kessenich54ee28f2017-03-11 14:13:00 -07002804 // call
John Kessenich5f12d2f2017-03-11 09:39:55 -07002805 node = parseContext.handleFunctionCall(callToken.loc, function, arguments);
John Kessenich4678ca92016-05-13 09:33:42 -06002806
2807 return true;
John Kessenich34fb0362016-05-03 23:17:20 -06002808}
2809
John Kessenich87142c72016-03-12 20:24:24 -07002810// arguments
John Kessenich078d7f22016-03-14 10:02:11 -06002811// : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002812//
John Kessenichd016be12016-03-13 11:24:20 -06002813// The arguments are pushed onto the 'function' argument list and
2814// onto the 'arguments' aggregate.
2815//
John Kessenich4678ca92016-05-13 09:33:42 -06002816bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
John Kessenich87142c72016-03-12 20:24:24 -07002817{
John Kessenich078d7f22016-03-14 10:02:11 -06002818 // LEFT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002819 if (! acceptTokenClass(EHTokLeftParen))
2820 return false;
2821
2822 do {
John Kessenichd016be12016-03-13 11:24:20 -06002823 // expression
John Kessenich87142c72016-03-12 20:24:24 -07002824 TIntermTyped* arg;
John Kessenich4678ca92016-05-13 09:33:42 -06002825 if (! acceptAssignmentExpression(arg))
John Kessenich87142c72016-03-12 20:24:24 -07002826 break;
John Kessenichd016be12016-03-13 11:24:20 -06002827
2828 // hook it up
2829 parseContext.handleFunctionArgument(function, arguments, arg);
2830
John Kessenich078d7f22016-03-14 10:02:11 -06002831 // COMMA
John Kessenich87142c72016-03-12 20:24:24 -07002832 if (! acceptTokenClass(EHTokComma))
2833 break;
2834 } while (true);
2835
John Kessenich078d7f22016-03-14 10:02:11 -06002836 // RIGHT_PAREN
John Kessenich87142c72016-03-12 20:24:24 -07002837 if (! acceptTokenClass(EHTokRightParen)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06002838 expected(")");
John Kessenich87142c72016-03-12 20:24:24 -07002839 return false;
2840 }
2841
2842 return true;
2843}
2844
2845bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
2846{
2847 switch (token.tokenClass) {
2848 case EHTokIntConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002849 node = intermediate.addConstantUnion(token.i, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002850 break;
steve-lunarg2de32912016-07-28 14:49:48 -06002851 case EHTokUintConstant:
2852 node = intermediate.addConstantUnion(token.u, token.loc, true);
2853 break;
John Kessenich87142c72016-03-12 20:24:24 -07002854 case EHTokFloatConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002855 node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002856 break;
2857 case EHTokDoubleConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002858 node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002859 break;
2860 case EHTokBoolConstant:
John Kessenich078d7f22016-03-14 10:02:11 -06002861 node = intermediate.addConstantUnion(token.b, token.loc, true);
John Kessenich87142c72016-03-12 20:24:24 -07002862 break;
John Kessenich86f71382016-09-19 20:23:18 -06002863 case EHTokStringConstant:
steve-lunarg858c9282017-01-07 08:54:10 -07002864 node = intermediate.addConstantUnion(token.string, token.loc, true);
John Kessenich86f71382016-09-19 20:23:18 -06002865 break;
John Kessenich87142c72016-03-12 20:24:24 -07002866
2867 default:
2868 return false;
2869 }
2870
2871 advanceToken();
2872
2873 return true;
2874}
2875
John Kessenich5f934b02016-03-13 17:58:25 -06002876// compound_statement
John Kessenich34fb0362016-05-03 23:17:20 -06002877// : LEFT_CURLY statement statement ... RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002878//
John Kessenich21472ae2016-06-04 11:46:33 -06002879bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
John Kessenich87142c72016-03-12 20:24:24 -07002880{
John Kessenich21472ae2016-06-04 11:46:33 -06002881 TIntermAggregate* compoundStatement = nullptr;
2882
John Kessenich34fb0362016-05-03 23:17:20 -06002883 // LEFT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002884 if (! acceptTokenClass(EHTokLeftBrace))
2885 return false;
2886
2887 // statement statement ...
2888 TIntermNode* statement = nullptr;
2889 while (acceptStatement(statement)) {
John Kessenichd02dc5d2016-07-01 00:04:11 -06002890 TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
2891 if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
2892 branch->getFlowOp() == EOpDefault)) {
2893 // hook up individual subsequences within a switch statement
2894 parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
2895 compoundStatement = nullptr;
2896 } else {
2897 // hook it up to the growing compound statement
2898 compoundStatement = intermediate.growAggregate(compoundStatement, statement);
2899 }
John Kessenich5f934b02016-03-13 17:58:25 -06002900 }
John Kessenich34fb0362016-05-03 23:17:20 -06002901 if (compoundStatement)
2902 compoundStatement->setOperator(EOpSequence);
John Kessenich5f934b02016-03-13 17:58:25 -06002903
John Kessenich21472ae2016-06-04 11:46:33 -06002904 retStatement = compoundStatement;
2905
John Kessenich34fb0362016-05-03 23:17:20 -06002906 // RIGHT_CURLY
John Kessenich5f934b02016-03-13 17:58:25 -06002907 return acceptTokenClass(EHTokRightBrace);
2908}
2909
John Kessenich0d2b6de2016-06-05 11:23:11 -06002910bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
2911{
2912 parseContext.pushScope();
John Kessenich077e0522016-06-09 02:02:17 -06002913 bool result = acceptStatement(statement);
John Kessenich0d2b6de2016-06-05 11:23:11 -06002914 parseContext.popScope();
2915
2916 return result;
2917}
2918
John Kessenich077e0522016-06-09 02:02:17 -06002919bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
John Kessenich0d2b6de2016-06-05 11:23:11 -06002920{
John Kessenich077e0522016-06-09 02:02:17 -06002921 parseContext.pushScope();
2922 bool result = acceptCompoundStatement(statement);
2923 parseContext.popScope();
John Kessenich0d2b6de2016-06-05 11:23:11 -06002924
2925 return result;
2926}
2927
John Kessenich5f934b02016-03-13 17:58:25 -06002928// statement
John Kessenich21472ae2016-06-04 11:46:33 -06002929// : attributes attributed_statement
2930//
2931// attributed_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002932// : compound_statement
John Kessenich21472ae2016-06-04 11:46:33 -06002933// | SEMICOLON
John Kessenich078d7f22016-03-14 10:02:11 -06002934// | expression SEMICOLON
John Kessenich21472ae2016-06-04 11:46:33 -06002935// | declaration_statement
2936// | selection_statement
2937// | switch_statement
2938// | case_label
2939// | iteration_statement
2940// | jump_statement
John Kessenich5f934b02016-03-13 17:58:25 -06002941//
2942bool HlslGrammar::acceptStatement(TIntermNode*& statement)
2943{
John Kessenich21472ae2016-06-04 11:46:33 -06002944 statement = nullptr;
John Kessenich5f934b02016-03-13 17:58:25 -06002945
John Kessenich21472ae2016-06-04 11:46:33 -06002946 // attributes
steve-lunarg1868b142016-10-20 13:07:10 -06002947 TAttributeMap attributes;
2948 acceptAttributes(attributes);
John Kessenich5f934b02016-03-13 17:58:25 -06002949
John Kessenich21472ae2016-06-04 11:46:33 -06002950 // attributed_statement
2951 switch (peek()) {
2952 case EHTokLeftBrace:
John Kessenich077e0522016-06-09 02:02:17 -06002953 return acceptScopedCompoundStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002954
John Kessenich21472ae2016-06-04 11:46:33 -06002955 case EHTokIf:
2956 return acceptSelectionStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002957
John Kessenich21472ae2016-06-04 11:46:33 -06002958 case EHTokSwitch:
2959 return acceptSwitchStatement(statement);
John Kessenich5f934b02016-03-13 17:58:25 -06002960
John Kessenich21472ae2016-06-04 11:46:33 -06002961 case EHTokFor:
2962 case EHTokDo:
2963 case EHTokWhile:
2964 return acceptIterationStatement(statement);
2965
2966 case EHTokContinue:
2967 case EHTokBreak:
2968 case EHTokDiscard:
2969 case EHTokReturn:
2970 return acceptJumpStatement(statement);
2971
2972 case EHTokCase:
2973 return acceptCaseLabel(statement);
John Kessenichd02dc5d2016-07-01 00:04:11 -06002974 case EHTokDefault:
2975 return acceptDefaultLabel(statement);
John Kessenich21472ae2016-06-04 11:46:33 -06002976
2977 case EHTokSemicolon:
2978 return acceptTokenClass(EHTokSemicolon);
2979
2980 case EHTokRightBrace:
2981 // Performance: not strictly necessary, but stops a bunch of hunting early,
2982 // and is how sequences of statements end.
John Kessenich5f934b02016-03-13 17:58:25 -06002983 return false;
2984
John Kessenich21472ae2016-06-04 11:46:33 -06002985 default:
2986 {
2987 // declaration
2988 if (acceptDeclaration(statement))
2989 return true;
2990
2991 // expression
2992 TIntermTyped* node;
2993 if (acceptExpression(node))
2994 statement = node;
2995 else
2996 return false;
2997
2998 // SEMICOLON (following an expression)
2999 if (! acceptTokenClass(EHTokSemicolon)) {
John Kessenich0d2b6de2016-06-05 11:23:11 -06003000 expected(";");
John Kessenich21472ae2016-06-04 11:46:33 -06003001 return false;
3002 }
3003 }
3004 }
3005
John Kessenich5f934b02016-03-13 17:58:25 -06003006 return true;
John Kessenich87142c72016-03-12 20:24:24 -07003007}
3008
John Kessenich21472ae2016-06-04 11:46:33 -06003009// attributes
3010// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
3011//
3012// attribute:
3013// : UNROLL
3014// | UNROLL LEFT_PAREN literal RIGHT_PAREN
3015// | FASTOPT
3016// | ALLOW_UAV_CONDITION
3017// | BRANCH
3018// | FLATTEN
3019// | FORCECASE
3020// | CALL
steve-lunarg1868b142016-10-20 13:07:10 -06003021// | DOMAIN
3022// | EARLYDEPTHSTENCIL
3023// | INSTANCE
3024// | MAXTESSFACTOR
3025// | OUTPUTCONTROLPOINTS
3026// | OUTPUTTOPOLOGY
3027// | PARTITIONING
3028// | PATCHCONSTANTFUNC
3029// | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
John Kessenich21472ae2016-06-04 11:46:33 -06003030//
steve-lunarg1868b142016-10-20 13:07:10 -06003031void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
John Kessenich21472ae2016-06-04 11:46:33 -06003032{
steve-lunarg1868b142016-10-20 13:07:10 -06003033 // For now, accept the [ XXX(X) ] syntax, but drop all but
3034 // numthreads, which is used to set the CS local size.
John Kessenich0d2b6de2016-06-05 11:23:11 -06003035 // TODO: subset to correct set? Pass on?
3036 do {
steve-lunarg1868b142016-10-20 13:07:10 -06003037 HlslToken idToken;
3038
John Kessenich0d2b6de2016-06-05 11:23:11 -06003039 // LEFT_BRACKET?
3040 if (! acceptTokenClass(EHTokLeftBracket))
3041 return;
3042
3043 // attribute
steve-lunarg1868b142016-10-20 13:07:10 -06003044 if (acceptIdentifier(idToken)) {
3045 // 'idToken.string' is the attribute
John Kessenich0d2b6de2016-06-05 11:23:11 -06003046 } else if (! peekTokenClass(EHTokRightBracket)) {
3047 expected("identifier");
3048 advanceToken();
3049 }
3050
steve-lunarga22f7db2016-11-11 08:17:44 -07003051 TIntermAggregate* expressions = nullptr;
steve-lunarg1868b142016-10-20 13:07:10 -06003052
3053 // (x, ...)
John Kessenich0d2b6de2016-06-05 11:23:11 -06003054 if (acceptTokenClass(EHTokLeftParen)) {
steve-lunarga22f7db2016-11-11 08:17:44 -07003055 expressions = new TIntermAggregate;
steve-lunarg1868b142016-10-20 13:07:10 -06003056
John Kessenich0d2b6de2016-06-05 11:23:11 -06003057 TIntermTyped* node;
steve-lunarga22f7db2016-11-11 08:17:44 -07003058 bool expectingExpression = false;
John Kessenichecba76f2017-01-06 00:34:48 -07003059
steve-lunarga22f7db2016-11-11 08:17:44 -07003060 while (acceptAssignmentExpression(node)) {
3061 expectingExpression = false;
3062 expressions->getSequence().push_back(node);
steve-lunarg1868b142016-10-20 13:07:10 -06003063 if (acceptTokenClass(EHTokComma))
steve-lunarga22f7db2016-11-11 08:17:44 -07003064 expectingExpression = true;
steve-lunarg1868b142016-10-20 13:07:10 -06003065 }
3066
steve-lunarga22f7db2016-11-11 08:17:44 -07003067 // 'expressions' is an aggregate with the expressions in it
John Kessenich0d2b6de2016-06-05 11:23:11 -06003068 if (! acceptTokenClass(EHTokRightParen))
3069 expected(")");
steve-lunarga22f7db2016-11-11 08:17:44 -07003070
3071 // Error for partial or missing expression
3072 if (expectingExpression || expressions->getSequence().empty())
3073 expected("expression");
John Kessenich0d2b6de2016-06-05 11:23:11 -06003074 }
3075
3076 // RIGHT_BRACKET
steve-lunarg1868b142016-10-20 13:07:10 -06003077 if (!acceptTokenClass(EHTokRightBracket)) {
3078 expected("]");
3079 return;
3080 }
John Kessenich0d2b6de2016-06-05 11:23:11 -06003081
steve-lunarg1868b142016-10-20 13:07:10 -06003082 // Add any values we found into the attribute map. This accepts
3083 // (and ignores) values not mapping to a known TAttributeType;
steve-lunarga22f7db2016-11-11 08:17:44 -07003084 attributes.setAttribute(idToken.string, expressions);
John Kessenich0d2b6de2016-06-05 11:23:11 -06003085 } while (true);
John Kessenich21472ae2016-06-04 11:46:33 -06003086}
3087
John Kessenich0d2b6de2016-06-05 11:23:11 -06003088// selection_statement
3089// : IF LEFT_PAREN expression RIGHT_PAREN statement
3090// : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
3091//
John Kessenich21472ae2016-06-04 11:46:33 -06003092bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
3093{
John Kessenich0d2b6de2016-06-05 11:23:11 -06003094 TSourceLoc loc = token.loc;
3095
3096 // IF
3097 if (! acceptTokenClass(EHTokIf))
3098 return false;
3099
3100 // so that something declared in the condition is scoped to the lifetimes
3101 // of the then-else statements
3102 parseContext.pushScope();
3103
3104 // LEFT_PAREN expression RIGHT_PAREN
3105 TIntermTyped* condition;
3106 if (! acceptParenExpression(condition))
3107 return false;
3108
3109 // create the child statements
3110 TIntermNodePair thenElse = { nullptr, nullptr };
3111
3112 // then statement
3113 if (! acceptScopedStatement(thenElse.node1)) {
3114 expected("then statement");
3115 return false;
3116 }
3117
3118 // ELSE
3119 if (acceptTokenClass(EHTokElse)) {
3120 // else statement
3121 if (! acceptScopedStatement(thenElse.node2)) {
3122 expected("else statement");
3123 return false;
3124 }
3125 }
3126
3127 // Put the pieces together
3128 statement = intermediate.addSelection(condition, thenElse, loc);
3129 parseContext.popScope();
3130
3131 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003132}
3133
John Kessenichd02dc5d2016-07-01 00:04:11 -06003134// switch_statement
3135// : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
3136//
John Kessenich21472ae2016-06-04 11:46:33 -06003137bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement)
3138{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003139 // SWITCH
3140 TSourceLoc loc = token.loc;
3141 if (! acceptTokenClass(EHTokSwitch))
3142 return false;
3143
3144 // LEFT_PAREN expression RIGHT_PAREN
3145 parseContext.pushScope();
3146 TIntermTyped* switchExpression;
3147 if (! acceptParenExpression(switchExpression)) {
3148 parseContext.popScope();
3149 return false;
3150 }
3151
3152 // compound_statement
3153 parseContext.pushSwitchSequence(new TIntermSequence);
3154 bool statementOkay = acceptCompoundStatement(statement);
3155 if (statementOkay)
3156 statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr);
3157
3158 parseContext.popSwitchSequence();
3159 parseContext.popScope();
3160
3161 return statementOkay;
John Kessenich21472ae2016-06-04 11:46:33 -06003162}
3163
John Kessenich119f8f62016-06-05 15:44:07 -06003164// iteration_statement
3165// : WHILE LEFT_PAREN condition RIGHT_PAREN statement
3166// | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
3167// | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
3168//
3169// Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
John Kessenich21472ae2016-06-04 11:46:33 -06003170bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
3171{
John Kessenich119f8f62016-06-05 15:44:07 -06003172 TSourceLoc loc = token.loc;
3173 TIntermTyped* condition = nullptr;
3174
3175 EHlslTokenClass loop = peek();
3176 assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
3177
3178 // WHILE or DO or FOR
3179 advanceToken();
3180
3181 switch (loop) {
3182 case EHTokWhile:
3183 // so that something declared in the condition is scoped to the lifetime
3184 // of the while sub-statement
3185 parseContext.pushScope();
3186 parseContext.nestLooping();
3187
3188 // LEFT_PAREN condition RIGHT_PAREN
3189 if (! acceptParenExpression(condition))
3190 return false;
3191
3192 // statement
3193 if (! acceptScopedStatement(statement)) {
3194 expected("while sub-statement");
3195 return false;
3196 }
3197
3198 parseContext.unnestLooping();
3199 parseContext.popScope();
3200
3201 statement = intermediate.addLoop(statement, condition, nullptr, true, loc);
3202
3203 return true;
3204
3205 case EHTokDo:
3206 parseContext.nestLooping();
3207
3208 if (! acceptTokenClass(EHTokLeftBrace))
3209 expected("{");
3210
3211 // statement
3212 if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) {
3213 expected("do sub-statement");
3214 return false;
3215 }
3216
3217 if (! acceptTokenClass(EHTokRightBrace))
3218 expected("}");
3219
3220 // WHILE
3221 if (! acceptTokenClass(EHTokWhile)) {
3222 expected("while");
3223 return false;
3224 }
3225
3226 // LEFT_PAREN condition RIGHT_PAREN
3227 TIntermTyped* condition;
3228 if (! acceptParenExpression(condition))
3229 return false;
3230
3231 if (! acceptTokenClass(EHTokSemicolon))
3232 expected(";");
3233
3234 parseContext.unnestLooping();
3235
3236 statement = intermediate.addLoop(statement, condition, 0, false, loc);
3237
3238 return true;
3239
3240 case EHTokFor:
3241 {
3242 // LEFT_PAREN
3243 if (! acceptTokenClass(EHTokLeftParen))
3244 expected("(");
3245
3246 // so that something declared in the condition is scoped to the lifetime
3247 // of the for sub-statement
3248 parseContext.pushScope();
3249
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003250 // initializer
3251 TIntermNode* initNode = nullptr;
3252 if (! acceptControlDeclaration(initNode)) {
3253 TIntermTyped* initExpr = nullptr;
3254 acceptExpression(initExpr);
3255 initNode = initExpr;
3256 }
3257 // SEMI_COLON
John Kessenich119f8f62016-06-05 15:44:07 -06003258 if (! acceptTokenClass(EHTokSemicolon))
3259 expected(";");
3260
3261 parseContext.nestLooping();
3262
3263 // condition SEMI_COLON
3264 acceptExpression(condition);
3265 if (! acceptTokenClass(EHTokSemicolon))
3266 expected(";");
3267
3268 // iterator SEMI_COLON
3269 TIntermTyped* iterator = nullptr;
3270 acceptExpression(iterator);
3271 if (! acceptTokenClass(EHTokRightParen))
3272 expected(")");
3273
3274 // statement
3275 if (! acceptScopedStatement(statement)) {
3276 expected("for sub-statement");
3277 return false;
3278 }
3279
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003280 statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc);
John Kessenich119f8f62016-06-05 15:44:07 -06003281
3282 parseContext.popScope();
3283 parseContext.unnestLooping();
3284
3285 return true;
3286 }
3287
3288 default:
3289 return false;
3290 }
John Kessenich21472ae2016-06-04 11:46:33 -06003291}
3292
3293// jump_statement
3294// : CONTINUE SEMICOLON
3295// | BREAK SEMICOLON
3296// | DISCARD SEMICOLON
3297// | RETURN SEMICOLON
3298// | RETURN expression SEMICOLON
3299//
3300bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
3301{
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003302 EHlslTokenClass jump = peek();
3303 switch (jump) {
John Kessenich21472ae2016-06-04 11:46:33 -06003304 case EHTokContinue:
3305 case EHTokBreak:
3306 case EHTokDiscard:
John Kessenich21472ae2016-06-04 11:46:33 -06003307 case EHTokReturn:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003308 advanceToken();
3309 break;
John Kessenich21472ae2016-06-04 11:46:33 -06003310 default:
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003311 // not something we handle in this function
John Kessenich21472ae2016-06-04 11:46:33 -06003312 return false;
3313 }
John Kessenich21472ae2016-06-04 11:46:33 -06003314
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003315 switch (jump) {
3316 case EHTokContinue:
3317 statement = intermediate.addBranch(EOpContinue, token.loc);
3318 break;
3319 case EHTokBreak:
3320 statement = intermediate.addBranch(EOpBreak, token.loc);
3321 break;
3322 case EHTokDiscard:
3323 statement = intermediate.addBranch(EOpKill, token.loc);
3324 break;
3325
3326 case EHTokReturn:
3327 {
3328 // expression
3329 TIntermTyped* node;
3330 if (acceptExpression(node)) {
3331 // hook it up
steve-lunargc4a13072016-08-09 11:28:03 -06003332 statement = parseContext.handleReturnValue(token.loc, node);
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003333 } else
3334 statement = intermediate.addBranch(EOpReturn, token.loc);
3335 break;
3336 }
3337
3338 default:
3339 assert(0);
3340 return false;
3341 }
3342
3343 // SEMICOLON
3344 if (! acceptTokenClass(EHTokSemicolon))
3345 expected(";");
John Kessenichecba76f2017-01-06 00:34:48 -07003346
John Kessenich5bc4d9a2016-06-20 01:22:38 -06003347 return true;
3348}
John Kessenich21472ae2016-06-04 11:46:33 -06003349
John Kessenichd02dc5d2016-07-01 00:04:11 -06003350// case_label
3351// : CASE expression COLON
3352//
John Kessenich21472ae2016-06-04 11:46:33 -06003353bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
3354{
John Kessenichd02dc5d2016-07-01 00:04:11 -06003355 TSourceLoc loc = token.loc;
3356 if (! acceptTokenClass(EHTokCase))
3357 return false;
3358
3359 TIntermTyped* expression;
3360 if (! acceptExpression(expression)) {
3361 expected("case expression");
3362 return false;
3363 }
3364
3365 if (! acceptTokenClass(EHTokColon)) {
3366 expected(":");
3367 return false;
3368 }
3369
3370 statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
3371
3372 return true;
3373}
3374
3375// default_label
3376// : DEFAULT COLON
3377//
3378bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
3379{
3380 TSourceLoc loc = token.loc;
3381 if (! acceptTokenClass(EHTokDefault))
3382 return false;
3383
3384 if (! acceptTokenClass(EHTokColon)) {
3385 expected(":");
3386 return false;
3387 }
3388
3389 statement = parseContext.intermediate.addBranch(EOpDefault, loc);
3390
3391 return true;
John Kessenich21472ae2016-06-04 11:46:33 -06003392}
3393
John Kessenich19b92ff2016-06-19 11:50:34 -06003394// array_specifier
steve-lunarg7b211a32016-10-13 12:26:18 -06003395// : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
3396// : LEFT_BRACKET RGHT_BRACKET // optional
John Kessenich19b92ff2016-06-19 11:50:34 -06003397//
3398void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
3399{
3400 arraySizes = nullptr;
3401
steve-lunarg7b211a32016-10-13 12:26:18 -06003402 // Early-out if there aren't any array dimensions
3403 if (!peekTokenClass(EHTokLeftBracket))
John Kessenich19b92ff2016-06-19 11:50:34 -06003404 return;
3405
steve-lunarg7b211a32016-10-13 12:26:18 -06003406 // 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 -06003407 arraySizes = new TArraySizes;
steve-lunarg7b211a32016-10-13 12:26:18 -06003408
3409 // Collect each array dimension.
3410 while (acceptTokenClass(EHTokLeftBracket)) {
3411 TSourceLoc loc = token.loc;
3412 TIntermTyped* sizeExpr = nullptr;
3413
John Kessenich057df292017-03-06 18:18:37 -07003414 // Array sizing expression is optional. If omitted, array will be later sized by initializer list.
steve-lunarg7b211a32016-10-13 12:26:18 -06003415 const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
3416
3417 if (! acceptTokenClass(EHTokRightBracket)) {
3418 expected("]");
3419 return;
3420 }
3421
3422 if (hasArraySize) {
3423 TArraySize arraySize;
3424 parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
3425 arraySizes->addInnerSize(arraySize);
3426 } else {
3427 arraySizes->addInnerSize(0); // sized by initializers.
3428 }
steve-lunarg265c0612016-09-27 10:57:35 -06003429 }
John Kessenich19b92ff2016-06-19 11:50:34 -06003430}
3431
John Kessenich630dd7d2016-06-12 23:52:12 -06003432// post_decls
John Kessenichcfd7ce82016-09-05 16:03:12 -06003433// : COLON semantic // optional
3434// COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
3435// COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
John Kesseniche3218e22016-09-05 14:37:03 -06003436// COLON LAYOUT layout_qualifier_list
John Kessenichcfd7ce82016-09-05 16:03:12 -06003437// annotations // optional
John Kessenich630dd7d2016-06-12 23:52:12 -06003438//
John Kessenich854fe242017-03-02 14:30:59 -07003439// Return true if any tokens were accepted. That is,
3440// false can be returned on successfully recognizing nothing,
3441// not necessarily meaning bad syntax.
3442//
3443bool HlslGrammar::acceptPostDecls(TQualifier& qualifier)
John Kessenich078d7f22016-03-14 10:02:11 -06003444{
John Kessenich854fe242017-03-02 14:30:59 -07003445 bool found = false;
3446
John Kessenich630dd7d2016-06-12 23:52:12 -06003447 do {
John Kessenichecba76f2017-01-06 00:34:48 -07003448 // COLON
John Kessenich630dd7d2016-06-12 23:52:12 -06003449 if (acceptTokenClass(EHTokColon)) {
John Kessenich854fe242017-03-02 14:30:59 -07003450 found = true;
John Kessenich630dd7d2016-06-12 23:52:12 -06003451 HlslToken idToken;
John Kesseniche3218e22016-09-05 14:37:03 -06003452 if (peekTokenClass(EHTokLayout))
3453 acceptLayoutQualifierList(qualifier);
3454 else if (acceptTokenClass(EHTokPackOffset)) {
John Kessenich96e9f472016-07-29 14:28:39 -06003455 // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003456 if (! acceptTokenClass(EHTokLeftParen)) {
3457 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003458 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003459 }
John Kessenich82d6baf2016-07-29 13:03:05 -06003460 HlslToken locationToken;
3461 if (! acceptIdentifier(locationToken)) {
3462 expected("c[subcomponent][.component]");
John Kessenich854fe242017-03-02 14:30:59 -07003463 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003464 }
3465 HlslToken componentToken;
3466 if (acceptTokenClass(EHTokDot)) {
3467 if (! acceptIdentifier(componentToken)) {
3468 expected("component");
John Kessenich854fe242017-03-02 14:30:59 -07003469 return false;
John Kessenich82d6baf2016-07-29 13:03:05 -06003470 }
3471 }
John Kessenich630dd7d2016-06-12 23:52:12 -06003472 if (! acceptTokenClass(EHTokRightParen)) {
3473 expected(")");
3474 break;
3475 }
John Kessenich7735b942016-09-05 12:40:06 -06003476 parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003477 } else if (! acceptIdentifier(idToken)) {
John Kesseniche3218e22016-09-05 14:37:03 -06003478 expected("layout, semantic, packoffset, or register");
John Kessenich854fe242017-03-02 14:30:59 -07003479 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003480 } else if (*idToken.string == "register") {
John Kessenichcfd7ce82016-09-05 16:03:12 -06003481 // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
3482 // LEFT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003483 if (! acceptTokenClass(EHTokLeftParen)) {
3484 expected("(");
John Kessenich854fe242017-03-02 14:30:59 -07003485 return false;
John Kessenich630dd7d2016-06-12 23:52:12 -06003486 }
John Kessenichb38f0712016-07-30 10:29:54 -06003487 HlslToken registerDesc; // for Type#
3488 HlslToken profile;
John Kessenich96e9f472016-07-29 14:28:39 -06003489 if (! acceptIdentifier(registerDesc)) {
3490 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003491 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003492 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003493 if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
3494 acceptTokenClass(EHTokComma)) {
John Kessenichb38f0712016-07-30 10:29:54 -06003495 // Then we didn't really see the registerDesc yet, it was
3496 // actually the profile. Adjust...
John Kessenich96e9f472016-07-29 14:28:39 -06003497 profile = registerDesc;
3498 if (! acceptIdentifier(registerDesc)) {
3499 expected("register number description");
John Kessenich854fe242017-03-02 14:30:59 -07003500 return false;
John Kessenich96e9f472016-07-29 14:28:39 -06003501 }
3502 }
John Kessenichb38f0712016-07-30 10:29:54 -06003503 int subComponent = 0;
3504 if (acceptTokenClass(EHTokLeftBracket)) {
3505 // LEFT_BRACKET subcomponent RIGHT_BRACKET
3506 if (! peekTokenClass(EHTokIntConstant)) {
3507 expected("literal integer");
John Kessenich854fe242017-03-02 14:30:59 -07003508 return false;
John Kessenichb38f0712016-07-30 10:29:54 -06003509 }
3510 subComponent = token.i;
3511 advanceToken();
3512 if (! acceptTokenClass(EHTokRightBracket)) {
3513 expected("]");
3514 break;
3515 }
3516 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003517 // (COMMA SPACEN)opt
3518 HlslToken spaceDesc;
3519 if (acceptTokenClass(EHTokComma)) {
3520 if (! acceptIdentifier(spaceDesc)) {
3521 expected ("space identifier");
John Kessenich854fe242017-03-02 14:30:59 -07003522 return false;
John Kessenichcfd7ce82016-09-05 16:03:12 -06003523 }
3524 }
3525 // RIGHT_PAREN
John Kessenich630dd7d2016-06-12 23:52:12 -06003526 if (! acceptTokenClass(EHTokRightParen)) {
3527 expected(")");
3528 break;
3529 }
John Kessenichcfd7ce82016-09-05 16:03:12 -06003530 parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
John Kessenich630dd7d2016-06-12 23:52:12 -06003531 } else {
3532 // semantic, in idToken.string
John Kessenich2dd643f2017-03-14 21:50:06 -06003533 TString semanticUpperCase = *idToken.string;
3534 std::transform(semanticUpperCase.begin(), semanticUpperCase.end(), semanticUpperCase.begin(), ::toupper);
3535 parseContext.handleSemantic(idToken.loc, qualifier, mapSemantic(semanticUpperCase.c_str()), semanticUpperCase);
John Kessenich630dd7d2016-06-12 23:52:12 -06003536 }
John Kessenich854fe242017-03-02 14:30:59 -07003537 } else if (peekTokenClass(EHTokLeftAngle)) {
3538 found = true;
John Kessenicha1e2d492016-09-20 13:22:58 -06003539 acceptAnnotations(qualifier);
John Kessenich854fe242017-03-02 14:30:59 -07003540 } else
John Kessenich630dd7d2016-06-12 23:52:12 -06003541 break;
John Kessenich078d7f22016-03-14 10:02:11 -06003542
John Kessenich630dd7d2016-06-12 23:52:12 -06003543 } while (true);
John Kessenich854fe242017-03-02 14:30:59 -07003544
3545 return found;
John Kessenich078d7f22016-03-14 10:02:11 -06003546}
3547
John Kessenichb16f7e62017-03-11 19:32:47 -07003548//
3549// Get the stream of tokens from the scanner, but skip all syntactic/semantic
3550// processing.
3551//
3552bool HlslGrammar::captureBlockTokens(TVector<HlslToken>& tokens)
3553{
3554 if (! peekTokenClass(EHTokLeftBrace))
3555 return false;
3556
3557 int braceCount = 0;
3558
3559 do {
3560 switch (peek()) {
3561 case EHTokLeftBrace:
3562 ++braceCount;
3563 break;
3564 case EHTokRightBrace:
3565 --braceCount;
3566 break;
3567 case EHTokNone:
3568 // End of input before balance { } is bad...
3569 return false;
3570 default:
3571 break;
3572 }
3573
3574 tokens.push_back(token);
3575 advanceToken();
3576 } while (braceCount > 0);
3577
3578 return true;
3579}
3580
John Kesseniche01a9bc2016-03-12 20:11:22 -07003581} // end namespace glslang