blob: 425377679ceacfebe1d42485ab8807de9061245d [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///
Chris Lattnereccc53a2008-10-26 22:36:07 +000060Parser::ExprResult Parser::
61ParseInitializerWithPotentialDesignator(InitListDesignations &Designations,
62 unsigned InitNum) {
63
64 // 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);
Reid Spencer5f016e22007-07-11 17:01:13 +000097 return ExprResult(true);
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 }
136
Chris Lattner7f9690d2008-10-26 22:49:49 +0000137 IdentifierInfo *Name = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000138 SourceLocation NameLoc = ConsumeToken();
139 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, NameLoc,
140 Name, 0);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000141 }
142
143 // 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 Redl0e9eabc2008-12-09 13:15:23 +0000146 ExprOwner Idx(Actions, ParseAssignmentExpression());
147 if (Idx.isInvalid()) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000148 SkipUntil(tok::r_square);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000149 return Idx.move();
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 }
170
Steve Naroff5cb93b82008-11-19 15:54:23 +0000171 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
172 SourceLocation(),
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000173 0, Idx.move());
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 Redl0e9eabc2008-12-09 13:15:23 +0000182 Desig->AddDesignator(Designator::getArray(Idx.move()));
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();
Reid Spencer5f016e22007-07-11 17:01:13 +0000187
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000188 ExprOwner RHS(Actions, ParseConstantExpression());
189 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 SkipUntil(tok::r_square);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000191 return RHS.move();
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000193 Desig->AddDesignator(Designator::getArrayRange(Idx.move(), RHS.move()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000195
Chris Lattner7f9690d2008-10-26 22:49:49 +0000196 MatchRHSPunctuation(tok::r_square, StartLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000198
Chris Lattner0a68b942008-10-26 22:59:19 +0000199 // Okay, we're done with the designator sequence. We know that there must be
200 // at least one designator, because the only case we can get into this method
201 // without a designator is when we have an objc message send. That case is
202 // handled and returned from above.
Chris Lattner79ed6b52008-10-26 23:22:23 +0000203 assert(Desig && "Designator didn't get created?");
Chris Lattner0a68b942008-10-26 22:59:19 +0000204
205 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000206 if (Tok.is(tok::equal)) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000207 ConsumeToken();
208 return ParseInitializer();
209 }
210
Chris Lattner0a68b942008-10-26 22:59:19 +0000211 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000212 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000213 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
214 // parse error.
Chris Lattner79ed6b52008-10-26 23:22:23 +0000215 if (Desig->getNumDesignators() == 1 &&
216 (Desig->getDesignator(0).isArrayDesignator() ||
217 Desig->getDesignator(0).isArrayRangeDesignator())) {
218 Diag(Tok, diag::ext_gnu_missing_equal_designator);
219 return ParseInitializer();
220 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000221
Chris Lattner79ed6b52008-10-26 23:22:23 +0000222 Diag(Tok, diag::err_expected_equal_designator);
223 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000224}
225
226
Chris Lattner0eec2b52008-10-26 22:38:55 +0000227/// ParseBraceInitializer - Called when parsing an initializer that has a
228/// leading open brace.
229///
Reid Spencer5f016e22007-07-11 17:01:13 +0000230/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000231/// '{' initializer-list '}'
232/// '{' initializer-list ',' '}'
233/// [GNU] '{' '}'
234///
235/// initializer-list:
236/// designation[opt] initializer
237/// initializer-list ',' designation[opt] initializer
238///
Chris Lattner0eec2b52008-10-26 22:38:55 +0000239Parser::ExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000241
Chris Lattnereccc53a2008-10-26 22:36:07 +0000242 /// InitExprs - This is the actual list of expressions contained in the
243 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000244 ExprVector InitExprs(Actions);
245
Chris Lattnereccc53a2008-10-26 22:36:07 +0000246 /// ExprDesignators - For each initializer, keep track of the designator that
247 /// was specified for it, if any.
248 InitListDesignations InitExprDesignations(Actions);
249
Chris Lattner220ad7c2008-10-26 23:35:51 +0000250 // We support empty initializers, but tell the user that they aren't using
251 // C99-clean code.
252 if (Tok.is(tok::r_brace)) {
253 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
254 // Match the '}'.
255 return Actions.ActOnInitList(LBraceLoc, 0, 0, InitExprDesignations,
256 ConsumeBrace());
257 }
258
Steve Naroff4aa88f82007-07-19 01:06:55 +0000259 bool InitExprsOk = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000260
Steve Naroff4aa88f82007-07-19 01:06:55 +0000261 while (1) {
262 // Parse: designation[opt] initializer
263
264 // If we know that this cannot be a designation, just parse the nested
265 // initializer directly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000266 ExprOwner SubElt(Actions);
Chris Lattnerefcadc62008-10-26 22:41:58 +0000267 if (!MayBeDesignationStart(Tok.getKind(), PP))
Steve Naroff4aa88f82007-07-19 01:06:55 +0000268 SubElt = ParseInitializer();
Chris Lattnere2f56192008-11-03 09:28:22 +0000269 else {
Chris Lattnereccc53a2008-10-26 22:36:07 +0000270 SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
271 InitExprs.size());
Chris Lattnere2f56192008-11-03 09:28:22 +0000272
273 // If we had an erroneous initializer, and we had a potentially valid
274 // designator, make sure to remove the designator from
275 // InitExprDesignations, otherwise we'll end up with a designator with no
276 // making initializer.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000277 if (SubElt.isInvalid())
Chris Lattnere2f56192008-11-03 09:28:22 +0000278 InitExprDesignations.EraseDesignation(InitExprs.size());
279 }
280
Steve Naroff4aa88f82007-07-19 01:06:55 +0000281 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000282 if (!SubElt.isInvalid()) {
283 InitExprs.push_back(SubElt.move());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000284 } else {
285 InitExprsOk = false;
286
287 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000288 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000289 // parsing the rest of the initializer. This allows us to emit
290 // diagnostics for later elements that we find. If we don't see a comma,
291 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000292 // FIXME: This comment doesn't sound right. If there is a r_brace
293 // immediately, it can't be an error, since there is no other way of
294 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000295 if (Tok.isNot(tok::comma)) {
296 SkipUntil(tok::r_brace, false, true);
297 break;
298 }
299 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000300
Steve Naroff4aa88f82007-07-19 01:06:55 +0000301 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000302 if (Tok.isNot(tok::comma)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000303
Chris Lattnereccc53a2008-10-26 22:36:07 +0000304 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000305 ConsumeToken();
306
307 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000308 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000309 }
Chris Lattner04d66662007-10-09 17:33:22 +0000310 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000311 return Actions.ActOnInitList(LBraceLoc, InitExprs.take(), InitExprs.size(),
Chris Lattner220ad7c2008-10-26 23:35:51 +0000312 InitExprDesignations, ConsumeBrace());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000313
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 // Match the '}'.
315 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000316 return ExprResult(true); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000317}
318