blob: 84e3166473508ab8b39fa2ef48a9a67c2ba30a68 [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 Lattner4b009652007-07-25 00:24:17 +000024static bool MayBeDesignationStart(tok::TokenKind K) {
25 switch (K) {
26 default: return false;
27 case tok::period: // designator: '.' identifier
28 case tok::l_square: // designator: array-designator
29 case tok::identifier: // designation: identifier ':'
30 return true;
31 }
32}
33
34/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
35/// checking to see if the token stream starts with a designator.
36///
37/// designation:
38/// designator-list '='
39/// [GNU] array-designator
40/// [GNU] identifier ':'
41///
42/// designator-list:
43/// designator
44/// designator-list designator
45///
46/// designator:
47/// array-designator
48/// '.' identifier
49///
50/// array-designator:
51/// '[' constant-expression ']'
52/// [GNU] '[' constant-expression '...' constant-expression ']'
53///
54/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattnere29ac822008-10-26 21:46:13 +000055/// initializer (because it is an expression). We need to consider this case
56/// when parsing array designators.
Chris Lattner4b009652007-07-25 00:24:17 +000057///
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000058Parser::ExprResult Parser::
59ParseInitializerWithPotentialDesignator(InitListDesignations &Designations,
60 unsigned InitNum) {
61
62 // 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)) {
67 if (NextToken().is(tok::colon)) {
68 Diag(Tok, diag::ext_gnu_old_style_field_designator);
69
70 Designation &D = Designations.CreateDesignation(InitNum);
71 D.AddDesignator(Designator::getField(Tok.getIdentifierInfo()));
72 ConsumeToken(); // Eat the identifier.
73
74 assert(Tok.is(tok::colon) && "NextToken() not working properly!");
75 ConsumeToken();
76 return ParseInitializer();
77 }
78
79 // Otherwise, parse the assignment-expression.
80 return ParseAssignmentExpression();
81 }
82
83
Chris Lattner4b009652007-07-25 00:24:17 +000084 // Parse each designator in the designator list until we find an initializer.
85 while (1) {
86 switch (Tok.getKind()) {
87 case tok::equal:
88 // We read some number (at least one due to the grammar we implemented)
89 // of designators and found an '=' sign. The following tokens must be
90 // the initializer.
91 ConsumeToken();
92 return ParseInitializer();
93
94 default: {
95 // We read some number (at least one due to the grammar we implemented)
96 // of designators and found something that isn't an = or an initializer.
97 // If we have exactly one array designator [TODO CHECK], this is the GNU
98 // 'designation: array-designator' extension. Otherwise, it is a parse
99 // error.
100 SourceLocation Loc = Tok.getLocation();
101 ExprResult Init = ParseInitializer();
102 if (Init.isInvalid) return Init;
103
104 Diag(Tok, diag::ext_gnu_missing_equal_designator);
105 return Init;
106 }
107 case tok::period:
108 // designator: '.' identifier
109 ConsumeToken();
110 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident))
111 return ExprResult(true);
112 break;
113
114 case tok::l_square: {
115 // array-designator: '[' constant-expression ']'
116 // array-designator: '[' constant-expression '...' constant-expression ']'
Chris Lattner16c865e2008-01-25 19:37:24 +0000117 // When designation is empty, this can be '[' objc-message-expr ']'. Note
118 // that we also have the case of [4][foo bar], which is the gnu designator
119 // extension + objc message send.
Chris Lattner4b009652007-07-25 00:24:17 +0000120 SourceLocation StartLoc = ConsumeBracket();
121
Chris Lattner16c865e2008-01-25 19:37:24 +0000122 // If Objective-C is enabled and this is a typename or other identifier
123 // receiver, parse this as a message send expression.
124 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattnere69015d2008-01-25 19:43:26 +0000125 // FIXME: Emit ext_gnu_missing_equal_designator for inits like
126 // [4][foo bar].
Chris Lattner16c865e2008-01-25 19:37:24 +0000127 IdentifierInfo *Name = Tok.getIdentifierInfo();
128 ConsumeToken();
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000129 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, Name, 0);
Chris Lattner16c865e2008-01-25 19:37:24 +0000130 }
131
132 // Note that we parse this as an assignment expression, not a constant
Chris Lattnere69015d2008-01-25 19:43:26 +0000133 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
134 // to validate that the expression is a constant.
Chris Lattner16c865e2008-01-25 19:37:24 +0000135 ExprResult Idx = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000136 if (Idx.isInvalid) {
137 SkipUntil(tok::r_square);
138 return Idx;
139 }
140
Chris Lattnere69015d2008-01-25 19:43:26 +0000141 // Given an expression, we could either have a designator (if the next
142 // tokens are '...' or ']' or an objc message send. If this is an objc
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000143 // message send, handle it now. An objc-message send is the start of
144 // an assignment-expression production.
Chris Lattnere69015d2008-01-25 19:43:26 +0000145 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
146 Tok.isNot(tok::r_square)) {
147 // FIXME: Emit ext_gnu_missing_equal_designator for inits like
148 // [4][foo bar].
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000149 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 0,Idx.Val);
Chris Lattnere69015d2008-01-25 19:43:26 +0000150 }
151
Chris Lattner4b009652007-07-25 00:24:17 +0000152 // Handle the gnu array range extension.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000153 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000154 Diag(Tok, diag::ext_gnu_array_range);
155 ConsumeToken();
156
157 ExprResult RHS = ParseConstantExpression();
158 if (RHS.isInvalid) {
159 SkipUntil(tok::r_square);
160 return RHS;
161 }
162 }
163
164 MatchRHSPunctuation(tok::r_square, StartLoc);
165 break;
166 }
Chris Lattner4b009652007-07-25 00:24:17 +0000167 }
168 }
169}
170
171
Chris Lattner70e9a092008-10-26 22:38:55 +0000172/// ParseBraceInitializer - Called when parsing an initializer that has a
173/// leading open brace.
174///
Chris Lattner4b009652007-07-25 00:24:17 +0000175/// initializer: [C99 6.7.8]
Chris Lattner4b009652007-07-25 00:24:17 +0000176/// '{' initializer-list '}'
177/// '{' initializer-list ',' '}'
178/// [GNU] '{' '}'
179///
180/// initializer-list:
181/// designation[opt] initializer
182/// initializer-list ',' designation[opt] initializer
183///
Chris Lattner70e9a092008-10-26 22:38:55 +0000184Parser::ExprResult Parser::ParseBraceInitializer() {
Chris Lattner4b009652007-07-25 00:24:17 +0000185 SourceLocation LBraceLoc = ConsumeBrace();
186
187 // We support empty initializers, but tell the user that they aren't using
188 // C99-clean code.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000189 if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000190 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
191 // Match the '}'.
Steve Naroff87d58b42007-09-16 03:34:24 +0000192 return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace());
Chris Lattner4b009652007-07-25 00:24:17 +0000193 }
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000194
195 /// InitExprs - This is the actual list of expressions contained in the
196 /// initializer.
Chris Lattner4b009652007-07-25 00:24:17 +0000197 llvm::SmallVector<ExprTy*, 8> InitExprs;
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000198
199 /// ExprDesignators - For each initializer, keep track of the designator that
200 /// was specified for it, if any.
201 InitListDesignations InitExprDesignations(Actions);
202
Chris Lattner4b009652007-07-25 00:24:17 +0000203 bool InitExprsOk = true;
204
205 while (1) {
206 // Parse: designation[opt] initializer
207
208 // If we know that this cannot be a designation, just parse the nested
209 // initializer directly.
210 ExprResult SubElt;
211 if (!MayBeDesignationStart(Tok.getKind()))
212 SubElt = ParseInitializer();
213 else
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000214 SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
215 InitExprs.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000216
217 // If we couldn't parse the subelement, bail out.
Chris Lattnere77e03d2008-04-20 19:07:56 +0000218 if (!SubElt.isInvalid) {
Chris Lattner4b009652007-07-25 00:24:17 +0000219 InitExprs.push_back(SubElt.Val);
Chris Lattnere77e03d2008-04-20 19:07:56 +0000220 } else {
221 InitExprsOk = false;
222
223 // We have two ways to try to recover from this error: if the code looks
Chris Lattnere29ac822008-10-26 21:46:13 +0000224 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattnere77e03d2008-04-20 19:07:56 +0000225 // parsing the rest of the initializer. This allows us to emit
226 // diagnostics for later elements that we find. If we don't see a comma,
227 // assume there is a parse error, and just skip to recover.
228 if (Tok.isNot(tok::comma)) {
229 SkipUntil(tok::r_brace, false, true);
230 break;
231 }
232 }
Chris Lattner4b009652007-07-25 00:24:17 +0000233
234 // If we don't have a comma continued list, we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000235 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000236
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000237 // TODO: save comma locations if some client cares.
Chris Lattner4b009652007-07-25 00:24:17 +0000238 ConsumeToken();
239
240 // Handle trailing comma.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000241 if (Tok.is(tok::r_brace)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000242 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000243 if (InitExprsOk && Tok.is(tok::r_brace))
Steve Naroff87d58b42007-09-16 03:34:24 +0000244 return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000245 ConsumeBrace());
Chris Lattnere77e03d2008-04-20 19:07:56 +0000246
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000247 // On error, delete any parsed subexpressions.
Chris Lattnere77e03d2008-04-20 19:07:56 +0000248 for (unsigned i = 0, e = InitExprs.size(); i != e; ++i)
249 Actions.DeleteExpr(InitExprs[i]);
250
Chris Lattner4b009652007-07-25 00:24:17 +0000251 // Match the '}'.
252 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
253 return ExprResult(true); // an error occurred.
254}
255