blob: c4ecd26f1b898ccad38bcdeb35fec0dd804e58a2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 ']'
89 SourceLocation StartLoc = ConsumeBracket();
90
91 ExprResult Idx = ParseConstantExpression();
92 if (Idx.isInvalid) {
93 SkipUntil(tok::r_square);
94 return Idx;
95 }
96
97 // Handle the gnu array range extension.
Chris Lattner04d66662007-10-09 17:33:22 +000098 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 Diag(Tok, diag::ext_gnu_array_range);
100 ConsumeToken();
101
102 ExprResult RHS = ParseConstantExpression();
103 if (RHS.isInvalid) {
104 SkipUntil(tok::r_square);
105 return RHS;
106 }
107 }
108
109 MatchRHSPunctuation(tok::r_square, StartLoc);
110 break;
111 }
112 case tok::identifier: {
113 // Due to the GNU "designation: identifier ':'" extension, we don't know
114 // whether something starting with an identifier is an
115 // assignment-expression or if it is an old-style structure field
116 // designator.
117 // TODO: Check that this is the first designator.
Chris Lattnerd2177732007-07-20 16:59:19 +0000118 Token Ident = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 ConsumeToken();
120
121 // If this is the gross GNU extension, handle it now.
Chris Lattner04d66662007-10-09 17:33:22 +0000122 if (Tok.is(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 Diag(Ident, diag::ext_gnu_old_style_field_designator);
124 ConsumeToken();
125 return ParseInitializer();
126 }
127
128 // Otherwise, we just consumed the first token of an expression. Parse
129 // the rest of it now.
130 return ParseAssignmentExprWithLeadingIdentifier(Ident);
131 }
132 }
133 }
134}
135
136
137/// ParseInitializer
138/// initializer: [C99 6.7.8]
139/// assignment-expression
140/// '{' initializer-list '}'
141/// '{' initializer-list ',' '}'
142/// [GNU] '{' '}'
143///
144/// initializer-list:
145/// designation[opt] initializer
146/// initializer-list ',' designation[opt] initializer
147///
148Parser::ExprResult Parser::ParseInitializer() {
Chris Lattner04d66662007-10-09 17:33:22 +0000149 if (Tok.isNot(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 return ParseAssignmentExpression();
151
152 SourceLocation LBraceLoc = ConsumeBrace();
153
154 // We support empty initializers, but tell the user that they aren't using
155 // C99-clean code.
Chris Lattner04d66662007-10-09 17:33:22 +0000156 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000158 // Match the '}'.
Steve Narofff69936d2007-09-16 03:34:24 +0000159 return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
Steve Naroff4aa88f82007-07-19 01:06:55 +0000161 llvm::SmallVector<ExprTy*, 8> InitExprs;
162 bool InitExprsOk = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000163
Steve Naroff4aa88f82007-07-19 01:06:55 +0000164 while (1) {
165 // Parse: designation[opt] initializer
166
167 // If we know that this cannot be a designation, just parse the nested
168 // initializer directly.
169 ExprResult SubElt;
170 if (!MayBeDesignationStart(Tok.getKind()))
171 SubElt = ParseInitializer();
172 else
173 SubElt = ParseInitializerWithPotentialDesignator();
174
175 // If we couldn't parse the subelement, bail out.
176 if (SubElt.isInvalid) {
177 InitExprsOk = false;
Chris Lattnere61933d2007-10-25 17:27:01 +0000178 SkipUntil(tok::r_brace, false, true);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000179 break;
180 } else
181 InitExprs.push_back(SubElt.Val);
182
183 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000184 if (Tok.isNot(tok::comma)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000185
186 // FIXME: save comma locations.
187 ConsumeToken();
188
189 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000190 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000191 }
Chris Lattner04d66662007-10-09 17:33:22 +0000192 if (InitExprsOk && Tok.is(tok::r_brace))
Steve Narofff69936d2007-09-16 03:34:24 +0000193 return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
Steve Naroff4aa88f82007-07-19 01:06:55 +0000194 ConsumeBrace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 // Match the '}'.
196 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000197 return ExprResult(true); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199