blob: 0b34feaedb9dc08cb67261c412fbe3e0a3eb0434 [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"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.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
Douglas Gregor05c13a32009-01-22 00:58:24 +000071 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
72 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Chris Lattnerefcadc62008-10-26 22:41:58 +000073
Chris Lattner7f9690d2008-10-26 22:49:49 +000074 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +000075 SourceLocation ColonLoc = ConsumeToken();
76
77 Designation &D = Designations.CreateDesignation(InitNum);
78 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
79 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
80 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +000081 }
82
Chris Lattner0a68b942008-10-26 22:59:19 +000083 // Desig - This is initialized when we see our first designator. We may have
84 // an objc message send with no designator, so we don't want to create this
85 // eagerly.
86 Designation *Desig = 0;
87
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +000089 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
90 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +000092 SourceLocation DotLoc = ConsumeToken();
Chris Lattner0a68b942008-10-26 22:59:19 +000093
94 // Create designation if we haven't already.
95 if (Desig == 0)
96 Desig = &Designations.CreateDesignation(InitNum);
97
98 if (Tok.isNot(tok::identifier)) {
99 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000100 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000101 }
102
Douglas Gregor05c13a32009-01-22 00:58:24 +0000103 Desig->AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
104 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000105 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000106 continue;
107 }
108
109 // We must have either an array designator now or an objc message send.
110 assert(Tok.is(tok::l_square) && "Unexpected token!");
111
Chris Lattnere2329422008-10-26 23:06:54 +0000112 // Handle the two forms of array designator:
113 // array-designator: '[' constant-expression ']'
114 // array-designator: '[' constant-expression '...' constant-expression ']'
115 //
116 // Also, we have to handle the case where the expression after the
117 // designator an an objc message send: '[' objc-message-expr ']'.
118 // Interesting cases are:
119 // [foo bar] -> objc message send
120 // [foo] -> array designator
121 // [foo ... bar] -> array designator
122 // [4][foo bar] -> obsolete GNU designation with objc message send.
123 //
Chris Lattner7f9690d2008-10-26 22:49:49 +0000124 SourceLocation StartLoc = ConsumeBracket();
125
126 // If Objective-C is enabled and this is a typename or other identifier
127 // receiver, parse this as a message send expression.
128 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattner0fc73f72008-10-26 23:29:41 +0000129 // If we have exactly one array designator, this used the GNU
130 // 'designation: array-designator' extension, otherwise there should be no
131 // designators at all!
132 if (Desig) {
133 if (Desig->getNumDesignators() == 1 &&
134 (Desig->getDesignator(0).isArrayDesignator() ||
135 Desig->getDesignator(0).isArrayRangeDesignator()))
136 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
137 else
138 Diag(Tok, diag::err_expected_equal_designator);
139 }
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 }
155
156 // 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
158 // message send, handle it now. An objc-message send is the start of
159 // an assignment-expression production.
160 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
161 Tok.isNot(tok::r_square)) {
Chris Lattner0fc73f72008-10-26 23:29:41 +0000162
163 // 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!
166 if (Desig) {
167 if (Desig->getNumDesignators() == 1 &&
168 (Desig->getDesignator(0).isArrayDesignator() ||
169 Desig->getDesignator(0).isArrayRangeDesignator()))
170 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
171 else
172 Diag(Tok, diag::err_expected_equal_designator);
173 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000174
Sebastian Redl1d922962008-12-13 15:32:12 +0000175 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
176 SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000177 0, move(Idx));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000178 }
Chris Lattnere2329422008-10-26 23:06:54 +0000179
180 // Create designation if we haven't already.
181 if (Desig == 0)
182 Desig = &Designations.CreateDesignation(InitNum);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000183
Chris Lattnere2329422008-10-26 23:06:54 +0000184 // If this is a normal array designator, remember it.
185 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000186 Desig->AddDesignator(Designator::getArray(Idx.release(),
187 StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000188 } else {
189 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000190 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000191 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000192
193 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000194 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000196 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000198 Desig->AddDesignator(Designator::getArrayRange(Idx.release(),
Douglas Gregor05c13a32009-01-22 00:58:24 +0000199 RHS.release(),
200 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000202
Douglas Gregor05c13a32009-01-22 00:58:24 +0000203 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
204 Desig->getDesignator(Desig->getNumDesignators() - 1).setRBracketLoc(EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000206
Chris Lattner0a68b942008-10-26 22:59:19 +0000207 // Okay, we're done with the designator sequence. We know that there must be
208 // at least one designator, because the only case we can get into this method
209 // without a designator is when we have an objc message send. That case is
210 // handled and returned from above.
Chris Lattner79ed6b52008-10-26 23:22:23 +0000211 assert(Desig && "Designator didn't get created?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000212
Chris Lattner0a68b942008-10-26 22:59:19 +0000213 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000214 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000215 SourceLocation EqualLoc = ConsumeToken();
216 return Actions.ActOnDesignatedInitializer(*Desig, EqualLoc, false,
217 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000218 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000219
Chris Lattner0a68b942008-10-26 22:59:19 +0000220 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000221 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000222 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
223 // parse error.
Chris Lattner79ed6b52008-10-26 23:22:23 +0000224 if (Desig->getNumDesignators() == 1 &&
225 (Desig->getDesignator(0).isArrayDesignator() ||
226 Desig->getDesignator(0).isArrayRangeDesignator())) {
227 Diag(Tok, diag::ext_gnu_missing_equal_designator);
228 return ParseInitializer();
229 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000230
Chris Lattner79ed6b52008-10-26 23:22:23 +0000231 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000232 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
235
Chris Lattner0eec2b52008-10-26 22:38:55 +0000236/// ParseBraceInitializer - Called when parsing an initializer that has a
237/// leading open brace.
238///
Reid Spencer5f016e22007-07-11 17:01:13 +0000239/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000240/// '{' initializer-list '}'
241/// '{' initializer-list ',' '}'
242/// [GNU] '{' '}'
243///
244/// initializer-list:
245/// designation[opt] initializer
246/// initializer-list ',' designation[opt] initializer
247///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000248Parser::OwningExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000250
Chris Lattnereccc53a2008-10-26 22:36:07 +0000251 /// InitExprs - This is the actual list of expressions contained in the
252 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000253 ExprVector InitExprs(Actions);
254
Chris Lattnereccc53a2008-10-26 22:36:07 +0000255 /// ExprDesignators - For each initializer, keep track of the designator that
256 /// was specified for it, if any.
257 InitListDesignations InitExprDesignations(Actions);
258
Chris Lattner220ad7c2008-10-26 23:35:51 +0000259 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000260 // Empty initializers are a C++ feature and a GNU extension to C.
261 if (!getLang().CPlusPlus)
262 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000263 // Match the '}'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000264 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
265 InitExprDesignations, ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000266 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000267
Steve Naroff4aa88f82007-07-19 01:06:55 +0000268 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000269
Steve Naroff4aa88f82007-07-19 01:06:55 +0000270 while (1) {
271 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000272
Steve Naroff4aa88f82007-07-19 01:06:55 +0000273 // If we know that this cannot be a designation, just parse the nested
274 // initializer directly.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000275 OwningExprResult SubElt(Actions);
Chris Lattnerefcadc62008-10-26 22:41:58 +0000276 if (!MayBeDesignationStart(Tok.getKind(), PP))
Steve Naroff4aa88f82007-07-19 01:06:55 +0000277 SubElt = ParseInitializer();
Chris Lattnere2f56192008-11-03 09:28:22 +0000278 else {
Chris Lattnereccc53a2008-10-26 22:36:07 +0000279 SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
280 InitExprs.size());
Chris Lattnere2f56192008-11-03 09:28:22 +0000281
282 // If we had an erroneous initializer, and we had a potentially valid
283 // designator, make sure to remove the designator from
284 // InitExprDesignations, otherwise we'll end up with a designator with no
Douglas Gregor05c13a32009-01-22 00:58:24 +0000285 // matching initializer.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000286 if (SubElt.isInvalid())
Chris Lattnere2f56192008-11-03 09:28:22 +0000287 InitExprDesignations.EraseDesignation(InitExprs.size());
288 }
289
Steve Naroff4aa88f82007-07-19 01:06:55 +0000290 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000291 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000292 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000293 } else {
294 InitExprsOk = false;
295
296 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000297 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000298 // parsing the rest of the initializer. This allows us to emit
299 // diagnostics for later elements that we find. If we don't see a comma,
300 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000301 // FIXME: This comment doesn't sound right. If there is a r_brace
302 // immediately, it can't be an error, since there is no other way of
303 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000304 if (Tok.isNot(tok::comma)) {
305 SkipUntil(tok::r_brace, false, true);
306 break;
307 }
308 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000309
Steve Naroff4aa88f82007-07-19 01:06:55 +0000310 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000311 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000312
Chris Lattnereccc53a2008-10-26 22:36:07 +0000313 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000314 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000315
Steve Naroff4aa88f82007-07-19 01:06:55 +0000316 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000317 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000318 }
Chris Lattner04d66662007-10-09 17:33:22 +0000319 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000320 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
321 InitExprDesignations, ConsumeBrace());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000322
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // Match the '}'.
324 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000325 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327