blob: 57751c9c3fb55be604cc08e629be11bb08add1fc [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
Chris Lattner8693a512006-08-13 21:54:02 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner8693a512006-08-13 21:54:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements initializer parsing as specified by C99 6.7.8.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerf3e58e22008-10-26 22:36:07 +000014#include "clang/Parse/Designator.h"
Chris Lattner8693a512006-08-13 21:54:02 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Steve Narofffbd09832007-07-19 01:06:55 +000017#include "llvm/ADT/SmallString.h"
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattner8693a512006-08-13 21:54:02 +000019using namespace clang;
20
21
22/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattner0c024602008-10-26 21:46:13 +000023/// designator. If we can tell it is impossible that it is a designator, return
Mike Stump11289f42009-09-09 15:08:12 +000024/// false.
Chris Lattnerdde65432008-10-26 22:41:58 +000025static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Chris Lattner8693a512006-08-13 21:54:02 +000026 switch (K) {
27 default: return false;
28 case tok::period: // designator: '.' identifier
29 case tok::l_square: // designator: array-designator
Chris Lattnerdde65432008-10-26 22:41:58 +000030 return true;
Chris Lattner8693a512006-08-13 21:54:02 +000031 case tok::identifier: // designation: identifier ':'
Chris Lattnerdde65432008-10-26 22:41:58 +000032 return PP.LookAhead(0).is(tok::colon);
Chris Lattner8693a512006-08-13 21:54:02 +000033 }
34}
35
Chris Lattnere7dab442006-08-13 21:54:51 +000036/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
37/// checking to see if the token stream starts with a designator.
38///
Chris Lattner8693a512006-08-13 21:54:02 +000039/// 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 Lattner0c024602008-10-26 21:46:13 +000057/// initializer (because it is an expression). We need to consider this case
58/// when parsing array designators.
Chris Lattner8693a512006-08-13 21:54:02 +000059///
Douglas Gregor85992cf2009-03-20 23:11:49 +000060Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redld65cea82008-12-11 22:51:44 +000061
Chris Lattnerf3e58e22008-10-26 22:36:07 +000062 // If this is the old-style GNU extension:
63 // designation ::= identifier ':'
64 // Handle it as a field designator. Otherwise, this must be the start of a
65 // normal expression.
66 if (Tok.is(tok::identifier)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +000067 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000068
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +000069 llvm::SmallString<256> NewSyntax;
Daniel Dunbar07d07852009-10-18 21:17:35 +000070 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +000071 << " = ";
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000072
Douglas Gregore4a0bb72009-01-22 00:58:24 +000073 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +000074
Chris Lattnere2b5e872008-10-26 22:49:49 +000075 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregore4a0bb72009-01-22 00:58:24 +000076 SourceLocation ColonLoc = ConsumeToken();
77
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000078 Diag(Tok, diag::ext_gnu_old_style_field_designator)
Douglas Gregora771f462010-03-31 17:46:05 +000079 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
80 NewSyntax.str());
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000081
Douglas Gregor85992cf2009-03-20 23:11:49 +000082 Designation D;
Douglas Gregore4a0bb72009-01-22 00:58:24 +000083 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump11289f42009-09-09 15:08:12 +000084 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregore4a0bb72009-01-22 00:58:24 +000085 ParseInitializer());
Chris Lattnerf3e58e22008-10-26 22:36:07 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner72432452008-10-26 22:59:19 +000088 // Desig - This is initialized when we see our first designator. We may have
89 // an objc message send with no designator, so we don't want to create this
90 // eagerly.
Douglas Gregor85992cf2009-03-20 23:11:49 +000091 Designation Desig;
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattner8693a512006-08-13 21:54:02 +000093 // Parse each designator in the designator list until we find an initializer.
Chris Lattnere2b5e872008-10-26 22:49:49 +000094 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
95 if (Tok.is(tok::period)) {
Chris Lattner8693a512006-08-13 21:54:02 +000096 // designator: '.' identifier
Douglas Gregore4a0bb72009-01-22 00:58:24 +000097 SourceLocation DotLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000098
Chris Lattner72432452008-10-26 22:59:19 +000099 if (Tok.isNot(tok::identifier)) {
100 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000101 return ExprError();
Chris Lattner72432452008-10-26 22:59:19 +0000102 }
Mike Stump11289f42009-09-09 15:08:12 +0000103
Douglas Gregor85992cf2009-03-20 23:11:49 +0000104 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
105 Tok.getLocation()));
Chris Lattner72432452008-10-26 22:59:19 +0000106 ConsumeToken(); // Eat the identifier.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000107 continue;
108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnere2b5e872008-10-26 22:49:49 +0000110 // We must have either an array designator now or an objc message send.
111 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +0000112
Chris Lattner8aafd352008-10-26 23:06:54 +0000113 // Handle the two forms of array designator:
114 // array-designator: '[' constant-expression ']'
115 // array-designator: '[' constant-expression '...' constant-expression ']'
116 //
117 // Also, we have to handle the case where the expression after the
118 // designator an an objc message send: '[' objc-message-expr ']'.
119 // Interesting cases are:
120 // [foo bar] -> objc message send
121 // [foo] -> array designator
122 // [foo ... bar] -> array designator
123 // [4][foo bar] -> obsolete GNU designation with objc message send.
124 //
Chris Lattnere2b5e872008-10-26 22:49:49 +0000125 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +0000126
Chris Lattnera36ec422010-04-11 08:28:14 +0000127 // If Objective-C is enabled and this is a typename (class message send) or
128 // 'super', parse this as a message send expression.
129 if (getLang().ObjC1 && Tok.is(tok::identifier)) {
130 IdentifierInfo *II = Tok.getIdentifierInfo();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000131
Chris Lattnera36ec422010-04-11 08:28:14 +0000132 if (II == Ident_super || Actions.getTypeName(*II, Tok.getLocation(),
133 CurScope)) {
134 // If we have exactly one array designator, this used the GNU
135 // 'designation: array-designator' extension, otherwise there should be no
136 // designators at all!
137 if (Desig.getNumDesignators() == 1 &&
138 (Desig.getDesignator(0).isArrayDesignator() ||
139 Desig.getDesignator(0).isArrayRangeDesignator()))
140 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
141 else if (Desig.getNumDesignators() > 0)
142 Diag(Tok, diag::err_expected_equal_designator);
143
144 SourceLocation NameLoc = ConsumeToken();
145 return ParseAssignmentExprWithObjCMessageExprStart(
146 StartLoc, NameLoc, II, ExprArg(Actions));
147 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000148 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000149
Chris Lattnere2b5e872008-10-26 22:49:49 +0000150 // Note that we parse this as an assignment expression, not a constant
151 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
152 // to validate that the expression is a constant.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000153 OwningExprResult Idx(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000154 if (Idx.isInvalid()) {
Chris Lattnere2b5e872008-10-26 22:49:49 +0000155 SkipUntil(tok::r_square);
Sebastian Redld65cea82008-12-11 22:51:44 +0000156 return move(Idx);
Chris Lattnere2b5e872008-10-26 22:49:49 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnere2b5e872008-10-26 22:49:49 +0000159 // Given an expression, we could either have a designator (if the next
160 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump11289f42009-09-09 15:08:12 +0000161 // message send, handle it now. An objc-message send is the start of
Chris Lattnere2b5e872008-10-26 22:49:49 +0000162 // an assignment-expression production.
Mike Stump11289f42009-09-09 15:08:12 +0000163 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattnere2b5e872008-10-26 22:49:49 +0000164 Tok.isNot(tok::r_square)) {
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattner9a53fdc2008-10-26 23:29:41 +0000166 // If we have exactly one array designator, this used the GNU
167 // 'designation: array-designator' extension, otherwise there should be no
168 // designators at all!
Mike Stump11289f42009-09-09 15:08:12 +0000169 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000170 (Desig.getDesignator(0).isArrayDesignator() ||
171 Desig.getDesignator(0).isArrayRangeDesignator()))
172 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
173 else if (Desig.getNumDesignators() > 0)
174 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000175
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000176 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
177 SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000178 0, move(Idx));
Chris Lattnere2b5e872008-10-26 22:49:49 +0000179 }
Chris Lattner8aafd352008-10-26 23:06:54 +0000180
Chris Lattner8aafd352008-10-26 23:06:54 +0000181 // If this is a normal array designator, remember it.
182 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor85992cf2009-03-20 23:11:49 +0000183 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattner8aafd352008-10-26 23:06:54 +0000184 } else {
185 // Handle the gnu array range extension.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000186 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000187 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000188
189 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000190 if (RHS.isInvalid()) {
Chris Lattner8693a512006-08-13 21:54:02 +0000191 SkipUntil(tok::r_square);
Sebastian Redld65cea82008-12-11 22:51:44 +0000192 return move(RHS);
Chris Lattner8693a512006-08-13 21:54:02 +0000193 }
Douglas Gregor85992cf2009-03-20 23:11:49 +0000194 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
195 RHS.release(),
196 StartLoc, EllipsisLoc));
Chris Lattner8693a512006-08-13 21:54:02 +0000197 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000198
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000199 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor85992cf2009-03-20 23:11:49 +0000200 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
Chris Lattner8693a512006-08-13 21:54:02 +0000201 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000202
Chris Lattner72432452008-10-26 22:59:19 +0000203 // Okay, we're done with the designator sequence. We know that there must be
204 // at least one designator, because the only case we can get into this method
205 // without a designator is when we have an objc message send. That case is
206 // handled and returned from above.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000207 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redld65cea82008-12-11 22:51:44 +0000208
Chris Lattner72432452008-10-26 22:59:19 +0000209 // Handle a normal designator sequence end, which is an equal.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000210 if (Tok.is(tok::equal)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000211 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor85992cf2009-03-20 23:11:49 +0000212 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000213 ParseInitializer());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000214 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000215
Chris Lattner72432452008-10-26 22:59:19 +0000216 // We read some number of designators and found something that isn't an = or
Chris Lattner46dcba62008-10-26 23:22:23 +0000217 // an initializer. If we have exactly one array designator, this
Chris Lattner72432452008-10-26 22:59:19 +0000218 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
219 // parse error.
Mike Stump11289f42009-09-09 15:08:12 +0000220 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000221 (Desig.getDesignator(0).isArrayDesignator() ||
222 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000223 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000224 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000225 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000226 true, ParseInitializer());
Chris Lattner46dcba62008-10-26 23:22:23 +0000227 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000228
Chris Lattner46dcba62008-10-26 23:22:23 +0000229 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000230 return ExprError();
Chris Lattner8693a512006-08-13 21:54:02 +0000231}
232
233
Chris Lattner3fa92392008-10-26 22:38:55 +0000234/// ParseBraceInitializer - Called when parsing an initializer that has a
235/// leading open brace.
236///
Chris Lattner8693a512006-08-13 21:54:02 +0000237/// initializer: [C99 6.7.8]
Chris Lattner8693a512006-08-13 21:54:02 +0000238/// '{' initializer-list '}'
239/// '{' initializer-list ',' '}'
240/// [GNU] '{' '}'
241///
242/// initializer-list:
243/// designation[opt] initializer
244/// initializer-list ',' designation[opt] initializer
245///
Sebastian Redld65cea82008-12-11 22:51:44 +0000246Parser::OwningExprResult Parser::ParseBraceInitializer() {
Chris Lattner04132372006-10-16 06:12:55 +0000247 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redl511ed552008-11-25 22:21:31 +0000248
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000249 /// InitExprs - This is the actual list of expressions contained in the
250 /// initializer.
Sebastian Redl511ed552008-11-25 22:21:31 +0000251 ExprVector InitExprs(Actions);
252
Chris Lattner248388e2008-10-26 23:35:51 +0000253 if (Tok.is(tok::r_brace)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000254 // Empty initializers are a C++ feature and a GNU extension to C.
255 if (!getLang().CPlusPlus)
256 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner248388e2008-10-26 23:35:51 +0000257 // Match the '}'.
Sebastian Redlb5d49352009-01-19 22:31:54 +0000258 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000259 ConsumeBrace());
Chris Lattner248388e2008-10-26 23:35:51 +0000260 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000261
Steve Narofffbd09832007-07-19 01:06:55 +0000262 bool InitExprsOk = true;
Sebastian Redld65cea82008-12-11 22:51:44 +0000263
Steve Narofffbd09832007-07-19 01:06:55 +0000264 while (1) {
265 // Parse: designation[opt] initializer
Sebastian Redld65cea82008-12-11 22:51:44 +0000266
Steve Narofffbd09832007-07-19 01:06:55 +0000267 // If we know that this cannot be a designation, just parse the nested
268 // initializer directly.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000269 OwningExprResult SubElt(Actions);
Douglas Gregor85992cf2009-03-20 23:11:49 +0000270 if (MayBeDesignationStart(Tok.getKind(), PP))
271 SubElt = ParseInitializerWithPotentialDesignator();
272 else
Steve Narofffbd09832007-07-19 01:06:55 +0000273 SubElt = ParseInitializer();
Mike Stump11289f42009-09-09 15:08:12 +0000274
Steve Narofffbd09832007-07-19 01:06:55 +0000275 // If we couldn't parse the subelement, bail out.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000276 if (!SubElt.isInvalid()) {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000277 InitExprs.push_back(SubElt.release());
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000278 } else {
279 InitExprsOk = false;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000281 // We have two ways to try to recover from this error: if the code looks
Chris Lattner0c024602008-10-26 21:46:13 +0000282 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000283 // parsing the rest of the initializer. This allows us to emit
284 // diagnostics for later elements that we find. If we don't see a comma,
285 // assume there is a parse error, and just skip to recover.
Sebastian Redl511ed552008-11-25 22:21:31 +0000286 // FIXME: This comment doesn't sound right. If there is a r_brace
287 // immediately, it can't be an error, since there is no other way of
288 // leaving this loop except through this if.
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000289 if (Tok.isNot(tok::comma)) {
290 SkipUntil(tok::r_brace, false, true);
291 break;
292 }
293 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000294
Steve Narofffbd09832007-07-19 01:06:55 +0000295 // If we don't have a comma continued list, we're done.
Chris Lattner76c72282007-10-09 17:33:22 +0000296 if (Tok.isNot(tok::comma)) break;
Sebastian Redld65cea82008-12-11 22:51:44 +0000297
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000298 // TODO: save comma locations if some client cares.
Steve Narofffbd09832007-07-19 01:06:55 +0000299 ConsumeToken();
Sebastian Redld65cea82008-12-11 22:51:44 +0000300
Steve Narofffbd09832007-07-19 01:06:55 +0000301 // Handle trailing comma.
Chris Lattner76c72282007-10-09 17:33:22 +0000302 if (Tok.is(tok::r_brace)) break;
Steve Narofffbd09832007-07-19 01:06:55 +0000303 }
Chris Lattner76c72282007-10-09 17:33:22 +0000304 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb5d49352009-01-19 22:31:54 +0000305 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000306 ConsumeBrace());
Sebastian Redld65cea82008-12-11 22:51:44 +0000307
Chris Lattner8693a512006-08-13 21:54:02 +0000308 // Match the '}'.
Chris Lattner04f80192006-08-15 04:55:54 +0000309 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redld65cea82008-12-11 22:51:44 +0000310 return ExprError(); // an error occurred.
Chris Lattner8693a512006-08-13 21:54:02 +0000311}
312