Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseInit.cpp - Initializer Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 16 | #include "RAIIObjectsForParser.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Designator.h" |
| 18 | #include "clang/Sema/Scope.h" |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | |
Douglas Gregor | b3f323d | 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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | case tok::period: // designator: '.' identifier |
Douglas Gregor | b3f323d | 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 | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 37 | return true; |
Douglas Gregor | b3f323d | 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 | |
Douglas Gregor | d267b3f | 2012-02-17 16:41:16 +0000 | [diff] [blame] | 61 | // Handle the complicated case below. |
| 62 | break; |
Douglas Gregor | b3f323d | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 63 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | case tok::identifier: // designation: identifier ':' |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 65 | return PP.LookAhead(0).is(tok::colon); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } |
Douglas Gregor | d267b3f | 2012-02-17 16:41:16 +0000 | [diff] [blame] | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 146 | /// initializer (because it is an expression). We need to consider this case |
| 147 | /// when parsing array designators. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 149 | ExprResult Parser::ParseInitializerWithPotentialDesignator() { |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 150 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 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)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 156 | const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 157 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 158 | SmallString<256> NewSyntax; |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 159 | llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName() |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 160 | << " = "; |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 162 | SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 164 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 165 | SourceLocation ColonLoc = ConsumeToken(); |
| 166 | |
Douglas Gregor | 40a0f9c | 2011-08-27 00:13:16 +0000 | [diff] [blame] | 167 | Diag(NameLoc, diag::ext_gnu_old_style_field_designator) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 168 | << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc), |
| 169 | NewSyntax.str()); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 170 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 171 | Designation D; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 172 | D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 174 | ParseInitializer()); |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 180 | Designation Desig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | // Parse each designator in the designator list until we find an initializer. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 183 | while (Tok.is(tok::period) || Tok.is(tok::l_square)) { |
| 184 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | // designator: '.' identifier |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 186 | SourceLocation DotLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 188 | if (Tok.isNot(tok::identifier)) { |
| 189 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 190 | return ExprError(); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 191 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 193 | Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, |
| 194 | Tok.getLocation())); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 195 | ConsumeToken(); // Eat the identifier. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 196 | continue; |
| 197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 199 | // We must have either an array designator now or an objc message send. |
| 200 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 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 | // |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 214 | InMessageExpressionRAIIObject InMessage(*this, true); |
| 215 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 216 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 217 | T.consumeOpen(); |
| 218 | SourceLocation StartLoc = T.getOpenLocation(); |
| 219 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 220 | ExprResult Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 222 | // If Objective-C is enabled and this is a typename (class message |
| 223 | // send) or send to 'super', parse this as a message send |
| 224 | // expression. We handle C++ and C separately, since C++ requires |
| 225 | // much more complicated parsing. |
| 226 | if (getLang().ObjC1 && getLang().CPlusPlus) { |
| 227 | // Send to 'super'. |
| 228 | if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 229 | NextToken().isNot(tok::period) && |
| 230 | getCurScope()->isInObjcMethodScope()) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 231 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
| 232 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 233 | ConsumeToken(), |
| 234 | ParsedType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 235 | 0); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Parse the receiver, which is either a type or an expression. |
| 239 | bool IsExpr; |
| 240 | void *TypeOrExpr; |
| 241 | if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { |
| 242 | SkipUntil(tok::r_square); |
| 243 | return ExprError(); |
| 244 | } |
| 245 | |
| 246 | // If the receiver was a type, we have a class message; parse |
| 247 | // the rest of it. |
| 248 | if (!IsExpr) { |
| 249 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
| 250 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 251 | SourceLocation(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 252 | ParsedType::getFromOpaquePtr(TypeOrExpr), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 253 | 0); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // If the receiver was an expression, we still don't know |
| 257 | // whether we have a message send or an array designator; just |
| 258 | // adopt the expression for further analysis below. |
| 259 | // FIXME: potentially-potentially evaluated expression above? |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 260 | Idx = ExprResult(static_cast<Expr*>(TypeOrExpr)); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 261 | } else if (getLang().ObjC1 && Tok.is(tok::identifier)) { |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 262 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 263 | SourceLocation IILoc = Tok.getLocation(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 264 | ParsedType ReceiverType; |
Chris Lattner | 1e46136 | 2010-04-12 06:36:00 +0000 | [diff] [blame] | 265 | // Three cases. This is a message send to a type: [type foo] |
| 266 | // This is a message send to super: [super foo] |
| 267 | // This is a message sent to an expr: [super.bar foo] |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 268 | switch (Sema::ObjCMessageKind Kind |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 269 | = Actions.getObjCMessageKind(getCurScope(), II, IILoc, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 270 | II == Ident_super, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 271 | NextToken().is(tok::period), |
| 272 | ReceiverType)) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 273 | case Sema::ObjCSuperMessage: |
| 274 | case Sema::ObjCClassMessage: |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 275 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 276 | if (Kind == Sema::ObjCSuperMessage) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 277 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 278 | ConsumeToken(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 279 | ParsedType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 280 | 0); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 281 | ConsumeToken(); // the identifier |
| 282 | if (!ReceiverType) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 283 | SkipUntil(tok::r_square); |
| 284 | return ExprError(); |
| 285 | } |
| 286 | |
| 287 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 288 | SourceLocation(), |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 289 | ReceiverType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 290 | 0); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 291 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 292 | case Sema::ObjCInstanceMessage: |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 293 | // Fall through; we'll just parse the expression and |
| 294 | // (possibly) treat this like an Objective-C message send |
| 295 | // later. |
| 296 | break; |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 297 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 298 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 300 | // Parse the index expression, if we haven't already gotten one |
| 301 | // above (which can only happen in Objective-C++). |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 302 | // Note that we parse this as an assignment expression, not a constant |
| 303 | // expression (allowing *=, =, etc) to handle the objc case. Sema needs |
| 304 | // to validate that the expression is a constant. |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 305 | // FIXME: We also need to tell Sema that we're in a |
| 306 | // potentially-potentially evaluated context. |
| 307 | if (!Idx.get()) { |
| 308 | Idx = ParseAssignmentExpression(); |
| 309 | if (Idx.isInvalid()) { |
| 310 | SkipUntil(tok::r_square); |
| 311 | return move(Idx); |
| 312 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 315 | // Given an expression, we could either have a designator (if the next |
| 316 | // tokens are '...' or ']' or an objc message send. If this is an objc |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | // message send, handle it now. An objc-message send is the start of |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 318 | // an assignment-expression production. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) && |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 320 | Tok.isNot(tok::r_square)) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 321 | CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 322 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 323 | SourceLocation(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 324 | ParsedType(), |
| 325 | Idx.take()); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 327 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 328 | // If this is a normal array designator, remember it. |
| 329 | if (Tok.isNot(tok::ellipsis)) { |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 330 | Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc)); |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 331 | } else { |
| 332 | // Handle the gnu array range extension. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 333 | Diag(Tok, diag::ext_gnu_array_range); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 334 | SourceLocation EllipsisLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 335 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 336 | ExprResult RHS(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 337 | if (RHS.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | SkipUntil(tok::r_square); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 339 | return move(RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | } |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 341 | Desig.AddDesignator(Designator::getArrayRange(Idx.release(), |
| 342 | RHS.release(), |
| 343 | StartLoc, EllipsisLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 346 | T.consumeClose(); |
| 347 | Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc( |
| 348 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 351 | // Okay, we're done with the designator sequence. We know that there must be |
| 352 | // at least one designator, because the only case we can get into this method |
| 353 | // without a designator is when we have an objc message send. That case is |
| 354 | // handled and returned from above. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 355 | assert(!Desig.empty() && "Designator is empty?"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 357 | // Handle a normal designator sequence end, which is an equal. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 358 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 359 | SourceLocation EqualLoc = ConsumeToken(); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 360 | return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 361 | ParseInitializer()); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 362 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 364 | // We read some number of designators and found something that isn't an = or |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 365 | // an initializer. If we have exactly one array designator, this |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 366 | // is the GNU 'designation: array-designator' extension. Otherwise, it is a |
| 367 | // parse error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | if (Desig.getNumDesignators() == 1 && |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 369 | (Desig.getDesignator(0).isArrayDesignator() || |
| 370 | Desig.getDesignator(0).isArrayRangeDesignator())) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 371 | Diag(Tok, diag::ext_gnu_missing_equal_designator) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 372 | << FixItHint::CreateInsertion(Tok.getLocation(), "= "); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 373 | return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), |
Douglas Gregor | 68c56de | 2009-03-27 23:40:29 +0000 | [diff] [blame] | 374 | true, ParseInitializer()); |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 375 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 377 | Diag(Tok, diag::err_expected_equal_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 378 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
Chris Lattner | 0eec2b5 | 2008-10-26 22:38:55 +0000 | [diff] [blame] | 382 | /// ParseBraceInitializer - Called when parsing an initializer that has a |
| 383 | /// leading open brace. |
| 384 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | /// initializer: [C99 6.7.8] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | /// '{' initializer-list '}' |
| 387 | /// '{' initializer-list ',' '}' |
| 388 | /// [GNU] '{' '}' |
| 389 | /// |
| 390 | /// initializer-list: |
Douglas Gregor | dcaa1ca | 2011-01-03 19:31:53 +0000 | [diff] [blame] | 391 | /// designation[opt] initializer ...[opt] |
| 392 | /// initializer-list ',' designation[opt] initializer ...[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 394 | ExprResult Parser::ParseBraceInitializer() { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 395 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 396 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 397 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 398 | T.consumeOpen(); |
| 399 | SourceLocation LBraceLoc = T.getOpenLocation(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 400 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 401 | /// InitExprs - This is the actual list of expressions contained in the |
| 402 | /// initializer. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 403 | ExprVector InitExprs(Actions); |
| 404 | |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 405 | if (Tok.is(tok::r_brace)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 406 | // Empty initializers are a C++ feature and a GNU extension to C. |
| 407 | if (!getLang().CPlusPlus) |
| 408 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 409 | // Match the '}'. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 410 | return Actions.ActOnInitList(LBraceLoc, MultiExprArg(Actions), |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 411 | ConsumeBrace()); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 412 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 413 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 414 | bool InitExprsOk = true; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 415 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 416 | while (1) { |
Francois Pichet | 9d24a8b | 2011-12-12 23:24:39 +0000 | [diff] [blame] | 417 | // Handle Microsoft __if_exists/if_not_exists if necessary. |
| 418 | if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
| 419 | Tok.is(tok::kw___if_not_exists))) { |
| 420 | if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) { |
| 421 | if (Tok.isNot(tok::comma)) break; |
| 422 | ConsumeToken(); |
| 423 | } |
| 424 | if (Tok.is(tok::r_brace)) break; |
| 425 | continue; |
| 426 | } |
| 427 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 428 | // Parse: designation[opt] initializer |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 429 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 430 | // If we know that this cannot be a designation, just parse the nested |
| 431 | // initializer directly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 432 | ExprResult SubElt; |
Douglas Gregor | b3f323d | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 433 | if (MayBeDesignationStart()) |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 434 | SubElt = ParseInitializerWithPotentialDesignator(); |
| 435 | else |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 436 | SubElt = ParseInitializer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | dcaa1ca | 2011-01-03 19:31:53 +0000 | [diff] [blame] | 438 | if (Tok.is(tok::ellipsis)) |
| 439 | SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); |
| 440 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 441 | // If we couldn't parse the subelement, bail out. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 442 | if (!SubElt.isInvalid()) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 443 | InitExprs.push_back(SubElt.release()); |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 444 | } else { |
| 445 | InitExprsOk = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 447 | // We have two ways to try to recover from this error: if the code looks |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 448 | // grammatically ok (i.e. we have a comma coming up) try to continue |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 449 | // parsing the rest of the initializer. This allows us to emit |
| 450 | // diagnostics for later elements that we find. If we don't see a comma, |
| 451 | // assume there is a parse error, and just skip to recover. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 452 | // FIXME: This comment doesn't sound right. If there is a r_brace |
| 453 | // immediately, it can't be an error, since there is no other way of |
| 454 | // leaving this loop except through this if. |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 455 | if (Tok.isNot(tok::comma)) { |
| 456 | SkipUntil(tok::r_brace, false, true); |
| 457 | break; |
| 458 | } |
| 459 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 460 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 461 | // If we don't have a comma continued list, we're done. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 462 | if (Tok.isNot(tok::comma)) break; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 463 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 464 | // TODO: save comma locations if some client cares. |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 465 | ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 466 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 467 | // Handle trailing comma. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 468 | if (Tok.is(tok::r_brace)) break; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 469 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 471 | bool closed = !T.consumeClose(); |
| 472 | |
| 473 | if (InitExprsOk && closed) |
| 474 | return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs), |
| 475 | T.getCloseLocation()); |
| 476 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 477 | return ExprError(); // an error occurred. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Francois Pichet | 9d24a8b | 2011-12-12 23:24:39 +0000 | [diff] [blame] | 480 | |
| 481 | // Return true if a comma (or closing brace) is necessary after the |
| 482 | // __if_exists/if_not_exists statement. |
| 483 | bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs, |
| 484 | bool &InitExprsOk) { |
| 485 | bool trailingComma = false; |
| 486 | IfExistsCondition Result; |
| 487 | if (ParseMicrosoftIfExistsCondition(Result)) |
| 488 | return false; |
| 489 | |
| 490 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 491 | if (Braces.consumeOpen()) { |
| 492 | Diag(Tok, diag::err_expected_lbrace); |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | switch (Result.Behavior) { |
| 497 | case IEB_Parse: |
| 498 | // Parse the declarations below. |
| 499 | break; |
| 500 | |
| 501 | case IEB_Dependent: |
| 502 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
| 503 | << Result.IsIfExists; |
| 504 | // Fall through to skip. |
| 505 | |
| 506 | case IEB_Skip: |
| 507 | Braces.skipToEnd(); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | while (Tok.isNot(tok::eof)) { |
| 512 | trailingComma = false; |
| 513 | // If we know that this cannot be a designation, just parse the nested |
| 514 | // initializer directly. |
| 515 | ExprResult SubElt; |
Douglas Gregor | b3f323d | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 516 | if (MayBeDesignationStart()) |
Francois Pichet | 9d24a8b | 2011-12-12 23:24:39 +0000 | [diff] [blame] | 517 | SubElt = ParseInitializerWithPotentialDesignator(); |
| 518 | else |
| 519 | SubElt = ParseInitializer(); |
| 520 | |
| 521 | if (Tok.is(tok::ellipsis)) |
| 522 | SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); |
| 523 | |
| 524 | // If we couldn't parse the subelement, bail out. |
| 525 | if (!SubElt.isInvalid()) |
| 526 | InitExprs.push_back(SubElt.release()); |
| 527 | else |
| 528 | InitExprsOk = false; |
| 529 | |
| 530 | if (Tok.is(tok::comma)) { |
| 531 | ConsumeToken(); |
| 532 | trailingComma = true; |
| 533 | } |
| 534 | |
| 535 | if (Tok.is(tok::r_brace)) |
| 536 | break; |
| 537 | } |
| 538 | |
| 539 | Braces.consumeClose(); |
| 540 | |
| 541 | return !trailingComma; |
| 542 | } |