blob: bbc2124e598630cd48f10ecc3b5282762f697994 [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"
Steve Naroff4aa88f82007-07-19 01:06:55 +000017#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
20
21/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattner838cb212008-10-26 21:46:13 +000022/// designator. If we can tell it is impossible that it is a designator, return
23/// false.
Chris Lattnerefcadc62008-10-26 22:41:58 +000024static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +000025 switch (K) {
26 default: return false;
27 case tok::period: // designator: '.' identifier
28 case tok::l_square: // designator: array-designator
Chris Lattnerefcadc62008-10-26 22:41:58 +000029 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +000030 case tok::identifier: // designation: identifier ':'
Chris Lattnerefcadc62008-10-26 22:41:58 +000031 return PP.LookAhead(0).is(tok::colon);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner838cb212008-10-26 21:46:13 +000056/// initializer (because it is an expression). We need to consider this case
57/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +000058///
Douglas Gregor5908a922009-03-20 23:11:49 +000059Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +000060
Chris Lattnereccc53a2008-10-26 22:36:07 +000061 // If this is the old-style GNU extension:
62 // designation ::= identifier ':'
63 // Handle it as a field designator. Otherwise, this must be the start of a
64 // normal expression.
65 if (Tok.is(tok::identifier)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +000066 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +000067
68 std::string NewSyntax(".");
69 NewSyntax += FieldName->getName();
70 NewSyntax += " = ";
71
Douglas Gregor05c13a32009-01-22 00:58:24 +000072 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
Douglas Gregoreeae8f02009-03-28 00:41:23 +000077 Diag(Tok, diag::ext_gnu_old_style_field_designator)
78 << CodeModificationHint::CreateReplacement(SourceRange(NameLoc,
79 ColonLoc),
80 NewSyntax);
81
Douglas Gregor5908a922009-03-20 23:11:49 +000082 Designation D;
Douglas Gregor05c13a32009-01-22 00:58:24 +000083 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
84 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
85 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +000086 }
87
Chris Lattner0a68b942008-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 Gregor5908a922009-03-20 23:11:49 +000091 Designation Desig;
Chris Lattner0a68b942008-10-26 22:59:19 +000092
Reid Spencer5f016e22007-07-11 17:01:13 +000093 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +000094 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
95 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000096 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +000097 SourceLocation DotLoc = ConsumeToken();
Chris Lattner0a68b942008-10-26 22:59:19 +000098
Chris Lattner0a68b942008-10-26 22:59:19 +000099 if (Tok.isNot(tok::identifier)) {
100 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000101 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000102 }
103
Douglas Gregor5908a922009-03-20 23:11:49 +0000104 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
105 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000106 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000107 continue;
108 }
109
110 // We must have either an array designator now or an objc message send.
111 assert(Tok.is(tok::l_square) && "Unexpected token!");
112
Chris Lattnere2329422008-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 Lattner7f9690d2008-10-26 22:49:49 +0000125 SourceLocation StartLoc = ConsumeBracket();
126
127 // If Objective-C is enabled and this is a typename or other identifier
128 // receiver, parse this as a message send expression.
129 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattner0fc73f72008-10-26 23:29:41 +0000130 // If we have exactly one array designator, this used the GNU
131 // 'designation: array-designator' extension, otherwise there should be no
132 // designators at all!
Douglas Gregor5908a922009-03-20 23:11:49 +0000133 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 if (Desig.getNumDesignators() > 0)
138 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl1d922962008-12-13 15:32:12 +0000139
Chris Lattner7f9690d2008-10-26 22:49:49 +0000140 IdentifierInfo *Name = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000141 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000142 return ParseAssignmentExprWithObjCMessageExprStart(
143 StartLoc, NameLoc, Name, ExprArg(Actions));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000144 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000145
Chris Lattner7f9690d2008-10-26 22:49:49 +0000146 // Note that we parse this as an assignment expression, not a constant
147 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
148 // to validate that the expression is a constant.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000149 OwningExprResult Idx(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000150 if (Idx.isInvalid()) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000151 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000152 return move(Idx);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000153 }
154
155 // Given an expression, we could either have a designator (if the next
156 // tokens are '...' or ']' or an objc message send. If this is an objc
157 // message send, handle it now. An objc-message send is the start of
158 // an assignment-expression production.
159 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
160 Tok.isNot(tok::r_square)) {
Chris Lattner0fc73f72008-10-26 23:29:41 +0000161
162 // If we have exactly one array designator, this used the GNU
163 // 'designation: array-designator' extension, otherwise there should be no
164 // designators at all!
Douglas Gregor5908a922009-03-20 23:11:49 +0000165 if (Desig.getNumDesignators() == 1 &&
166 (Desig.getDesignator(0).isArrayDesignator() ||
167 Desig.getDesignator(0).isArrayRangeDesignator()))
168 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
169 else if (Desig.getNumDesignators() > 0)
170 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000171
Sebastian Redl1d922962008-12-13 15:32:12 +0000172 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
173 SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000174 0, move(Idx));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000175 }
Chris Lattnere2329422008-10-26 23:06:54 +0000176
Chris Lattnere2329422008-10-26 23:06:54 +0000177 // If this is a normal array designator, remember it.
178 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000179 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000180 } else {
181 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000182 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000183 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000184
185 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000186 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000188 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000190 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
191 RHS.release(),
192 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000194
Douglas Gregor05c13a32009-01-22 00:58:24 +0000195 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor5908a922009-03-20 23:11:49 +0000196 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
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.
Douglas Gregor5908a922009-03-20 23:11:49 +0000203 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000204
Chris Lattner0a68b942008-10-26 22:59:19 +0000205 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000206 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000207 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000208 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000209 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000210 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000211
Chris Lattner0a68b942008-10-26 22:59:19 +0000212 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000213 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000214 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
215 // parse error.
Douglas Gregor5908a922009-03-20 23:11:49 +0000216 if (Desig.getNumDesignators() == 1 &&
217 (Desig.getDesignator(0).isArrayDesignator() ||
218 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000219 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor558cb562009-04-02 01:08:08 +0000220 << CodeModificationHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000221 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000222 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000223 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000224
Chris Lattner79ed6b52008-10-26 23:22:23 +0000225 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000226 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000227}
228
229
Chris Lattner0eec2b52008-10-26 22:38:55 +0000230/// ParseBraceInitializer - Called when parsing an initializer that has a
231/// leading open brace.
232///
Reid Spencer5f016e22007-07-11 17:01:13 +0000233/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000234/// '{' initializer-list '}'
235/// '{' initializer-list ',' '}'
236/// [GNU] '{' '}'
237///
238/// initializer-list:
239/// designation[opt] initializer
240/// initializer-list ',' designation[opt] initializer
241///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000242Parser::OwningExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000244
Chris Lattnereccc53a2008-10-26 22:36:07 +0000245 /// InitExprs - This is the actual list of expressions contained in the
246 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000247 ExprVector InitExprs(Actions);
248
Chris Lattner220ad7c2008-10-26 23:35:51 +0000249 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000250 // Empty initializers are a C++ feature and a GNU extension to C.
251 if (!getLang().CPlusPlus)
252 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000253 // Match the '}'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000254 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
Douglas Gregor5908a922009-03-20 23:11:49 +0000255 ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000256 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000257
Steve Naroff4aa88f82007-07-19 01:06:55 +0000258 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000259
Steve Naroff4aa88f82007-07-19 01:06:55 +0000260 while (1) {
261 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000262
Steve Naroff4aa88f82007-07-19 01:06:55 +0000263 // If we know that this cannot be a designation, just parse the nested
264 // initializer directly.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000265 OwningExprResult SubElt(Actions);
Douglas Gregor5908a922009-03-20 23:11:49 +0000266 if (MayBeDesignationStart(Tok.getKind(), PP))
267 SubElt = ParseInitializerWithPotentialDesignator();
268 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000269 SubElt = ParseInitializer();
Chris Lattnere2f56192008-11-03 09:28:22 +0000270
Steve Naroff4aa88f82007-07-19 01:06:55 +0000271 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000272 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000273 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000274 } else {
275 InitExprsOk = false;
276
277 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000278 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000279 // parsing the rest of the initializer. This allows us to emit
280 // diagnostics for later elements that we find. If we don't see a comma,
281 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000282 // FIXME: This comment doesn't sound right. If there is a r_brace
283 // immediately, it can't be an error, since there is no other way of
284 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000285 if (Tok.isNot(tok::comma)) {
286 SkipUntil(tok::r_brace, false, true);
287 break;
288 }
289 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000290
Steve Naroff4aa88f82007-07-19 01:06:55 +0000291 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000292 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000293
Chris Lattnereccc53a2008-10-26 22:36:07 +0000294 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000295 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000296
Steve Naroff4aa88f82007-07-19 01:06:55 +0000297 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000298 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000299 }
Chris Lattner04d66662007-10-09 17:33:22 +0000300 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000301 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor5908a922009-03-20 23:11:49 +0000302 ConsumeBrace());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000303
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // Match the '}'.
305 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000306 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000307}
308