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