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