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 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 14 | #include "clang/Parse/Designator.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
Chris Lattner | 1e46136 | 2010-04-12 06:36:00 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Scope.h" |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | |
| 23 | /// MayBeDesignationStart - Return true if this token might be the start of a |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 24 | /// designator. If we can tell it is impossible that it is a designator, return |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | /// false. |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 26 | static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | switch (K) { |
| 28 | default: return false; |
| 29 | case tok::period: // designator: '.' identifier |
| 30 | case tok::l_square: // designator: array-designator |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 31 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | case tok::identifier: // designation: identifier ':' |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 33 | return PP.LookAhead(0).is(tok::colon); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 37 | static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc, |
| 38 | Designation &Desig) { |
| 39 | // If we have exactly one array designator, this used the GNU |
| 40 | // 'designation: array-designator' extension, otherwise there should be no |
| 41 | // designators at all! |
| 42 | if (Desig.getNumDesignators() == 1 && |
| 43 | (Desig.getDesignator(0).isArrayDesignator() || |
| 44 | Desig.getDesignator(0).isArrayRangeDesignator())) |
| 45 | P.Diag(Loc, diag::ext_gnu_missing_equal_designator); |
| 46 | else if (Desig.getNumDesignators() > 0) |
| 47 | P.Diag(Loc, diag::err_expected_equal_designator); |
| 48 | } |
| 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production |
| 51 | /// checking to see if the token stream starts with a designator. |
| 52 | /// |
| 53 | /// designation: |
| 54 | /// designator-list '=' |
| 55 | /// [GNU] array-designator |
| 56 | /// [GNU] identifier ':' |
| 57 | /// |
| 58 | /// designator-list: |
| 59 | /// designator |
| 60 | /// designator-list designator |
| 61 | /// |
| 62 | /// designator: |
| 63 | /// array-designator |
| 64 | /// '.' identifier |
| 65 | /// |
| 66 | /// array-designator: |
| 67 | /// '[' constant-expression ']' |
| 68 | /// [GNU] '[' constant-expression '...' constant-expression ']' |
| 69 | /// |
| 70 | /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 71 | /// initializer (because it is an expression). We need to consider this case |
| 72 | /// when parsing array designators. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | /// |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 74 | Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() { |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 75 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 76 | // If this is the old-style GNU extension: |
| 77 | // designation ::= identifier ':' |
| 78 | // Handle it as a field designator. Otherwise, this must be the start of a |
| 79 | // normal expression. |
| 80 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 81 | const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 83 | llvm::SmallString<256> NewSyntax; |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 84 | llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName() |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 85 | << " = "; |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 86 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 87 | SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 89 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 90 | SourceLocation ColonLoc = ConsumeToken(); |
| 91 | |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 92 | Diag(Tok, diag::ext_gnu_old_style_field_designator) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 93 | << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc), |
| 94 | NewSyntax.str()); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 96 | Designation D; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 97 | D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 99 | ParseInitializer()); |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 102 | // Desig - This is initialized when we see our first designator. We may have |
| 103 | // an objc message send with no designator, so we don't want to create this |
| 104 | // eagerly. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 105 | Designation Desig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | // Parse each designator in the designator list until we find an initializer. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 108 | while (Tok.is(tok::period) || Tok.is(tok::l_square)) { |
| 109 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | // designator: '.' identifier |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 111 | SourceLocation DotLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 113 | if (Tok.isNot(tok::identifier)) { |
| 114 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 115 | return ExprError(); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 118 | Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, |
| 119 | Tok.getLocation())); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 120 | ConsumeToken(); // Eat the identifier. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 121 | continue; |
| 122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 124 | // We must have either an array designator now or an objc message send. |
| 125 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 127 | // Handle the two forms of array designator: |
| 128 | // array-designator: '[' constant-expression ']' |
| 129 | // array-designator: '[' constant-expression '...' constant-expression ']' |
| 130 | // |
| 131 | // Also, we have to handle the case where the expression after the |
| 132 | // designator an an objc message send: '[' objc-message-expr ']'. |
| 133 | // Interesting cases are: |
| 134 | // [foo bar] -> objc message send |
| 135 | // [foo] -> array designator |
| 136 | // [foo ... bar] -> array designator |
| 137 | // [4][foo bar] -> obsolete GNU designation with objc message send. |
| 138 | // |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 139 | SourceLocation StartLoc = ConsumeBracket(); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 140 | OwningExprResult Idx(Actions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 142 | // If Objective-C is enabled and this is a typename (class message |
| 143 | // send) or send to 'super', parse this as a message send |
| 144 | // expression. We handle C++ and C separately, since C++ requires |
| 145 | // much more complicated parsing. |
| 146 | if (getLang().ObjC1 && getLang().CPlusPlus) { |
| 147 | // Send to 'super'. |
| 148 | if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 149 | NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope()) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 150 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
| 151 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 152 | ConsumeToken(), 0, |
| 153 | ExprArg(Actions)); |
| 154 | } |
| 155 | |
| 156 | // Parse the receiver, which is either a type or an expression. |
| 157 | bool IsExpr; |
| 158 | void *TypeOrExpr; |
| 159 | if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { |
| 160 | SkipUntil(tok::r_square); |
| 161 | return ExprError(); |
| 162 | } |
| 163 | |
| 164 | // If the receiver was a type, we have a class message; parse |
| 165 | // the rest of it. |
| 166 | if (!IsExpr) { |
| 167 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
| 168 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 169 | SourceLocation(), |
| 170 | TypeOrExpr, |
| 171 | ExprArg(Actions)); |
| 172 | } |
| 173 | |
| 174 | // If the receiver was an expression, we still don't know |
| 175 | // whether we have a message send or an array designator; just |
| 176 | // adopt the expression for further analysis below. |
| 177 | // FIXME: potentially-potentially evaluated expression above? |
| 178 | Idx = OwningExprResult(Actions, TypeOrExpr); |
| 179 | } else if (getLang().ObjC1 && Tok.is(tok::identifier)) { |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 180 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 181 | SourceLocation IILoc = Tok.getLocation(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 182 | TypeTy *ReceiverType; |
Chris Lattner | 1e46136 | 2010-04-12 06:36:00 +0000 | [diff] [blame] | 183 | // Three cases. This is a message send to a type: [type foo] |
| 184 | // This is a message send to super: [super foo] |
| 185 | // This is a message sent to an expr: [super.bar foo] |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 186 | switch (Action::ObjCMessageKind Kind |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 187 | = Actions.getObjCMessageKind(getCurScope(), II, IILoc, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 188 | II == Ident_super, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 189 | NextToken().is(tok::period), |
| 190 | ReceiverType)) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 191 | case Action::ObjCSuperMessage: |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 192 | case Action::ObjCClassMessage: |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 193 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 194 | if (Kind == Action::ObjCSuperMessage) |
| 195 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 196 | ConsumeToken(), |
| 197 | 0, |
| 198 | ExprArg(Actions)); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 199 | ConsumeToken(); // the identifier |
| 200 | if (!ReceiverType) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 201 | SkipUntil(tok::r_square); |
| 202 | return ExprError(); |
| 203 | } |
| 204 | |
| 205 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 206 | SourceLocation(), |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 207 | ReceiverType, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 208 | ExprArg(Actions)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 209 | |
| 210 | case Action::ObjCInstanceMessage: |
| 211 | // Fall through; we'll just parse the expression and |
| 212 | // (possibly) treat this like an Objective-C message send |
| 213 | // later. |
| 214 | break; |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 216 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 218 | // Parse the index expression, if we haven't already gotten one |
| 219 | // above (which can only happen in Objective-C++). |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 220 | // Note that we parse this as an assignment expression, not a constant |
| 221 | // expression (allowing *=, =, etc) to handle the objc case. Sema needs |
| 222 | // to validate that the expression is a constant. |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 223 | // FIXME: We also need to tell Sema that we're in a |
| 224 | // potentially-potentially evaluated context. |
| 225 | if (!Idx.get()) { |
| 226 | Idx = ParseAssignmentExpression(); |
| 227 | if (Idx.isInvalid()) { |
| 228 | SkipUntil(tok::r_square); |
| 229 | return move(Idx); |
| 230 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 233 | // Given an expression, we could either have a designator (if the next |
| 234 | // 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] | 235 | // 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] | 236 | // an assignment-expression production. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) && |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 238 | Tok.isNot(tok::r_square)) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 239 | CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 240 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 241 | SourceLocation(), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 242 | 0, move(Idx)); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 244 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 245 | // If this is a normal array designator, remember it. |
| 246 | if (Tok.isNot(tok::ellipsis)) { |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 247 | Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc)); |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 248 | } else { |
| 249 | // Handle the gnu array range extension. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 250 | Diag(Tok, diag::ext_gnu_array_range); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 251 | SourceLocation EllipsisLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 252 | |
| 253 | OwningExprResult RHS(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 254 | if (RHS.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | SkipUntil(tok::r_square); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 256 | return move(RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | } |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 258 | Desig.AddDesignator(Designator::getArrayRange(Idx.release(), |
| 259 | RHS.release(), |
| 260 | StartLoc, EllipsisLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 263 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 264 | Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 267 | // Okay, we're done with the designator sequence. We know that there must be |
| 268 | // at least one designator, because the only case we can get into this method |
| 269 | // without a designator is when we have an objc message send. That case is |
| 270 | // handled and returned from above. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 271 | assert(!Desig.empty() && "Designator is empty?"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 273 | // Handle a normal designator sequence end, which is an equal. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 274 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 275 | SourceLocation EqualLoc = ConsumeToken(); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 276 | return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 277 | ParseInitializer()); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 278 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 280 | // 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] | 281 | // an initializer. If we have exactly one array designator, this |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 282 | // is the GNU 'designation: array-designator' extension. Otherwise, it is a |
| 283 | // parse error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | if (Desig.getNumDesignators() == 1 && |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 285 | (Desig.getDesignator(0).isArrayDesignator() || |
| 286 | Desig.getDesignator(0).isArrayRangeDesignator())) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 287 | Diag(Tok, diag::ext_gnu_missing_equal_designator) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 288 | << FixItHint::CreateInsertion(Tok.getLocation(), "= "); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 289 | return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), |
Douglas Gregor | 68c56de | 2009-03-27 23:40:29 +0000 | [diff] [blame] | 290 | true, ParseInitializer()); |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 291 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 293 | Diag(Tok, diag::err_expected_equal_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 294 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
Chris Lattner | 0eec2b5 | 2008-10-26 22:38:55 +0000 | [diff] [blame] | 298 | /// ParseBraceInitializer - Called when parsing an initializer that has a |
| 299 | /// leading open brace. |
| 300 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | /// initializer: [C99 6.7.8] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | /// '{' initializer-list '}' |
| 303 | /// '{' initializer-list ',' '}' |
| 304 | /// [GNU] '{' '}' |
| 305 | /// |
| 306 | /// initializer-list: |
| 307 | /// designation[opt] initializer |
| 308 | /// initializer-list ',' designation[opt] initializer |
| 309 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 310 | Parser::OwningExprResult Parser::ParseBraceInitializer() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | SourceLocation LBraceLoc = ConsumeBrace(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 312 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 313 | /// InitExprs - This is the actual list of expressions contained in the |
| 314 | /// initializer. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 315 | ExprVector InitExprs(Actions); |
| 316 | |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 317 | if (Tok.is(tok::r_brace)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 318 | // Empty initializers are a C++ feature and a GNU extension to C. |
| 319 | if (!getLang().CPlusPlus) |
| 320 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 321 | // Match the '}'. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 322 | return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions), |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 323 | ConsumeBrace()); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 324 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 325 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 326 | bool InitExprsOk = true; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 327 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 328 | while (1) { |
| 329 | // Parse: designation[opt] initializer |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 330 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 331 | // If we know that this cannot be a designation, just parse the nested |
| 332 | // initializer directly. |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 333 | OwningExprResult SubElt(Actions); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 334 | if (MayBeDesignationStart(Tok.getKind(), PP)) |
| 335 | SubElt = ParseInitializerWithPotentialDesignator(); |
| 336 | else |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 337 | SubElt = ParseInitializer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 339 | // If we couldn't parse the subelement, bail out. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 340 | if (!SubElt.isInvalid()) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 341 | InitExprs.push_back(SubElt.release()); |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 342 | } else { |
| 343 | InitExprsOk = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 345 | // We have two ways to try to recover from this error: if the code looks |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 346 | // gramatically ok (i.e. we have a comma coming up) try to continue |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 347 | // parsing the rest of the initializer. This allows us to emit |
| 348 | // diagnostics for later elements that we find. If we don't see a comma, |
| 349 | // assume there is a parse error, and just skip to recover. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 350 | // FIXME: This comment doesn't sound right. If there is a r_brace |
| 351 | // immediately, it can't be an error, since there is no other way of |
| 352 | // leaving this loop except through this if. |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 353 | if (Tok.isNot(tok::comma)) { |
| 354 | SkipUntil(tok::r_brace, false, true); |
| 355 | break; |
| 356 | } |
| 357 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 358 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 359 | // If we don't have a comma continued list, we're done. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 360 | if (Tok.isNot(tok::comma)) break; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 361 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 362 | // TODO: save comma locations if some client cares. |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 363 | ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 364 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 365 | // Handle trailing comma. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 366 | if (Tok.is(tok::r_brace)) break; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 368 | if (InitExprsOk && Tok.is(tok::r_brace)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 369 | return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs), |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 370 | ConsumeBrace()); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 371 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | // Match the '}'. |
| 373 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 374 | return ExprError(); // an error occurred. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | } |
| 376 | |