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