1 //===--- ParseInit.cpp - Initializer Parsing ------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements initializer parsing as specified by C99 6.7.8. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Parse/Parser.h" 15 #include "RAIIObjectsForParser.h" 16 #include "clang/Parse/ParseDiagnostic.h" 17 #include "clang/Sema/Designator.h" 18 #include "clang/Sema/Scope.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/Support/raw_ostream.h" 21 using namespace clang; 22 23 24 /// MayBeDesignationStart - Return true if the current token might be the start 25 /// of a designator. If we can tell it is impossible that it is a designator, 26 /// return false. 27 bool Parser::MayBeDesignationStart() { 28 switch (Tok.getKind()) { 29 default: 30 return false; 31 32 case tok::period: // designator: '.' identifier 33 return true; 34 35 case tok::l_square: { // designator: array-designator 36 if (!PP.getLangOpts().CPlusPlus11) 37 return true; 38 39 // C++11 lambda expressions and C99 designators can be ambiguous all the 40 // way through the closing ']' and to the next character. Handle the easy 41 // cases here, and fall back to tentative parsing if those fail. 42 switch (PP.LookAhead(0).getKind()) { 43 case tok::equal: 44 case tok::r_square: 45 // Definitely starts a lambda expression. 46 return false; 47 48 case tok::amp: 49 case tok::kw_this: 50 case tok::identifier: 51 // We have to do additional analysis, because these could be the 52 // start of a constant expression or a lambda capture list. 53 break; 54 55 default: 56 // Anything not mentioned above cannot occur following a '[' in a 57 // lambda expression. 58 return true; 59 } 60 61 // Handle the complicated case below. 62 break; 63 } 64 case tok::identifier: // designation: identifier ':' 65 return PP.LookAhead(0).is(tok::colon); 66 } 67 68 // Parse up to (at most) the token after the closing ']' to determine 69 // whether this is a C99 designator or a lambda. 70 TentativeParsingAction Tentative(*this); 71 72 LambdaIntroducer Intro; 73 bool SkippedInits = false; 74 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits)); 75 76 if (DiagID) { 77 // If this can't be a lambda capture list, it's a designator. 78 Tentative.Revert(); 79 return true; 80 } 81 82 // Once we hit the closing square bracket, we look at the next 83 // token. If it's an '=', this is a designator. Otherwise, it's a 84 // lambda expression. This decision favors lambdas over the older 85 // GNU designator syntax, which allows one to omit the '=', but is 86 // consistent with GCC. 87 tok::TokenKind Kind = Tok.getKind(); 88 // FIXME: If we didn't skip any inits, parse the lambda from here 89 // rather than throwing away then reparsing the LambdaIntroducer. 90 Tentative.Revert(); 91 return Kind == tok::equal; 92 } 93 94 static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc, 95 Designation &Desig) { 96 // If we have exactly one array designator, this used the GNU 97 // 'designation: array-designator' extension, otherwise there should be no 98 // designators at all! 99 if (Desig.getNumDesignators() == 1 && 100 (Desig.getDesignator(0).isArrayDesignator() || 101 Desig.getDesignator(0).isArrayRangeDesignator())) 102 P.Diag(Loc, diag::ext_gnu_missing_equal_designator); 103 else if (Desig.getNumDesignators() > 0) 104 P.Diag(Loc, diag::err_expected_equal_designator); 105 } 106 107 /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production 108 /// checking to see if the token stream starts with a designator. 109 /// 110 /// designation: 111 /// designator-list '=' 112 /// [GNU] array-designator 113 /// [GNU] identifier ':' 114 /// 115 /// designator-list: 116 /// designator 117 /// designator-list designator 118 /// 119 /// designator: 120 /// array-designator 121 /// '.' identifier 122 /// 123 /// array-designator: 124 /// '[' constant-expression ']' 125 /// [GNU] '[' constant-expression '...' constant-expression ']' 126 /// 127 /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an 128 /// initializer (because it is an expression). We need to consider this case 129 /// when parsing array designators. 130 /// 131 ExprResult Parser::ParseInitializerWithPotentialDesignator() { 132 133 // If this is the old-style GNU extension: 134 // designation ::= identifier ':' 135 // Handle it as a field designator. Otherwise, this must be the start of a 136 // normal expression. 137 if (Tok.is(tok::identifier)) { 138 const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); 139 140 SmallString<256> NewSyntax; 141 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName() 142 << " = "; 143 144 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. 145 146 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); 147 SourceLocation ColonLoc = ConsumeToken(); 148 149 Diag(NameLoc, diag::ext_gnu_old_style_field_designator) 150 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc), 151 NewSyntax.str()); 152 153 Designation D; 154 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); 155 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, 156 ParseInitializer()); 157 } 158 159 // Desig - This is initialized when we see our first designator. We may have 160 // an objc message send with no designator, so we don't want to create this 161 // eagerly. 162 Designation Desig; 163 164 // Parse each designator in the designator list until we find an initializer. 165 while (Tok.is(tok::period) || Tok.is(tok::l_square)) { 166 if (Tok.is(tok::period)) { 167 // designator: '.' identifier 168 SourceLocation DotLoc = ConsumeToken(); 169 170 if (Tok.isNot(tok::identifier)) { 171 Diag(Tok.getLocation(), diag::err_expected_field_designator); 172 return ExprError(); 173 } 174 175 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, 176 Tok.getLocation())); 177 ConsumeToken(); // Eat the identifier. 178 continue; 179 } 180 181 // We must have either an array designator now or an objc message send. 182 assert(Tok.is(tok::l_square) && "Unexpected token!"); 183 184 // Handle the two forms of array designator: 185 // array-designator: '[' constant-expression ']' 186 // array-designator: '[' constant-expression '...' constant-expression ']' 187 // 188 // Also, we have to handle the case where the expression after the 189 // designator an an objc message send: '[' objc-message-expr ']'. 190 // Interesting cases are: 191 // [foo bar] -> objc message send 192 // [foo] -> array designator 193 // [foo ... bar] -> array designator 194 // [4][foo bar] -> obsolete GNU designation with objc message send. 195 // 196 // We do not need to check for an expression starting with [[ here. If it 197 // contains an Objective-C message send, then it is not an ill-formed 198 // attribute. If it is a lambda-expression within an array-designator, then 199 // it will be rejected because a constant-expression cannot begin with a 200 // lambda-expression. 201 InMessageExpressionRAIIObject InMessage(*this, true); 202 203 BalancedDelimiterTracker T(*this, tok::l_square); 204 T.consumeOpen(); 205 SourceLocation StartLoc = T.getOpenLocation(); 206 207 ExprResult Idx; 208 209 // If Objective-C is enabled and this is a typename (class message 210 // send) or send to 'super', parse this as a message send 211 // expression. We handle C++ and C separately, since C++ requires 212 // much more complicated parsing. 213 if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus) { 214 // Send to 'super'. 215 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && 216 NextToken().isNot(tok::period) && 217 getCurScope()->isInObjcMethodScope()) { 218 CheckArrayDesignatorSyntax(*this, StartLoc, Desig); 219 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 220 ConsumeToken(), 221 ParsedType(), 222 nullptr); 223 } 224 225 // Parse the receiver, which is either a type or an expression. 226 bool IsExpr; 227 void *TypeOrExpr; 228 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { 229 SkipUntil(tok::r_square, StopAtSemi); 230 return ExprError(); 231 } 232 233 // If the receiver was a type, we have a class message; parse 234 // the rest of it. 235 if (!IsExpr) { 236 CheckArrayDesignatorSyntax(*this, StartLoc, Desig); 237 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 238 SourceLocation(), 239 ParsedType::getFromOpaquePtr(TypeOrExpr), 240 nullptr); 241 } 242 243 // If the receiver was an expression, we still don't know 244 // whether we have a message send or an array designator; just 245 // adopt the expression for further analysis below. 246 // FIXME: potentially-potentially evaluated expression above? 247 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr)); 248 } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) { 249 IdentifierInfo *II = Tok.getIdentifierInfo(); 250 SourceLocation IILoc = Tok.getLocation(); 251 ParsedType ReceiverType; 252 // Three cases. This is a message send to a type: [type foo] 253 // This is a message send to super: [super foo] 254 // This is a message sent to an expr: [super.bar foo] 255 switch (Sema::ObjCMessageKind Kind 256 = Actions.getObjCMessageKind(getCurScope(), II, IILoc, 257 II == Ident_super, 258 NextToken().is(tok::period), 259 ReceiverType)) { 260 case Sema::ObjCSuperMessage: 261 case Sema::ObjCClassMessage: 262 CheckArrayDesignatorSyntax(*this, StartLoc, Desig); 263 if (Kind == Sema::ObjCSuperMessage) 264 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 265 ConsumeToken(), 266 ParsedType(), 267 nullptr); 268 ConsumeToken(); // the identifier 269 if (!ReceiverType) { 270 SkipUntil(tok::r_square, StopAtSemi); 271 return ExprError(); 272 } 273 274 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 275 SourceLocation(), 276 ReceiverType, 277 nullptr); 278 279 case Sema::ObjCInstanceMessage: 280 // Fall through; we'll just parse the expression and 281 // (possibly) treat this like an Objective-C message send 282 // later. 283 break; 284 } 285 } 286 287 // Parse the index expression, if we haven't already gotten one 288 // above (which can only happen in Objective-C++). 289 // Note that we parse this as an assignment expression, not a constant 290 // expression (allowing *=, =, etc) to handle the objc case. Sema needs 291 // to validate that the expression is a constant. 292 // FIXME: We also need to tell Sema that we're in a 293 // potentially-potentially evaluated context. 294 if (!Idx.get()) { 295 Idx = ParseAssignmentExpression(); 296 if (Idx.isInvalid()) { 297 SkipUntil(tok::r_square, StopAtSemi); 298 return Idx; 299 } 300 } 301 302 // Given an expression, we could either have a designator (if the next 303 // tokens are '...' or ']' or an objc message send. If this is an objc 304 // message send, handle it now. An objc-message send is the start of 305 // an assignment-expression production. 306 if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) && 307 Tok.isNot(tok::r_square)) { 308 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig); 309 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 310 SourceLocation(), 311 ParsedType(), 312 Idx.get()); 313 } 314 315 // If this is a normal array designator, remember it. 316 if (Tok.isNot(tok::ellipsis)) { 317 Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc)); 318 } else { 319 // Handle the gnu array range extension. 320 Diag(Tok, diag::ext_gnu_array_range); 321 SourceLocation EllipsisLoc = ConsumeToken(); 322 323 ExprResult RHS(ParseConstantExpression()); 324 if (RHS.isInvalid()) { 325 SkipUntil(tok::r_square, StopAtSemi); 326 return RHS; 327 } 328 Desig.AddDesignator(Designator::getArrayRange(Idx.get(), 329 RHS.get(), 330 StartLoc, EllipsisLoc)); 331 } 332 333 T.consumeClose(); 334 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc( 335 T.getCloseLocation()); 336 } 337 338 // Okay, we're done with the designator sequence. We know that there must be 339 // at least one designator, because the only case we can get into this method 340 // without a designator is when we have an objc message send. That case is 341 // handled and returned from above. 342 assert(!Desig.empty() && "Designator is empty?"); 343 344 // Handle a normal designator sequence end, which is an equal. 345 if (Tok.is(tok::equal)) { 346 SourceLocation EqualLoc = ConsumeToken(); 347 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, 348 ParseInitializer()); 349 } 350 351 // We read some number of designators and found something that isn't an = or 352 // an initializer. If we have exactly one array designator, this 353 // is the GNU 'designation: array-designator' extension. Otherwise, it is a 354 // parse error. 355 if (Desig.getNumDesignators() == 1 && 356 (Desig.getDesignator(0).isArrayDesignator() || 357 Desig.getDesignator(0).isArrayRangeDesignator())) { 358 Diag(Tok, diag::ext_gnu_missing_equal_designator) 359 << FixItHint::CreateInsertion(Tok.getLocation(), "= "); 360 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), 361 true, ParseInitializer()); 362 } 363 364 Diag(Tok, diag::err_expected_equal_designator); 365 return ExprError(); 366 } 367 368 369 /// ParseBraceInitializer - Called when parsing an initializer that has a 370 /// leading open brace. 371 /// 372 /// initializer: [C99 6.7.8] 373 /// '{' initializer-list '}' 374 /// '{' initializer-list ',' '}' 375 /// [GNU] '{' '}' 376 /// 377 /// initializer-list: 378 /// designation[opt] initializer ...[opt] 379 /// initializer-list ',' designation[opt] initializer ...[opt] 380 /// 381 ExprResult Parser::ParseBraceInitializer() { 382 InMessageExpressionRAIIObject InMessage(*this, false); 383 384 BalancedDelimiterTracker T(*this, tok::l_brace); 385 T.consumeOpen(); 386 SourceLocation LBraceLoc = T.getOpenLocation(); 387 388 /// InitExprs - This is the actual list of expressions contained in the 389 /// initializer. 390 ExprVector InitExprs; 391 392 if (Tok.is(tok::r_brace)) { 393 // Empty initializers are a C++ feature and a GNU extension to C. 394 if (!getLangOpts().CPlusPlus) 395 Diag(LBraceLoc, diag::ext_gnu_empty_initializer); 396 // Match the '}'. 397 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace()); 398 } 399 400 bool InitExprsOk = true; 401 402 while (1) { 403 // Handle Microsoft __if_exists/if_not_exists if necessary. 404 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || 405 Tok.is(tok::kw___if_not_exists))) { 406 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) { 407 if (Tok.isNot(tok::comma)) break; 408 ConsumeToken(); 409 } 410 if (Tok.is(tok::r_brace)) break; 411 continue; 412 } 413 414 // Parse: designation[opt] initializer 415 416 // If we know that this cannot be a designation, just parse the nested 417 // initializer directly. 418 ExprResult SubElt; 419 if (MayBeDesignationStart()) 420 SubElt = ParseInitializerWithPotentialDesignator(); 421 else 422 SubElt = ParseInitializer(); 423 424 if (Tok.is(tok::ellipsis)) 425 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); 426 427 // If we couldn't parse the subelement, bail out. 428 if (!SubElt.isInvalid()) { 429 InitExprs.push_back(SubElt.get()); 430 } else { 431 InitExprsOk = false; 432 433 // We have two ways to try to recover from this error: if the code looks 434 // grammatically ok (i.e. we have a comma coming up) try to continue 435 // parsing the rest of the initializer. This allows us to emit 436 // diagnostics for later elements that we find. If we don't see a comma, 437 // assume there is a parse error, and just skip to recover. 438 // FIXME: This comment doesn't sound right. If there is a r_brace 439 // immediately, it can't be an error, since there is no other way of 440 // leaving this loop except through this if. 441 if (Tok.isNot(tok::comma)) { 442 SkipUntil(tok::r_brace, StopBeforeMatch); 443 break; 444 } 445 } 446 447 // If we don't have a comma continued list, we're done. 448 if (Tok.isNot(tok::comma)) break; 449 450 // TODO: save comma locations if some client cares. 451 ConsumeToken(); 452 453 // Handle trailing comma. 454 if (Tok.is(tok::r_brace)) break; 455 } 456 457 bool closed = !T.consumeClose(); 458 459 if (InitExprsOk && closed) 460 return Actions.ActOnInitList(LBraceLoc, InitExprs, 461 T.getCloseLocation()); 462 463 return ExprError(); // an error occurred. 464 } 465 466 467 // Return true if a comma (or closing brace) is necessary after the 468 // __if_exists/if_not_exists statement. 469 bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs, 470 bool &InitExprsOk) { 471 bool trailingComma = false; 472 IfExistsCondition Result; 473 if (ParseMicrosoftIfExistsCondition(Result)) 474 return false; 475 476 BalancedDelimiterTracker Braces(*this, tok::l_brace); 477 if (Braces.consumeOpen()) { 478 Diag(Tok, diag::err_expected) << tok::l_brace; 479 return false; 480 } 481 482 switch (Result.Behavior) { 483 case IEB_Parse: 484 // Parse the declarations below. 485 break; 486 487 case IEB_Dependent: 488 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) 489 << Result.IsIfExists; 490 // Fall through to skip. 491 492 case IEB_Skip: 493 Braces.skipToEnd(); 494 return false; 495 } 496 497 while (!isEofOrEom()) { 498 trailingComma = false; 499 // If we know that this cannot be a designation, just parse the nested 500 // initializer directly. 501 ExprResult SubElt; 502 if (MayBeDesignationStart()) 503 SubElt = ParseInitializerWithPotentialDesignator(); 504 else 505 SubElt = ParseInitializer(); 506 507 if (Tok.is(tok::ellipsis)) 508 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); 509 510 // If we couldn't parse the subelement, bail out. 511 if (!SubElt.isInvalid()) 512 InitExprs.push_back(SubElt.get()); 513 else 514 InitExprsOk = false; 515 516 if (Tok.is(tok::comma)) { 517 ConsumeToken(); 518 trailingComma = true; 519 } 520 521 if (Tok.is(tok::r_brace)) 522 break; 523 } 524 525 Braces.consumeClose(); 526 527 return !trailingComma; 528 } 529