clang API Documentation

ParseInit.cpp
Go to the documentation of this file.
00001 //===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements initializer parsing as specified by C99 6.7.8.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Parse/Parser.h"
00015 #include "RAIIObjectsForParser.h"
00016 #include "clang/Parse/ParseDiagnostic.h"
00017 #include "clang/Sema/Designator.h"
00018 #include "clang/Sema/Scope.h"
00019 #include "llvm/ADT/SmallString.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 using namespace clang;
00022 
00023 
00024 /// MayBeDesignationStart - Return true if the current token might be the start 
00025 /// of a designator.  If we can tell it is impossible that it is a designator, 
00026 /// return false.
00027 bool Parser::MayBeDesignationStart() {
00028   switch (Tok.getKind()) {
00029   default: 
00030     return false;
00031       
00032   case tok::period:      // designator: '.' identifier
00033     return true;
00034       
00035   case tok::l_square: {  // designator: array-designator
00036     if (!PP.getLangOpts().CPlusPlus11)
00037       return true;
00038     
00039     // C++11 lambda expressions and C99 designators can be ambiguous all the
00040     // way through the closing ']' and to the next character. Handle the easy
00041     // cases here, and fall back to tentative parsing if those fail.
00042     switch (PP.LookAhead(0).getKind()) {
00043     case tok::equal:
00044     case tok::r_square:
00045       // Definitely starts a lambda expression.
00046       return false;
00047       
00048     case tok::amp:
00049     case tok::kw_this:
00050     case tok::identifier:
00051       // We have to do additional analysis, because these could be the
00052       // start of a constant expression or a lambda capture list.
00053       break;
00054         
00055     default:
00056       // Anything not mentioned above cannot occur following a '[' in a 
00057       // lambda expression.
00058       return true;        
00059     }
00060     
00061     // Handle the complicated case below.
00062     break;    
00063   }
00064   case tok::identifier:  // designation: identifier ':'
00065     return PP.LookAhead(0).is(tok::colon);
00066   }
00067   
00068   // Parse up to (at most) the token after the closing ']' to determine 
00069   // whether this is a C99 designator or a lambda.
00070   TentativeParsingAction Tentative(*this);
00071 
00072   LambdaIntroducer Intro;
00073   bool SkippedInits = false;
00074   Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
00075 
00076   if (DiagID) {
00077     // If this can't be a lambda capture list, it's a designator.
00078     Tentative.Revert();
00079     return true;
00080   }
00081 
00082   // Once we hit the closing square bracket, we look at the next
00083   // token. If it's an '=', this is a designator. Otherwise, it's a
00084   // lambda expression. This decision favors lambdas over the older
00085   // GNU designator syntax, which allows one to omit the '=', but is
00086   // consistent with GCC.
00087   tok::TokenKind Kind = Tok.getKind();
00088   // FIXME: If we didn't skip any inits, parse the lambda from here
00089   // rather than throwing away then reparsing the LambdaIntroducer.
00090   Tentative.Revert();
00091   return Kind == tok::equal;
00092 }
00093 
00094 static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
00095                                        Designation &Desig) {
00096   // If we have exactly one array designator, this used the GNU
00097   // 'designation: array-designator' extension, otherwise there should be no
00098   // designators at all!
00099   if (Desig.getNumDesignators() == 1 &&
00100       (Desig.getDesignator(0).isArrayDesignator() ||
00101        Desig.getDesignator(0).isArrayRangeDesignator()))
00102     P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
00103   else if (Desig.getNumDesignators() > 0)
00104     P.Diag(Loc, diag::err_expected_equal_designator);
00105 }
00106 
00107 /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
00108 /// checking to see if the token stream starts with a designator.
00109 ///
00110 ///       designation:
00111 ///         designator-list '='
00112 /// [GNU]   array-designator
00113 /// [GNU]   identifier ':'
00114 ///
00115 ///       designator-list:
00116 ///         designator
00117 ///         designator-list designator
00118 ///
00119 ///       designator:
00120 ///         array-designator
00121 ///         '.' identifier
00122 ///
00123 ///       array-designator:
00124 ///         '[' constant-expression ']'
00125 /// [GNU]   '[' constant-expression '...' constant-expression ']'
00126 ///
00127 /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
00128 /// initializer (because it is an expression).  We need to consider this case
00129 /// when parsing array designators.
00130 ///
00131 ExprResult Parser::ParseInitializerWithPotentialDesignator() {
00132 
00133   // If this is the old-style GNU extension:
00134   //   designation ::= identifier ':'
00135   // Handle it as a field designator.  Otherwise, this must be the start of a
00136   // normal expression.
00137   if (Tok.is(tok::identifier)) {
00138     const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
00139 
00140     SmallString<256> NewSyntax;
00141     llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
00142                                          << " = ";
00143 
00144     SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
00145 
00146     assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
00147     SourceLocation ColonLoc = ConsumeToken();
00148 
00149     Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
00150       << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
00151                                       NewSyntax.str());
00152 
00153     Designation D;
00154     D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
00155     return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
00156                                               ParseInitializer());
00157   }
00158 
00159   // Desig - This is initialized when we see our first designator.  We may have
00160   // an objc message send with no designator, so we don't want to create this
00161   // eagerly.
00162   Designation Desig;
00163 
00164   // Parse each designator in the designator list until we find an initializer.
00165   while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
00166     if (Tok.is(tok::period)) {
00167       // designator: '.' identifier
00168       SourceLocation DotLoc = ConsumeToken();
00169 
00170       if (Tok.isNot(tok::identifier)) {
00171         Diag(Tok.getLocation(), diag::err_expected_field_designator);
00172         return ExprError();
00173       }
00174 
00175       Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
00176                                                Tok.getLocation()));
00177       ConsumeToken(); // Eat the identifier.
00178       continue;
00179     }
00180 
00181     // We must have either an array designator now or an objc message send.
00182     assert(Tok.is(tok::l_square) && "Unexpected token!");
00183 
00184     // Handle the two forms of array designator:
00185     //   array-designator: '[' constant-expression ']'
00186     //   array-designator: '[' constant-expression '...' constant-expression ']'
00187     //
00188     // Also, we have to handle the case where the expression after the
00189     // designator an an objc message send: '[' objc-message-expr ']'.
00190     // Interesting cases are:
00191     //   [foo bar]         -> objc message send
00192     //   [foo]             -> array designator
00193     //   [foo ... bar]     -> array designator
00194     //   [4][foo bar]      -> obsolete GNU designation with objc message send.
00195     //
00196     // We do not need to check for an expression starting with [[ here. If it
00197     // contains an Objective-C message send, then it is not an ill-formed
00198     // attribute. If it is a lambda-expression within an array-designator, then
00199     // it will be rejected because a constant-expression cannot begin with a
00200     // lambda-expression.
00201     InMessageExpressionRAIIObject InMessage(*this, true);
00202     
00203     BalancedDelimiterTracker T(*this, tok::l_square);
00204     T.consumeOpen();
00205     SourceLocation StartLoc = T.getOpenLocation();
00206 
00207     ExprResult Idx;
00208 
00209     // If Objective-C is enabled and this is a typename (class message
00210     // send) or send to 'super', parse this as a message send
00211     // expression.  We handle C++ and C separately, since C++ requires
00212     // much more complicated parsing.
00213     if  (getLangOpts().ObjC1 && getLangOpts().CPlusPlus) {
00214       // Send to 'super'.
00215       if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
00216           NextToken().isNot(tok::period) && 
00217           getCurScope()->isInObjcMethodScope()) {
00218         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
00219         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
00220                                                            ConsumeToken(),
00221                                                            ParsedType(), 
00222                                                            nullptr);
00223       }
00224 
00225       // Parse the receiver, which is either a type or an expression.
00226       bool IsExpr;
00227       void *TypeOrExpr;
00228       if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
00229         SkipUntil(tok::r_square, StopAtSemi);
00230         return ExprError();
00231       }
00232       
00233       // If the receiver was a type, we have a class message; parse
00234       // the rest of it.
00235       if (!IsExpr) {
00236         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
00237         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 
00238                                                            SourceLocation(), 
00239                                    ParsedType::getFromOpaquePtr(TypeOrExpr),
00240                                                            nullptr);
00241       }
00242 
00243       // If the receiver was an expression, we still don't know
00244       // whether we have a message send or an array designator; just
00245       // adopt the expression for further analysis below.
00246       // FIXME: potentially-potentially evaluated expression above?
00247       Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
00248     } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) {
00249       IdentifierInfo *II = Tok.getIdentifierInfo();
00250       SourceLocation IILoc = Tok.getLocation();
00251       ParsedType ReceiverType;
00252       // Three cases. This is a message send to a type: [type foo]
00253       // This is a message send to super:  [super foo]
00254       // This is a message sent to an expr:  [super.bar foo]
00255       switch (Sema::ObjCMessageKind Kind
00256                 = Actions.getObjCMessageKind(getCurScope(), II, IILoc, 
00257                                              II == Ident_super,
00258                                              NextToken().is(tok::period),
00259                                              ReceiverType)) {
00260       case Sema::ObjCSuperMessage:
00261       case Sema::ObjCClassMessage:
00262         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
00263         if (Kind == Sema::ObjCSuperMessage)
00264           return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
00265                                                              ConsumeToken(),
00266                                                              ParsedType(),
00267                                                              nullptr);
00268         ConsumeToken(); // the identifier
00269         if (!ReceiverType) {
00270           SkipUntil(tok::r_square, StopAtSemi);
00271           return ExprError();
00272         }
00273 
00274         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 
00275                                                            SourceLocation(), 
00276                                                            ReceiverType, 
00277                                                            nullptr);
00278 
00279       case Sema::ObjCInstanceMessage:
00280         // Fall through; we'll just parse the expression and
00281         // (possibly) treat this like an Objective-C message send
00282         // later.
00283         break;
00284       }
00285     }
00286 
00287     // Parse the index expression, if we haven't already gotten one
00288     // above (which can only happen in Objective-C++).
00289     // Note that we parse this as an assignment expression, not a constant
00290     // expression (allowing *=, =, etc) to handle the objc case.  Sema needs
00291     // to validate that the expression is a constant.
00292     // FIXME: We also need to tell Sema that we're in a
00293     // potentially-potentially evaluated context.
00294     if (!Idx.get()) {
00295       Idx = ParseAssignmentExpression();
00296       if (Idx.isInvalid()) {
00297         SkipUntil(tok::r_square, StopAtSemi);
00298         return Idx;
00299       }
00300     }
00301 
00302     // Given an expression, we could either have a designator (if the next
00303     // tokens are '...' or ']' or an objc message send.  If this is an objc
00304     // message send, handle it now.  An objc-message send is the start of
00305     // an assignment-expression production.
00306     if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) &&
00307         Tok.isNot(tok::r_square)) {
00308       CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
00309       return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
00310                                                          SourceLocation(),
00311                                                          ParsedType(),
00312                                                          Idx.get());
00313     }
00314 
00315     // If this is a normal array designator, remember it.
00316     if (Tok.isNot(tok::ellipsis)) {
00317       Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
00318     } else {
00319       // Handle the gnu array range extension.
00320       Diag(Tok, diag::ext_gnu_array_range);
00321       SourceLocation EllipsisLoc = ConsumeToken();
00322 
00323       ExprResult RHS(ParseConstantExpression());
00324       if (RHS.isInvalid()) {
00325         SkipUntil(tok::r_square, StopAtSemi);
00326         return RHS;
00327       }
00328       Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
00329                                                     RHS.get(),
00330                                                     StartLoc, EllipsisLoc));
00331     }
00332 
00333     T.consumeClose();
00334     Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
00335                                                         T.getCloseLocation());
00336   }
00337 
00338   // Okay, we're done with the designator sequence.  We know that there must be
00339   // at least one designator, because the only case we can get into this method
00340   // without a designator is when we have an objc message send.  That case is
00341   // handled and returned from above.
00342   assert(!Desig.empty() && "Designator is empty?");
00343 
00344   // Handle a normal designator sequence end, which is an equal.
00345   if (Tok.is(tok::equal)) {
00346     SourceLocation EqualLoc = ConsumeToken();
00347     return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
00348                                               ParseInitializer());
00349   }
00350 
00351   // We read some number of designators and found something that isn't an = or
00352   // an initializer.  If we have exactly one array designator, this
00353   // is the GNU 'designation: array-designator' extension.  Otherwise, it is a
00354   // parse error.
00355   if (Desig.getNumDesignators() == 1 &&
00356       (Desig.getDesignator(0).isArrayDesignator() ||
00357        Desig.getDesignator(0).isArrayRangeDesignator())) {
00358     Diag(Tok, diag::ext_gnu_missing_equal_designator)
00359       << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
00360     return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
00361                                               true, ParseInitializer());
00362   }
00363 
00364   Diag(Tok, diag::err_expected_equal_designator);
00365   return ExprError();
00366 }
00367 
00368 
00369 /// ParseBraceInitializer - Called when parsing an initializer that has a
00370 /// leading open brace.
00371 ///
00372 ///       initializer: [C99 6.7.8]
00373 ///         '{' initializer-list '}'
00374 ///         '{' initializer-list ',' '}'
00375 /// [GNU]   '{' '}'
00376 ///
00377 ///       initializer-list:
00378 ///         designation[opt] initializer ...[opt]
00379 ///         initializer-list ',' designation[opt] initializer ...[opt]
00380 ///
00381 ExprResult Parser::ParseBraceInitializer() {
00382   InMessageExpressionRAIIObject InMessage(*this, false);
00383   
00384   BalancedDelimiterTracker T(*this, tok::l_brace);
00385   T.consumeOpen();
00386   SourceLocation LBraceLoc = T.getOpenLocation();
00387 
00388   /// InitExprs - This is the actual list of expressions contained in the
00389   /// initializer.
00390   ExprVector InitExprs;
00391 
00392   if (Tok.is(tok::r_brace)) {
00393     // Empty initializers are a C++ feature and a GNU extension to C.
00394     if (!getLangOpts().CPlusPlus)
00395       Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
00396     // Match the '}'.
00397     return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
00398   }
00399 
00400   bool InitExprsOk = true;
00401 
00402   while (1) {
00403     // Handle Microsoft __if_exists/if_not_exists if necessary.
00404     if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
00405         Tok.is(tok::kw___if_not_exists))) {
00406       if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
00407         if (Tok.isNot(tok::comma)) break;
00408         ConsumeToken();
00409       }
00410       if (Tok.is(tok::r_brace)) break;
00411       continue;
00412     }
00413 
00414     // Parse: designation[opt] initializer
00415 
00416     // If we know that this cannot be a designation, just parse the nested
00417     // initializer directly.
00418     ExprResult SubElt;
00419     if (MayBeDesignationStart())
00420       SubElt = ParseInitializerWithPotentialDesignator();
00421     else
00422       SubElt = ParseInitializer();
00423 
00424     if (Tok.is(tok::ellipsis))
00425       SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
00426     
00427     // If we couldn't parse the subelement, bail out.
00428     if (!SubElt.isInvalid()) {
00429       InitExprs.push_back(SubElt.get());
00430     } else {
00431       InitExprsOk = false;
00432 
00433       // We have two ways to try to recover from this error: if the code looks
00434       // grammatically ok (i.e. we have a comma coming up) try to continue
00435       // parsing the rest of the initializer.  This allows us to emit
00436       // diagnostics for later elements that we find.  If we don't see a comma,
00437       // assume there is a parse error, and just skip to recover.
00438       // FIXME: This comment doesn't sound right. If there is a r_brace
00439       // immediately, it can't be an error, since there is no other way of
00440       // leaving this loop except through this if.
00441       if (Tok.isNot(tok::comma)) {
00442         SkipUntil(tok::r_brace, StopBeforeMatch);
00443         break;
00444       }
00445     }
00446 
00447     // If we don't have a comma continued list, we're done.
00448     if (Tok.isNot(tok::comma)) break;
00449 
00450     // TODO: save comma locations if some client cares.
00451     ConsumeToken();
00452 
00453     // Handle trailing comma.
00454     if (Tok.is(tok::r_brace)) break;
00455   }
00456 
00457   bool closed = !T.consumeClose();
00458 
00459   if (InitExprsOk && closed)
00460     return Actions.ActOnInitList(LBraceLoc, InitExprs,
00461                                  T.getCloseLocation());
00462 
00463   return ExprError(); // an error occurred.
00464 }
00465 
00466 
00467 // Return true if a comma (or closing brace) is necessary after the
00468 // __if_exists/if_not_exists statement.
00469 bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
00470                                                     bool &InitExprsOk) {
00471   bool trailingComma = false;
00472   IfExistsCondition Result;
00473   if (ParseMicrosoftIfExistsCondition(Result))
00474     return false;
00475   
00476   BalancedDelimiterTracker Braces(*this, tok::l_brace);
00477   if (Braces.consumeOpen()) {
00478     Diag(Tok, diag::err_expected) << tok::l_brace;
00479     return false;
00480   }
00481 
00482   switch (Result.Behavior) {
00483   case IEB_Parse:
00484     // Parse the declarations below.
00485     break;
00486         
00487   case IEB_Dependent:
00488     Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
00489       << Result.IsIfExists;
00490     // Fall through to skip.
00491       
00492   case IEB_Skip:
00493     Braces.skipToEnd();
00494     return false;
00495   }
00496 
00497   while (!isEofOrEom()) {
00498     trailingComma = false;
00499     // If we know that this cannot be a designation, just parse the nested
00500     // initializer directly.
00501     ExprResult SubElt;
00502     if (MayBeDesignationStart())
00503       SubElt = ParseInitializerWithPotentialDesignator();
00504     else
00505       SubElt = ParseInitializer();
00506 
00507     if (Tok.is(tok::ellipsis))
00508       SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
00509     
00510     // If we couldn't parse the subelement, bail out.
00511     if (!SubElt.isInvalid())
00512       InitExprs.push_back(SubElt.get());
00513     else
00514       InitExprsOk = false;
00515 
00516     if (Tok.is(tok::comma)) {
00517       ConsumeToken();
00518       trailingComma = true;
00519     }
00520 
00521     if (Tok.is(tok::r_brace))
00522       break;
00523   }
00524 
00525   Braces.consumeClose();
00526 
00527   return !trailingComma;
00528 }