John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 1 | // |
| 2 | //Copyright (C) 2016 Google, Inc. |
LoopDawg | 6daaa4f | 2016-06-23 19:13:48 -0600 | [diff] [blame] | 3 | //Copyright (C) 2016 LunarG, Inc. |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 4 | // |
| 5 | //All rights reserved. |
| 6 | // |
| 7 | //Redistribution and use in source and binary forms, with or without |
| 8 | //modification, are permitted provided that the following conditions |
| 9 | //are met: |
| 10 | // |
| 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 | // |
| 23 | //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. |
| 35 | // |
| 36 | |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 37 | // |
| 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 Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 46 | // with all other work being farmed out to hlslParseHelper.cpp, which in turn |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 47 | // 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 Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 54 | #include "hlslTokens.h" |
| 55 | #include "hlslGrammar.h" |
| 56 | |
| 57 | namespace glslang { |
| 58 | |
| 59 | // Root entry point to this recursive decent parser. |
| 60 | // Return true if compilation unit was successfully accepted. |
| 61 | bool HlslGrammar::parse() |
| 62 | { |
| 63 | advanceToken(); |
| 64 | return acceptCompilationUnit(); |
| 65 | } |
| 66 | |
| 67 | void HlslGrammar::expected(const char* syntax) |
| 68 | { |
| 69 | parseContext.error(token.loc, "Expected", syntax, ""); |
| 70 | } |
| 71 | |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 72 | void HlslGrammar::unimplemented(const char* error) |
| 73 | { |
| 74 | parseContext.error(token.loc, "Unimplemented", error, ""); |
| 75 | } |
| 76 | |
John Kessenich | aecd497 | 2016-03-14 10:46:34 -0600 | [diff] [blame] | 77 | // Only process the next token if it is an identifier. |
| 78 | // Return true if it was an identifier. |
| 79 | bool HlslGrammar::acceptIdentifier(HlslToken& idToken) |
| 80 | { |
| 81 | if (peekTokenClass(EHTokIdentifier)) { |
| 82 | idToken = token; |
| 83 | advanceToken(); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 90 | // compilationUnit |
| 91 | // : list of externalDeclaration |
steve-lunarg | cb88de5 | 2016-08-03 07:04:18 -0600 | [diff] [blame] | 92 | // | SEMICOLONS |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 93 | // |
| 94 | bool HlslGrammar::acceptCompilationUnit() |
| 95 | { |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 96 | TIntermNode* unitNode = nullptr; |
| 97 | |
John Kessenich | 9c86c6a | 2016-05-03 22:49:24 -0600 | [diff] [blame] | 98 | while (! peekTokenClass(EHTokNone)) { |
steve-lunarg | cb88de5 | 2016-08-03 07:04:18 -0600 | [diff] [blame] | 99 | // HLSL allows semicolons between global declarations, e.g, between functions. |
| 100 | if (acceptTokenClass(EHTokSemicolon)) |
| 101 | continue; |
| 102 | |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 103 | // externalDeclaration |
| 104 | TIntermNode* declarationNode; |
| 105 | if (! acceptDeclaration(declarationNode)) |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 106 | return false; |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 107 | |
| 108 | // hook it up |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 109 | unitNode = intermediate.growAggregate(unitNode, declarationNode); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 110 | } |
| 111 | |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 112 | // set root of AST |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 113 | intermediate.setTreeRoot(unitNode); |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 114 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 118 | // sampler_state |
| 119 | // : LEFT_BRACE [sampler_state_assignment ... ] RIGHT_BRACE |
| 120 | // |
| 121 | // sampler_state_assignment |
| 122 | // : sampler_state_identifier EQUAL value SEMICOLON |
| 123 | // |
| 124 | // sampler_state_identifier |
| 125 | // : ADDRESSU |
| 126 | // | ADDRESSV |
| 127 | // | ADDRESSW |
| 128 | // | BORDERCOLOR |
| 129 | // | FILTER |
| 130 | // | MAXANISOTROPY |
| 131 | // | MAXLOD |
| 132 | // | MINLOD |
| 133 | // | MIPLODBIAS |
| 134 | // |
| 135 | bool HlslGrammar::acceptSamplerState() |
| 136 | { |
| 137 | // TODO: this should be genericized to accept a list of valid tokens and |
| 138 | // return token/value pairs. Presently it is specific to texture values. |
| 139 | |
| 140 | if (! acceptTokenClass(EHTokLeftBrace)) |
| 141 | return true; |
| 142 | |
| 143 | parseContext.warn(token.loc, "unimplemented", "immediate sampler state", ""); |
| 144 | |
| 145 | do { |
| 146 | // read state name |
| 147 | HlslToken state; |
| 148 | if (! acceptIdentifier(state)) |
| 149 | break; // end of list |
| 150 | |
| 151 | // FXC accepts any case |
| 152 | TString stateName = *state.string; |
| 153 | std::transform(stateName.begin(), stateName.end(), stateName.begin(), ::tolower); |
| 154 | |
| 155 | if (! acceptTokenClass(EHTokAssign)) { |
| 156 | expected("assign"); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | if (stateName == "minlod" || stateName == "maxlod") { |
| 161 | if (! peekTokenClass(EHTokIntConstant)) { |
| 162 | expected("integer"); |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | TIntermTyped* lod = nullptr; |
| 167 | if (! acceptLiteral(lod)) // should never fail, since we just looked for an integer |
| 168 | return false; |
| 169 | } else if (stateName == "maxanisotropy") { |
| 170 | if (! peekTokenClass(EHTokIntConstant)) { |
| 171 | expected("integer"); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | TIntermTyped* maxAnisotropy = nullptr; |
| 176 | if (! acceptLiteral(maxAnisotropy)) // should never fail, since we just looked for an integer |
| 177 | return false; |
| 178 | } else if (stateName == "filter") { |
| 179 | HlslToken filterMode; |
| 180 | if (! acceptIdentifier(filterMode)) { |
| 181 | expected("filter mode"); |
| 182 | return false; |
| 183 | } |
| 184 | } else if (stateName == "addressu" || stateName == "addressv" || stateName == "addressw") { |
| 185 | HlslToken addrMode; |
| 186 | if (! acceptIdentifier(addrMode)) { |
| 187 | expected("texture address mode"); |
| 188 | return false; |
| 189 | } |
| 190 | } else if (stateName == "miplodbias") { |
| 191 | TIntermTyped* lodBias = nullptr; |
| 192 | if (! acceptLiteral(lodBias)) { |
| 193 | expected("lod bias"); |
| 194 | return false; |
| 195 | } |
| 196 | } else if (stateName == "bordercolor") { |
| 197 | return false; |
| 198 | } else { |
| 199 | expected("texture state"); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | // SEMICOLON |
| 204 | if (! acceptTokenClass(EHTokSemicolon)) { |
| 205 | expected("semicolon"); |
| 206 | return false; |
| 207 | } |
| 208 | } while (true); |
| 209 | |
| 210 | if (! acceptTokenClass(EHTokRightBrace)) |
| 211 | return false; |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | // sampler_declaration_dx9 |
| 217 | // : SAMPLER identifier EQUAL sampler_type sampler_state |
| 218 | // |
John Kessenich | e4821e4 | 2016-07-16 10:19:43 -0600 | [diff] [blame] | 219 | bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/) |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 220 | { |
| 221 | if (! acceptTokenClass(EHTokSampler)) |
| 222 | return false; |
| 223 | |
| 224 | // TODO: remove this when DX9 style declarations are implemented. |
| 225 | unimplemented("Direct3D 9 sampler declaration"); |
| 226 | |
| 227 | // read sampler name |
| 228 | HlslToken name; |
| 229 | if (! acceptIdentifier(name)) { |
| 230 | expected("sampler name"); |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | if (! acceptTokenClass(EHTokAssign)) { |
| 235 | expected("="); |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 243 | // declaration |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 244 | // : sampler_declaration_dx9 post_decls SEMICOLON |
| 245 | // | fully_specified_type declarator_list SEMICOLON |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 246 | // | fully_specified_type identifier function_parameters post_decls compound_statement // function definition |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 247 | // | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 248 | // | typedef declaration |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 249 | // |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 250 | // declarator_list |
| 251 | // : declarator COMMA declarator COMMA declarator... // zero or more declarators |
John Kessenich | 532543c | 2016-07-01 19:06:44 -0600 | [diff] [blame] | 252 | // |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 253 | // declarator |
John Kessenich | 532543c | 2016-07-01 19:06:44 -0600 | [diff] [blame] | 254 | // : identifier array_specifier post_decls |
| 255 | // | identifier array_specifier post_decls EQUAL assignment_expression |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 256 | // | identifier function_parameters post_decls // function prototype |
John Kessenich | 532543c | 2016-07-01 19:06:44 -0600 | [diff] [blame] | 257 | // |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 258 | // Parsing has to go pretty far in to know whether it's a variable, prototype, or |
| 259 | // function definition, so the implementation below doesn't perfectly divide up the grammar |
John Kessenich | 532543c | 2016-07-01 19:06:44 -0600 | [diff] [blame] | 260 | // as above. (The 'identifier' in the first item in init_declarator list is the |
| 261 | // same as 'identifier' for function declarations.) |
| 262 | // |
| 263 | // 'node' could get populated if the declaration creates code, like an initializer |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 264 | // or a function body. |
| 265 | // |
| 266 | bool HlslGrammar::acceptDeclaration(TIntermNode*& node) |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 267 | { |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 268 | node = nullptr; |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 269 | bool list = false; |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 270 | |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 271 | // typedef |
| 272 | bool typedefDecl = acceptTokenClass(EHTokTypedef); |
| 273 | |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 274 | TType type; |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 275 | |
| 276 | // DX9 sampler declaration use a different syntax |
John Kessenich | 267590d | 2016-08-05 17:34:34 -0600 | [diff] [blame] | 277 | // DX9 shaders need to run through HLSL compiler (fxc) via a back compat mode, it isn't going to |
| 278 | // be possible to simultaneously compile D3D10+ style shaders and DX9 shaders. If we want to compile DX9 |
| 279 | // HLSL shaders, this will have to be a master level switch |
| 280 | // As such, the sampler keyword in D3D10+ turns into an automatic sampler type, and is commonly used |
| 281 | // For that reason, this line is commented out |
Dan Baker | c7e5016 | 2016-08-05 14:52:38 -0400 | [diff] [blame] | 282 | |
| 283 | // if (acceptSamplerDeclarationDX9(type)) |
| 284 | // return true; |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 285 | |
| 286 | // fully_specified_type |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 287 | if (! acceptFullySpecifiedType(type)) |
| 288 | return false; |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 289 | |
| 290 | if (type.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel()) { |
| 291 | if (type.getBasicType() == EbtSampler) { |
| 292 | // Sampler/textures are uniform by default (if no explicit qualifier is present) in |
| 293 | // HLSL. This line silently converts samplers *explicitly* declared static to uniform, |
| 294 | // which is incorrect but harmless. |
| 295 | type.getQualifier().storage = EvqUniform; |
| 296 | } else { |
| 297 | type.getQualifier().storage = EvqGlobal; |
| 298 | } |
| 299 | } |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 300 | |
| 301 | // identifier |
John Kessenich | aecd497 | 2016-03-14 10:46:34 -0600 | [diff] [blame] | 302 | HlslToken idToken; |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 303 | while (acceptIdentifier(idToken)) { |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 304 | // function_parameters |
John Kessenich | 9e07953 | 2016-09-02 20:05:19 -0600 | [diff] [blame] | 305 | TFunction& function = *new TFunction(idToken.string, type); |
| 306 | if (acceptFunctionParameters(function)) { |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 307 | // post_decls |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 308 | acceptPostDecls(function.getWritableType().getQualifier()); |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 309 | |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 310 | // compound_statement (function body definition) or just a prototype? |
| 311 | if (peekTokenClass(EHTokLeftBrace)) { |
| 312 | if (list) |
| 313 | parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", ""); |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 314 | if (typedefDecl) |
| 315 | parseContext.error(idToken.loc, "function body can't be in a typedef", "{", ""); |
John Kessenich | 9e07953 | 2016-09-02 20:05:19 -0600 | [diff] [blame] | 316 | return acceptFunctionDefinition(function, node); |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 317 | } else { |
| 318 | if (typedefDecl) |
| 319 | parseContext.error(idToken.loc, "function typedefs not implemented", "{", ""); |
John Kessenich | 9e07953 | 2016-09-02 20:05:19 -0600 | [diff] [blame] | 320 | parseContext.handleFunctionDeclarator(idToken.loc, function, true); |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 321 | } |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 322 | } else { |
| 323 | // a variable declaration |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 324 | |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 325 | // array_specifier |
| 326 | TArraySizes* arraySizes = nullptr; |
| 327 | acceptArraySpecifier(arraySizes); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 328 | |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 329 | // samplers accept immediate sampler state |
| 330 | if (type.getBasicType() == EbtSampler) { |
| 331 | if (! acceptSamplerState()) |
| 332 | return false; |
| 333 | } |
| 334 | |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 335 | // post_decls |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 336 | acceptPostDecls(type.getQualifier()); |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 337 | |
| 338 | // EQUAL assignment_expression |
| 339 | TIntermTyped* expressionNode = nullptr; |
| 340 | if (acceptTokenClass(EHTokAssign)) { |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 341 | if (typedefDecl) |
| 342 | parseContext.error(idToken.loc, "can't have an initializer", "typedef", ""); |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 343 | if (! acceptAssignmentExpression(expressionNode)) { |
| 344 | expected("initializer"); |
| 345 | return false; |
| 346 | } |
| 347 | } |
| 348 | |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 349 | if (typedefDecl) |
| 350 | parseContext.declareTypedef(idToken.loc, *idToken.string, type, arraySizes); |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 351 | else if (type.getBasicType() == EbtBlock) |
| 352 | parseContext.declareBlock(idToken.loc, type, idToken.string); |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 353 | else { |
| 354 | // Declare the variable and add any initializer code to the AST. |
| 355 | // The top-level node is always made into an aggregate, as that's |
| 356 | // historically how the AST has been. |
| 357 | node = intermediate.growAggregate(node, |
| 358 | parseContext.declareVariable(idToken.loc, *idToken.string, type, |
| 359 | arraySizes, expressionNode), |
| 360 | idToken.loc); |
| 361 | } |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 362 | } |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 363 | |
| 364 | if (acceptTokenClass(EHTokComma)) { |
| 365 | list = true; |
| 366 | continue; |
| 367 | } |
| 368 | }; |
| 369 | |
| 370 | // The top-level node is a sequence. |
| 371 | if (node != nullptr) |
| 372 | node->getAsAggregate()->setOperator(EOpSequence); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 373 | |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 374 | // SEMICOLON |
John Kessenich | d5ed0b6 | 2016-07-04 17:32:45 -0600 | [diff] [blame] | 375 | if (! acceptTokenClass(EHTokSemicolon)) { |
| 376 | expected(";"); |
| 377 | return false; |
| 378 | } |
| 379 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 383 | // control_declaration |
| 384 | // : fully_specified_type identifier EQUAL expression |
| 385 | // |
| 386 | bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node) |
| 387 | { |
| 388 | node = nullptr; |
| 389 | |
| 390 | // fully_specified_type |
| 391 | TType type; |
| 392 | if (! acceptFullySpecifiedType(type)) |
| 393 | return false; |
| 394 | |
| 395 | // identifier |
| 396 | HlslToken idToken; |
| 397 | if (! acceptIdentifier(idToken)) { |
| 398 | expected("identifier"); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | // EQUAL |
| 403 | TIntermTyped* expressionNode = nullptr; |
| 404 | if (! acceptTokenClass(EHTokAssign)) { |
| 405 | expected("="); |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | // expression |
| 410 | if (! acceptExpression(expressionNode)) { |
| 411 | expected("initializer"); |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | node = parseContext.declareVariable(idToken.loc, *idToken.string, type, 0, expressionNode); |
| 416 | |
| 417 | return true; |
| 418 | } |
| 419 | |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 420 | // fully_specified_type |
| 421 | // : type_specifier |
| 422 | // | type_qualifier type_specifier |
| 423 | // |
| 424 | bool HlslGrammar::acceptFullySpecifiedType(TType& type) |
| 425 | { |
| 426 | // type_qualifier |
| 427 | TQualifier qualifier; |
| 428 | qualifier.clear(); |
John Kessenich | b9e3912 | 2016-08-17 10:22:08 -0600 | [diff] [blame] | 429 | if (! acceptQualifier(qualifier)) |
| 430 | return false; |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 431 | TSourceLoc loc = token.loc; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 432 | |
| 433 | // type_specifier |
| 434 | if (! acceptType(type)) |
| 435 | return false; |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 436 | if (type.getBasicType() == EbtBlock) { |
| 437 | // the type was a block, which set some parts of the qualifier |
John Kessenich | 34e7ee7 | 2016-09-16 17:10:39 -0600 | [diff] [blame] | 438 | parseContext.mergeQualifiers(type.getQualifier(), qualifier); |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 439 | // further, it can create an anonymous instance of the block |
| 440 | if (peekTokenClass(EHTokSemicolon)) |
| 441 | parseContext.declareBlock(loc, type); |
| 442 | } else |
| 443 | type.getQualifier() = qualifier; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 444 | |
| 445 | return true; |
| 446 | } |
| 447 | |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 448 | // type_qualifier |
| 449 | // : qualifier qualifier ... |
| 450 | // |
| 451 | // Zero or more of these, so this can't return false. |
| 452 | // |
John Kessenich | b9e3912 | 2016-08-17 10:22:08 -0600 | [diff] [blame] | 453 | bool HlslGrammar::acceptQualifier(TQualifier& qualifier) |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 454 | { |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 455 | do { |
| 456 | switch (peek()) { |
| 457 | case EHTokStatic: |
| 458 | // normal glslang default |
| 459 | break; |
| 460 | case EHTokExtern: |
| 461 | // TODO: no meaning in glslang? |
| 462 | break; |
| 463 | case EHTokShared: |
| 464 | // TODO: hint |
| 465 | break; |
| 466 | case EHTokGroupShared: |
| 467 | qualifier.storage = EvqShared; |
| 468 | break; |
| 469 | case EHTokUniform: |
| 470 | qualifier.storage = EvqUniform; |
| 471 | break; |
| 472 | case EHTokConst: |
| 473 | qualifier.storage = EvqConst; |
| 474 | break; |
| 475 | case EHTokVolatile: |
| 476 | qualifier.volatil = true; |
| 477 | break; |
| 478 | case EHTokLinear: |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 479 | qualifier.smooth = true; |
| 480 | break; |
| 481 | case EHTokCentroid: |
| 482 | qualifier.centroid = true; |
| 483 | break; |
| 484 | case EHTokNointerpolation: |
| 485 | qualifier.flat = true; |
| 486 | break; |
| 487 | case EHTokNoperspective: |
| 488 | qualifier.nopersp = true; |
| 489 | break; |
| 490 | case EHTokSample: |
| 491 | qualifier.sample = true; |
| 492 | break; |
| 493 | case EHTokRowMajor: |
| 494 | qualifier.layoutMatrix = ElmRowMajor; |
| 495 | break; |
| 496 | case EHTokColumnMajor: |
| 497 | qualifier.layoutMatrix = ElmColumnMajor; |
| 498 | break; |
| 499 | case EHTokPrecise: |
| 500 | qualifier.noContraction = true; |
| 501 | break; |
LoopDawg | 9249c70 | 2016-07-12 20:44:32 -0600 | [diff] [blame] | 502 | case EHTokIn: |
| 503 | qualifier.storage = EvqIn; |
| 504 | break; |
| 505 | case EHTokOut: |
| 506 | qualifier.storage = EvqOut; |
| 507 | break; |
| 508 | case EHTokInOut: |
| 509 | qualifier.storage = EvqInOut; |
| 510 | break; |
John Kessenich | b9e3912 | 2016-08-17 10:22:08 -0600 | [diff] [blame] | 511 | case EHTokLayout: |
| 512 | if (! acceptLayoutQualifierList(qualifier)) |
| 513 | return false; |
| 514 | continue; |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 515 | default: |
John Kessenich | b9e3912 | 2016-08-17 10:22:08 -0600 | [diff] [blame] | 516 | return true; |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 517 | } |
| 518 | advanceToken(); |
| 519 | } while (true); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 520 | } |
| 521 | |
John Kessenich | b9e3912 | 2016-08-17 10:22:08 -0600 | [diff] [blame] | 522 | // layout_qualifier_list |
John Kessenich | e3218e2 | 2016-09-05 14:37:03 -0600 | [diff] [blame] | 523 | // : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN |
John Kessenich | b9e3912 | 2016-08-17 10:22:08 -0600 | [diff] [blame] | 524 | // |
| 525 | // layout_qualifier |
| 526 | // : identifier |
John Kessenich | 841db35 | 2016-09-02 21:12:23 -0600 | [diff] [blame] | 527 | // | identifier EQUAL expression |
John Kessenich | b9e3912 | 2016-08-17 10:22:08 -0600 | [diff] [blame] | 528 | // |
| 529 | // Zero or more of these, so this can't return false. |
| 530 | // |
| 531 | bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier) |
| 532 | { |
| 533 | if (! acceptTokenClass(EHTokLayout)) |
| 534 | return false; |
| 535 | |
| 536 | // LEFT_PAREN |
| 537 | if (! acceptTokenClass(EHTokLeftParen)) |
| 538 | return false; |
| 539 | |
| 540 | do { |
| 541 | // identifier |
| 542 | HlslToken idToken; |
| 543 | if (! acceptIdentifier(idToken)) |
| 544 | break; |
| 545 | |
| 546 | // EQUAL expression |
| 547 | if (acceptTokenClass(EHTokAssign)) { |
| 548 | TIntermTyped* expr; |
| 549 | if (! acceptConditionalExpression(expr)) { |
| 550 | expected("expression"); |
| 551 | return false; |
| 552 | } |
| 553 | parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr); |
| 554 | } else |
| 555 | parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string); |
| 556 | |
| 557 | // COMMA |
| 558 | if (! acceptTokenClass(EHTokComma)) |
| 559 | break; |
| 560 | } while (true); |
| 561 | |
| 562 | // RIGHT_PAREN |
| 563 | if (! acceptTokenClass(EHTokRightParen)) { |
| 564 | expected(")"); |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | return true; |
| 569 | } |
| 570 | |
LoopDawg | 6daaa4f | 2016-06-23 19:13:48 -0600 | [diff] [blame] | 571 | // template_type |
| 572 | // : FLOAT |
| 573 | // | DOUBLE |
| 574 | // | INT |
| 575 | // | DWORD |
| 576 | // | UINT |
| 577 | // | BOOL |
| 578 | // |
| 579 | bool HlslGrammar::acceptTemplateType(TBasicType& basicType) |
| 580 | { |
| 581 | switch (peek()) { |
| 582 | case EHTokFloat: |
| 583 | basicType = EbtFloat; |
| 584 | break; |
| 585 | case EHTokDouble: |
| 586 | basicType = EbtDouble; |
| 587 | break; |
| 588 | case EHTokInt: |
| 589 | case EHTokDword: |
| 590 | basicType = EbtInt; |
| 591 | break; |
| 592 | case EHTokUint: |
| 593 | basicType = EbtUint; |
| 594 | break; |
| 595 | case EHTokBool: |
| 596 | basicType = EbtBool; |
| 597 | break; |
| 598 | default: |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | advanceToken(); |
| 603 | |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | // vector_template_type |
| 608 | // : VECTOR |
| 609 | // | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE |
| 610 | // |
| 611 | bool HlslGrammar::acceptVectorTemplateType(TType& type) |
| 612 | { |
| 613 | if (! acceptTokenClass(EHTokVector)) |
| 614 | return false; |
| 615 | |
| 616 | if (! acceptTokenClass(EHTokLeftAngle)) { |
| 617 | // in HLSL, 'vector' alone means float4. |
| 618 | new(&type) TType(EbtFloat, EvqTemporary, 4); |
| 619 | return true; |
| 620 | } |
| 621 | |
| 622 | TBasicType basicType; |
| 623 | if (! acceptTemplateType(basicType)) { |
| 624 | expected("scalar type"); |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | // COMMA |
| 629 | if (! acceptTokenClass(EHTokComma)) { |
| 630 | expected(","); |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | // integer |
| 635 | if (! peekTokenClass(EHTokIntConstant)) { |
| 636 | expected("literal integer"); |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | TIntermTyped* vecSize; |
| 641 | if (! acceptLiteral(vecSize)) |
| 642 | return false; |
| 643 | |
| 644 | const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst(); |
| 645 | |
| 646 | new(&type) TType(basicType, EvqTemporary, vecSizeI); |
| 647 | |
| 648 | if (vecSizeI == 1) |
| 649 | type.makeVector(); |
| 650 | |
| 651 | if (!acceptTokenClass(EHTokRightAngle)) { |
| 652 | expected("right angle bracket"); |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | return true; |
| 657 | } |
| 658 | |
| 659 | // matrix_template_type |
| 660 | // : MATRIX |
| 661 | // | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE |
| 662 | // |
| 663 | bool HlslGrammar::acceptMatrixTemplateType(TType& type) |
| 664 | { |
| 665 | if (! acceptTokenClass(EHTokMatrix)) |
| 666 | return false; |
| 667 | |
| 668 | if (! acceptTokenClass(EHTokLeftAngle)) { |
| 669 | // in HLSL, 'matrix' alone means float4x4. |
| 670 | new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4); |
| 671 | return true; |
| 672 | } |
| 673 | |
| 674 | TBasicType basicType; |
| 675 | if (! acceptTemplateType(basicType)) { |
| 676 | expected("scalar type"); |
| 677 | return false; |
| 678 | } |
| 679 | |
| 680 | // COMMA |
| 681 | if (! acceptTokenClass(EHTokComma)) { |
| 682 | expected(","); |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | // integer rows |
| 687 | if (! peekTokenClass(EHTokIntConstant)) { |
| 688 | expected("literal integer"); |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | TIntermTyped* rows; |
| 693 | if (! acceptLiteral(rows)) |
| 694 | return false; |
| 695 | |
| 696 | // COMMA |
| 697 | if (! acceptTokenClass(EHTokComma)) { |
| 698 | expected(","); |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | // integer cols |
| 703 | if (! peekTokenClass(EHTokIntConstant)) { |
| 704 | expected("literal integer"); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | TIntermTyped* cols; |
| 709 | if (! acceptLiteral(cols)) |
| 710 | return false; |
| 711 | |
| 712 | new(&type) TType(basicType, EvqTemporary, 0, |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 713 | rows->getAsConstantUnion()->getConstArray()[0].getIConst(), |
| 714 | cols->getAsConstantUnion()->getConstArray()[0].getIConst()); |
LoopDawg | 6daaa4f | 2016-06-23 19:13:48 -0600 | [diff] [blame] | 715 | |
| 716 | if (!acceptTokenClass(EHTokRightAngle)) { |
| 717 | expected("right angle bracket"); |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | return true; |
| 722 | } |
| 723 | |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 724 | // annotations |
| 725 | // : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 726 | // |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 727 | bool HlslGrammar::acceptAnnotations(TQualifier&) |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 728 | { |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 729 | if (! acceptTokenClass(EHTokLeftAngle)) |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 730 | return false; |
| 731 | |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 732 | // note that we are nesting a name space |
| 733 | parseContext.nestAnnotations(); |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 734 | |
| 735 | // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE |
| 736 | do { |
| 737 | // eat any extra SEMI_COLON; don't know if the grammar calls for this or not |
| 738 | while (acceptTokenClass(EHTokSemicolon)) |
| 739 | ; |
| 740 | |
| 741 | if (acceptTokenClass(EHTokRightAngle)) |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 742 | break; |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 743 | |
| 744 | // declaration |
| 745 | TIntermNode* node; |
| 746 | if (! acceptDeclaration(node)) { |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 747 | expected("declaration in annotation"); |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 748 | return false; |
| 749 | } |
| 750 | } while (true); |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 751 | |
| 752 | parseContext.unnestAnnotations(); |
| 753 | return true; |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 754 | } |
LoopDawg | 6daaa4f | 2016-06-23 19:13:48 -0600 | [diff] [blame] | 755 | |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 756 | // sampler_type |
| 757 | // : SAMPLER |
| 758 | // | SAMPLER1D |
| 759 | // | SAMPLER2D |
| 760 | // | SAMPLER3D |
| 761 | // | SAMPLERCUBE |
| 762 | // | SAMPLERSTATE |
| 763 | // | SAMPLERCOMPARISONSTATE |
| 764 | bool HlslGrammar::acceptSamplerType(TType& type) |
| 765 | { |
| 766 | // read sampler type |
| 767 | const EHlslTokenClass samplerType = peek(); |
| 768 | |
LoopDawg | a78b029 | 2016-07-19 14:28:05 -0600 | [diff] [blame] | 769 | // TODO: for DX9 |
LoopDawg | 5d58fae | 2016-07-15 11:22:24 -0600 | [diff] [blame] | 770 | // TSamplerDim dim = EsdNone; |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 771 | |
LoopDawg | a78b029 | 2016-07-19 14:28:05 -0600 | [diff] [blame] | 772 | bool isShadow = false; |
| 773 | |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 774 | switch (samplerType) { |
| 775 | case EHTokSampler: break; |
LoopDawg | 5d58fae | 2016-07-15 11:22:24 -0600 | [diff] [blame] | 776 | case EHTokSampler1d: /*dim = Esd1D*/; break; |
| 777 | case EHTokSampler2d: /*dim = Esd2D*/; break; |
| 778 | case EHTokSampler3d: /*dim = Esd3D*/; break; |
| 779 | case EHTokSamplerCube: /*dim = EsdCube*/; break; |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 780 | case EHTokSamplerState: break; |
LoopDawg | a78b029 | 2016-07-19 14:28:05 -0600 | [diff] [blame] | 781 | case EHTokSamplerComparisonState: isShadow = true; break; |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 782 | default: |
| 783 | return false; // not a sampler declaration |
| 784 | } |
| 785 | |
| 786 | advanceToken(); // consume the sampler type keyword |
| 787 | |
| 788 | TArraySizes* arraySizes = nullptr; // TODO: array |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 789 | |
| 790 | TSampler sampler; |
LoopDawg | a78b029 | 2016-07-19 14:28:05 -0600 | [diff] [blame] | 791 | sampler.setPureSampler(isShadow); |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 792 | |
| 793 | type.shallowCopy(TType(sampler, EvqUniform, arraySizes)); |
| 794 | |
| 795 | return true; |
| 796 | } |
| 797 | |
| 798 | // texture_type |
| 799 | // | BUFFER |
| 800 | // | TEXTURE1D |
| 801 | // | TEXTURE1DARRAY |
| 802 | // | TEXTURE2D |
| 803 | // | TEXTURE2DARRAY |
| 804 | // | TEXTURE3D |
| 805 | // | TEXTURECUBE |
| 806 | // | TEXTURECUBEARRAY |
| 807 | // | TEXTURE2DMS |
| 808 | // | TEXTURE2DMSARRAY |
| 809 | bool HlslGrammar::acceptTextureType(TType& type) |
| 810 | { |
| 811 | const EHlslTokenClass textureType = peek(); |
| 812 | |
| 813 | TSamplerDim dim = EsdNone; |
| 814 | bool array = false; |
| 815 | bool ms = false; |
| 816 | |
| 817 | switch (textureType) { |
| 818 | case EHTokBuffer: dim = EsdBuffer; break; |
| 819 | case EHTokTexture1d: dim = Esd1D; break; |
| 820 | case EHTokTexture1darray: dim = Esd1D; array = true; break; |
| 821 | case EHTokTexture2d: dim = Esd2D; break; |
| 822 | case EHTokTexture2darray: dim = Esd2D; array = true; break; |
| 823 | case EHTokTexture3d: dim = Esd3D; break; |
| 824 | case EHTokTextureCube: dim = EsdCube; break; |
| 825 | case EHTokTextureCubearray: dim = EsdCube; array = true; break; |
| 826 | case EHTokTexture2DMS: dim = Esd2D; ms = true; break; |
| 827 | case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break; |
| 828 | default: |
| 829 | return false; // not a texture declaration |
| 830 | } |
| 831 | |
| 832 | advanceToken(); // consume the texture object keyword |
| 833 | |
| 834 | TType txType(EbtFloat, EvqUniform, 4); // default type is float4 |
| 835 | |
| 836 | TIntermTyped* msCount = nullptr; |
| 837 | |
| 838 | // texture type: required for multisample types! |
| 839 | if (acceptTokenClass(EHTokLeftAngle)) { |
| 840 | if (! acceptType(txType)) { |
| 841 | expected("scalar or vector type"); |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | const TBasicType basicRetType = txType.getBasicType() ; |
| 846 | |
| 847 | if (basicRetType != EbtFloat && basicRetType != EbtUint && basicRetType != EbtInt) { |
| 848 | unimplemented("basic type in texture"); |
| 849 | return false; |
| 850 | } |
| 851 | |
steve-lunarg | d53f717 | 2016-07-27 15:46:48 -0600 | [diff] [blame] | 852 | // Buffers can handle small mats if they fit in 4 components |
| 853 | if (dim == EsdBuffer && txType.isMatrix()) { |
| 854 | if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) { |
| 855 | expected("components < 4 in matrix buffer type"); |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | // TODO: except we don't handle it yet... |
| 860 | unimplemented("matrix type in buffer"); |
| 861 | return false; |
| 862 | } |
| 863 | |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 864 | if (!txType.isScalar() && !txType.isVector()) { |
| 865 | expected("scalar or vector type"); |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | if (txType.getVectorSize() != 1 && txType.getVectorSize() != 4) { |
| 870 | // TODO: handle vec2/3 types |
| 871 | expected("vector size not yet supported in texture type"); |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | if (ms && acceptTokenClass(EHTokComma)) { |
| 876 | // read sample count for multisample types, if given |
| 877 | if (! peekTokenClass(EHTokIntConstant)) { |
| 878 | expected("multisample count"); |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | if (! acceptLiteral(msCount)) // should never fail, since we just found an integer |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | if (! acceptTokenClass(EHTokRightAngle)) { |
| 887 | expected("right angle bracket"); |
| 888 | return false; |
| 889 | } |
| 890 | } else if (ms) { |
| 891 | expected("texture type for multisample"); |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | TArraySizes* arraySizes = nullptr; |
| 896 | const bool shadow = txType.isScalar() || (txType.isVector() && txType.getVectorSize() == 1); |
| 897 | |
| 898 | TSampler sampler; |
steve-lunarg | d53f717 | 2016-07-27 15:46:48 -0600 | [diff] [blame] | 899 | |
| 900 | // Buffers are combined. |
| 901 | if (dim == EsdBuffer) { |
| 902 | sampler.set(txType.getBasicType(), dim, array); |
| 903 | } else { |
| 904 | // DX10 textures are separated. TODO: DX9. |
| 905 | sampler.setTexture(txType.getBasicType(), dim, array, shadow, ms); |
| 906 | } |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 907 | |
| 908 | type.shallowCopy(TType(sampler, EvqUniform, arraySizes)); |
| 909 | |
| 910 | return true; |
| 911 | } |
| 912 | |
| 913 | |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 914 | // If token is for a type, update 'type' with the type information, |
| 915 | // and return true and advance. |
| 916 | // Otherwise, return false, and don't advance |
| 917 | bool HlslGrammar::acceptType(TType& type) |
| 918 | { |
John Kessenich | 9c86c6a | 2016-05-03 22:49:24 -0600 | [diff] [blame] | 919 | switch (peek()) { |
LoopDawg | 6daaa4f | 2016-06-23 19:13:48 -0600 | [diff] [blame] | 920 | case EHTokVector: |
| 921 | return acceptVectorTemplateType(type); |
| 922 | break; |
| 923 | |
| 924 | case EHTokMatrix: |
| 925 | return acceptMatrixTemplateType(type); |
| 926 | break; |
| 927 | |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 928 | case EHTokSampler: // fall through |
| 929 | case EHTokSampler1d: // ... |
| 930 | case EHTokSampler2d: // ... |
| 931 | case EHTokSampler3d: // ... |
| 932 | case EHTokSamplerCube: // ... |
| 933 | case EHTokSamplerState: // ... |
| 934 | case EHTokSamplerComparisonState: // ... |
| 935 | return acceptSamplerType(type); |
| 936 | break; |
| 937 | |
| 938 | case EHTokBuffer: // fall through |
| 939 | case EHTokTexture1d: // ... |
| 940 | case EHTokTexture1darray: // ... |
| 941 | case EHTokTexture2d: // ... |
| 942 | case EHTokTexture2darray: // ... |
| 943 | case EHTokTexture3d: // ... |
| 944 | case EHTokTextureCube: // ... |
| 945 | case EHTokTextureCubearray: // ... |
| 946 | case EHTokTexture2DMS: // ... |
| 947 | case EHTokTexture2DMSarray: // ... |
| 948 | return acceptTextureType(type); |
| 949 | break; |
| 950 | |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 951 | case EHTokStruct: |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 952 | case EHTokCBuffer: |
| 953 | case EHTokTBuffer: |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 954 | return acceptStruct(type); |
| 955 | break; |
| 956 | |
| 957 | case EHTokIdentifier: |
| 958 | // An identifier could be for a user-defined type. |
| 959 | // Note we cache the symbol table lookup, to save for a later rule |
| 960 | // when this is not a type. |
| 961 | token.symbol = parseContext.symbolTable.find(*token.string); |
| 962 | if (token.symbol && token.symbol->getAsVariable() && token.symbol->getAsVariable()->isUserType()) { |
| 963 | type.shallowCopy(token.symbol->getType()); |
| 964 | advanceToken(); |
| 965 | return true; |
| 966 | } else |
| 967 | return false; |
| 968 | |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 969 | case EHTokVoid: |
| 970 | new(&type) TType(EbtVoid); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 971 | break; |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 972 | |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 973 | case EHTokString: |
| 974 | new(&type) TType(EbtString); |
| 975 | break; |
| 976 | |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 977 | case EHTokFloat: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 978 | new(&type) TType(EbtFloat); |
| 979 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 980 | case EHTokFloat1: |
| 981 | new(&type) TType(EbtFloat); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 982 | type.makeVector(); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 983 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 984 | case EHTokFloat2: |
| 985 | new(&type) TType(EbtFloat, EvqTemporary, 2); |
| 986 | break; |
| 987 | case EHTokFloat3: |
| 988 | new(&type) TType(EbtFloat, EvqTemporary, 3); |
| 989 | break; |
| 990 | case EHTokFloat4: |
| 991 | new(&type) TType(EbtFloat, EvqTemporary, 4); |
| 992 | break; |
| 993 | |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 994 | case EHTokDouble: |
| 995 | new(&type) TType(EbtDouble); |
| 996 | break; |
| 997 | case EHTokDouble1: |
| 998 | new(&type) TType(EbtDouble); |
| 999 | type.makeVector(); |
| 1000 | break; |
| 1001 | case EHTokDouble2: |
| 1002 | new(&type) TType(EbtDouble, EvqTemporary, 2); |
| 1003 | break; |
| 1004 | case EHTokDouble3: |
| 1005 | new(&type) TType(EbtDouble, EvqTemporary, 3); |
| 1006 | break; |
| 1007 | case EHTokDouble4: |
| 1008 | new(&type) TType(EbtDouble, EvqTemporary, 4); |
| 1009 | break; |
| 1010 | |
| 1011 | case EHTokInt: |
| 1012 | case EHTokDword: |
| 1013 | new(&type) TType(EbtInt); |
| 1014 | break; |
| 1015 | case EHTokInt1: |
| 1016 | new(&type) TType(EbtInt); |
| 1017 | type.makeVector(); |
| 1018 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1019 | case EHTokInt2: |
| 1020 | new(&type) TType(EbtInt, EvqTemporary, 2); |
| 1021 | break; |
| 1022 | case EHTokInt3: |
| 1023 | new(&type) TType(EbtInt, EvqTemporary, 3); |
| 1024 | break; |
| 1025 | case EHTokInt4: |
| 1026 | new(&type) TType(EbtInt, EvqTemporary, 4); |
| 1027 | break; |
| 1028 | |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1029 | case EHTokUint: |
| 1030 | new(&type) TType(EbtUint); |
| 1031 | break; |
| 1032 | case EHTokUint1: |
| 1033 | new(&type) TType(EbtUint); |
| 1034 | type.makeVector(); |
| 1035 | break; |
| 1036 | case EHTokUint2: |
| 1037 | new(&type) TType(EbtUint, EvqTemporary, 2); |
| 1038 | break; |
| 1039 | case EHTokUint3: |
| 1040 | new(&type) TType(EbtUint, EvqTemporary, 3); |
| 1041 | break; |
| 1042 | case EHTokUint4: |
| 1043 | new(&type) TType(EbtUint, EvqTemporary, 4); |
| 1044 | break; |
| 1045 | |
LoopDawg | 6daaa4f | 2016-06-23 19:13:48 -0600 | [diff] [blame] | 1046 | |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1047 | case EHTokBool: |
| 1048 | new(&type) TType(EbtBool); |
| 1049 | break; |
| 1050 | case EHTokBool1: |
| 1051 | new(&type) TType(EbtBool); |
| 1052 | type.makeVector(); |
| 1053 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1054 | case EHTokBool2: |
| 1055 | new(&type) TType(EbtBool, EvqTemporary, 2); |
| 1056 | break; |
| 1057 | case EHTokBool3: |
| 1058 | new(&type) TType(EbtBool, EvqTemporary, 3); |
| 1059 | break; |
| 1060 | case EHTokBool4: |
| 1061 | new(&type) TType(EbtBool, EvqTemporary, 4); |
| 1062 | break; |
| 1063 | |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1064 | case EHTokInt1x1: |
| 1065 | new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1); |
| 1066 | break; |
| 1067 | case EHTokInt1x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1068 | new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1069 | break; |
| 1070 | case EHTokInt1x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1071 | new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1072 | break; |
| 1073 | case EHTokInt1x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1074 | new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1075 | break; |
| 1076 | case EHTokInt2x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1077 | new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1078 | break; |
| 1079 | case EHTokInt2x2: |
| 1080 | new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2); |
| 1081 | break; |
| 1082 | case EHTokInt2x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1083 | new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1084 | break; |
| 1085 | case EHTokInt2x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1086 | new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1087 | break; |
| 1088 | case EHTokInt3x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1089 | new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1090 | break; |
| 1091 | case EHTokInt3x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1092 | new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1093 | break; |
| 1094 | case EHTokInt3x3: |
| 1095 | new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3); |
| 1096 | break; |
| 1097 | case EHTokInt3x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1098 | new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1099 | break; |
| 1100 | case EHTokInt4x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1101 | new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1102 | break; |
| 1103 | case EHTokInt4x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1104 | new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1105 | break; |
| 1106 | case EHTokInt4x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1107 | new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1108 | break; |
| 1109 | case EHTokInt4x4: |
| 1110 | new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4); |
| 1111 | break; |
| 1112 | |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1113 | case EHTokUint1x1: |
| 1114 | new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1); |
| 1115 | break; |
| 1116 | case EHTokUint1x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1117 | new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1118 | break; |
| 1119 | case EHTokUint1x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1120 | new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1121 | break; |
| 1122 | case EHTokUint1x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1123 | new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1124 | break; |
| 1125 | case EHTokUint2x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1126 | new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1127 | break; |
| 1128 | case EHTokUint2x2: |
| 1129 | new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2); |
| 1130 | break; |
| 1131 | case EHTokUint2x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1132 | new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1133 | break; |
| 1134 | case EHTokUint2x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1135 | new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1136 | break; |
| 1137 | case EHTokUint3x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1138 | new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1139 | break; |
| 1140 | case EHTokUint3x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1141 | new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1142 | break; |
| 1143 | case EHTokUint3x3: |
| 1144 | new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3); |
| 1145 | break; |
| 1146 | case EHTokUint3x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1147 | new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1148 | break; |
| 1149 | case EHTokUint4x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1150 | new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1151 | break; |
| 1152 | case EHTokUint4x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1153 | new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1154 | break; |
| 1155 | case EHTokUint4x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1156 | new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1157 | break; |
| 1158 | case EHTokUint4x4: |
| 1159 | new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4); |
| 1160 | break; |
| 1161 | |
| 1162 | case EHTokBool1x1: |
| 1163 | new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1); |
| 1164 | break; |
| 1165 | case EHTokBool1x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1166 | new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1167 | break; |
| 1168 | case EHTokBool1x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1169 | new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1170 | break; |
| 1171 | case EHTokBool1x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1172 | new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1173 | break; |
| 1174 | case EHTokBool2x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1175 | new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1176 | break; |
| 1177 | case EHTokBool2x2: |
| 1178 | new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2); |
| 1179 | break; |
| 1180 | case EHTokBool2x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1181 | new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1182 | break; |
| 1183 | case EHTokBool2x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1184 | new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1185 | break; |
| 1186 | case EHTokBool3x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1187 | new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1188 | break; |
| 1189 | case EHTokBool3x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1190 | new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1191 | break; |
| 1192 | case EHTokBool3x3: |
| 1193 | new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3); |
| 1194 | break; |
| 1195 | case EHTokBool3x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1196 | new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1197 | break; |
| 1198 | case EHTokBool4x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1199 | new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1200 | break; |
| 1201 | case EHTokBool4x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1202 | new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1203 | break; |
| 1204 | case EHTokBool4x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1205 | new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3); |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1206 | break; |
| 1207 | case EHTokBool4x4: |
| 1208 | new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4); |
| 1209 | break; |
| 1210 | |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1211 | case EHTokFloat1x1: |
| 1212 | new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1); |
| 1213 | break; |
| 1214 | case EHTokFloat1x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1215 | new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1216 | break; |
| 1217 | case EHTokFloat1x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1218 | new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1219 | break; |
| 1220 | case EHTokFloat1x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1221 | new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1222 | break; |
| 1223 | case EHTokFloat2x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1224 | new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1225 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1226 | case EHTokFloat2x2: |
| 1227 | new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2); |
| 1228 | break; |
| 1229 | case EHTokFloat2x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1230 | new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1231 | break; |
| 1232 | case EHTokFloat2x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1233 | new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1234 | break; |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1235 | case EHTokFloat3x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1236 | new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1237 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1238 | case EHTokFloat3x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1239 | new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | case EHTokFloat3x3: |
| 1242 | new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3); |
| 1243 | break; |
| 1244 | case EHTokFloat3x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1245 | new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1246 | break; |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1247 | case EHTokFloat4x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1248 | new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1249 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1250 | case EHTokFloat4x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1251 | new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1252 | break; |
| 1253 | case EHTokFloat4x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1254 | new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1255 | break; |
| 1256 | case EHTokFloat4x4: |
| 1257 | new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4); |
| 1258 | break; |
| 1259 | |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1260 | case EHTokDouble1x1: |
| 1261 | new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1); |
| 1262 | break; |
| 1263 | case EHTokDouble1x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1264 | new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1265 | break; |
| 1266 | case EHTokDouble1x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1267 | new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1268 | break; |
| 1269 | case EHTokDouble1x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1270 | new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1271 | break; |
| 1272 | case EHTokDouble2x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1273 | new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1274 | break; |
| 1275 | case EHTokDouble2x2: |
| 1276 | new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2); |
| 1277 | break; |
| 1278 | case EHTokDouble2x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1279 | new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1280 | break; |
| 1281 | case EHTokDouble2x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1282 | new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1283 | break; |
| 1284 | case EHTokDouble3x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1285 | new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1286 | break; |
| 1287 | case EHTokDouble3x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1288 | new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1289 | break; |
| 1290 | case EHTokDouble3x3: |
| 1291 | new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3); |
| 1292 | break; |
| 1293 | case EHTokDouble3x4: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1294 | new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1295 | break; |
| 1296 | case EHTokDouble4x1: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1297 | new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1298 | break; |
| 1299 | case EHTokDouble4x2: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1300 | new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1301 | break; |
| 1302 | case EHTokDouble4x3: |
steve-lunarg | 297ae21 | 2016-08-24 14:36:13 -0600 | [diff] [blame] | 1303 | new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3); |
John Kessenich | 0133c12 | 2016-05-20 12:17:26 -0600 | [diff] [blame] | 1304 | break; |
| 1305 | case EHTokDouble4x4: |
| 1306 | new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4); |
| 1307 | break; |
| 1308 | |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1309 | default: |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | advanceToken(); |
| 1314 | |
| 1315 | return true; |
| 1316 | } |
| 1317 | |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1318 | // struct |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1319 | // : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE |
| 1320 | // | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE |
| 1321 | // |
| 1322 | // struct_type |
| 1323 | // : STRUCT |
| 1324 | // | CBUFFER |
| 1325 | // | TBUFFER |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1326 | // |
| 1327 | bool HlslGrammar::acceptStruct(TType& type) |
| 1328 | { |
John Kessenich | b804de6 | 2016-09-05 12:19:18 -0600 | [diff] [blame] | 1329 | // This storage qualifier will tell us whether it's an AST |
| 1330 | // block type or just a generic structure type. |
| 1331 | TStorageQualifier storageQualifier = EvqTemporary; |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1332 | |
| 1333 | // CBUFFER |
| 1334 | if (acceptTokenClass(EHTokCBuffer)) |
John Kessenich | b804de6 | 2016-09-05 12:19:18 -0600 | [diff] [blame] | 1335 | storageQualifier = EvqUniform; |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1336 | // TBUFFER |
| 1337 | else if (acceptTokenClass(EHTokTBuffer)) |
John Kessenich | b804de6 | 2016-09-05 12:19:18 -0600 | [diff] [blame] | 1338 | storageQualifier = EvqBuffer; |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1339 | // STRUCT |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1340 | else if (! acceptTokenClass(EHTokStruct)) |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1341 | return false; |
| 1342 | |
| 1343 | // IDENTIFIER |
| 1344 | TString structName = ""; |
| 1345 | if (peekTokenClass(EHTokIdentifier)) { |
| 1346 | structName = *token.string; |
| 1347 | advanceToken(); |
| 1348 | } |
| 1349 | |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1350 | // post_decls |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 1351 | TQualifier postDeclQualifier; |
| 1352 | postDeclQualifier.clear(); |
| 1353 | acceptPostDecls(postDeclQualifier); |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1354 | |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1355 | // LEFT_BRACE |
| 1356 | if (! acceptTokenClass(EHTokLeftBrace)) { |
| 1357 | expected("{"); |
| 1358 | return false; |
| 1359 | } |
| 1360 | |
| 1361 | // struct_declaration_list |
| 1362 | TTypeList* typeList; |
| 1363 | if (! acceptStructDeclarationList(typeList)) { |
| 1364 | expected("struct member declarations"); |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
| 1368 | // RIGHT_BRACE |
| 1369 | if (! acceptTokenClass(EHTokRightBrace)) { |
| 1370 | expected("}"); |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
| 1374 | // create the user-defined type |
John Kessenich | b804de6 | 2016-09-05 12:19:18 -0600 | [diff] [blame] | 1375 | if (storageQualifier == EvqTemporary) |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1376 | new(&type) TType(typeList, structName); |
John Kessenich | b804de6 | 2016-09-05 12:19:18 -0600 | [diff] [blame] | 1377 | else { |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 1378 | postDeclQualifier.storage = storageQualifier; |
| 1379 | new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock |
John Kessenich | b804de6 | 2016-09-05 12:19:18 -0600 | [diff] [blame] | 1380 | } |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1381 | |
John Kessenich | 3d157c5 | 2016-07-25 16:05:33 -0600 | [diff] [blame] | 1382 | // If it was named, which means the type can be reused later, add |
| 1383 | // it to the symbol table. (Unless it's a block, in which |
| 1384 | // case the name is not a type.) |
| 1385 | if (type.getBasicType() != EbtBlock && structName.size() > 0) { |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1386 | TVariable* userTypeDef = new TVariable(&structName, type, true); |
| 1387 | if (! parseContext.symbolTable.insert(*userTypeDef)) |
| 1388 | parseContext.error(token.loc, "redefinition", structName.c_str(), "struct"); |
| 1389 | } |
| 1390 | |
| 1391 | return true; |
| 1392 | } |
| 1393 | |
| 1394 | // struct_declaration_list |
| 1395 | // : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ... |
| 1396 | // |
| 1397 | // struct_declaration |
| 1398 | // : fully_specified_type struct_declarator COMMA struct_declarator ... |
| 1399 | // |
| 1400 | // struct_declarator |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 1401 | // : IDENTIFIER post_decls |
| 1402 | // | IDENTIFIER array_specifier post_decls |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1403 | // |
| 1404 | bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList) |
| 1405 | { |
| 1406 | typeList = new TTypeList(); |
| 1407 | |
| 1408 | do { |
| 1409 | // success on seeing the RIGHT_BRACE coming up |
| 1410 | if (peekTokenClass(EHTokRightBrace)) |
| 1411 | return true; |
| 1412 | |
| 1413 | // struct_declaration |
| 1414 | |
| 1415 | // fully_specified_type |
| 1416 | TType memberType; |
| 1417 | if (! acceptFullySpecifiedType(memberType)) { |
| 1418 | expected("member type"); |
| 1419 | return false; |
| 1420 | } |
| 1421 | |
| 1422 | // struct_declarator COMMA struct_declarator ... |
| 1423 | do { |
| 1424 | // peek IDENTIFIER |
| 1425 | if (! peekTokenClass(EHTokIdentifier)) { |
| 1426 | expected("member name"); |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | // add it to the list of members |
| 1431 | TTypeLoc member = { new TType(EbtVoid), token.loc }; |
| 1432 | member.type->shallowCopy(memberType); |
| 1433 | member.type->setFieldName(*token.string); |
| 1434 | typeList->push_back(member); |
| 1435 | |
| 1436 | // accept IDENTIFIER |
| 1437 | advanceToken(); |
| 1438 | |
| 1439 | // array_specifier |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1440 | TArraySizes* arraySizes = nullptr; |
| 1441 | acceptArraySpecifier(arraySizes); |
| 1442 | if (arraySizes) |
| 1443 | typeList->back().type->newArraySizes(*arraySizes); |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1444 | |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 1445 | acceptPostDecls(member.type->getQualifier()); |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 1446 | |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1447 | // success on seeing the SEMICOLON coming up |
| 1448 | if (peekTokenClass(EHTokSemicolon)) |
| 1449 | break; |
| 1450 | |
| 1451 | // COMMA |
| 1452 | if (! acceptTokenClass(EHTokComma)) { |
| 1453 | expected(","); |
| 1454 | return false; |
| 1455 | } |
| 1456 | |
| 1457 | } while (true); |
| 1458 | |
| 1459 | // SEMI_COLON |
| 1460 | if (! acceptTokenClass(EHTokSemicolon)) { |
| 1461 | expected(";"); |
| 1462 | return false; |
| 1463 | } |
| 1464 | |
| 1465 | } while (true); |
| 1466 | } |
| 1467 | |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1468 | // function_parameters |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 1469 | // : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1470 | // | LEFT_PAREN VOID RIGHT_PAREN |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1471 | // |
| 1472 | bool HlslGrammar::acceptFunctionParameters(TFunction& function) |
| 1473 | { |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 1474 | // LEFT_PAREN |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1475 | if (! acceptTokenClass(EHTokLeftParen)) |
| 1476 | return false; |
| 1477 | |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1478 | // VOID RIGHT_PAREN |
| 1479 | if (! acceptTokenClass(EHTokVoid)) { |
| 1480 | do { |
| 1481 | // parameter_declaration |
| 1482 | if (! acceptParameterDeclaration(function)) |
| 1483 | break; |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1484 | |
John Kessenich | 71351de | 2016-06-08 12:50:56 -0600 | [diff] [blame] | 1485 | // COMMA |
| 1486 | if (! acceptTokenClass(EHTokComma)) |
| 1487 | break; |
| 1488 | } while (true); |
| 1489 | } |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1490 | |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 1491 | // RIGHT_PAREN |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1492 | if (! acceptTokenClass(EHTokRightParen)) { |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 1493 | expected(")"); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1494 | return false; |
| 1495 | } |
| 1496 | |
| 1497 | return true; |
| 1498 | } |
| 1499 | |
| 1500 | // parameter_declaration |
John Kessenich | c3387d3 | 2016-06-17 14:21:02 -0600 | [diff] [blame] | 1501 | // : fully_specified_type post_decls |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1502 | // | fully_specified_type identifier array_specifier post_decls |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1503 | // |
| 1504 | bool HlslGrammar::acceptParameterDeclaration(TFunction& function) |
| 1505 | { |
| 1506 | // fully_specified_type |
| 1507 | TType* type = new TType; |
| 1508 | if (! acceptFullySpecifiedType(*type)) |
| 1509 | return false; |
| 1510 | |
| 1511 | // identifier |
John Kessenich | aecd497 | 2016-03-14 10:46:34 -0600 | [diff] [blame] | 1512 | HlslToken idToken; |
| 1513 | acceptIdentifier(idToken); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1514 | |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1515 | // array_specifier |
| 1516 | TArraySizes* arraySizes = nullptr; |
| 1517 | acceptArraySpecifier(arraySizes); |
| 1518 | if (arraySizes) |
| 1519 | type->newArraySizes(*arraySizes); |
| 1520 | |
| 1521 | // post_decls |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 1522 | acceptPostDecls(type->getQualifier()); |
John Kessenich | c3387d3 | 2016-06-17 14:21:02 -0600 | [diff] [blame] | 1523 | |
John Kessenich | 5aa59e2 | 2016-06-17 15:50:47 -0600 | [diff] [blame] | 1524 | parseContext.paramFix(*type); |
| 1525 | |
John Kessenich | aecd497 | 2016-03-14 10:46:34 -0600 | [diff] [blame] | 1526 | TParameter param = { idToken.string, type }; |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1527 | function.addParameter(param); |
| 1528 | |
| 1529 | return true; |
| 1530 | } |
| 1531 | |
| 1532 | // Do the work to create the function definition in addition to |
| 1533 | // parsing the body (compound_statement). |
| 1534 | bool HlslGrammar::acceptFunctionDefinition(TFunction& function, TIntermNode*& node) |
| 1535 | { |
John Kessenich | a305166 | 2016-09-02 19:13:36 -0600 | [diff] [blame] | 1536 | TFunction& functionDeclarator = parseContext.handleFunctionDeclarator(token.loc, function, false /* not prototype */); |
John Kessenich | 1a4b775 | 2016-09-02 19:05:24 -0600 | [diff] [blame] | 1537 | TSourceLoc loc = token.loc; |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1538 | |
John Kessenich | 077e052 | 2016-06-09 02:02:17 -0600 | [diff] [blame] | 1539 | // This does a pushScope() |
John Kessenich | a305166 | 2016-09-02 19:13:36 -0600 | [diff] [blame] | 1540 | node = parseContext.handleFunctionDefinition(loc, functionDeclarator); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1541 | |
| 1542 | // compound_statement |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 1543 | TIntermNode* functionBody = nullptr; |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1544 | if (acceptCompoundStatement(functionBody)) { |
John Kessenich | a305166 | 2016-09-02 19:13:36 -0600 | [diff] [blame] | 1545 | parseContext.handleFunctionBody(loc, functionDeclarator, functionBody, node); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1546 | return true; |
| 1547 | } |
| 1548 | |
| 1549 | return false; |
| 1550 | } |
| 1551 | |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 1552 | // Accept an expression with parenthesis around it, where |
| 1553 | // the parenthesis ARE NOT expression parenthesis, but the |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 1554 | // syntactically required ones like in "if ( expression )". |
| 1555 | // |
| 1556 | // Also accepts a declaration expression; "if (int a = expression)". |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 1557 | // |
| 1558 | // Note this one is not set up to be speculative; as it gives |
| 1559 | // errors if not found. |
| 1560 | // |
| 1561 | bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression) |
| 1562 | { |
| 1563 | // LEFT_PAREN |
| 1564 | if (! acceptTokenClass(EHTokLeftParen)) |
| 1565 | expected("("); |
| 1566 | |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 1567 | bool decl = false; |
| 1568 | TIntermNode* declNode = nullptr; |
| 1569 | decl = acceptControlDeclaration(declNode); |
| 1570 | if (decl) { |
| 1571 | if (declNode == nullptr || declNode->getAsTyped() == nullptr) { |
| 1572 | expected("initialized declaration"); |
| 1573 | return false; |
| 1574 | } else |
| 1575 | expression = declNode->getAsTyped(); |
| 1576 | } else { |
| 1577 | // no declaration |
| 1578 | if (! acceptExpression(expression)) { |
| 1579 | expected("expression"); |
| 1580 | return false; |
| 1581 | } |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | // RIGHT_PAREN |
| 1585 | if (! acceptTokenClass(EHTokRightParen)) |
| 1586 | expected(")"); |
| 1587 | |
| 1588 | return true; |
| 1589 | } |
| 1590 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1591 | // The top-level full expression recognizer. |
| 1592 | // |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1593 | // expression |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1594 | // : assignment_expression COMMA assignment_expression COMMA assignment_expression ... |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1595 | // |
| 1596 | bool HlslGrammar::acceptExpression(TIntermTyped*& node) |
| 1597 | { |
LoopDawg | ef764a2 | 2016-06-03 09:17:51 -0600 | [diff] [blame] | 1598 | node = nullptr; |
| 1599 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1600 | // assignment_expression |
| 1601 | if (! acceptAssignmentExpression(node)) |
| 1602 | return false; |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1603 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1604 | if (! peekTokenClass(EHTokComma)) |
| 1605 | return true; |
| 1606 | |
| 1607 | do { |
| 1608 | // ... COMMA |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1609 | TSourceLoc loc = token.loc; |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1610 | advanceToken(); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1611 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1612 | // ... assignment_expression |
| 1613 | TIntermTyped* rightNode = nullptr; |
| 1614 | if (! acceptAssignmentExpression(rightNode)) { |
| 1615 | expected("assignment expression"); |
| 1616 | return false; |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1617 | } |
| 1618 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1619 | node = intermediate.addComma(node, rightNode, loc); |
| 1620 | |
| 1621 | if (! peekTokenClass(EHTokComma)) |
| 1622 | return true; |
| 1623 | } while (true); |
| 1624 | } |
| 1625 | |
John Kessenich | 0735424 | 2016-07-01 19:58:06 -0600 | [diff] [blame] | 1626 | // initializer |
| 1627 | // : LEFT_BRACE initializer_list RIGHT_BRACE |
| 1628 | // |
| 1629 | // initializer_list |
| 1630 | // : assignment_expression COMMA assignment_expression COMMA ... |
| 1631 | // |
| 1632 | bool HlslGrammar::acceptInitializer(TIntermTyped*& node) |
| 1633 | { |
| 1634 | // LEFT_BRACE |
| 1635 | if (! acceptTokenClass(EHTokLeftBrace)) |
| 1636 | return false; |
| 1637 | |
| 1638 | // initializer_list |
| 1639 | TSourceLoc loc = token.loc; |
| 1640 | node = nullptr; |
| 1641 | do { |
| 1642 | // assignment_expression |
| 1643 | TIntermTyped* expr; |
| 1644 | if (! acceptAssignmentExpression(expr)) { |
| 1645 | expected("assignment expression in initializer list"); |
| 1646 | return false; |
| 1647 | } |
| 1648 | node = intermediate.growAggregate(node, expr, loc); |
| 1649 | |
| 1650 | // COMMA |
steve-lunarg | fe5a3ff | 2016-07-30 10:36:09 -0600 | [diff] [blame] | 1651 | if (acceptTokenClass(EHTokComma)) { |
| 1652 | if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma |
| 1653 | return true; |
John Kessenich | 0735424 | 2016-07-01 19:58:06 -0600 | [diff] [blame] | 1654 | continue; |
steve-lunarg | fe5a3ff | 2016-07-30 10:36:09 -0600 | [diff] [blame] | 1655 | } |
John Kessenich | 0735424 | 2016-07-01 19:58:06 -0600 | [diff] [blame] | 1656 | |
| 1657 | // RIGHT_BRACE |
| 1658 | if (acceptTokenClass(EHTokRightBrace)) |
| 1659 | return true; |
| 1660 | |
| 1661 | expected(", or }"); |
| 1662 | return false; |
| 1663 | } while (true); |
| 1664 | } |
| 1665 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1666 | // Accept an assignment expression, where assignment operations |
John Kessenich | 0735424 | 2016-07-01 19:58:06 -0600 | [diff] [blame] | 1667 | // associate right-to-left. That is, it is implicit, for example |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1668 | // |
| 1669 | // a op (b op (c op d)) |
| 1670 | // |
| 1671 | // assigment_expression |
John Kessenich | 00957f8 | 2016-07-27 10:39:57 -0600 | [diff] [blame] | 1672 | // : initializer |
| 1673 | // | conditional_expression |
| 1674 | // | conditional_expression assign_op conditional_expression assign_op conditional_expression ... |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1675 | // |
| 1676 | bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node) |
| 1677 | { |
John Kessenich | 0735424 | 2016-07-01 19:58:06 -0600 | [diff] [blame] | 1678 | // initializer |
| 1679 | if (peekTokenClass(EHTokLeftBrace)) { |
| 1680 | if (acceptInitializer(node)) |
| 1681 | return true; |
| 1682 | |
| 1683 | expected("initializer"); |
| 1684 | return false; |
| 1685 | } |
| 1686 | |
John Kessenich | 00957f8 | 2016-07-27 10:39:57 -0600 | [diff] [blame] | 1687 | // conditional_expression |
| 1688 | if (! acceptConditionalExpression(node)) |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1689 | return false; |
| 1690 | |
John Kessenich | 0735424 | 2016-07-01 19:58:06 -0600 | [diff] [blame] | 1691 | // assignment operation? |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1692 | TOperator assignOp = HlslOpMap::assignment(peek()); |
| 1693 | if (assignOp == EOpNull) |
| 1694 | return true; |
| 1695 | |
John Kessenich | 00957f8 | 2016-07-27 10:39:57 -0600 | [diff] [blame] | 1696 | // assign_op |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1697 | TSourceLoc loc = token.loc; |
| 1698 | advanceToken(); |
| 1699 | |
John Kessenich | 00957f8 | 2016-07-27 10:39:57 -0600 | [diff] [blame] | 1700 | // conditional_expression assign_op conditional_expression ... |
| 1701 | // Done by recursing this function, which automatically |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1702 | // gets the right-to-left associativity. |
| 1703 | TIntermTyped* rightNode = nullptr; |
| 1704 | if (! acceptAssignmentExpression(rightNode)) { |
| 1705 | expected("assignment expression"); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 1706 | return false; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
John Kessenich | d21baed | 2016-09-16 03:05:12 -0600 | [diff] [blame] | 1709 | node = parseContext.handleAssign(loc, assignOp, node, rightNode); |
John Kessenich | fea226b | 2016-07-28 17:53:56 -0600 | [diff] [blame] | 1710 | if (node == nullptr) { |
| 1711 | parseContext.error(loc, "could not create assignment", "", ""); |
| 1712 | return false; |
| 1713 | } |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1714 | |
| 1715 | if (! peekTokenClass(EHTokComma)) |
| 1716 | return true; |
| 1717 | |
| 1718 | return true; |
| 1719 | } |
| 1720 | |
John Kessenich | 00957f8 | 2016-07-27 10:39:57 -0600 | [diff] [blame] | 1721 | // Accept a conditional expression, which associates right-to-left, |
| 1722 | // accomplished by the "true" expression calling down to lower |
| 1723 | // precedence levels than this level. |
| 1724 | // |
| 1725 | // conditional_expression |
| 1726 | // : binary_expression |
| 1727 | // | binary_expression QUESTION expression COLON assignment_expression |
| 1728 | // |
| 1729 | bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node) |
| 1730 | { |
| 1731 | // binary_expression |
| 1732 | if (! acceptBinaryExpression(node, PlLogicalOr)) |
| 1733 | return false; |
| 1734 | |
| 1735 | if (! acceptTokenClass(EHTokQuestion)) |
| 1736 | return true; |
| 1737 | |
| 1738 | TIntermTyped* trueNode = nullptr; |
| 1739 | if (! acceptExpression(trueNode)) { |
| 1740 | expected("expression after ?"); |
| 1741 | return false; |
| 1742 | } |
| 1743 | TSourceLoc loc = token.loc; |
| 1744 | |
| 1745 | if (! acceptTokenClass(EHTokColon)) { |
| 1746 | expected(":"); |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
| 1750 | TIntermTyped* falseNode = nullptr; |
| 1751 | if (! acceptAssignmentExpression(falseNode)) { |
| 1752 | expected("expression after :"); |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
| 1756 | node = intermediate.addSelection(node, trueNode, falseNode, loc); |
| 1757 | |
| 1758 | return true; |
| 1759 | } |
| 1760 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1761 | // Accept a binary expression, for binary operations that |
| 1762 | // associate left-to-right. This is, it is implicit, for example |
| 1763 | // |
| 1764 | // ((a op b) op c) op d |
| 1765 | // |
| 1766 | // binary_expression |
| 1767 | // : expression op expression op expression ... |
| 1768 | // |
| 1769 | // where 'expression' is the next higher level in precedence. |
| 1770 | // |
| 1771 | bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel) |
| 1772 | { |
| 1773 | if (precedenceLevel > PlMul) |
| 1774 | return acceptUnaryExpression(node); |
| 1775 | |
| 1776 | // assignment_expression |
| 1777 | if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1))) |
| 1778 | return false; |
| 1779 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1780 | do { |
John Kessenich | 64076ed | 2016-07-28 21:43:17 -0600 | [diff] [blame] | 1781 | TOperator op = HlslOpMap::binary(peek()); |
| 1782 | PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op); |
| 1783 | if (tokenLevel < precedenceLevel) |
| 1784 | return true; |
| 1785 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1786 | // ... op |
| 1787 | TSourceLoc loc = token.loc; |
| 1788 | advanceToken(); |
| 1789 | |
| 1790 | // ... expression |
| 1791 | TIntermTyped* rightNode = nullptr; |
| 1792 | if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) { |
| 1793 | expected("expression"); |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
| 1797 | node = intermediate.addBinaryMath(op, node, rightNode, loc); |
John Kessenich | fea226b | 2016-07-28 17:53:56 -0600 | [diff] [blame] | 1798 | if (node == nullptr) { |
| 1799 | parseContext.error(loc, "Could not perform requested binary operation", "", ""); |
| 1800 | return false; |
| 1801 | } |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1802 | } while (true); |
| 1803 | } |
| 1804 | |
| 1805 | // unary_expression |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 1806 | // : (type) unary_expression |
| 1807 | // | + unary_expression |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1808 | // | - unary_expression |
| 1809 | // | ! unary_expression |
| 1810 | // | ~ unary_expression |
| 1811 | // | ++ unary_expression |
| 1812 | // | -- unary_expression |
| 1813 | // | postfix_expression |
| 1814 | // |
| 1815 | bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node) |
| 1816 | { |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 1817 | // (type) unary_expression |
| 1818 | // Have to look two steps ahead, because this could be, e.g., a |
| 1819 | // postfix_expression instead, since that also starts with at "(". |
| 1820 | if (acceptTokenClass(EHTokLeftParen)) { |
| 1821 | TType castType; |
| 1822 | if (acceptType(castType)) { |
steve-lunarg | 5964c64 | 2016-07-30 07:38:55 -0600 | [diff] [blame] | 1823 | if (acceptTokenClass(EHTokRightParen)) { |
| 1824 | // We've matched "(type)" now, get the expression to cast |
| 1825 | TSourceLoc loc = token.loc; |
| 1826 | if (! acceptUnaryExpression(node)) |
| 1827 | return false; |
| 1828 | |
| 1829 | // Hook it up like a constructor |
| 1830 | TFunction* constructorFunction = parseContext.handleConstructorCall(loc, castType); |
| 1831 | if (constructorFunction == nullptr) { |
| 1832 | expected("type that can be constructed"); |
| 1833 | return false; |
| 1834 | } |
| 1835 | TIntermTyped* arguments = nullptr; |
| 1836 | parseContext.handleFunctionArgument(constructorFunction, arguments, node); |
| 1837 | node = parseContext.handleFunctionCall(loc, constructorFunction, arguments); |
| 1838 | |
| 1839 | return true; |
| 1840 | } else { |
| 1841 | // This could be a parenthesized constructor, ala (int(3)), and we just accepted |
| 1842 | // the '(int' part. We must back up twice. |
| 1843 | recedeToken(); |
| 1844 | recedeToken(); |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 1845 | } |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 1846 | } else { |
| 1847 | // This isn't a type cast, but it still started "(", so if it is a |
| 1848 | // unary expression, it can only be a postfix_expression, so try that. |
| 1849 | // Back it up first. |
| 1850 | recedeToken(); |
| 1851 | return acceptPostfixExpression(node); |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | // peek for "op unary_expression" |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1856 | TOperator unaryOp = HlslOpMap::preUnary(peek()); |
| 1857 | |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 1858 | // postfix_expression (if no unary operator) |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1859 | if (unaryOp == EOpNull) |
| 1860 | return acceptPostfixExpression(node); |
| 1861 | |
| 1862 | // op unary_expression |
| 1863 | TSourceLoc loc = token.loc; |
| 1864 | advanceToken(); |
| 1865 | if (! acceptUnaryExpression(node)) |
| 1866 | return false; |
| 1867 | |
| 1868 | // + is a no-op |
| 1869 | if (unaryOp == EOpAdd) |
| 1870 | return true; |
| 1871 | |
| 1872 | node = intermediate.addUnaryMath(unaryOp, node, loc); |
| 1873 | |
| 1874 | return node != nullptr; |
| 1875 | } |
| 1876 | |
| 1877 | // postfix_expression |
| 1878 | // : LEFT_PAREN expression RIGHT_PAREN |
| 1879 | // | literal |
| 1880 | // | constructor |
| 1881 | // | identifier |
| 1882 | // | function_call |
| 1883 | // | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET |
| 1884 | // | postfix_expression DOT IDENTIFIER |
| 1885 | // | postfix_expression INC_OP |
| 1886 | // | postfix_expression DEC_OP |
| 1887 | // |
| 1888 | bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node) |
| 1889 | { |
| 1890 | // Not implemented as self-recursive: |
| 1891 | // The logical "right recursion" is done with an loop at the end |
| 1892 | |
| 1893 | // idToken will pick up either a variable or a function name in a function call |
| 1894 | HlslToken idToken; |
| 1895 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 1896 | // Find something before the postfix operations, as they can't operate |
| 1897 | // on nothing. So, no "return true", they fall through, only "return false". |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1898 | if (acceptTokenClass(EHTokLeftParen)) { |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 1899 | // LEFT_PAREN expression RIGHT_PAREN |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1900 | if (! acceptExpression(node)) { |
| 1901 | expected("expression"); |
| 1902 | return false; |
| 1903 | } |
| 1904 | if (! acceptTokenClass(EHTokRightParen)) { |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 1905 | expected(")"); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1906 | return false; |
| 1907 | } |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1908 | } else if (acceptLiteral(node)) { |
| 1909 | // literal (nothing else to do yet), go on to the |
| 1910 | } else if (acceptConstructor(node)) { |
| 1911 | // constructor (nothing else to do yet) |
| 1912 | } else if (acceptIdentifier(idToken)) { |
| 1913 | // identifier or function_call name |
| 1914 | if (! peekTokenClass(EHTokLeftParen)) { |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 1915 | node = parseContext.handleVariable(idToken.loc, idToken.symbol, token.string); |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1916 | } else if (acceptFunctionCall(idToken, node)) { |
| 1917 | // function_call (nothing else to do yet) |
| 1918 | } else { |
| 1919 | expected("function call arguments"); |
| 1920 | return false; |
| 1921 | } |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 1922 | } else { |
| 1923 | // nothing found, can't post operate |
| 1924 | return false; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 1927 | // Something was found, chain as many postfix operations as exist. |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1928 | do { |
| 1929 | TSourceLoc loc = token.loc; |
| 1930 | TOperator postOp = HlslOpMap::postUnary(peek()); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1931 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1932 | // Consume only a valid post-unary operator, otherwise we are done. |
| 1933 | switch (postOp) { |
| 1934 | case EOpIndexDirectStruct: |
| 1935 | case EOpIndexIndirect: |
| 1936 | case EOpPostIncrement: |
| 1937 | case EOpPostDecrement: |
| 1938 | advanceToken(); |
| 1939 | break; |
| 1940 | default: |
| 1941 | return true; |
| 1942 | } |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1943 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1944 | // We have a valid post-unary operator, process it. |
| 1945 | switch (postOp) { |
| 1946 | case EOpIndexDirectStruct: |
John Kessenich | 93a162a | 2016-06-17 17:16:27 -0600 | [diff] [blame] | 1947 | { |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1948 | // DOT IDENTIFIER |
| 1949 | // includes swizzles and struct members |
John Kessenich | 93a162a | 2016-06-17 17:16:27 -0600 | [diff] [blame] | 1950 | HlslToken field; |
| 1951 | if (! acceptIdentifier(field)) { |
| 1952 | expected("swizzle or member"); |
| 1953 | return false; |
| 1954 | } |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 1955 | |
| 1956 | TIntermTyped* base = node; // preserve for method function calls |
John Kessenich | 93a162a | 2016-06-17 17:16:27 -0600 | [diff] [blame] | 1957 | node = parseContext.handleDotDereference(field.loc, node, *field.string); |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 1958 | |
| 1959 | // In the event of a method node, we look for an open paren and accept the function call. |
| 1960 | if (node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) { |
| 1961 | if (! acceptFunctionCall(field, node, base)) { |
| 1962 | expected("function parameters"); |
| 1963 | return false; |
| 1964 | } |
| 1965 | } |
| 1966 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1967 | break; |
John Kessenich | 93a162a | 2016-06-17 17:16:27 -0600 | [diff] [blame] | 1968 | } |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1969 | case EOpIndexIndirect: |
| 1970 | { |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1971 | // LEFT_BRACKET integer_expression RIGHT_BRACKET |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1972 | TIntermTyped* indexNode = nullptr; |
| 1973 | if (! acceptExpression(indexNode) || |
| 1974 | ! peekTokenClass(EHTokRightBracket)) { |
| 1975 | expected("expression followed by ']'"); |
| 1976 | return false; |
| 1977 | } |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1978 | advanceToken(); |
| 1979 | node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode); |
| 1980 | break; |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1981 | } |
| 1982 | case EOpPostIncrement: |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1983 | // INC_OP |
| 1984 | // fall through |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1985 | case EOpPostDecrement: |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 1986 | // DEC_OP |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 1987 | node = intermediate.addUnaryMath(postOp, node, loc); |
| 1988 | break; |
| 1989 | default: |
| 1990 | assert(0); |
| 1991 | break; |
| 1992 | } |
| 1993 | } while (true); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 1996 | // constructor |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 1997 | // : type argument_list |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 1998 | // |
| 1999 | bool HlslGrammar::acceptConstructor(TIntermTyped*& node) |
| 2000 | { |
| 2001 | // type |
| 2002 | TType type; |
| 2003 | if (acceptType(type)) { |
| 2004 | TFunction* constructorFunction = parseContext.handleConstructorCall(token.loc, type); |
| 2005 | if (constructorFunction == nullptr) |
| 2006 | return false; |
| 2007 | |
| 2008 | // arguments |
John Kessenich | 4678ca9 | 2016-05-13 09:33:42 -0600 | [diff] [blame] | 2009 | TIntermTyped* arguments = nullptr; |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 2010 | if (! acceptArguments(constructorFunction, arguments)) { |
| 2011 | expected("constructor arguments"); |
| 2012 | return false; |
| 2013 | } |
| 2014 | |
| 2015 | // hook it up |
| 2016 | node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments); |
| 2017 | |
| 2018 | return true; |
| 2019 | } |
| 2020 | |
| 2021 | return false; |
| 2022 | } |
| 2023 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 2024 | // The function_call identifier was already recognized, and passed in as idToken. |
| 2025 | // |
| 2026 | // function_call |
| 2027 | // : [idToken] arguments |
| 2028 | // |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 2029 | bool HlslGrammar::acceptFunctionCall(HlslToken idToken, TIntermTyped*& node, TIntermTyped* base) |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 2030 | { |
John Kessenich | 4678ca9 | 2016-05-13 09:33:42 -0600 | [diff] [blame] | 2031 | // arguments |
| 2032 | TFunction* function = new TFunction(idToken.string, TType(EbtVoid)); |
| 2033 | TIntermTyped* arguments = nullptr; |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 2034 | |
| 2035 | // methods have an implicit first argument of the calling object. |
| 2036 | if (base != nullptr) |
| 2037 | parseContext.handleFunctionArgument(function, arguments, base); |
| 2038 | |
John Kessenich | 4678ca9 | 2016-05-13 09:33:42 -0600 | [diff] [blame] | 2039 | if (! acceptArguments(function, arguments)) |
| 2040 | return false; |
| 2041 | |
| 2042 | node = parseContext.handleFunctionCall(idToken.loc, function, arguments); |
| 2043 | |
| 2044 | return true; |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 2045 | } |
| 2046 | |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2047 | // arguments |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2048 | // : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2049 | // |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 2050 | // The arguments are pushed onto the 'function' argument list and |
| 2051 | // onto the 'arguments' aggregate. |
| 2052 | // |
John Kessenich | 4678ca9 | 2016-05-13 09:33:42 -0600 | [diff] [blame] | 2053 | bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments) |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2054 | { |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2055 | // LEFT_PAREN |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2056 | if (! acceptTokenClass(EHTokLeftParen)) |
| 2057 | return false; |
| 2058 | |
| 2059 | do { |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 2060 | // expression |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2061 | TIntermTyped* arg; |
John Kessenich | 4678ca9 | 2016-05-13 09:33:42 -0600 | [diff] [blame] | 2062 | if (! acceptAssignmentExpression(arg)) |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2063 | break; |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 2064 | |
| 2065 | // hook it up |
| 2066 | parseContext.handleFunctionArgument(function, arguments, arg); |
| 2067 | |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2068 | // COMMA |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2069 | if (! acceptTokenClass(EHTokComma)) |
| 2070 | break; |
| 2071 | } while (true); |
| 2072 | |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2073 | // RIGHT_PAREN |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2074 | if (! acceptTokenClass(EHTokRightParen)) { |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2075 | expected(")"); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | return true; |
| 2080 | } |
| 2081 | |
| 2082 | bool HlslGrammar::acceptLiteral(TIntermTyped*& node) |
| 2083 | { |
| 2084 | switch (token.tokenClass) { |
| 2085 | case EHTokIntConstant: |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2086 | node = intermediate.addConstantUnion(token.i, token.loc, true); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2087 | break; |
steve-lunarg | 2de3291 | 2016-07-28 14:49:48 -0600 | [diff] [blame] | 2088 | case EHTokUintConstant: |
| 2089 | node = intermediate.addConstantUnion(token.u, token.loc, true); |
| 2090 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2091 | case EHTokFloatConstant: |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2092 | node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2093 | break; |
| 2094 | case EHTokDoubleConstant: |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2095 | node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2096 | break; |
| 2097 | case EHTokBoolConstant: |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2098 | node = intermediate.addConstantUnion(token.b, token.loc, true); |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2099 | break; |
John Kessenich | 86f7138 | 2016-09-19 20:23:18 -0600 | [diff] [blame] | 2100 | case EHTokStringConstant: |
| 2101 | node = nullptr; |
| 2102 | break; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2103 | |
| 2104 | default: |
| 2105 | return false; |
| 2106 | } |
| 2107 | |
| 2108 | advanceToken(); |
| 2109 | |
| 2110 | return true; |
| 2111 | } |
| 2112 | |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2113 | // compound_statement |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 2114 | // : LEFT_CURLY statement statement ... RIGHT_CURLY |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2115 | // |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2116 | bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement) |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2117 | { |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2118 | TIntermAggregate* compoundStatement = nullptr; |
| 2119 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 2120 | // LEFT_CURLY |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2121 | if (! acceptTokenClass(EHTokLeftBrace)) |
| 2122 | return false; |
| 2123 | |
| 2124 | // statement statement ... |
| 2125 | TIntermNode* statement = nullptr; |
| 2126 | while (acceptStatement(statement)) { |
John Kessenich | d02dc5d | 2016-07-01 00:04:11 -0600 | [diff] [blame] | 2127 | TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr; |
| 2128 | if (branch != nullptr && (branch->getFlowOp() == EOpCase || |
| 2129 | branch->getFlowOp() == EOpDefault)) { |
| 2130 | // hook up individual subsequences within a switch statement |
| 2131 | parseContext.wrapupSwitchSubsequence(compoundStatement, statement); |
| 2132 | compoundStatement = nullptr; |
| 2133 | } else { |
| 2134 | // hook it up to the growing compound statement |
| 2135 | compoundStatement = intermediate.growAggregate(compoundStatement, statement); |
| 2136 | } |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2137 | } |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 2138 | if (compoundStatement) |
| 2139 | compoundStatement->setOperator(EOpSequence); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2140 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2141 | retStatement = compoundStatement; |
| 2142 | |
John Kessenich | 34fb036 | 2016-05-03 23:17:20 -0600 | [diff] [blame] | 2143 | // RIGHT_CURLY |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2144 | return acceptTokenClass(EHTokRightBrace); |
| 2145 | } |
| 2146 | |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2147 | bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement) |
| 2148 | { |
| 2149 | parseContext.pushScope(); |
John Kessenich | 077e052 | 2016-06-09 02:02:17 -0600 | [diff] [blame] | 2150 | bool result = acceptStatement(statement); |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2151 | parseContext.popScope(); |
| 2152 | |
| 2153 | return result; |
| 2154 | } |
| 2155 | |
John Kessenich | 077e052 | 2016-06-09 02:02:17 -0600 | [diff] [blame] | 2156 | bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement) |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2157 | { |
John Kessenich | 077e052 | 2016-06-09 02:02:17 -0600 | [diff] [blame] | 2158 | parseContext.pushScope(); |
| 2159 | bool result = acceptCompoundStatement(statement); |
| 2160 | parseContext.popScope(); |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2161 | |
| 2162 | return result; |
| 2163 | } |
| 2164 | |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2165 | // statement |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2166 | // : attributes attributed_statement |
| 2167 | // |
| 2168 | // attributed_statement |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2169 | // : compound_statement |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2170 | // | SEMICOLON |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2171 | // | expression SEMICOLON |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2172 | // | declaration_statement |
| 2173 | // | selection_statement |
| 2174 | // | switch_statement |
| 2175 | // | case_label |
| 2176 | // | iteration_statement |
| 2177 | // | jump_statement |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2178 | // |
| 2179 | bool HlslGrammar::acceptStatement(TIntermNode*& statement) |
| 2180 | { |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2181 | statement = nullptr; |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2182 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2183 | // attributes |
| 2184 | acceptAttributes(); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2185 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2186 | // attributed_statement |
| 2187 | switch (peek()) { |
| 2188 | case EHTokLeftBrace: |
John Kessenich | 077e052 | 2016-06-09 02:02:17 -0600 | [diff] [blame] | 2189 | return acceptScopedCompoundStatement(statement); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2190 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2191 | case EHTokIf: |
| 2192 | return acceptSelectionStatement(statement); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2193 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2194 | case EHTokSwitch: |
| 2195 | return acceptSwitchStatement(statement); |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2196 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2197 | case EHTokFor: |
| 2198 | case EHTokDo: |
| 2199 | case EHTokWhile: |
| 2200 | return acceptIterationStatement(statement); |
| 2201 | |
| 2202 | case EHTokContinue: |
| 2203 | case EHTokBreak: |
| 2204 | case EHTokDiscard: |
| 2205 | case EHTokReturn: |
| 2206 | return acceptJumpStatement(statement); |
| 2207 | |
| 2208 | case EHTokCase: |
| 2209 | return acceptCaseLabel(statement); |
John Kessenich | d02dc5d | 2016-07-01 00:04:11 -0600 | [diff] [blame] | 2210 | case EHTokDefault: |
| 2211 | return acceptDefaultLabel(statement); |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2212 | |
| 2213 | case EHTokSemicolon: |
| 2214 | return acceptTokenClass(EHTokSemicolon); |
| 2215 | |
| 2216 | case EHTokRightBrace: |
| 2217 | // Performance: not strictly necessary, but stops a bunch of hunting early, |
| 2218 | // and is how sequences of statements end. |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2219 | return false; |
| 2220 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2221 | default: |
| 2222 | { |
| 2223 | // declaration |
| 2224 | if (acceptDeclaration(statement)) |
| 2225 | return true; |
| 2226 | |
| 2227 | // expression |
| 2228 | TIntermTyped* node; |
| 2229 | if (acceptExpression(node)) |
| 2230 | statement = node; |
| 2231 | else |
| 2232 | return false; |
| 2233 | |
| 2234 | // SEMICOLON (following an expression) |
| 2235 | if (! acceptTokenClass(EHTokSemicolon)) { |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2236 | expected(";"); |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2237 | return false; |
| 2238 | } |
| 2239 | } |
| 2240 | } |
| 2241 | |
John Kessenich | 5f934b0 | 2016-03-13 17:58:25 -0600 | [diff] [blame] | 2242 | return true; |
John Kessenich | 87142c7 | 2016-03-12 20:24:24 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2245 | // attributes |
| 2246 | // : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET |
| 2247 | // |
| 2248 | // attribute: |
| 2249 | // : UNROLL |
| 2250 | // | UNROLL LEFT_PAREN literal RIGHT_PAREN |
| 2251 | // | FASTOPT |
| 2252 | // | ALLOW_UAV_CONDITION |
| 2253 | // | BRANCH |
| 2254 | // | FLATTEN |
| 2255 | // | FORCECASE |
| 2256 | // | CALL |
| 2257 | // |
| 2258 | void HlslGrammar::acceptAttributes() |
| 2259 | { |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2260 | // For now, accept the [ XXX(X) ] syntax, but drop. |
| 2261 | // TODO: subset to correct set? Pass on? |
| 2262 | do { |
| 2263 | // LEFT_BRACKET? |
| 2264 | if (! acceptTokenClass(EHTokLeftBracket)) |
| 2265 | return; |
| 2266 | |
| 2267 | // attribute |
| 2268 | if (peekTokenClass(EHTokIdentifier)) { |
| 2269 | // 'token.string' is the attribute |
| 2270 | advanceToken(); |
| 2271 | } else if (! peekTokenClass(EHTokRightBracket)) { |
| 2272 | expected("identifier"); |
| 2273 | advanceToken(); |
| 2274 | } |
| 2275 | |
| 2276 | // (x) |
| 2277 | if (acceptTokenClass(EHTokLeftParen)) { |
| 2278 | TIntermTyped* node; |
| 2279 | if (! acceptLiteral(node)) |
| 2280 | expected("literal"); |
| 2281 | // 'node' has the literal in it |
| 2282 | if (! acceptTokenClass(EHTokRightParen)) |
| 2283 | expected(")"); |
| 2284 | } |
| 2285 | |
| 2286 | // RIGHT_BRACKET |
| 2287 | if (acceptTokenClass(EHTokRightBracket)) |
| 2288 | continue; |
| 2289 | |
| 2290 | expected("]"); |
| 2291 | return; |
| 2292 | |
| 2293 | } while (true); |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2294 | } |
| 2295 | |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2296 | // selection_statement |
| 2297 | // : IF LEFT_PAREN expression RIGHT_PAREN statement |
| 2298 | // : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement |
| 2299 | // |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2300 | bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement) |
| 2301 | { |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 2302 | TSourceLoc loc = token.loc; |
| 2303 | |
| 2304 | // IF |
| 2305 | if (! acceptTokenClass(EHTokIf)) |
| 2306 | return false; |
| 2307 | |
| 2308 | // so that something declared in the condition is scoped to the lifetimes |
| 2309 | // of the then-else statements |
| 2310 | parseContext.pushScope(); |
| 2311 | |
| 2312 | // LEFT_PAREN expression RIGHT_PAREN |
| 2313 | TIntermTyped* condition; |
| 2314 | if (! acceptParenExpression(condition)) |
| 2315 | return false; |
| 2316 | |
| 2317 | // create the child statements |
| 2318 | TIntermNodePair thenElse = { nullptr, nullptr }; |
| 2319 | |
| 2320 | // then statement |
| 2321 | if (! acceptScopedStatement(thenElse.node1)) { |
| 2322 | expected("then statement"); |
| 2323 | return false; |
| 2324 | } |
| 2325 | |
| 2326 | // ELSE |
| 2327 | if (acceptTokenClass(EHTokElse)) { |
| 2328 | // else statement |
| 2329 | if (! acceptScopedStatement(thenElse.node2)) { |
| 2330 | expected("else statement"); |
| 2331 | return false; |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | // Put the pieces together |
| 2336 | statement = intermediate.addSelection(condition, thenElse, loc); |
| 2337 | parseContext.popScope(); |
| 2338 | |
| 2339 | return true; |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2340 | } |
| 2341 | |
John Kessenich | d02dc5d | 2016-07-01 00:04:11 -0600 | [diff] [blame] | 2342 | // switch_statement |
| 2343 | // : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement |
| 2344 | // |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2345 | bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement) |
| 2346 | { |
John Kessenich | d02dc5d | 2016-07-01 00:04:11 -0600 | [diff] [blame] | 2347 | // SWITCH |
| 2348 | TSourceLoc loc = token.loc; |
| 2349 | if (! acceptTokenClass(EHTokSwitch)) |
| 2350 | return false; |
| 2351 | |
| 2352 | // LEFT_PAREN expression RIGHT_PAREN |
| 2353 | parseContext.pushScope(); |
| 2354 | TIntermTyped* switchExpression; |
| 2355 | if (! acceptParenExpression(switchExpression)) { |
| 2356 | parseContext.popScope(); |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
| 2360 | // compound_statement |
| 2361 | parseContext.pushSwitchSequence(new TIntermSequence); |
| 2362 | bool statementOkay = acceptCompoundStatement(statement); |
| 2363 | if (statementOkay) |
| 2364 | statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr); |
| 2365 | |
| 2366 | parseContext.popSwitchSequence(); |
| 2367 | parseContext.popScope(); |
| 2368 | |
| 2369 | return statementOkay; |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2370 | } |
| 2371 | |
John Kessenich | 119f8f6 | 2016-06-05 15:44:07 -0600 | [diff] [blame] | 2372 | // iteration_statement |
| 2373 | // : WHILE LEFT_PAREN condition RIGHT_PAREN statement |
| 2374 | // | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON |
| 2375 | // | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement |
| 2376 | // |
| 2377 | // Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen. |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2378 | bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement) |
| 2379 | { |
John Kessenich | 119f8f6 | 2016-06-05 15:44:07 -0600 | [diff] [blame] | 2380 | TSourceLoc loc = token.loc; |
| 2381 | TIntermTyped* condition = nullptr; |
| 2382 | |
| 2383 | EHlslTokenClass loop = peek(); |
| 2384 | assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile); |
| 2385 | |
| 2386 | // WHILE or DO or FOR |
| 2387 | advanceToken(); |
| 2388 | |
| 2389 | switch (loop) { |
| 2390 | case EHTokWhile: |
| 2391 | // so that something declared in the condition is scoped to the lifetime |
| 2392 | // of the while sub-statement |
| 2393 | parseContext.pushScope(); |
| 2394 | parseContext.nestLooping(); |
| 2395 | |
| 2396 | // LEFT_PAREN condition RIGHT_PAREN |
| 2397 | if (! acceptParenExpression(condition)) |
| 2398 | return false; |
| 2399 | |
| 2400 | // statement |
| 2401 | if (! acceptScopedStatement(statement)) { |
| 2402 | expected("while sub-statement"); |
| 2403 | return false; |
| 2404 | } |
| 2405 | |
| 2406 | parseContext.unnestLooping(); |
| 2407 | parseContext.popScope(); |
| 2408 | |
| 2409 | statement = intermediate.addLoop(statement, condition, nullptr, true, loc); |
| 2410 | |
| 2411 | return true; |
| 2412 | |
| 2413 | case EHTokDo: |
| 2414 | parseContext.nestLooping(); |
| 2415 | |
| 2416 | if (! acceptTokenClass(EHTokLeftBrace)) |
| 2417 | expected("{"); |
| 2418 | |
| 2419 | // statement |
| 2420 | if (! peekTokenClass(EHTokRightBrace) && ! acceptScopedStatement(statement)) { |
| 2421 | expected("do sub-statement"); |
| 2422 | return false; |
| 2423 | } |
| 2424 | |
| 2425 | if (! acceptTokenClass(EHTokRightBrace)) |
| 2426 | expected("}"); |
| 2427 | |
| 2428 | // WHILE |
| 2429 | if (! acceptTokenClass(EHTokWhile)) { |
| 2430 | expected("while"); |
| 2431 | return false; |
| 2432 | } |
| 2433 | |
| 2434 | // LEFT_PAREN condition RIGHT_PAREN |
| 2435 | TIntermTyped* condition; |
| 2436 | if (! acceptParenExpression(condition)) |
| 2437 | return false; |
| 2438 | |
| 2439 | if (! acceptTokenClass(EHTokSemicolon)) |
| 2440 | expected(";"); |
| 2441 | |
| 2442 | parseContext.unnestLooping(); |
| 2443 | |
| 2444 | statement = intermediate.addLoop(statement, condition, 0, false, loc); |
| 2445 | |
| 2446 | return true; |
| 2447 | |
| 2448 | case EHTokFor: |
| 2449 | { |
| 2450 | // LEFT_PAREN |
| 2451 | if (! acceptTokenClass(EHTokLeftParen)) |
| 2452 | expected("("); |
| 2453 | |
| 2454 | // so that something declared in the condition is scoped to the lifetime |
| 2455 | // of the for sub-statement |
| 2456 | parseContext.pushScope(); |
| 2457 | |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 2458 | // initializer |
| 2459 | TIntermNode* initNode = nullptr; |
| 2460 | if (! acceptControlDeclaration(initNode)) { |
| 2461 | TIntermTyped* initExpr = nullptr; |
| 2462 | acceptExpression(initExpr); |
| 2463 | initNode = initExpr; |
| 2464 | } |
| 2465 | // SEMI_COLON |
John Kessenich | 119f8f6 | 2016-06-05 15:44:07 -0600 | [diff] [blame] | 2466 | if (! acceptTokenClass(EHTokSemicolon)) |
| 2467 | expected(";"); |
| 2468 | |
| 2469 | parseContext.nestLooping(); |
| 2470 | |
| 2471 | // condition SEMI_COLON |
| 2472 | acceptExpression(condition); |
| 2473 | if (! acceptTokenClass(EHTokSemicolon)) |
| 2474 | expected(";"); |
| 2475 | |
| 2476 | // iterator SEMI_COLON |
| 2477 | TIntermTyped* iterator = nullptr; |
| 2478 | acceptExpression(iterator); |
| 2479 | if (! acceptTokenClass(EHTokRightParen)) |
| 2480 | expected(")"); |
| 2481 | |
| 2482 | // statement |
| 2483 | if (! acceptScopedStatement(statement)) { |
| 2484 | expected("for sub-statement"); |
| 2485 | return false; |
| 2486 | } |
| 2487 | |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 2488 | statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc); |
John Kessenich | 119f8f6 | 2016-06-05 15:44:07 -0600 | [diff] [blame] | 2489 | |
| 2490 | parseContext.popScope(); |
| 2491 | parseContext.unnestLooping(); |
| 2492 | |
| 2493 | return true; |
| 2494 | } |
| 2495 | |
| 2496 | default: |
| 2497 | return false; |
| 2498 | } |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | // jump_statement |
| 2502 | // : CONTINUE SEMICOLON |
| 2503 | // | BREAK SEMICOLON |
| 2504 | // | DISCARD SEMICOLON |
| 2505 | // | RETURN SEMICOLON |
| 2506 | // | RETURN expression SEMICOLON |
| 2507 | // |
| 2508 | bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement) |
| 2509 | { |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 2510 | EHlslTokenClass jump = peek(); |
| 2511 | switch (jump) { |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2512 | case EHTokContinue: |
| 2513 | case EHTokBreak: |
| 2514 | case EHTokDiscard: |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2515 | case EHTokReturn: |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 2516 | advanceToken(); |
| 2517 | break; |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2518 | default: |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 2519 | // not something we handle in this function |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2520 | return false; |
| 2521 | } |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2522 | |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 2523 | switch (jump) { |
| 2524 | case EHTokContinue: |
| 2525 | statement = intermediate.addBranch(EOpContinue, token.loc); |
| 2526 | break; |
| 2527 | case EHTokBreak: |
| 2528 | statement = intermediate.addBranch(EOpBreak, token.loc); |
| 2529 | break; |
| 2530 | case EHTokDiscard: |
| 2531 | statement = intermediate.addBranch(EOpKill, token.loc); |
| 2532 | break; |
| 2533 | |
| 2534 | case EHTokReturn: |
| 2535 | { |
| 2536 | // expression |
| 2537 | TIntermTyped* node; |
| 2538 | if (acceptExpression(node)) { |
| 2539 | // hook it up |
steve-lunarg | c4a1307 | 2016-08-09 11:28:03 -0600 | [diff] [blame] | 2540 | statement = parseContext.handleReturnValue(token.loc, node); |
John Kessenich | 5bc4d9a | 2016-06-20 01:22:38 -0600 | [diff] [blame] | 2541 | } else |
| 2542 | statement = intermediate.addBranch(EOpReturn, token.loc); |
| 2543 | break; |
| 2544 | } |
| 2545 | |
| 2546 | default: |
| 2547 | assert(0); |
| 2548 | return false; |
| 2549 | } |
| 2550 | |
| 2551 | // SEMICOLON |
| 2552 | if (! acceptTokenClass(EHTokSemicolon)) |
| 2553 | expected(";"); |
| 2554 | |
| 2555 | return true; |
| 2556 | } |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2557 | |
John Kessenich | d02dc5d | 2016-07-01 00:04:11 -0600 | [diff] [blame] | 2558 | // case_label |
| 2559 | // : CASE expression COLON |
| 2560 | // |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2561 | bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement) |
| 2562 | { |
John Kessenich | d02dc5d | 2016-07-01 00:04:11 -0600 | [diff] [blame] | 2563 | TSourceLoc loc = token.loc; |
| 2564 | if (! acceptTokenClass(EHTokCase)) |
| 2565 | return false; |
| 2566 | |
| 2567 | TIntermTyped* expression; |
| 2568 | if (! acceptExpression(expression)) { |
| 2569 | expected("case expression"); |
| 2570 | return false; |
| 2571 | } |
| 2572 | |
| 2573 | if (! acceptTokenClass(EHTokColon)) { |
| 2574 | expected(":"); |
| 2575 | return false; |
| 2576 | } |
| 2577 | |
| 2578 | statement = parseContext.intermediate.addBranch(EOpCase, expression, loc); |
| 2579 | |
| 2580 | return true; |
| 2581 | } |
| 2582 | |
| 2583 | // default_label |
| 2584 | // : DEFAULT COLON |
| 2585 | // |
| 2586 | bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement) |
| 2587 | { |
| 2588 | TSourceLoc loc = token.loc; |
| 2589 | if (! acceptTokenClass(EHTokDefault)) |
| 2590 | return false; |
| 2591 | |
| 2592 | if (! acceptTokenClass(EHTokColon)) { |
| 2593 | expected(":"); |
| 2594 | return false; |
| 2595 | } |
| 2596 | |
| 2597 | statement = parseContext.intermediate.addBranch(EOpDefault, loc); |
| 2598 | |
| 2599 | return true; |
John Kessenich | 21472ae | 2016-06-04 11:46:33 -0600 | [diff] [blame] | 2600 | } |
| 2601 | |
John Kessenich | 19b92ff | 2016-06-19 11:50:34 -0600 | [diff] [blame] | 2602 | // array_specifier |
| 2603 | // : LEFT_BRACKET integer_expression RGHT_BRACKET post_decls // optional |
| 2604 | // |
| 2605 | void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes) |
| 2606 | { |
| 2607 | arraySizes = nullptr; |
| 2608 | |
| 2609 | if (! acceptTokenClass(EHTokLeftBracket)) |
| 2610 | return; |
| 2611 | |
| 2612 | TSourceLoc loc = token.loc; |
| 2613 | TIntermTyped* sizeExpr; |
| 2614 | if (! acceptAssignmentExpression(sizeExpr)) { |
| 2615 | expected("array-sizing expression"); |
| 2616 | return; |
| 2617 | } |
| 2618 | |
| 2619 | if (! acceptTokenClass(EHTokRightBracket)) { |
| 2620 | expected("]"); |
| 2621 | return; |
| 2622 | } |
| 2623 | |
| 2624 | TArraySize arraySize; |
| 2625 | parseContext.arraySizeCheck(loc, sizeExpr, arraySize); |
| 2626 | arraySizes = new TArraySizes; |
| 2627 | arraySizes->addInnerSize(arraySize); |
| 2628 | } |
| 2629 | |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2630 | // post_decls |
John Kessenich | cfd7ce8 | 2016-09-05 16:03:12 -0600 | [diff] [blame] | 2631 | // : COLON semantic // optional |
| 2632 | // COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional |
| 2633 | // COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional |
John Kessenich | e3218e2 | 2016-09-05 14:37:03 -0600 | [diff] [blame] | 2634 | // COLON LAYOUT layout_qualifier_list |
John Kessenich | cfd7ce8 | 2016-09-05 16:03:12 -0600 | [diff] [blame] | 2635 | // annotations // optional |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2636 | // |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 2637 | void HlslGrammar::acceptPostDecls(TQualifier& qualifier) |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2638 | { |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2639 | do { |
| 2640 | // COLON |
| 2641 | if (acceptTokenClass(EHTokColon)) { |
| 2642 | HlslToken idToken; |
John Kessenich | e3218e2 | 2016-09-05 14:37:03 -0600 | [diff] [blame] | 2643 | if (peekTokenClass(EHTokLayout)) |
| 2644 | acceptLayoutQualifierList(qualifier); |
| 2645 | else if (acceptTokenClass(EHTokPackOffset)) { |
John Kessenich | 96e9f47 | 2016-07-29 14:28:39 -0600 | [diff] [blame] | 2646 | // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2647 | if (! acceptTokenClass(EHTokLeftParen)) { |
| 2648 | expected("("); |
| 2649 | return; |
| 2650 | } |
John Kessenich | 82d6baf | 2016-07-29 13:03:05 -0600 | [diff] [blame] | 2651 | HlslToken locationToken; |
| 2652 | if (! acceptIdentifier(locationToken)) { |
| 2653 | expected("c[subcomponent][.component]"); |
| 2654 | return; |
| 2655 | } |
| 2656 | HlslToken componentToken; |
| 2657 | if (acceptTokenClass(EHTokDot)) { |
| 2658 | if (! acceptIdentifier(componentToken)) { |
| 2659 | expected("component"); |
| 2660 | return; |
| 2661 | } |
| 2662 | } |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2663 | if (! acceptTokenClass(EHTokRightParen)) { |
| 2664 | expected(")"); |
| 2665 | break; |
| 2666 | } |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 2667 | parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string); |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2668 | } else if (! acceptIdentifier(idToken)) { |
John Kessenich | e3218e2 | 2016-09-05 14:37:03 -0600 | [diff] [blame] | 2669 | expected("layout, semantic, packoffset, or register"); |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2670 | return; |
| 2671 | } else if (*idToken.string == "register") { |
John Kessenich | cfd7ce8 | 2016-09-05 16:03:12 -0600 | [diff] [blame] | 2672 | // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN |
| 2673 | // LEFT_PAREN |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2674 | if (! acceptTokenClass(EHTokLeftParen)) { |
| 2675 | expected("("); |
| 2676 | return; |
| 2677 | } |
John Kessenich | b38f071 | 2016-07-30 10:29:54 -0600 | [diff] [blame] | 2678 | HlslToken registerDesc; // for Type# |
| 2679 | HlslToken profile; |
John Kessenich | 96e9f47 | 2016-07-29 14:28:39 -0600 | [diff] [blame] | 2680 | if (! acceptIdentifier(registerDesc)) { |
| 2681 | expected("register number description"); |
| 2682 | return; |
| 2683 | } |
John Kessenich | cfd7ce8 | 2016-09-05 16:03:12 -0600 | [diff] [blame] | 2684 | if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) && |
| 2685 | acceptTokenClass(EHTokComma)) { |
John Kessenich | b38f071 | 2016-07-30 10:29:54 -0600 | [diff] [blame] | 2686 | // Then we didn't really see the registerDesc yet, it was |
| 2687 | // actually the profile. Adjust... |
John Kessenich | 96e9f47 | 2016-07-29 14:28:39 -0600 | [diff] [blame] | 2688 | profile = registerDesc; |
| 2689 | if (! acceptIdentifier(registerDesc)) { |
| 2690 | expected("register number description"); |
| 2691 | return; |
| 2692 | } |
| 2693 | } |
John Kessenich | b38f071 | 2016-07-30 10:29:54 -0600 | [diff] [blame] | 2694 | int subComponent = 0; |
| 2695 | if (acceptTokenClass(EHTokLeftBracket)) { |
| 2696 | // LEFT_BRACKET subcomponent RIGHT_BRACKET |
| 2697 | if (! peekTokenClass(EHTokIntConstant)) { |
| 2698 | expected("literal integer"); |
| 2699 | return; |
| 2700 | } |
| 2701 | subComponent = token.i; |
| 2702 | advanceToken(); |
| 2703 | if (! acceptTokenClass(EHTokRightBracket)) { |
| 2704 | expected("]"); |
| 2705 | break; |
| 2706 | } |
| 2707 | } |
John Kessenich | cfd7ce8 | 2016-09-05 16:03:12 -0600 | [diff] [blame] | 2708 | // (COMMA SPACEN)opt |
| 2709 | HlslToken spaceDesc; |
| 2710 | if (acceptTokenClass(EHTokComma)) { |
| 2711 | if (! acceptIdentifier(spaceDesc)) { |
| 2712 | expected ("space identifier"); |
| 2713 | return; |
| 2714 | } |
| 2715 | } |
| 2716 | // RIGHT_PAREN |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2717 | if (! acceptTokenClass(EHTokRightParen)) { |
| 2718 | expected(")"); |
| 2719 | break; |
| 2720 | } |
John Kessenich | cfd7ce8 | 2016-09-05 16:03:12 -0600 | [diff] [blame] | 2721 | parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string); |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2722 | } else { |
| 2723 | // semantic, in idToken.string |
John Kessenich | 7735b94 | 2016-09-05 12:40:06 -0600 | [diff] [blame] | 2724 | parseContext.handleSemantic(idToken.loc, qualifier, *idToken.string); |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2725 | } |
John Kessenich | a1e2d49 | 2016-09-20 13:22:58 -0600 | [diff] [blame^] | 2726 | } else if (peekTokenClass(EHTokLeftAngle)) |
| 2727 | acceptAnnotations(qualifier); |
| 2728 | else |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2729 | break; |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2730 | |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 2731 | } while (true); |
John Kessenich | 078d7f2 | 2016-03-14 10:02:11 -0600 | [diff] [blame] | 2732 | } |
| 2733 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 2734 | } // end namespace glslang |