ParseInit.cpp revision 302408
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" 21using 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. 27bool 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 94static 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/// 131ExprResult 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); 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 (Actions.getObjCMessageKind( 256 getCurScope(), II, IILoc, II == Ident_super, 257 NextToken().is(tok::period), ReceiverType)) { 258 case Sema::ObjCSuperMessage: 259 CheckArrayDesignatorSyntax(*this, StartLoc, Desig); 260 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 261 ConsumeToken(), 262 ParsedType(), 263 nullptr); 264 265 case Sema::ObjCClassMessage: 266 CheckArrayDesignatorSyntax(*this, StartLoc, Desig); 267 ConsumeToken(); // the identifier 268 if (!ReceiverType) { 269 SkipUntil(tok::r_square, StopAtSemi); 270 return ExprError(); 271 } 272 273 // Parse type arguments and protocol qualifiers. 274 if (Tok.is(tok::less)) { 275 SourceLocation NewEndLoc; 276 TypeResult NewReceiverType 277 = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType, 278 /*consumeLastToken=*/true, 279 NewEndLoc); 280 if (!NewReceiverType.isUsable()) { 281 SkipUntil(tok::r_square, StopAtSemi); 282 return ExprError(); 283 } 284 285 ReceiverType = NewReceiverType.get(); 286 } 287 288 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 289 SourceLocation(), 290 ReceiverType, 291 nullptr); 292 293 case Sema::ObjCInstanceMessage: 294 // Fall through; we'll just parse the expression and 295 // (possibly) treat this like an Objective-C message send 296 // later. 297 break; 298 } 299 } 300 301 // Parse the index expression, if we haven't already gotten one 302 // above (which can only happen in Objective-C++). 303 // Note that we parse this as an assignment expression, not a constant 304 // expression (allowing *=, =, etc) to handle the objc case. Sema needs 305 // to validate that the expression is a constant. 306 // FIXME: We also need to tell Sema that we're in a 307 // potentially-potentially evaluated context. 308 if (!Idx.get()) { 309 Idx = ParseAssignmentExpression(); 310 if (Idx.isInvalid()) { 311 SkipUntil(tok::r_square, StopAtSemi); 312 return Idx; 313 } 314 } 315 316 // Given an expression, we could either have a designator (if the next 317 // tokens are '...' or ']' or an objc message send. If this is an objc 318 // message send, handle it now. An objc-message send is the start of 319 // an assignment-expression production. 320 if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) && 321 Tok.isNot(tok::r_square)) { 322 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig); 323 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 324 SourceLocation(), 325 ParsedType(), 326 Idx.get()); 327 } 328 329 // If this is a normal array designator, remember it. 330 if (Tok.isNot(tok::ellipsis)) { 331 Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc)); 332 } else { 333 // Handle the gnu array range extension. 334 Diag(Tok, diag::ext_gnu_array_range); 335 SourceLocation EllipsisLoc = ConsumeToken(); 336 337 ExprResult RHS(ParseConstantExpression()); 338 if (RHS.isInvalid()) { 339 SkipUntil(tok::r_square, StopAtSemi); 340 return RHS; 341 } 342 Desig.AddDesignator(Designator::getArrayRange(Idx.get(), 343 RHS.get(), 344 StartLoc, EllipsisLoc)); 345 } 346 347 T.consumeClose(); 348 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc( 349 T.getCloseLocation()); 350 } 351 352 // Okay, we're done with the designator sequence. We know that there must be 353 // at least one designator, because the only case we can get into this method 354 // without a designator is when we have an objc message send. That case is 355 // handled and returned from above. 356 assert(!Desig.empty() && "Designator is empty?"); 357 358 // Handle a normal designator sequence end, which is an equal. 359 if (Tok.is(tok::equal)) { 360 SourceLocation EqualLoc = ConsumeToken(); 361 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, 362 ParseInitializer()); 363 } 364 365 // We read some number of designators and found something that isn't an = or 366 // an initializer. If we have exactly one array designator, this 367 // is the GNU 'designation: array-designator' extension. Otherwise, it is a 368 // parse error. 369 if (Desig.getNumDesignators() == 1 && 370 (Desig.getDesignator(0).isArrayDesignator() || 371 Desig.getDesignator(0).isArrayRangeDesignator())) { 372 Diag(Tok, diag::ext_gnu_missing_equal_designator) 373 << FixItHint::CreateInsertion(Tok.getLocation(), "= "); 374 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), 375 true, ParseInitializer()); 376 } 377 378 Diag(Tok, diag::err_expected_equal_designator); 379 return ExprError(); 380} 381 382 383/// ParseBraceInitializer - Called when parsing an initializer that has a 384/// leading open brace. 385/// 386/// initializer: [C99 6.7.8] 387/// '{' initializer-list '}' 388/// '{' initializer-list ',' '}' 389/// [GNU] '{' '}' 390/// 391/// initializer-list: 392/// designation[opt] initializer ...[opt] 393/// initializer-list ',' designation[opt] initializer ...[opt] 394/// 395ExprResult Parser::ParseBraceInitializer() { 396 InMessageExpressionRAIIObject InMessage(*this, false); 397 398 BalancedDelimiterTracker T(*this, tok::l_brace); 399 T.consumeOpen(); 400 SourceLocation LBraceLoc = T.getOpenLocation(); 401 402 /// InitExprs - This is the actual list of expressions contained in the 403 /// initializer. 404 ExprVector InitExprs; 405 406 if (Tok.is(tok::r_brace)) { 407 // Empty initializers are a C++ feature and a GNU extension to C. 408 if (!getLangOpts().CPlusPlus) 409 Diag(LBraceLoc, diag::ext_gnu_empty_initializer); 410 // Match the '}'. 411 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace()); 412 } 413 414 bool InitExprsOk = true; 415 416 while (1) { 417 // Handle Microsoft __if_exists/if_not_exists if necessary. 418 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || 419 Tok.is(tok::kw___if_not_exists))) { 420 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) { 421 if (Tok.isNot(tok::comma)) break; 422 ConsumeToken(); 423 } 424 if (Tok.is(tok::r_brace)) break; 425 continue; 426 } 427 428 // Parse: designation[opt] initializer 429 430 // If we know that this cannot be a designation, just parse the nested 431 // initializer directly. 432 ExprResult SubElt; 433 if (MayBeDesignationStart()) 434 SubElt = ParseInitializerWithPotentialDesignator(); 435 else 436 SubElt = ParseInitializer(); 437 438 if (Tok.is(tok::ellipsis)) 439 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); 440 441 SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get()); 442 443 // If we couldn't parse the subelement, bail out. 444 if (SubElt.isUsable()) { 445 InitExprs.push_back(SubElt.get()); 446 } else { 447 InitExprsOk = false; 448 449 // We have two ways to try to recover from this error: if the code looks 450 // grammatically ok (i.e. we have a comma coming up) try to continue 451 // parsing the rest of the initializer. This allows us to emit 452 // diagnostics for later elements that we find. If we don't see a comma, 453 // assume there is a parse error, and just skip to recover. 454 // FIXME: This comment doesn't sound right. If there is a r_brace 455 // immediately, it can't be an error, since there is no other way of 456 // leaving this loop except through this if. 457 if (Tok.isNot(tok::comma)) { 458 SkipUntil(tok::r_brace, StopBeforeMatch); 459 break; 460 } 461 } 462 463 // If we don't have a comma continued list, we're done. 464 if (Tok.isNot(tok::comma)) break; 465 466 // TODO: save comma locations if some client cares. 467 ConsumeToken(); 468 469 // Handle trailing comma. 470 if (Tok.is(tok::r_brace)) break; 471 } 472 473 bool closed = !T.consumeClose(); 474 475 if (InitExprsOk && closed) 476 return Actions.ActOnInitList(LBraceLoc, InitExprs, 477 T.getCloseLocation()); 478 479 return ExprError(); // an error occurred. 480} 481 482 483// Return true if a comma (or closing brace) is necessary after the 484// __if_exists/if_not_exists statement. 485bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs, 486 bool &InitExprsOk) { 487 bool trailingComma = false; 488 IfExistsCondition Result; 489 if (ParseMicrosoftIfExistsCondition(Result)) 490 return false; 491 492 BalancedDelimiterTracker Braces(*this, tok::l_brace); 493 if (Braces.consumeOpen()) { 494 Diag(Tok, diag::err_expected) << tok::l_brace; 495 return false; 496 } 497 498 switch (Result.Behavior) { 499 case IEB_Parse: 500 // Parse the declarations below. 501 break; 502 503 case IEB_Dependent: 504 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) 505 << Result.IsIfExists; 506 // Fall through to skip. 507 508 case IEB_Skip: 509 Braces.skipToEnd(); 510 return false; 511 } 512 513 while (!isEofOrEom()) { 514 trailingComma = false; 515 // If we know that this cannot be a designation, just parse the nested 516 // initializer directly. 517 ExprResult SubElt; 518 if (MayBeDesignationStart()) 519 SubElt = ParseInitializerWithPotentialDesignator(); 520 else 521 SubElt = ParseInitializer(); 522 523 if (Tok.is(tok::ellipsis)) 524 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); 525 526 // If we couldn't parse the subelement, bail out. 527 if (!SubElt.isInvalid()) 528 InitExprs.push_back(SubElt.get()); 529 else 530 InitExprsOk = false; 531 532 if (Tok.is(tok::comma)) { 533 ConsumeToken(); 534 trailingComma = true; 535 } 536 537 if (Tok.is(tok::r_brace)) 538 break; 539 } 540 541 Braces.consumeClose(); 542 543 return !trailingComma; 544} 545