blob: c40fe88c217bc7b54c95ea95d8630cdb2267fec2 [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
14#include "clang/Parse/Parser.h"
15#include "clang/Basic/Diagnostic.h"
Steve Naroff4aa88f82007-07-19 01:06:55 +000016#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using 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 Lattnerda46f3b2008-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.
Reid Spencer5f016e22007-07-11 17:01:13 +000092 SourceLocation StartLoc = ConsumeBracket();
93
Chris Lattnerda46f3b2008-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 Lattner5c749422008-01-25 19:43:26 +000097 // FIXME: Emit ext_gnu_missing_equal_designator for inits like
98 // [4][foo bar].
Chris Lattnerda46f3b2008-01-25 19:37:24 +000099 IdentifierInfo *Name = Tok.getIdentifierInfo();
100 ConsumeToken();
101 ExprResult R = ParseObjCMessageExpressionBody(StartLoc, Name, 0);
102 return ParsePostfixExpressionSuffix(R);
103 }
104
105 // Note that we parse this as an assignment expression, not a constant
Chris Lattner5c749422008-01-25 19:43:26 +0000106 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
107 // to validate that the expression is a constant.
Chris Lattnerda46f3b2008-01-25 19:37:24 +0000108 ExprResult Idx = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 if (Idx.isInvalid) {
110 SkipUntil(tok::r_square);
111 return Idx;
112 }
113
Chris Lattner5c749422008-01-25 19:43:26 +0000114 // Given an expression, we could either have a designator (if the next
115 // tokens are '...' or ']' or an objc message send. If this is an objc
116 // message send, handle it now.
117 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].
121 ExprResult R = ParseObjCMessageExpressionBody(StartLoc, 0, Idx.Val);
122 return ParsePostfixExpressionSuffix(R);
123 }
124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 // Handle the gnu array range extension.
Chris Lattner04d66662007-10-09 17:33:22 +0000126 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 Diag(Tok, diag::ext_gnu_array_range);
128 ConsumeToken();
129
130 ExprResult RHS = ParseConstantExpression();
131 if (RHS.isInvalid) {
132 SkipUntil(tok::r_square);
133 return RHS;
134 }
135 }
136
137 MatchRHSPunctuation(tok::r_square, StartLoc);
138 break;
139 }
140 case tok::identifier: {
141 // Due to the GNU "designation: identifier ':'" extension, we don't know
142 // whether something starting with an identifier is an
143 // assignment-expression or if it is an old-style structure field
144 // designator.
145 // TODO: Check that this is the first designator.
Chris Lattnerd2177732007-07-20 16:59:19 +0000146 Token Ident = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ConsumeToken();
148
149 // If this is the gross GNU extension, handle it now.
Chris Lattner04d66662007-10-09 17:33:22 +0000150 if (Tok.is(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 Diag(Ident, diag::ext_gnu_old_style_field_designator);
152 ConsumeToken();
153 return ParseInitializer();
154 }
155
156 // Otherwise, we just consumed the first token of an expression. Parse
157 // the rest of it now.
158 return ParseAssignmentExprWithLeadingIdentifier(Ident);
159 }
160 }
161 }
162}
163
164
165/// ParseInitializer
166/// initializer: [C99 6.7.8]
167/// assignment-expression
168/// '{' initializer-list '}'
169/// '{' initializer-list ',' '}'
170/// [GNU] '{' '}'
171///
172/// initializer-list:
173/// designation[opt] initializer
174/// initializer-list ',' designation[opt] initializer
175///
176Parser::ExprResult Parser::ParseInitializer() {
Chris Lattner04d66662007-10-09 17:33:22 +0000177 if (Tok.isNot(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 return ParseAssignmentExpression();
179
180 SourceLocation LBraceLoc = ConsumeBrace();
181
182 // We support empty initializers, but tell the user that they aren't using
183 // C99-clean code.
Chris Lattner04d66662007-10-09 17:33:22 +0000184 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000186 // Match the '}'.
Steve Narofff69936d2007-09-16 03:34:24 +0000187 return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 }
Steve Naroff4aa88f82007-07-19 01:06:55 +0000189 llvm::SmallVector<ExprTy*, 8> InitExprs;
190 bool InitExprsOk = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000191
Steve Naroff4aa88f82007-07-19 01:06:55 +0000192 while (1) {
193 // Parse: designation[opt] initializer
194
195 // If we know that this cannot be a designation, just parse the nested
196 // initializer directly.
197 ExprResult SubElt;
198 if (!MayBeDesignationStart(Tok.getKind()))
199 SubElt = ParseInitializer();
200 else
201 SubElt = ParseInitializerWithPotentialDesignator();
202
203 // If we couldn't parse the subelement, bail out.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000204 if (!SubElt.isInvalid) {
Steve Naroff4aa88f82007-07-19 01:06:55 +0000205 InitExprs.push_back(SubElt.Val);
Chris Lattner65bb89c2008-04-20 19:07:56 +0000206 } else {
207 InitExprsOk = false;
208
209 // We have two ways to try to recover from this error: if the code looks
210 // gramatically ok (i.e. we have a comma comming up) try to continue
211 // parsing the rest of the initializer. This allows us to emit
212 // diagnostics for later elements that we find. If we don't see a comma,
213 // assume there is a parse error, and just skip to recover.
214 if (Tok.isNot(tok::comma)) {
215 SkipUntil(tok::r_brace, false, true);
216 break;
217 }
218 }
Steve Naroff4aa88f82007-07-19 01:06:55 +0000219
220 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000221 if (Tok.isNot(tok::comma)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000222
223 // FIXME: save comma locations.
224 ConsumeToken();
225
226 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000227 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000228 }
Chris Lattner04d66662007-10-09 17:33:22 +0000229 if (InitExprsOk && Tok.is(tok::r_brace))
Steve Narofff69936d2007-09-16 03:34:24 +0000230 return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
Steve Naroff4aa88f82007-07-19 01:06:55 +0000231 ConsumeBrace());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000232
233 // Delete any parsed subexpressions.
234 for (unsigned i = 0, e = InitExprs.size(); i != e; ++i)
235 Actions.DeleteExpr(InitExprs[i]);
236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Match the '}'.
238 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000239 return ExprResult(true); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000240}
241