blob: e2ba7661f9cdc4d847c250804402e6d5d3b6f13c [file] [log] [blame]
Chris Lattner8693a512006-08-13 21:54:02 +00001//===--- Initializer.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"
16using namespace llvm;
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
33/// designation:
34/// designator-list '='
35/// [GNU] array-designator
36/// [GNU] identifier ':'
37///
38/// designator-list:
39/// designator
40/// designator-list designator
41///
42/// designator:
43/// array-designator
44/// '.' identifier
45///
46/// array-designator:
47/// '[' constant-expression ']'
48/// [GNU] '[' constant-expression '...' constant-expression ']'
49///
50/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
51/// initializer. We need to consider this case when parsing array designators.
52///
53Parser::ExprResult Parser::ParseInitializerWithPotentialDesignator() {
54 // Parse each designator in the designator list until we find an initializer.
55 while (1) {
56 switch (Tok.getKind()) {
57 case tok::equal:
58 // We read some number (at least one due to the grammar we implemented)
59 // of designators and found an '=' sign. The following tokens must be
60 // the initializer.
61 ConsumeToken();
62 return ParseInitializer();
63
64 default: {
65 // We read some number (at least one due to the grammar we implemented)
66 // of designators and found something that isn't an = or an initializer.
67 // If we have exactly one array designator [TODO CHECK], this is the GNU
68 // 'designation: array-designator' extension. Otherwise, it is a parse
69 // error.
70 SourceLocation Loc = Tok.getLocation();
71 ExprResult Init = ParseInitializer();
72 if (Init.isInvalid) return Init;
73
74 Diag(Tok, diag::ext_gnu_missing_equal_designator);
75 return Init;
76 }
77 case tok::period:
78 // designator: '.' identifier
79 ConsumeToken();
80 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident))
81 return ExprResult(true);
82 break;
83
84 case tok::l_square: {
85 // array-designator: '[' constant-expression ']'
86 // array-designator: '[' constant-expression '...' constant-expression ']'
87 SourceLocation StartLoc = Tok.getLocation();
88 ConsumeBracket();
89
90 ExprResult Idx = ParseConstantExpression();
91 if (Idx.isInvalid) {
92 SkipUntil(tok::r_square);
93 return Idx;
94 }
95
96 // Handle the gnu array range extension.
97 if (Tok.getKind() == tok::ellipsis) {
98 Diag(Tok, diag::ext_gnu_array_range);
99 ConsumeToken();
100
101 ExprResult RHS = ParseConstantExpression();
102 if (RHS.isInvalid) {
103 SkipUntil(tok::r_square);
104 return RHS;
105 }
106 }
107
108 MatchRHSPunctuation(tok::r_square, StartLoc, "[",
109 diag::err_expected_rsquare);
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.
118 LexerToken Ident = Tok;
119 ConsumeToken();
120
121 // If this is the gross GNU extension, handle it now.
122 if (Tok.getKind() == tok::colon) {
123 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() {
149 if (Tok.getKind() != tok::l_brace)
150 return ParseAssignmentExpression();
151
152 SourceLocation LBraceLoc = Tok.getLocation();
153 ConsumeBrace();
154
155 // We support empty initializers, but tell the user that they aren't using
156 // C99-clean code.
157 if (Tok.getKind() == tok::r_brace)
158 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
159 else {
160 while (1) {
161 // Parse: designation[opt] initializer
162
163 // If we know that this cannot be a designation, just parse the nested
164 // initializer directly.
165 ExprResult SubElt;
166 if (!MayBeDesignationStart(Tok.getKind()))
167 SubElt = ParseInitializer();
168 else
169 SubElt = ParseInitializerWithPotentialDesignator();
170
171 // If we couldn't parse the subelement, bail out.
172 if (SubElt.isInvalid) {
173 SkipUntil(tok::r_brace);
174 return SubElt;
175 }
176
177 // If we don't have a comma continued list, we're done.
178 if (Tok.getKind() != tok::comma) break;
179 ConsumeToken();
180
181 // Handle trailing comma.
182 if (Tok.getKind() == tok::r_brace) break;
183 }
184 }
185
186 // Match the '}'.
187 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
188 diag::err_expected_rbrace);
189 return ExprResult(false);
190}
191