blob: ac13902c74406930ae7526c1e363380d0c276434 [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"
Sebastian Redla55e52c2008-11-25 22:21:31 +000016#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/Diagnostic.h"
Steve Naroff4aa88f82007-07-19 01:06:55 +000018#include "llvm/ADT/SmallString.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
24/// 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///
Sebastian Redl20df9b72008-12-11 22:51:44 +000060Parser::OwningExprResult Parser::
Chris Lattnereccc53a2008-10-26 22:36:07 +000061ParseInitializerWithPotentialDesignator(InitListDesignations &Designations,
62 unsigned InitNum) {
Sebastian Redl20df9b72008-12-11 22:51:44 +000063
Chris Lattnereccc53a2008-10-26 22:36:07 +000064 // If this is the old-style GNU extension:
65 // designation ::= identifier ':'
66 // Handle it as a field designator. Otherwise, this must be the start of a
67 // normal expression.
68 if (Tok.is(tok::identifier)) {
Chris Lattnerefcadc62008-10-26 22:41:58 +000069 Diag(Tok, diag::ext_gnu_old_style_field_designator);
Chris Lattnereccc53a2008-10-26 22:36:07 +000070
Chris Lattnerefcadc62008-10-26 22:41:58 +000071 Designation &D = Designations.CreateDesignation(InitNum);
72 D.AddDesignator(Designator::getField(Tok.getIdentifierInfo()));
73 ConsumeToken(); // Eat the identifier.
74
Chris Lattner7f9690d2008-10-26 22:49:49 +000075 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Chris Lattnerefcadc62008-10-26 22:41:58 +000076 ConsumeToken();
77 return ParseInitializer();
Chris Lattnereccc53a2008-10-26 22:36:07 +000078 }
79
Chris Lattner0a68b942008-10-26 22:59:19 +000080 // Desig - This is initialized when we see our first designator. We may have
81 // an objc message send with no designator, so we don't want to create this
82 // eagerly.
83 Designation *Desig = 0;
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +000086 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
87 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // designator: '.' identifier
89 ConsumeToken();
Chris Lattner0a68b942008-10-26 22:59:19 +000090
91 // Create designation if we haven't already.
92 if (Desig == 0)
93 Desig = &Designations.CreateDesignation(InitNum);
94
95 if (Tok.isNot(tok::identifier)) {
96 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +000097 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +000098 }
99
100 Desig->AddDesignator(Designator::getField(Tok.getIdentifierInfo()));
101 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000102 continue;
103 }
104
105 // We must have either an array designator now or an objc message send.
106 assert(Tok.is(tok::l_square) && "Unexpected token!");
107
Chris Lattnere2329422008-10-26 23:06:54 +0000108 // Handle the two forms of array designator:
109 // array-designator: '[' constant-expression ']'
110 // array-designator: '[' constant-expression '...' constant-expression ']'
111 //
112 // Also, we have to handle the case where the expression after the
113 // designator an an objc message send: '[' objc-message-expr ']'.
114 // Interesting cases are:
115 // [foo bar] -> objc message send
116 // [foo] -> array designator
117 // [foo ... bar] -> array designator
118 // [4][foo bar] -> obsolete GNU designation with objc message send.
119 //
Chris Lattner7f9690d2008-10-26 22:49:49 +0000120 SourceLocation StartLoc = ConsumeBracket();
121
122 // If Objective-C is enabled and this is a typename or other identifier
123 // receiver, parse this as a message send expression.
124 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattner0fc73f72008-10-26 23:29:41 +0000125 // If we have exactly one array designator, this used the GNU
126 // 'designation: array-designator' extension, otherwise there should be no
127 // designators at all!
128 if (Desig) {
129 if (Desig->getNumDesignators() == 1 &&
130 (Desig->getDesignator(0).isArrayDesignator() ||
131 Desig->getDesignator(0).isArrayRangeDesignator()))
132 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
133 else
134 Diag(Tok, diag::err_expected_equal_designator);
135 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000136
Chris Lattner7f9690d2008-10-26 22:49:49 +0000137 IdentifierInfo *Name = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000138 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000139 return ParseAssignmentExprWithObjCMessageExprStart(
140 StartLoc, NameLoc, Name, ExprArg(Actions));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000141 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000142
Chris Lattner7f9690d2008-10-26 22:49:49 +0000143 // Note that we parse this as an assignment expression, not a constant
144 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
145 // to validate that the expression is a constant.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000146 OwningExprResult Idx(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000147 if (Idx.isInvalid()) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000148 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000149 return move(Idx);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000150 }
151
152 // Given an expression, we could either have a designator (if the next
153 // tokens are '...' or ']' or an objc message send. If this is an objc
154 // message send, handle it now. An objc-message send is the start of
155 // an assignment-expression production.
156 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
157 Tok.isNot(tok::r_square)) {
Chris Lattner0fc73f72008-10-26 23:29:41 +0000158
159 // If we have exactly one array designator, this used the GNU
160 // 'designation: array-designator' extension, otherwise there should be no
161 // designators at all!
162 if (Desig) {
163 if (Desig->getNumDesignators() == 1 &&
164 (Desig->getDesignator(0).isArrayDesignator() ||
165 Desig->getDesignator(0).isArrayRangeDesignator()))
166 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
167 else
168 Diag(Tok, diag::err_expected_equal_designator);
169 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000170
Sebastian Redl1d922962008-12-13 15:32:12 +0000171 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
172 SourceLocation(),
173 0, move_convert(Idx));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000174 }
Chris Lattnere2329422008-10-26 23:06:54 +0000175
176 // Create designation if we haven't already.
177 if (Desig == 0)
178 Desig = &Designations.CreateDesignation(InitNum);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000179
Chris Lattnere2329422008-10-26 23:06:54 +0000180 // If this is a normal array designator, remember it.
181 if (Tok.isNot(tok::ellipsis)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000182 Desig->AddDesignator(Designator::getArray(Idx.release()));
Chris Lattnere2329422008-10-26 23:06:54 +0000183 } else {
184 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000185 Diag(Tok, diag::ext_gnu_array_range);
186 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000187
188 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000189 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000191 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000193 Desig->AddDesignator(Designator::getArrayRange(Idx.release(),
194 RHS.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000196
Chris Lattner7f9690d2008-10-26 22:49:49 +0000197 MatchRHSPunctuation(tok::r_square, StartLoc);
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.
Chris Lattner79ed6b52008-10-26 23:22:23 +0000204 assert(Desig && "Designator didn't get created?");
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)) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000208 ConsumeToken();
209 return ParseInitializer();
210 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000211
Chris Lattner0a68b942008-10-26 22:59:19 +0000212 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000213 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000214 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
215 // parse error.
Chris Lattner79ed6b52008-10-26 23:22:23 +0000216 if (Desig->getNumDesignators() == 1 &&
217 (Desig->getDesignator(0).isArrayDesignator() ||
218 Desig->getDesignator(0).isArrayRangeDesignator())) {
219 Diag(Tok, diag::ext_gnu_missing_equal_designator);
220 return ParseInitializer();
221 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000222
Chris Lattner79ed6b52008-10-26 23:22:23 +0000223 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000224 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000225}
226
227
Chris Lattner0eec2b52008-10-26 22:38:55 +0000228/// ParseBraceInitializer - Called when parsing an initializer that has a
229/// leading open brace.
230///
Reid Spencer5f016e22007-07-11 17:01:13 +0000231/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000232/// '{' initializer-list '}'
233/// '{' initializer-list ',' '}'
234/// [GNU] '{' '}'
235///
236/// initializer-list:
237/// designation[opt] initializer
238/// initializer-list ',' designation[opt] initializer
239///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000240Parser::OwningExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000242
Chris Lattnereccc53a2008-10-26 22:36:07 +0000243 /// InitExprs - This is the actual list of expressions contained in the
244 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000245 ExprVector InitExprs(Actions);
246
Chris Lattnereccc53a2008-10-26 22:36:07 +0000247 /// ExprDesignators - For each initializer, keep track of the designator that
248 /// was specified for it, if any.
249 InitListDesignations InitExprDesignations(Actions);
250
Chris Lattner220ad7c2008-10-26 23:35:51 +0000251 // We support empty initializers, but tell the user that they aren't using
252 // C99-clean code.
253 if (Tok.is(tok::r_brace)) {
254 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
255 // Match the '}'.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000256 return Owned(Actions.ActOnInitList(LBraceLoc, 0, 0, InitExprDesignations,
257 ConsumeBrace()));
Chris Lattner220ad7c2008-10-26 23:35:51 +0000258 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000259
Steve Naroff4aa88f82007-07-19 01:06:55 +0000260 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000261
Steve Naroff4aa88f82007-07-19 01:06:55 +0000262 while (1) {
263 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000264
Steve Naroff4aa88f82007-07-19 01:06:55 +0000265 // If we know that this cannot be a designation, just parse the nested
266 // initializer directly.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000267 OwningExprResult SubElt(Actions);
Chris Lattnerefcadc62008-10-26 22:41:58 +0000268 if (!MayBeDesignationStart(Tok.getKind(), PP))
Steve Naroff4aa88f82007-07-19 01:06:55 +0000269 SubElt = ParseInitializer();
Chris Lattnere2f56192008-11-03 09:28:22 +0000270 else {
Chris Lattnereccc53a2008-10-26 22:36:07 +0000271 SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
272 InitExprs.size());
Chris Lattnere2f56192008-11-03 09:28:22 +0000273
274 // If we had an erroneous initializer, and we had a potentially valid
275 // designator, make sure to remove the designator from
276 // InitExprDesignations, otherwise we'll end up with a designator with no
277 // making initializer.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000278 if (SubElt.isInvalid())
Chris Lattnere2f56192008-11-03 09:28:22 +0000279 InitExprDesignations.EraseDesignation(InitExprs.size());
280 }
281
Steve Naroff4aa88f82007-07-19 01:06:55 +0000282 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000283 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000284 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000285 } else {
286 InitExprsOk = false;
287
288 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000289 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000290 // parsing the rest of the initializer. This allows us to emit
291 // diagnostics for later elements that we find. If we don't see a comma,
292 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000293 // FIXME: This comment doesn't sound right. If there is a r_brace
294 // immediately, it can't be an error, since there is no other way of
295 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000296 if (Tok.isNot(tok::comma)) {
297 SkipUntil(tok::r_brace, false, true);
298 break;
299 }
300 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000301
Steve Naroff4aa88f82007-07-19 01:06:55 +0000302 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000303 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000304
Chris Lattnereccc53a2008-10-26 22:36:07 +0000305 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000306 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000307
Steve Naroff4aa88f82007-07-19 01:06:55 +0000308 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000309 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000310 }
Chris Lattner04d66662007-10-09 17:33:22 +0000311 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000312 return Owned(Actions.ActOnInitList(LBraceLoc, InitExprs.take(),
313 InitExprs.size(),
314 InitExprDesignations, ConsumeBrace()));
315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 // Match the '}'.
317 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000318 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000319}
320