blob: c3d2cd2c4ccc5daa4111feb035f3ba49c91b4386 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements initializer parsing as specified by C99 6.7.8.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000014#include "clang/Parse/Designator.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "clang/Parse/Parser.h"
16#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallString.h"
18using namespace clang;
19
20
21/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattnere29ac822008-10-26 21:46:13 +000022/// designator. If we can tell it is impossible that it is a designator, return
23/// false.
Chris Lattner23c12ef2008-10-26 22:41:58 +000024static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +000025 switch (K) {
26 default: return false;
27 case tok::period: // designator: '.' identifier
28 case tok::l_square: // designator: array-designator
Chris Lattner23c12ef2008-10-26 22:41:58 +000029 return true;
Chris Lattner4b009652007-07-25 00:24:17 +000030 case tok::identifier: // designation: identifier ':'
Chris Lattner23c12ef2008-10-26 22:41:58 +000031 return PP.LookAhead(0).is(tok::colon);
Chris Lattner4b009652007-07-25 00:24:17 +000032 }
33}
34
35/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
36/// checking to see if the token stream starts with a designator.
37///
38/// designation:
39/// designator-list '='
40/// [GNU] array-designator
41/// [GNU] identifier ':'
42///
43/// designator-list:
44/// designator
45/// designator-list designator
46///
47/// designator:
48/// array-designator
49/// '.' identifier
50///
51/// array-designator:
52/// '[' constant-expression ']'
53/// [GNU] '[' constant-expression '...' constant-expression ']'
54///
55/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattnere29ac822008-10-26 21:46:13 +000056/// initializer (because it is an expression). We need to consider this case
57/// when parsing array designators.
Chris Lattner4b009652007-07-25 00:24:17 +000058///
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000059Parser::ExprResult Parser::
60ParseInitializerWithPotentialDesignator(InitListDesignations &Designations,
61 unsigned InitNum) {
62
63 // If this is the old-style GNU extension:
64 // designation ::= identifier ':'
65 // Handle it as a field designator. Otherwise, this must be the start of a
66 // normal expression.
67 if (Tok.is(tok::identifier)) {
Chris Lattner23c12ef2008-10-26 22:41:58 +000068 Diag(Tok, diag::ext_gnu_old_style_field_designator);
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000069
Chris Lattner23c12ef2008-10-26 22:41:58 +000070 Designation &D = Designations.CreateDesignation(InitNum);
71 D.AddDesignator(Designator::getField(Tok.getIdentifierInfo()));
72 ConsumeToken(); // Eat the identifier.
73
Chris Lattner620da262008-10-26 22:49:49 +000074 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Chris Lattner23c12ef2008-10-26 22:41:58 +000075 ConsumeToken();
76 return ParseInitializer();
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000077 }
78
Chris Lattner1aee2322008-10-26 22:59:19 +000079 // Desig - This is initialized when we see our first designator. We may have
80 // an objc message send with no designator, so we don't want to create this
81 // eagerly.
82 Designation *Desig = 0;
83
Chris Lattner4b009652007-07-25 00:24:17 +000084 // Parse each designator in the designator list until we find an initializer.
Chris Lattner620da262008-10-26 22:49:49 +000085 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
86 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +000087 // designator: '.' identifier
88 ConsumeToken();
Chris Lattner1aee2322008-10-26 22:59:19 +000089
90 // Create designation if we haven't already.
91 if (Desig == 0)
92 Desig = &Designations.CreateDesignation(InitNum);
93
94 if (Tok.isNot(tok::identifier)) {
95 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Chris Lattner4b009652007-07-25 00:24:17 +000096 return ExprResult(true);
Chris Lattner1aee2322008-10-26 22:59:19 +000097 }
98
99 Desig->AddDesignator(Designator::getField(Tok.getIdentifierInfo()));
100 ConsumeToken(); // Eat the identifier.
Chris Lattner620da262008-10-26 22:49:49 +0000101 continue;
102 }
103
104 // We must have either an array designator now or an objc message send.
105 assert(Tok.is(tok::l_square) && "Unexpected token!");
106
Chris Lattner9c665cf2008-10-26 23:06:54 +0000107 // Handle the two forms of array designator:
108 // array-designator: '[' constant-expression ']'
109 // array-designator: '[' constant-expression '...' constant-expression ']'
110 //
111 // Also, we have to handle the case where the expression after the
112 // designator an an objc message send: '[' objc-message-expr ']'.
113 // Interesting cases are:
114 // [foo bar] -> objc message send
115 // [foo] -> array designator
116 // [foo ... bar] -> array designator
117 // [4][foo bar] -> obsolete GNU designation with objc message send.
118 //
Chris Lattner620da262008-10-26 22:49:49 +0000119 SourceLocation StartLoc = ConsumeBracket();
120
121 // If Objective-C is enabled and this is a typename or other identifier
122 // receiver, parse this as a message send expression.
123 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattner034198c2008-10-26 23:29:41 +0000124 // If we have exactly one array designator, this used the GNU
125 // 'designation: array-designator' extension, otherwise there should be no
126 // designators at all!
127 if (Desig) {
128 if (Desig->getNumDesignators() == 1 &&
129 (Desig->getDesignator(0).isArrayDesignator() ||
130 Desig->getDesignator(0).isArrayRangeDesignator()))
131 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
132 else
133 Diag(Tok, diag::err_expected_equal_designator);
134 }
135
Chris Lattner620da262008-10-26 22:49:49 +0000136 IdentifierInfo *Name = Tok.getIdentifierInfo();
Steve Naroffc64a53d2008-11-19 15:54:23 +0000137 SourceLocation NameLoc = ConsumeToken();
138 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, NameLoc,
139 Name, 0);
Chris Lattner620da262008-10-26 22:49:49 +0000140 }
141
142 // Note that we parse this as an assignment expression, not a constant
143 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
144 // to validate that the expression is a constant.
145 ExprResult Idx = ParseAssignmentExpression();
146 if (Idx.isInvalid) {
147 SkipUntil(tok::r_square);
148 return Idx;
149 }
150
151 // Given an expression, we could either have a designator (if the next
152 // tokens are '...' or ']' or an objc message send. If this is an objc
153 // message send, handle it now. An objc-message send is the start of
154 // an assignment-expression production.
155 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
156 Tok.isNot(tok::r_square)) {
Chris Lattner034198c2008-10-26 23:29:41 +0000157
158 // If we have exactly one array designator, this used the GNU
159 // 'designation: array-designator' extension, otherwise there should be no
160 // designators at all!
161 if (Desig) {
162 if (Desig->getNumDesignators() == 1 &&
163 (Desig->getDesignator(0).isArrayDesignator() ||
164 Desig->getDesignator(0).isArrayRangeDesignator()))
165 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
166 else
167 Diag(Tok, diag::err_expected_equal_designator);
168 }
169
Steve Naroffc64a53d2008-11-19 15:54:23 +0000170 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
171 SourceLocation(),
172 0, Idx.Val);
Chris Lattner620da262008-10-26 22:49:49 +0000173 }
Chris Lattner9c665cf2008-10-26 23:06:54 +0000174
175 // Create designation if we haven't already.
176 if (Desig == 0)
177 Desig = &Designations.CreateDesignation(InitNum);
Chris Lattner620da262008-10-26 22:49:49 +0000178
Chris Lattner9c665cf2008-10-26 23:06:54 +0000179 // If this is a normal array designator, remember it.
180 if (Tok.isNot(tok::ellipsis)) {
181 Desig->AddDesignator(Designator::getArray(Idx.Val));
182 } else {
183 // Handle the gnu array range extension.
Chris Lattner620da262008-10-26 22:49:49 +0000184 Diag(Tok, diag::ext_gnu_array_range);
185 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000186
Chris Lattner620da262008-10-26 22:49:49 +0000187 ExprResult RHS = ParseConstantExpression();
188 if (RHS.isInvalid) {
Chris Lattner9c665cf2008-10-26 23:06:54 +0000189 Actions.DeleteExpr(Idx.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000190 SkipUntil(tok::r_square);
Chris Lattner620da262008-10-26 22:49:49 +0000191 return RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000192 }
Chris Lattner9c665cf2008-10-26 23:06:54 +0000193 Desig->AddDesignator(Designator::getArrayRange(Idx.Val, RHS.Val));
Chris Lattner4b009652007-07-25 00:24:17 +0000194 }
Chris Lattner620da262008-10-26 22:49:49 +0000195
196 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000197 }
Chris Lattner620da262008-10-26 22:49:49 +0000198
Chris Lattner1aee2322008-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 Lattner245d36d2008-10-26 23:22:23 +0000203 assert(Desig && "Designator didn't get created?");
Chris Lattner1aee2322008-10-26 22:59:19 +0000204
205 // Handle a normal designator sequence end, which is an equal.
Chris Lattner620da262008-10-26 22:49:49 +0000206 if (Tok.is(tok::equal)) {
Chris Lattner620da262008-10-26 22:49:49 +0000207 ConsumeToken();
208 return ParseInitializer();
209 }
210
Chris Lattner1aee2322008-10-26 22:59:19 +0000211 // We read some number of designators and found something that isn't an = or
Chris Lattner245d36d2008-10-26 23:22:23 +0000212 // an initializer. If we have exactly one array designator, this
Chris Lattner1aee2322008-10-26 22:59:19 +0000213 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
214 // parse error.
Chris Lattner245d36d2008-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 Lattner620da262008-10-26 22:49:49 +0000221
Chris Lattner245d36d2008-10-26 23:22:23 +0000222 Diag(Tok, diag::err_expected_equal_designator);
223 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000224}
225
226
Chris Lattner70e9a092008-10-26 22:38:55 +0000227/// ParseBraceInitializer - Called when parsing an initializer that has a
228/// leading open brace.
229///
Chris Lattner4b009652007-07-25 00:24:17 +0000230/// initializer: [C99 6.7.8]
Chris Lattner4b009652007-07-25 00:24:17 +0000231/// '{' initializer-list '}'
232/// '{' initializer-list ',' '}'
233/// [GNU] '{' '}'
234///
235/// initializer-list:
236/// designation[opt] initializer
237/// initializer-list ',' designation[opt] initializer
238///
Chris Lattner70e9a092008-10-26 22:38:55 +0000239Parser::ExprResult Parser::ParseBraceInitializer() {
Chris Lattner4b009652007-07-25 00:24:17 +0000240 SourceLocation LBraceLoc = ConsumeBrace();
241
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000242 /// InitExprs - This is the actual list of expressions contained in the
243 /// initializer.
Chris Lattner4b009652007-07-25 00:24:17 +0000244 llvm::SmallVector<ExprTy*, 8> InitExprs;
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000245
246 /// ExprDesignators - For each initializer, keep track of the designator that
247 /// was specified for it, if any.
248 InitListDesignations InitExprDesignations(Actions);
249
Chris Lattnerce236e72008-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
Chris Lattner4b009652007-07-25 00:24:17 +0000259 bool InitExprsOk = true;
260
261 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.
266 ExprResult SubElt;
Chris Lattner23c12ef2008-10-26 22:41:58 +0000267 if (!MayBeDesignationStart(Tok.getKind(), PP))
Chris Lattner4b009652007-07-25 00:24:17 +0000268 SubElt = ParseInitializer();
Chris Lattnerd30283f2008-11-03 09:28:22 +0000269 else {
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000270 SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
271 InitExprs.size());
Chris Lattnerd30283f2008-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.
277 if (SubElt.isInvalid)
278 InitExprDesignations.EraseDesignation(InitExprs.size());
279 }
280
Chris Lattner4b009652007-07-25 00:24:17 +0000281 // If we couldn't parse the subelement, bail out.
Chris Lattnere77e03d2008-04-20 19:07:56 +0000282 if (!SubElt.isInvalid) {
Chris Lattner4b009652007-07-25 00:24:17 +0000283 InitExprs.push_back(SubElt.Val);
Chris Lattnere77e03d2008-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 Lattnere29ac822008-10-26 21:46:13 +0000288 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattnere77e03d2008-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.
292 if (Tok.isNot(tok::comma)) {
293 SkipUntil(tok::r_brace, false, true);
294 break;
295 }
296 }
Chris Lattner4b009652007-07-25 00:24:17 +0000297
298 // If we don't have a comma continued list, we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000299 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000300
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000301 // TODO: save comma locations if some client cares.
Chris Lattner4b009652007-07-25 00:24:17 +0000302 ConsumeToken();
303
304 // Handle trailing comma.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000305 if (Tok.is(tok::r_brace)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000306 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000307 if (InitExprsOk && Tok.is(tok::r_brace))
Chris Lattnerce236e72008-10-26 23:35:51 +0000308 return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
309 InitExprDesignations, ConsumeBrace());
Chris Lattnere77e03d2008-04-20 19:07:56 +0000310
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000311 // On error, delete any parsed subexpressions.
Chris Lattnere77e03d2008-04-20 19:07:56 +0000312 for (unsigned i = 0, e = InitExprs.size(); i != e; ++i)
313 Actions.DeleteExpr(InitExprs[i]);
314
Chris Lattner4b009652007-07-25 00:24:17 +0000315 // Match the '}'.
316 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
317 return ExprResult(true); // an error occurred.
318}
319