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