blob: 123908d25a4be2328db7b96b7d0f7ee46fbba779 [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"
Chris Lattner1e461362010-04-12 06:36:00 +000017#include "clang/Parse/Scope.h"
Steve Naroff4aa88f82007-07-19 01:06:55 +000018#include "llvm/ADT/SmallString.h"
Daniel Dunbar62a72172009-10-17 23:52:50 +000019#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
22
23/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattner838cb212008-10-26 21:46:13 +000024/// designator. If we can tell it is impossible that it is a designator, return
Mike Stump1eb44332009-09-09 15:08:12 +000025/// false.
Chris Lattnerefcadc62008-10-26 22:41:58 +000026static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +000027 switch (K) {
28 default: return false;
29 case tok::period: // designator: '.' identifier
30 case tok::l_square: // designator: array-designator
Chris Lattnerefcadc62008-10-26 22:41:58 +000031 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 case tok::identifier: // designation: identifier ':'
Chris Lattnerefcadc62008-10-26 22:41:58 +000033 return PP.LookAhead(0).is(tok::colon);
Reid Spencer5f016e22007-07-11 17:01:13 +000034 }
35}
36
37/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
38/// checking to see if the token stream starts with a designator.
39///
40/// designation:
41/// designator-list '='
42/// [GNU] array-designator
43/// [GNU] identifier ':'
44///
45/// designator-list:
46/// designator
47/// designator-list designator
48///
49/// designator:
50/// array-designator
51/// '.' identifier
52///
53/// array-designator:
54/// '[' constant-expression ']'
55/// [GNU] '[' constant-expression '...' constant-expression ']'
56///
57/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner838cb212008-10-26 21:46:13 +000058/// initializer (because it is an expression). We need to consider this case
59/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +000060///
Douglas Gregor5908a922009-03-20 23:11:49 +000061Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +000062
Chris Lattnereccc53a2008-10-26 22:36:07 +000063 // 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)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +000068 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +000069
Daniel Dunbar62a72172009-10-17 23:52:50 +000070 llvm::SmallString<256> NewSyntax;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +000071 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbar62a72172009-10-17 23:52:50 +000072 << " = ";
Douglas Gregoreeae8f02009-03-28 00:41:23 +000073
Douglas Gregor05c13a32009-01-22 00:58:24 +000074 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +000075
Chris Lattner7f9690d2008-10-26 22:49:49 +000076 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +000077 SourceLocation ColonLoc = ConsumeToken();
78
Douglas Gregoreeae8f02009-03-28 00:41:23 +000079 Diag(Tok, diag::ext_gnu_old_style_field_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +000080 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
81 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 Lattnereb483eb2010-04-11 08:28:14 +0000128 // If Objective-C is enabled and this is a typename (class message send) or
Chris Lattner1e461362010-04-12 06:36:00 +0000129 // send to 'super', parse this as a message send expression.
Chris Lattnereb483eb2010-04-11 08:28:14 +0000130 if (getLang().ObjC1 && Tok.is(tok::identifier)) {
131 IdentifierInfo *II = Tok.getIdentifierInfo();
Sebastian Redl1d922962008-12-13 15:32:12 +0000132
Chris Lattner1e461362010-04-12 06:36:00 +0000133 // Three cases. This is a message send to a type: [type foo]
134 // This is a message send to super: [super foo]
135 // This is a message sent to an expr: [super.bar foo]
136 if (Actions.getTypeName(*II, Tok.getLocation(), CurScope) ||
137 (II == Ident_super && GetLookAheadToken(1).isNot(tok::period) &&
138 CurScope->isInObjcMethodScope())) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000139 // If we have exactly one array designator, this used the GNU
140 // 'designation: array-designator' extension, otherwise there should be no
141 // designators at all!
142 if (Desig.getNumDesignators() == 1 &&
143 (Desig.getDesignator(0).isArrayDesignator() ||
144 Desig.getDesignator(0).isArrayRangeDesignator()))
145 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
146 else if (Desig.getNumDesignators() > 0)
147 Diag(Tok, diag::err_expected_equal_designator);
148
149 SourceLocation NameLoc = ConsumeToken();
150 return ParseAssignmentExprWithObjCMessageExprStart(
151 StartLoc, NameLoc, II, ExprArg(Actions));
152 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000153 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000154
Chris Lattner7f9690d2008-10-26 22:49:49 +0000155 // Note that we parse this as an assignment expression, not a constant
156 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
157 // to validate that the expression is a constant.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000158 OwningExprResult Idx(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000159 if (Idx.isInvalid()) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000160 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000161 return move(Idx);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner7f9690d2008-10-26 22:49:49 +0000164 // Given an expression, we could either have a designator (if the next
165 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000166 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000167 // an assignment-expression production.
Mike Stump1eb44332009-09-09 15:08:12 +0000168 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000169 Tok.isNot(tok::r_square)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattner0fc73f72008-10-26 23:29:41 +0000171 // If we have exactly one array designator, this used the GNU
172 // 'designation: array-designator' extension, otherwise there should be no
173 // designators at all!
Mike Stump1eb44332009-09-09 15:08:12 +0000174 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000175 (Desig.getDesignator(0).isArrayDesignator() ||
176 Desig.getDesignator(0).isArrayRangeDesignator()))
177 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
178 else if (Desig.getNumDesignators() > 0)
179 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000180
Sebastian Redl1d922962008-12-13 15:32:12 +0000181 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
182 SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000183 0, move(Idx));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000184 }
Chris Lattnere2329422008-10-26 23:06:54 +0000185
Chris Lattnere2329422008-10-26 23:06:54 +0000186 // If this is a normal array designator, remember it.
187 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000188 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000189 } else {
190 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000191 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000192 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000193
194 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000195 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000197 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000199 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
200 RHS.release(),
201 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000203
Douglas Gregor05c13a32009-01-22 00:58:24 +0000204 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor5908a922009-03-20 23:11:49 +0000205 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000207
Chris Lattner0a68b942008-10-26 22:59:19 +0000208 // Okay, we're done with the designator sequence. We know that there must be
209 // at least one designator, because the only case we can get into this method
210 // without a designator is when we have an objc message send. That case is
211 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000212 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000213
Chris Lattner0a68b942008-10-26 22:59:19 +0000214 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000215 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000216 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000217 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000218 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000219 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000220
Chris Lattner0a68b942008-10-26 22:59:19 +0000221 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000222 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000223 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
224 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000225 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000226 (Desig.getDesignator(0).isArrayDesignator() ||
227 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000228 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000229 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000230 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000231 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000232 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000233
Chris Lattner79ed6b52008-10-26 23:22:23 +0000234 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000235 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000236}
237
238
Chris Lattner0eec2b52008-10-26 22:38:55 +0000239/// ParseBraceInitializer - Called when parsing an initializer that has a
240/// leading open brace.
241///
Reid Spencer5f016e22007-07-11 17:01:13 +0000242/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000243/// '{' initializer-list '}'
244/// '{' initializer-list ',' '}'
245/// [GNU] '{' '}'
246///
247/// initializer-list:
248/// designation[opt] initializer
249/// initializer-list ',' designation[opt] initializer
250///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000251Parser::OwningExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000253
Chris Lattnereccc53a2008-10-26 22:36:07 +0000254 /// InitExprs - This is the actual list of expressions contained in the
255 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000256 ExprVector InitExprs(Actions);
257
Chris Lattner220ad7c2008-10-26 23:35:51 +0000258 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000259 // Empty initializers are a C++ feature and a GNU extension to C.
260 if (!getLang().CPlusPlus)
261 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000262 // Match the '}'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000263 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
Douglas Gregor5908a922009-03-20 23:11:49 +0000264 ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000265 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000266
Steve Naroff4aa88f82007-07-19 01:06:55 +0000267 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000268
Steve Naroff4aa88f82007-07-19 01:06:55 +0000269 while (1) {
270 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000271
Steve Naroff4aa88f82007-07-19 01:06:55 +0000272 // If we know that this cannot be a designation, just parse the nested
273 // initializer directly.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000274 OwningExprResult SubElt(Actions);
Douglas Gregor5908a922009-03-20 23:11:49 +0000275 if (MayBeDesignationStart(Tok.getKind(), PP))
276 SubElt = ParseInitializerWithPotentialDesignator();
277 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000278 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Steve Naroff4aa88f82007-07-19 01:06:55 +0000280 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000281 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000282 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000283 } else {
284 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner65bb89c2008-04-20 19:07:56 +0000286 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000287 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000288 // parsing the rest of the initializer. This allows us to emit
289 // diagnostics for later elements that we find. If we don't see a comma,
290 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000291 // FIXME: This comment doesn't sound right. If there is a r_brace
292 // immediately, it can't be an error, since there is no other way of
293 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000294 if (Tok.isNot(tok::comma)) {
295 SkipUntil(tok::r_brace, false, true);
296 break;
297 }
298 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000299
Steve Naroff4aa88f82007-07-19 01:06:55 +0000300 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000301 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000302
Chris Lattnereccc53a2008-10-26 22:36:07 +0000303 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000304 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000305
Steve Naroff4aa88f82007-07-19 01:06:55 +0000306 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000307 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000308 }
Chris Lattner04d66662007-10-09 17:33:22 +0000309 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000310 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor5908a922009-03-20 23:11:49 +0000311 ConsumeBrace());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000312
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 // Match the '}'.
314 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000315 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000316}
317