blob: c4e79cae3f54dcf06c69a6e130f2f9db53d3cf78 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements initializer parsing as specified by C99 6.7.8.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnereccc53a2008-10-26 22:36:07 +000014#include "clang/Parse/Designator.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Steve Naroff4aa88f82007-07-19 01:06:55 +000017#include "llvm/ADT/SmallString.h"
Daniel Dunbar62a72172009-10-17 23:52:50 +000018#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
21
22/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattner838cb212008-10-26 21:46:13 +000023/// designator. If we can tell it is impossible that it is a designator, return
Mike Stump1eb44332009-09-09 15:08:12 +000024/// false.
Chris Lattnerefcadc62008-10-26 22:41:58 +000025static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +000026 switch (K) {
27 default: return false;
28 case tok::period: // designator: '.' identifier
29 case tok::l_square: // designator: array-designator
Chris Lattnerefcadc62008-10-26 22:41:58 +000030 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +000031 case tok::identifier: // designation: identifier ':'
Chris Lattnerefcadc62008-10-26 22:41:58 +000032 return PP.LookAhead(0).is(tok::colon);
Reid Spencer5f016e22007-07-11 17:01:13 +000033 }
34}
35
36/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
37/// checking to see if the token stream starts with a designator.
38///
39/// designation:
40/// designator-list '='
41/// [GNU] array-designator
42/// [GNU] identifier ':'
43///
44/// designator-list:
45/// designator
46/// designator-list designator
47///
48/// designator:
49/// array-designator
50/// '.' identifier
51///
52/// array-designator:
53/// '[' constant-expression ']'
54/// [GNU] '[' constant-expression '...' constant-expression ']'
55///
56/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner838cb212008-10-26 21:46:13 +000057/// initializer (because it is an expression). We need to consider this case
58/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +000059///
Douglas Gregor5908a922009-03-20 23:11:49 +000060Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +000061
Chris Lattnereccc53a2008-10-26 22:36:07 +000062 // If this is the old-style GNU extension:
63 // designation ::= identifier ':'
64 // Handle it as a field designator. Otherwise, this must be the start of a
65 // normal expression.
66 if (Tok.is(tok::identifier)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +000067 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +000068
Daniel Dunbar62a72172009-10-17 23:52:50 +000069 llvm::SmallString<256> NewSyntax;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +000070 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbar62a72172009-10-17 23:52:50 +000071 << " = ";
Douglas Gregoreeae8f02009-03-28 00:41:23 +000072
Douglas Gregor05c13a32009-01-22 00:58:24 +000073 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner7f9690d2008-10-26 22:49:49 +000075 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +000076 SourceLocation ColonLoc = ConsumeToken();
77
Douglas Gregoreeae8f02009-03-28 00:41:23 +000078 Diag(Tok, diag::ext_gnu_old_style_field_designator)
Mike Stump1eb44332009-09-09 15:08:12 +000079 << CodeModificationHint::CreateReplacement(SourceRange(NameLoc,
Douglas Gregoreeae8f02009-03-28 00:41:23 +000080 ColonLoc),
Daniel Dunbar62a72172009-10-17 23:52:50 +000081 NewSyntax.str());
Douglas Gregoreeae8f02009-03-28 00:41:23 +000082
Douglas Gregor5908a922009-03-20 23:11:49 +000083 Designation D;
Douglas Gregor05c13a32009-01-22 00:58:24 +000084 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump1eb44332009-09-09 15:08:12 +000085 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregor05c13a32009-01-22 00:58:24 +000086 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +000087 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner0a68b942008-10-26 22:59:19 +000089 // Desig - This is initialized when we see our first designator. We may have
90 // an objc message send with no designator, so we don't want to create this
91 // eagerly.
Douglas Gregor5908a922009-03-20 23:11:49 +000092 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +000095 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
96 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +000098 SourceLocation DotLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000099
Chris Lattner0a68b942008-10-26 22:59:19 +0000100 if (Tok.isNot(tok::identifier)) {
101 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000102 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor5908a922009-03-20 23:11:49 +0000105 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
106 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000107 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000108 continue;
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Chris Lattner7f9690d2008-10-26 22:49:49 +0000111 // We must have either an array designator now or an objc message send.
112 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattnere2329422008-10-26 23:06:54 +0000114 // Handle the two forms of array designator:
115 // array-designator: '[' constant-expression ']'
116 // array-designator: '[' constant-expression '...' constant-expression ']'
117 //
118 // Also, we have to handle the case where the expression after the
119 // designator an an objc message send: '[' objc-message-expr ']'.
120 // Interesting cases are:
121 // [foo bar] -> objc message send
122 // [foo] -> array designator
123 // [foo ... bar] -> array designator
124 // [4][foo bar] -> obsolete GNU designation with objc message send.
125 //
Chris Lattner7f9690d2008-10-26 22:49:49 +0000126 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattner7f9690d2008-10-26 22:49:49 +0000128 // If Objective-C is enabled and this is a typename or other identifier
129 // receiver, parse this as a message send expression.
130 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattner0fc73f72008-10-26 23:29:41 +0000131 // If we have exactly one array designator, this used the GNU
132 // 'designation: array-designator' extension, otherwise there should be no
133 // designators at all!
Mike Stump1eb44332009-09-09 15:08:12 +0000134 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000135 (Desig.getDesignator(0).isArrayDesignator() ||
136 Desig.getDesignator(0).isArrayRangeDesignator()))
137 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
138 else if (Desig.getNumDesignators() > 0)
139 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl1d922962008-12-13 15:32:12 +0000140
Chris Lattner7f9690d2008-10-26 22:49:49 +0000141 IdentifierInfo *Name = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000142 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000143 return ParseAssignmentExprWithObjCMessageExprStart(
144 StartLoc, NameLoc, Name, ExprArg(Actions));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000145 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000146
Chris Lattner7f9690d2008-10-26 22:49:49 +0000147 // Note that we parse this as an assignment expression, not a constant
148 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
149 // to validate that the expression is a constant.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000150 OwningExprResult Idx(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000151 if (Idx.isInvalid()) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000152 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000153 return move(Idx);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner7f9690d2008-10-26 22:49:49 +0000156 // Given an expression, we could either have a designator (if the next
157 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000158 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000159 // an assignment-expression production.
Mike Stump1eb44332009-09-09 15:08:12 +0000160 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000161 Tok.isNot(tok::r_square)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattner0fc73f72008-10-26 23:29:41 +0000163 // If we have exactly one array designator, this used the GNU
164 // 'designation: array-designator' extension, otherwise there should be no
165 // designators at all!
Mike Stump1eb44332009-09-09 15:08:12 +0000166 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000167 (Desig.getDesignator(0).isArrayDesignator() ||
168 Desig.getDesignator(0).isArrayRangeDesignator()))
169 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
170 else if (Desig.getNumDesignators() > 0)
171 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000172
Sebastian Redl1d922962008-12-13 15:32:12 +0000173 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
174 SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000175 0, move(Idx));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000176 }
Chris Lattnere2329422008-10-26 23:06:54 +0000177
Chris Lattnere2329422008-10-26 23:06:54 +0000178 // If this is a normal array designator, remember it.
179 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000180 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000181 } else {
182 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000183 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000184 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000185
186 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000187 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000189 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000191 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
192 RHS.release(),
193 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000195
Douglas Gregor05c13a32009-01-22 00:58:24 +0000196 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor5908a922009-03-20 23:11:49 +0000197 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000199
Chris Lattner0a68b942008-10-26 22:59:19 +0000200 // Okay, we're done with the designator sequence. We know that there must be
201 // at least one designator, because the only case we can get into this method
202 // without a designator is when we have an objc message send. That case is
203 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000204 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000205
Chris Lattner0a68b942008-10-26 22:59:19 +0000206 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000207 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000208 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000209 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000210 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000211 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000212
Chris Lattner0a68b942008-10-26 22:59:19 +0000213 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000214 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000215 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
216 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000217 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000218 (Desig.getDesignator(0).isArrayDesignator() ||
219 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000220 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor558cb562009-04-02 01:08:08 +0000221 << CodeModificationHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000222 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000223 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000224 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000225
Chris Lattner79ed6b52008-10-26 23:22:23 +0000226 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000227 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000228}
229
230
Chris Lattner0eec2b52008-10-26 22:38:55 +0000231/// ParseBraceInitializer - Called when parsing an initializer that has a
232/// leading open brace.
233///
Reid Spencer5f016e22007-07-11 17:01:13 +0000234/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000235/// '{' initializer-list '}'
236/// '{' initializer-list ',' '}'
237/// [GNU] '{' '}'
238///
239/// initializer-list:
240/// designation[opt] initializer
241/// initializer-list ',' designation[opt] initializer
242///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000243Parser::OwningExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000245
Chris Lattnereccc53a2008-10-26 22:36:07 +0000246 /// InitExprs - This is the actual list of expressions contained in the
247 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000248 ExprVector InitExprs(Actions);
249
Chris Lattner220ad7c2008-10-26 23:35:51 +0000250 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000251 // Empty initializers are a C++ feature and a GNU extension to C.
252 if (!getLang().CPlusPlus)
253 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000254 // Match the '}'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000255 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
Douglas Gregor5908a922009-03-20 23:11:49 +0000256 ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000257 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000258
Steve Naroff4aa88f82007-07-19 01:06:55 +0000259 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000260
Steve Naroff4aa88f82007-07-19 01:06:55 +0000261 while (1) {
262 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000263
Steve Naroff4aa88f82007-07-19 01:06:55 +0000264 // If we know that this cannot be a designation, just parse the nested
265 // initializer directly.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000266 OwningExprResult SubElt(Actions);
Douglas Gregor5908a922009-03-20 23:11:49 +0000267 if (MayBeDesignationStart(Tok.getKind(), PP))
268 SubElt = ParseInitializerWithPotentialDesignator();
269 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000270 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Steve Naroff4aa88f82007-07-19 01:06:55 +0000272 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000273 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000274 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000275 } else {
276 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner65bb89c2008-04-20 19:07:56 +0000278 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000279 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000280 // parsing the rest of the initializer. This allows us to emit
281 // diagnostics for later elements that we find. If we don't see a comma,
282 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000283 // FIXME: This comment doesn't sound right. If there is a r_brace
284 // immediately, it can't be an error, since there is no other way of
285 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000286 if (Tok.isNot(tok::comma)) {
287 SkipUntil(tok::r_brace, false, true);
288 break;
289 }
290 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000291
Steve Naroff4aa88f82007-07-19 01:06:55 +0000292 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000293 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000294
Chris Lattnereccc53a2008-10-26 22:36:07 +0000295 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000296 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000297
Steve Naroff4aa88f82007-07-19 01:06:55 +0000298 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000299 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000300 }
Chris Lattner04d66662007-10-09 17:33:22 +0000301 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000302 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor5908a922009-03-20 23:11:49 +0000303 ConsumeBrace());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Match the '}'.
306 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000307 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000308}
309