blob: 4c49a6eca336efa06639365f94233a47cf8fcf04 [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 Lattner23c12ef2008-10-26 22:41:58 +000024static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +000025 switch (K) {
26 default: return false;
27 case tok::period: // designator: '.' identifier
28 case tok::l_square: // designator: array-designator
Chris Lattner23c12ef2008-10-26 22:41:58 +000029 return true;
Chris Lattner4b009652007-07-25 00:24:17 +000030 case tok::identifier: // designation: identifier ':'
Chris Lattner23c12ef2008-10-26 22:41:58 +000031 return PP.LookAhead(0).is(tok::colon);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnere29ac822008-10-26 21:46:13 +000056/// initializer (because it is an expression). We need to consider this case
57/// when parsing array designators.
Chris Lattner4b009652007-07-25 00:24:17 +000058///
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000059Parser::ExprResult Parser::
60ParseInitializerWithPotentialDesignator(InitListDesignations &Designations,
61 unsigned InitNum) {
62
63 // If this is the old-style GNU extension:
64 // designation ::= identifier ':'
65 // Handle it as a field designator. Otherwise, this must be the start of a
66 // normal expression.
67 if (Tok.is(tok::identifier)) {
Chris Lattner23c12ef2008-10-26 22:41:58 +000068 Diag(Tok, diag::ext_gnu_old_style_field_designator);
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000069
Chris Lattner23c12ef2008-10-26 22:41:58 +000070 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();
Chris Lattnerbc9e95d2008-10-26 22:36:07 +000077 }
78
Chris Lattner4b009652007-07-25 00:24:17 +000079 // Parse each designator in the designator list until we find an initializer.
80 while (1) {
81 switch (Tok.getKind()) {
82 case tok::equal:
83 // We read some number (at least one due to the grammar we implemented)
84 // of designators and found an '=' sign. The following tokens must be
85 // the initializer.
86 ConsumeToken();
87 return ParseInitializer();
88
89 default: {
90 // We read some number (at least one due to the grammar we implemented)
91 // of designators and found something that isn't an = or an initializer.
92 // If we have exactly one array designator [TODO CHECK], this is the GNU
93 // 'designation: array-designator' extension. Otherwise, it is a parse
94 // error.
95 SourceLocation Loc = Tok.getLocation();
96 ExprResult Init = ParseInitializer();
97 if (Init.isInvalid) return Init;
98
99 Diag(Tok, diag::ext_gnu_missing_equal_designator);
100 return Init;
101 }
102 case tok::period:
103 // designator: '.' identifier
104 ConsumeToken();
105 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident))
106 return ExprResult(true);
107 break;
108
109 case tok::l_square: {
110 // array-designator: '[' constant-expression ']'
111 // array-designator: '[' constant-expression '...' constant-expression ']'
Chris Lattner16c865e2008-01-25 19:37:24 +0000112 // When designation is empty, this can be '[' objc-message-expr ']'. Note
113 // that we also have the case of [4][foo bar], which is the gnu designator
114 // extension + objc message send.
Chris Lattner4b009652007-07-25 00:24:17 +0000115 SourceLocation StartLoc = ConsumeBracket();
116
Chris Lattner16c865e2008-01-25 19:37:24 +0000117 // If Objective-C is enabled and this is a typename or other identifier
118 // receiver, parse this as a message send expression.
119 if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
Chris Lattnere69015d2008-01-25 19:43:26 +0000120 // FIXME: Emit ext_gnu_missing_equal_designator for inits like
121 // [4][foo bar].
Chris Lattner16c865e2008-01-25 19:37:24 +0000122 IdentifierInfo *Name = Tok.getIdentifierInfo();
123 ConsumeToken();
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000124 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, Name, 0);
Chris Lattner16c865e2008-01-25 19:37:24 +0000125 }
126
127 // Note that we parse this as an assignment expression, not a constant
Chris Lattnere69015d2008-01-25 19:43:26 +0000128 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
129 // to validate that the expression is a constant.
Chris Lattner16c865e2008-01-25 19:37:24 +0000130 ExprResult Idx = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000131 if (Idx.isInvalid) {
132 SkipUntil(tok::r_square);
133 return Idx;
134 }
135
Chris Lattnere69015d2008-01-25 19:43:26 +0000136 // Given an expression, we could either have a designator (if the next
137 // tokens are '...' or ']' or an objc message send. If this is an objc
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000138 // message send, handle it now. An objc-message send is the start of
139 // an assignment-expression production.
Chris Lattnere69015d2008-01-25 19:43:26 +0000140 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
141 Tok.isNot(tok::r_square)) {
142 // FIXME: Emit ext_gnu_missing_equal_designator for inits like
143 // [4][foo bar].
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000144 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 0,Idx.Val);
Chris Lattnere69015d2008-01-25 19:43:26 +0000145 }
146
Chris Lattner4b009652007-07-25 00:24:17 +0000147 // Handle the gnu array range extension.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000148 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000149 Diag(Tok, diag::ext_gnu_array_range);
150 ConsumeToken();
151
152 ExprResult RHS = ParseConstantExpression();
153 if (RHS.isInvalid) {
154 SkipUntil(tok::r_square);
155 return RHS;
156 }
157 }
158
159 MatchRHSPunctuation(tok::r_square, StartLoc);
160 break;
161 }
Chris Lattner4b009652007-07-25 00:24:17 +0000162 }
163 }
164}
165
166
Chris Lattner70e9a092008-10-26 22:38:55 +0000167/// ParseBraceInitializer - Called when parsing an initializer that has a
168/// leading open brace.
169///
Chris Lattner4b009652007-07-25 00:24:17 +0000170/// initializer: [C99 6.7.8]
Chris Lattner4b009652007-07-25 00:24:17 +0000171/// '{' initializer-list '}'
172/// '{' initializer-list ',' '}'
173/// [GNU] '{' '}'
174///
175/// initializer-list:
176/// designation[opt] initializer
177/// initializer-list ',' designation[opt] initializer
178///
Chris Lattner70e9a092008-10-26 22:38:55 +0000179Parser::ExprResult Parser::ParseBraceInitializer() {
Chris Lattner4b009652007-07-25 00:24:17 +0000180 SourceLocation LBraceLoc = ConsumeBrace();
181
182 // We support empty initializers, but tell the user that they aren't using
183 // C99-clean code.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000184 if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000185 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
186 // Match the '}'.
Steve Naroff87d58b42007-09-16 03:34:24 +0000187 return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace());
Chris Lattner4b009652007-07-25 00:24:17 +0000188 }
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000189
190 /// InitExprs - This is the actual list of expressions contained in the
191 /// initializer.
Chris Lattner4b009652007-07-25 00:24:17 +0000192 llvm::SmallVector<ExprTy*, 8> InitExprs;
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000193
194 /// ExprDesignators - For each initializer, keep track of the designator that
195 /// was specified for it, if any.
196 InitListDesignations InitExprDesignations(Actions);
197
Chris Lattner4b009652007-07-25 00:24:17 +0000198 bool InitExprsOk = true;
199
200 while (1) {
201 // Parse: designation[opt] initializer
202
203 // If we know that this cannot be a designation, just parse the nested
204 // initializer directly.
205 ExprResult SubElt;
Chris Lattner23c12ef2008-10-26 22:41:58 +0000206 if (!MayBeDesignationStart(Tok.getKind(), PP))
Chris Lattner4b009652007-07-25 00:24:17 +0000207 SubElt = ParseInitializer();
208 else
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000209 SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
210 InitExprs.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000211
212 // If we couldn't parse the subelement, bail out.
Chris Lattnere77e03d2008-04-20 19:07:56 +0000213 if (!SubElt.isInvalid) {
Chris Lattner4b009652007-07-25 00:24:17 +0000214 InitExprs.push_back(SubElt.Val);
Chris Lattnere77e03d2008-04-20 19:07:56 +0000215 } else {
216 InitExprsOk = false;
217
218 // We have two ways to try to recover from this error: if the code looks
Chris Lattnere29ac822008-10-26 21:46:13 +0000219 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattnere77e03d2008-04-20 19:07:56 +0000220 // parsing the rest of the initializer. This allows us to emit
221 // diagnostics for later elements that we find. If we don't see a comma,
222 // assume there is a parse error, and just skip to recover.
223 if (Tok.isNot(tok::comma)) {
224 SkipUntil(tok::r_brace, false, true);
225 break;
226 }
227 }
Chris Lattner4b009652007-07-25 00:24:17 +0000228
229 // If we don't have a comma continued list, we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000230 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000231
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000232 // TODO: save comma locations if some client cares.
Chris Lattner4b009652007-07-25 00:24:17 +0000233 ConsumeToken();
234
235 // Handle trailing comma.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000236 if (Tok.is(tok::r_brace)) break;
Chris Lattner4b009652007-07-25 00:24:17 +0000237 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000238 if (InitExprsOk && Tok.is(tok::r_brace))
Steve Naroff87d58b42007-09-16 03:34:24 +0000239 return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000240 ConsumeBrace());
Chris Lattnere77e03d2008-04-20 19:07:56 +0000241
Chris Lattnerbc9e95d2008-10-26 22:36:07 +0000242 // On error, delete any parsed subexpressions.
Chris Lattnere77e03d2008-04-20 19:07:56 +0000243 for (unsigned i = 0, e = InitExprs.size(); i != e; ++i)
244 Actions.DeleteExpr(InitExprs[i]);
245
Chris Lattner4b009652007-07-25 00:24:17 +0000246 // Match the '}'.
247 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
248 return ExprResult(true); // an error occurred.
249}
250