blob: c7c08dccbcba78b2116a74a4e7c10ed92860e818 [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
14#include "clang/Parse/Parser.h"
15#include "clang/Basic/Diagnostic.h"
16#include "llvm/ADT/SmallString.h"
17using namespace clang;
18
19
20/// MayBeDesignationStart - Return true if this token might be the start of a
21/// designator.
22static bool MayBeDesignationStart(tok::TokenKind K) {
23 switch (K) {
24 default: return false;
25 case tok::period: // designator: '.' identifier
26 case tok::l_square: // designator: array-designator
27 case tok::identifier: // designation: identifier ':'
28 return true;
29 }
30}
31
32/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
33/// checking to see if the token stream starts with a designator.
34///
35/// designation:
36/// designator-list '='
37/// [GNU] array-designator
38/// [GNU] identifier ':'
39///
40/// designator-list:
41/// designator
42/// designator-list designator
43///
44/// designator:
45/// array-designator
46/// '.' identifier
47///
48/// array-designator:
49/// '[' constant-expression ']'
50/// [GNU] '[' constant-expression '...' constant-expression ']'
51///
52/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
53/// initializer. We need to consider this case when parsing array designators.
54///
55Parser::ExprResult Parser::ParseInitializerWithPotentialDesignator() {
56 // Parse each designator in the designator list until we find an initializer.
57 while (1) {
58 switch (Tok.getKind()) {
59 case tok::equal:
60 // We read some number (at least one due to the grammar we implemented)
61 // of designators and found an '=' sign. The following tokens must be
62 // the initializer.
63 ConsumeToken();
64 return ParseInitializer();
65
66 default: {
67 // We read some number (at least one due to the grammar we implemented)
68 // of designators and found something that isn't an = or an initializer.
69 // If we have exactly one array designator [TODO CHECK], this is the GNU
70 // 'designation: array-designator' extension. Otherwise, it is a parse
71 // error.
72 SourceLocation Loc = Tok.getLocation();
73 ExprResult Init = ParseInitializer();
74 if (Init.isInvalid) return Init;
75
76 Diag(Tok, diag::ext_gnu_missing_equal_designator);
77 return Init;
78 }
79 case tok::period:
80 // designator: '.' identifier
81 ConsumeToken();
82 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident))
83 return ExprResult(true);
84 break;
85
86 case tok::l_square: {
87 // array-designator: '[' constant-expression ']'
88 // array-designator: '[' constant-expression '...' constant-expression ']'
Chris Lattner16c865e2008-01-25 19:37:24 +000089 // When designation is empty, this can be '[' objc-message-expr ']'. Note
90 // that we also have the case of [4][foo bar], which is the gnu designator
91 // extension + objc message send.
Chris Lattner4b009652007-07-25 00:24:17 +000092 SourceLocation StartLoc = ConsumeBracket();
93
Chris Lattner16c865e2008-01-25 19:37:24 +000094 // If Objective-C is enabled and this is a typename or other identifier
95 // receiver, parse this as a message send expression.
96 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattnere69015d2008-01-25 19:43:26 +000097 // FIXME: Emit ext_gnu_missing_equal_designator for inits like
98 // [4][foo bar].
Chris Lattner16c865e2008-01-25 19:37:24 +000099 IdentifierInfo *Name = Tok.getIdentifierInfo();
100 ConsumeToken();
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000101 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, Name, 0);
Chris Lattner16c865e2008-01-25 19:37:24 +0000102 }
103
104 // Note that we parse this as an assignment expression, not a constant
Chris Lattnere69015d2008-01-25 19:43:26 +0000105 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
106 // to validate that the expression is a constant.
Chris Lattner16c865e2008-01-25 19:37:24 +0000107 ExprResult Idx = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000108 if (Idx.isInvalid) {
109 SkipUntil(tok::r_square);
110 return Idx;
111 }
112
Chris Lattnere69015d2008-01-25 19:43:26 +0000113 // Given an expression, we could either have a designator (if the next
114 // tokens are '...' or ']' or an objc message send. If this is an objc
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000115 // message send, handle it now. An objc-message send is the start of
116 // an assignment-expression production.
Chris Lattnere69015d2008-01-25 19:43:26 +0000117 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
118 Tok.isNot(tok::r_square)) {
119 // FIXME: Emit ext_gnu_missing_equal_designator for inits like
120 // [4][foo bar].
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000121 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 0,Idx.Val);
Chris Lattnere69015d2008-01-25 19:43:26 +0000122 }
123
Chris Lattner4b009652007-07-25 00:24:17 +0000124 // Handle the gnu array range extension.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000125 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000126 Diag(Tok, diag::ext_gnu_array_range);
127 ConsumeToken();
128
129 ExprResult RHS = ParseConstantExpression();
130 if (RHS.isInvalid) {
131 SkipUntil(tok::r_square);
132 return RHS;
133 }
134 }
135
136 MatchRHSPunctuation(tok::r_square, StartLoc);
137 break;
138 }
139 case tok::identifier: {
140 // Due to the GNU "designation: identifier ':'" extension, we don't know
141 // whether something starting with an identifier is an
142 // assignment-expression or if it is an old-style structure field
143 // designator.
144 // TODO: Check that this is the first designator.
Chris Lattner4b009652007-07-25 00:24:17 +0000145
146 // If this is the gross GNU extension, handle it now.
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000147 if (NextToken().is(tok::colon)) {
148 Diag(Tok, diag::ext_gnu_old_style_field_designator);
149 ConsumeToken(); // The identifier.
150 assert(Tok.is(tok::colon) && "NextToken() not working properly!");
Chris Lattner4b009652007-07-25 00:24:17 +0000151 ConsumeToken();
152 return ParseInitializer();
153 }
154
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000155 // Otherwise, parse the assignment-expression.
156 return ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000157 }
158 }
159 }
160}
161
162
163/// ParseInitializer
164/// initializer: [C99 6.7.8]
165/// assignment-expression
166/// '{' initializer-list '}'
167/// '{' initializer-list ',' '}'
168/// [GNU] '{' '}'
169///
170/// initializer-list:
171/// designation[opt] initializer
172/// initializer-list ',' designation[opt] initializer
173///
174Parser::ExprResult Parser::ParseInitializer() {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000175 if (Tok.isNot(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000176 return ParseAssignmentExpression();
177
178 SourceLocation LBraceLoc = ConsumeBrace();
179
180 // We support empty initializers, but tell the user that they aren't using
181 // C99-clean code.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000182 if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000183 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
184 // Match the '}'.
Steve Naroff87d58b42007-09-16 03:34:24 +0000185 return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace());
Chris Lattner4b009652007-07-25 00:24:17 +0000186 }
187 llvm::SmallVector<ExprTy*, 8> InitExprs;
188 bool InitExprsOk = true;
189
190 while (1) {
191 // Parse: designation[opt] initializer
192
193 // If we know that this cannot be a designation, just parse the nested
194 // initializer directly.
195 ExprResult SubElt;
196 if (!MayBeDesignationStart(Tok.getKind()))
197 SubElt = ParseInitializer();
198 else
199 SubElt = ParseInitializerWithPotentialDesignator();
200
201 // If we couldn't parse the subelement, bail out.
Chris Lattnere77e03d2008-04-20 19:07:56 +0000202 if (!SubElt.isInvalid) {
Chris Lattner4b009652007-07-25 00:24:17 +0000203 InitExprs.push_back(SubElt.Val);
Chris Lattnere77e03d2008-04-20 19:07:56 +0000204 } else {
205 InitExprsOk = false;
206
207 // We have two ways to try to recover from this error: if the code looks
208 // gramatically ok (i.e. we have a comma comming up) try to continue
209 // parsing the rest of the initializer. This allows us to emit
210 // diagnostics for later elements that we find. If we don't see a comma,
211 // assume there is a parse error, and just skip to recover.
212 if (Tok.isNot(tok::comma)) {
213 SkipUntil(tok::r_brace, false, true);
214 break;
215 }
216 }
Chris Lattner4b009652007-07-25 00:24:17 +0000217
218 // If we don't have a comma continued list, we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000219 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000220
221 // FIXME: save comma locations.
222 ConsumeToken();
223
224 // Handle trailing comma.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000225 if (Tok.is(tok::r_brace)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000226 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000227 if (InitExprsOk && Tok.is(tok::r_brace))
Steve Naroff87d58b42007-09-16 03:34:24 +0000228 return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000229 ConsumeBrace());
Chris Lattnere77e03d2008-04-20 19:07:56 +0000230
231 // Delete any parsed subexpressions.
232 for (unsigned i = 0, e = InitExprs.size(); i != e; ++i)
233 Actions.DeleteExpr(InitExprs[i]);
234
Chris Lattner4b009652007-07-25 00:24:17 +0000235 // Match the '}'.
236 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
237 return ExprResult(true); // an error occurred.
238}
239