blob: cd9ac2f484a8281a3825fb024ff7c945f169cd32 [file] [log] [blame]
Chris Lattnereb8a28f2006-08-10 18:43:39 +00001//===--- Declaration.cpp - Declaration Parsing ----------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
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 the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattnerb9093cd2006-08-04 04:39:53 +000015#include "clang/Parse/Declarations.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000016using namespace llvm;
17using namespace clang;
18
19//===----------------------------------------------------------------------===//
20// C99 6.7: Declarations.
21//===----------------------------------------------------------------------===//
22
Chris Lattnerf5fbd792006-08-10 23:56:11 +000023/// ParseTypeName
24/// type-name: [C99 6.7.6]
25/// specifier-qualifier-list abstract-declarator[opt]
26void Parser::ParseTypeName() {
27 // Parse the common declaration-specifiers piece.
28 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000029 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030
31 // Parse the abstract-declarator, if present.
32 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
33 ParseDeclarator(DeclaratorInfo);
34}
35
36
Chris Lattner53361ac2006-08-10 05:19:57 +000037/// ParseDeclaration - Parse a full 'declaration', which consists of
38/// declaration-specifiers, some number of declarators, and a semicolon.
39/// 'Context' should be a Declarator::TheContext value.
40void Parser::ParseDeclaration(unsigned Context) {
41 // Parse the common declaration-specifiers piece.
42 DeclSpec DS;
43 ParseDeclarationSpecifiers(DS);
44
Chris Lattner0e894622006-08-13 19:58:17 +000045 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
46 // declaration-specifiers init-declarator-list[opt] ';'
47 if (Tok.getKind() == tok::semi) {
48 // TODO: emit error on 'int;' or 'const enum foo;'.
49 // if (!DS.isMissingDeclaratorOk()) Diag(...);
50
51 ConsumeToken();
52 return;
53 }
54
Chris Lattner53361ac2006-08-10 05:19:57 +000055 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
56 ParseDeclarator(DeclaratorInfo);
57
58 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
59}
60
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000061/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
62/// parsing 'declaration-specifiers declarator'. This method is split out this
63/// way to handle the ambiguity between top-level function-definitions and
64/// declarations.
65///
66/// declaration: [C99 6.7]
67/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
68/// [!C99] init-declarator-list ';' [TODO]
69/// [OMP] threadprivate-directive [TODO]
70///
71/// init-declarator-list: [C99 6.7]
72/// init-declarator
73/// init-declarator-list ',' init-declarator
74/// init-declarator: [C99 6.7]
75/// declarator
76/// declarator '=' initializer
77///
Chris Lattner53361ac2006-08-10 05:19:57 +000078void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
79 // At this point, we know that it is not a function definition. Parse the
80 // rest of the init-declarator-list.
81 while (1) {
82 // must be: decl-spec[opt] declarator init-declarator-list
83 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000084 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +000085 if (Tok.getKind() == tok::equal) {
86 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000087 Init = ParseInitializer();
88 if (!Init.isInvalid) {
89 SkipUntil(tok::semi);
90 return;
91 }
Chris Lattner53361ac2006-08-10 05:19:57 +000092 }
93
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000094 // Inform the current actions module that we just parsed a declarator.
95 Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +000096
97 // If we don't have a comma, it is either the end of the list (a ';') or an
98 // error, bail out.
99 if (Tok.getKind() != tok::comma)
100 break;
101
102 // Consume the comma.
103 ConsumeToken();
104
105 // Parse the next declarator.
106 D.clear();
107 ParseDeclarator(D);
108 }
109
110 if (Tok.getKind() == tok::semi) {
111 ConsumeToken();
112 } else {
113 Diag(Tok, diag::err_parse_error);
114 // Skip to end of block or statement
115 SkipUntil(tok::r_brace, true);
116 if (Tok.getKind() == tok::semi)
117 ConsumeToken();
118 }
119}
120
Chris Lattner1890ac82006-08-13 01:16:23 +0000121/// ParseSpecifierQualifierList
122/// specifier-qualifier-list:
123/// type-specifier specifier-qualifier-list[opt]
124/// type-qualifier specifier-qualifier-list[opt]
125///
126void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
127 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
128 /// parse declaration-specifiers and complain about extra stuff.
129 SourceLocation Loc = Tok.getLocation();
130 ParseDeclarationSpecifiers(DS);
131
132 // Validate declspec for type-name.
133 unsigned Specs = DS.getParsedSpecifiers();
134 if (Specs == DeclSpec::PQ_None)
135 Diag(Tok, diag::err_typename_requires_specqual);
136
137 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
138 Diag(Loc, diag::err_typename_invalid_storageclass);
139 // Remove storage class.
140 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
141 DS.SCS_thread_specified = false;
142 }
143 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
144 Diag(Loc, diag::err_typename_invalid_functionspec);
145 DS.FS_inline_specified = false;
146 }
147}
Chris Lattner53361ac2006-08-10 05:19:57 +0000148
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000149/// ParseDeclarationSpecifiers
150/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000151/// storage-class-specifier declaration-specifiers[opt]
152/// type-specifier declaration-specifiers[opt]
153/// type-qualifier declaration-specifiers[opt]
154/// [C99] function-specifier declaration-specifiers[opt]
155/// [GNU] attributes declaration-specifiers[opt] [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000156///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000157/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000158/// 'typedef'
159/// 'extern'
160/// 'static'
161/// 'auto'
162/// 'register'
163/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000164/// type-specifier: [C99 6.7.2]
165/// 'void'
166/// 'char'
167/// 'short'
168/// 'int'
169/// 'long'
170/// 'float'
171/// 'double'
172/// 'signed'
173/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000174/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000175/// enum-specifier
Chris Lattner8e90ef62006-08-05 03:30:45 +0000176/// typedef-name [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000177/// [C99] '_Bool'
178/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000179/// [C99] '_Imaginary' // Removed in TC2?
180/// [GNU] '_Decimal32'
181/// [GNU] '_Decimal64'
182/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000183/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000184/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000185/// [OBJC] typedef-name objc-protocol-refs [TODO]
186/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000187/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000188/// 'const'
189/// 'volatile'
190/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000191/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000192/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000193///
194void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
195 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000196 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000197 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000198 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000199 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000200 default:
201 // If this is not a declaration specifier token, we're done reading decl
202 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000203 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000204 return;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000205
206 // storage-class-specifier
207 case tok::kw_typedef:
208 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
209 break;
210 case tok::kw_extern:
211 if (DS.SCS_thread_specified)
212 Diag(Tok, diag::ext_thread_before, "extern");
213 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
214 break;
215 case tok::kw_static:
216 if (DS.SCS_thread_specified)
217 Diag(Tok, diag::ext_thread_before, "static");
218 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
219 break;
220 case tok::kw_auto:
221 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
222 break;
223 case tok::kw_register:
224 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
225 break;
226 case tok::kw___thread:
227 if (DS.SCS_thread_specified)
228 isInvalid = 2, PrevSpec = "__thread";
229 else
230 DS.SCS_thread_specified = true;
231 break;
232
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000233 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000234 case tok::kw_short:
235 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
236 break;
237 case tok::kw_long:
238 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
239 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
240 } else {
241 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
242 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
243 }
244 break;
245 case tok::kw_signed:
246 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
247 break;
248 case tok::kw_unsigned:
249 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
250 break;
251 case tok::kw__Complex:
252 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
253 break;
254 case tok::kw__Imaginary:
255 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
256 break;
257 case tok::kw_void:
258 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
259 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000260 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000261 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
262 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000263 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000264 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
265 break;
266 case tok::kw_float:
267 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
268 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000269 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000270 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
271 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000272 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000273 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
274 break;
275 case tok::kw__Decimal32:
276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
277 break;
278 case tok::kw__Decimal64:
279 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
280 break;
281 case tok::kw__Decimal128:
282 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000283 break;
284
Chris Lattner1890ac82006-08-13 01:16:23 +0000285 case tok::kw_struct:
286 case tok::kw_union:
287 ParseStructUnionSpecifier(DS);
288 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000289 case tok::kw_enum:
290 ParseEnumSpecifier(DS);
291 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000292
Chris Lattneracd58a32006-08-06 17:24:14 +0000293 //case tok::identifier:
294 // TODO: handle typedef names.
295
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000296 // type-qualifier
297 case tok::kw_const:
298 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
299 break;
300 case tok::kw_volatile:
301 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
302 break;
303 case tok::kw_restrict:
304 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
305 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000306
307 // function-specifier
308 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000309 // 'inline inline' is ok.
310 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000311 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000312 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000313 // If the specifier combination wasn't legal, issue a diagnostic.
314 if (isInvalid) {
315 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000316 if (isInvalid == 1) // Error.
317 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
318 else // extwarn.
319 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000320 }
321 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000322 }
323}
324
Chris Lattner1890ac82006-08-13 01:16:23 +0000325
326/// ParseStructUnionSpecifier
327/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000328/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000329/// struct-or-union identifier
330/// struct-or-union:
331/// 'struct'
332/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000333/// struct-contents:
334/// struct-declaration-list
335/// [EXT] empty
336/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000337/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000338/// struct-declaration
339/// struct-declaration-list struct-declaration
340/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
341/// struct-declaration:
342/// specifier-qualifier-list struct-declarator-list ';'
343/// [GNU] __extension__ struct-declaration [TODO]
344/// [GNU] specifier-qualifier-list ';' [TODO]
345/// struct-declarator-list:
346/// struct-declarator
347/// struct-declarator-list ',' struct-declarator
348/// struct-declarator:
349/// declarator
350/// declarator[opt] ':' constant-expression
Chris Lattner1890ac82006-08-13 01:16:23 +0000351///
352void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
353 assert((Tok.getKind() == tok::kw_struct ||
354 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000355 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000356 bool isUnion = Tok.getKind() == tok::kw_union;
357 ConsumeToken();
358
359 // Must have either 'struct name' or 'struct {...}'.
360 if (Tok.getKind() != tok::identifier &&
361 Tok.getKind() != tok::l_brace) {
362 Diag(Tok, diag::err_expected_ident_lbrace);
363 return;
364 }
365
366 if (Tok.getKind() == tok::identifier)
367 ConsumeToken();
368
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000369 if (Tok.getKind() == tok::l_brace) {
370 SourceLocation LBraceLoc = Tok.getLocation();
371 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000372
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000373 if (Tok.getKind() == tok::r_brace)
374 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000375
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000376 while (Tok.getKind() != tok::r_brace &&
377 Tok.getKind() != tok::eof) {
378 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000379
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000380 // Parse the common specifier-qualifiers-list piece.
381 DeclSpec DS;
382 SourceLocation SpecQualLoc = Tok.getLocation();
383 ParseSpecifierQualifierList(DS);
384 // TODO: Does specifier-qualifier list correctly check that *something* is
385 // specified?
386
387 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000388
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000389 // If there are no declarators, issue a warning.
390 if (Tok.getKind() == tok::semi) {
391 Diag(SpecQualLoc, diag::w_no_declarators);
392 } else {
393 // Read struct-declarators until we find the semicolon.
394 while (1) {
395 /// struct-declarator: declarator
396 /// struct-declarator: declarator[opt] ':' constant-expression
397 if (Tok.getKind() != tok::colon)
398 ParseDeclarator(DeclaratorInfo);
399
400 if (Tok.getKind() == tok::colon) {
401 ConsumeToken();
402 ExprResult Res = ParseConstantExpression();
403 if (Res.isInvalid) {
404 SkipUntil(tok::semi, true, true);
405 } else {
406 // Process it.
407 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000408 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000409
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000410 // TODO: install declarator.
411
412 // If we don't have a comma, it is either the end of the list (a ';')
413 // or an error, bail out.
414 if (Tok.getKind() != tok::comma)
415 break;
416
417 // Consume the comma.
418 ConsumeToken();
419
420 // Parse the next declarator.
421 DeclaratorInfo.clear();
422 }
423 }
424
425 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000426 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000427 } else {
428 Diag(Tok, diag::err_expected_semi_decl_list);
429 // Skip to end of block or statement
430 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000431 }
432 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000433
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000434 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace);
435 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000436
437 const char *PrevSpec = 0;
438 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
439 PrevSpec))
440 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000441}
442
443
Chris Lattner3b561a32006-08-13 00:12:11 +0000444/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000445/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000446/// 'enum' identifier[opt] '{' enumerator-list '}'
447/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000448/// [GNU] 'enum' identifier[opt] '{' enumerator-list '}' attributes [TODO]
449/// [GNU] 'enum' identifier[opt] '{' enumerator-list ',' '}' attributes [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000450/// 'enum' identifier
451/// enumerator-list:
452/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000453/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000454/// enumerator:
455/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000456/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000457/// enumeration-constant:
458/// identifier
459///
460void Parser::ParseEnumSpecifier(DeclSpec &DS) {
461 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000462 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000463 ConsumeToken();
464
465 // Must have either 'enum name' or 'enum {...}'.
466 if (Tok.getKind() != tok::identifier &&
467 Tok.getKind() != tok::l_brace) {
468 Diag(Tok, diag::err_expected_ident_lbrace);
469 return;
470 }
471
472 if (Tok.getKind() == tok::identifier)
473 ConsumeToken();
474
Chris Lattner1890ac82006-08-13 01:16:23 +0000475 if (Tok.getKind() != tok::l_brace)
476 return;
477
478 SourceLocation LBraceLoc = Tok.getLocation();
479 ConsumeBrace();
480
481 if (Tok.getKind() == tok::r_brace)
482 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
483
484 // Parse the enumerator-list.
485 while (Tok.getKind() == tok::identifier) {
486 ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000487
Chris Lattner1890ac82006-08-13 01:16:23 +0000488 if (Tok.getKind() == tok::equal) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000489 ConsumeToken();
Chris Lattner1890ac82006-08-13 01:16:23 +0000490 ExprResult Res = ParseConstantExpression();
491 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
Chris Lattner3b561a32006-08-13 00:12:11 +0000492 }
493
Chris Lattner1890ac82006-08-13 01:16:23 +0000494 if (Tok.getKind() != tok::comma)
495 break;
496 SourceLocation CommaLoc = Tok.getLocation();
497 ConsumeToken();
498
499 if (Tok.getKind() != tok::identifier && !getLang().C99)
500 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000501 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000502
503 // Eat the }.
504 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
505 diag::err_expected_rbrace);
Chris Lattner3b561a32006-08-13 00:12:11 +0000506 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000507
508
509 const char *PrevSpec = 0;
510 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
511 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000512}
513
514
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000515/// isTypeSpecifierQualifier - Return true if the current token could be the
516/// start of a specifier-qualifier-list.
517bool Parser::isTypeSpecifierQualifier() const {
518 switch (Tok.getKind()) {
519 default: return false;
520 // type-specifiers
521 case tok::kw_short:
522 case tok::kw_long:
523 case tok::kw_signed:
524 case tok::kw_unsigned:
525 case tok::kw__Complex:
526 case tok::kw__Imaginary:
527 case tok::kw_void:
528 case tok::kw_char:
529 case tok::kw_int:
530 case tok::kw_float:
531 case tok::kw_double:
532 case tok::kw__Bool:
533 case tok::kw__Decimal32:
534 case tok::kw__Decimal64:
535 case tok::kw__Decimal128:
536
537 // struct-or-union-specifier
538 case tok::kw_struct:
539 case tok::kw_union:
540 // enum-specifier
541 case tok::kw_enum:
542
543 // type-qualifier
544 case tok::kw_const:
545 case tok::kw_volatile:
546 case tok::kw_restrict:
547 return true;
548
549 // typedef-name
550 case tok::identifier:
551 // FIXME: if this is a typedef return true.
552 return false;
553
554 // TODO: Attributes.
555 }
556}
557
Chris Lattneracd58a32006-08-06 17:24:14 +0000558/// isDeclarationSpecifier() - Return true if the current token is part of a
559/// declaration specifier.
560bool Parser::isDeclarationSpecifier() const {
561 switch (Tok.getKind()) {
562 default: return false;
563 // storage-class-specifier
564 case tok::kw_typedef:
565 case tok::kw_extern:
566 case tok::kw_static:
567 case tok::kw_auto:
568 case tok::kw_register:
569 case tok::kw___thread:
570
571 // type-specifiers
572 case tok::kw_short:
573 case tok::kw_long:
574 case tok::kw_signed:
575 case tok::kw_unsigned:
576 case tok::kw__Complex:
577 case tok::kw__Imaginary:
578 case tok::kw_void:
579 case tok::kw_char:
580 case tok::kw_int:
581 case tok::kw_float:
582 case tok::kw_double:
583 case tok::kw__Bool:
584 case tok::kw__Decimal32:
585 case tok::kw__Decimal64:
586 case tok::kw__Decimal128:
587
588 // struct-or-union-specifier
589 case tok::kw_struct:
590 case tok::kw_union:
591 // enum-specifier
592 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000593
Chris Lattneracd58a32006-08-06 17:24:14 +0000594 // type-qualifier
595 case tok::kw_const:
596 case tok::kw_volatile:
597 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000598
Chris Lattneracd58a32006-08-06 17:24:14 +0000599 // function-specifier
600 case tok::kw_inline:
601 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000602
Chris Lattneracd58a32006-08-06 17:24:14 +0000603 // typedef-name
604 case tok::identifier:
605 // FIXME: if this is a typedef return true.
606 return false;
607 // TODO: Attributes.
608 }
609}
610
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000611
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000612/// ParseTypeQualifierListOpt
613/// type-qualifier-list: [C99 6.7.5]
614/// type-qualifier
615/// [GNU] attributes [TODO]
616/// type-qualifier-list type-qualifier
617/// [GNU] type-qualifier-list attributes [TODO]
618///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000619void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
620 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000621 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000622 int isInvalid = false;
623 const char *PrevSpec = 0;
624
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000625 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000626 default:
627 // If this is not a declaration specifier token, we're done reading decl
628 // specifiers. First verify that DeclSpec's are consistent.
629 DS.Finish(StartLoc, Diags, getLang());
630 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000631 // TODO: attributes.
632 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000633 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
634 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000635 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000636 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
637 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000638 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000639 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000640 break;
641 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000642
643 // If the specifier combination wasn't legal, issue a diagnostic.
644 if (isInvalid) {
645 assert(PrevSpec && "Method did not return previous specifier!");
646 if (isInvalid == 1) // Error.
647 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
648 else // extwarn.
649 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
650 }
651 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000652 }
653}
654
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000655
656/// ParseDeclarator - Parse and verify a newly-initialized declarator.
657///
658void Parser::ParseDeclarator(Declarator &D) {
659 /// This implements the 'declarator' production in the C grammar, then checks
660 /// for well-formedness and issues diagnostics.
661 ParseDeclaratorInternal(D);
662
Chris Lattner9fab3b92006-08-12 18:25:42 +0000663 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000664
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000665}
666
667/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000668/// declarator: [C99 6.7.5]
669/// pointer[opt] direct-declarator
670///
671/// pointer: [C99 6.7.5]
672/// '*' type-qualifier-list[opt]
673/// '*' type-qualifier-list[opt] pointer
674///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000675void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000676 if (Tok.getKind() != tok::star)
677 return ParseDirectDeclarator(D);
678
679 // Otherwise, '*' -> pointer.
680 SourceLocation Loc = Tok.getLocation();
681 ConsumeToken(); // Eat the *.
682 DeclSpec DS;
683 ParseTypeQualifierListOpt(DS);
684
685 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000686 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000687
688 // Remember that we parsed a pointer type, and remember the type-quals.
689 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
690}
691
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000692
693/// ParseDirectDeclarator
694/// direct-declarator: [C99 6.7.5]
695/// identifier
696/// '(' declarator ')'
697/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000698/// [C90] direct-declarator '[' constant-expression[opt] ']'
699/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
700/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
701/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
702/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000703/// direct-declarator '(' parameter-type-list ')'
704/// direct-declarator '(' identifier-list[opt] ')'
705/// [GNU] direct-declarator '(' parameter-forward-declarations
706/// parameter-type-list[opt] ')'
707///
Chris Lattneracd58a32006-08-06 17:24:14 +0000708void Parser::ParseDirectDeclarator(Declarator &D) {
709 // Parse the first direct-declarator seen.
710 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
711 assert(Tok.getIdentifierInfo() && "Not an identifier?");
712 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
713 ConsumeToken();
714 } else if (Tok.getKind() == tok::l_paren) {
715 // direct-declarator: '(' declarator ')'
716 // direct-declarator: '(' attributes declarator ')' [TODO]
717 // Example: 'char (*X)' or 'int (*XX)(void)'
718 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000719 } else if (D.mayOmitIdentifier()) {
720 // This could be something simple like "int" (in which case the declarator
721 // portion is empty), if an abstract-declarator is allowed.
722 D.SetIdentifier(0, Tok.getLocation());
723 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000724 // Expected identifier or '('.
725 Diag(Tok, diag::err_expected_ident_lparen);
726 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000727 }
728
729 assert(D.isPastIdentifier() &&
730 "Haven't past the location of the identifier yet?");
731
732 while (1) {
733 if (Tok.getKind() == tok::l_paren) {
734 ParseParenDeclarator(D);
735 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000736 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000737 } else {
738 break;
739 }
740 }
741}
742
743/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
744/// either be before the identifier (in which case these are just grouping
745/// parens for precedence) or it may be after the identifier, in which case
746/// these are function arguments.
747///
748/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000749/// parameter-type-list: [C99 6.7.5]
750/// parameter-list
751/// parameter-list ',' '...'
752///
753/// parameter-list: [C99 6.7.5]
754/// parameter-declaration
755/// parameter-list ',' parameter-declaration
756///
757/// parameter-declaration: [C99 6.7.5]
758/// declaration-specifiers declarator
Chris Lattneracd58a32006-08-06 17:24:14 +0000759/// [GNU] declaration-specifiers declarator attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000760/// declaration-specifiers abstract-declarator[opt]
Chris Lattneracd58a32006-08-06 17:24:14 +0000761/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000762///
763/// identifier-list: [C99 6.7.5]
764/// identifier
765/// identifier-list ',' identifier
766///
Chris Lattneracd58a32006-08-06 17:24:14 +0000767void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000768 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000769 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000770
Chris Lattneracd58a32006-08-06 17:24:14 +0000771 // If we haven't past the identifier yet (or where the identifier would be
772 // stored, if this is an abstract declarator), then this is probably just
773 // grouping parens.
774 if (!D.isPastIdentifier()) {
775 // Okay, this is probably a grouping paren. However, if this could be an
776 // abstract-declarator, then this could also be the start of function
777 // arguments (consider 'void()').
778 bool isGrouping;
779
780 if (!D.mayOmitIdentifier()) {
781 // If this can't be an abstract-declarator, this *must* be a grouping
782 // paren, because we haven't seen the identifier yet.
783 isGrouping = true;
784 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
785 isDeclarationSpecifier()) { // 'int(int)' is a function.
786
787 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000788 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000789 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000790 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000791 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000792
793 // If this is a grouping paren, handle:
794 // direct-declarator: '(' declarator ')'
795 // direct-declarator: '(' attributes declarator ')' [TODO]
796 if (isGrouping) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000797 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000798 // Match the ')'.
799 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
800 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000801 return;
802 }
803
804 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000805 // argument list. Recognize that this declarator will never have an
806 // identifier (and remember where it would have been), then fall through to
807 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000808 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000809 }
810
Chris Lattneracd58a32006-08-06 17:24:14 +0000811 // Okay, this is the parameter list of a function definition, or it is an
812 // identifier list of a K&R-style function.
813
Chris Lattner9fab3b92006-08-12 18:25:42 +0000814 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000815 // arguments to the function scope.
816 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000817 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000818 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000819 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000820 bool ErrorEmitted = false;
821
Chris Lattneracd58a32006-08-06 17:24:14 +0000822 if (Tok.getKind() == tok::r_paren) {
823 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000824 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000825 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000826 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000827 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000828 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000829 // Identifier list. Note that '(' identifier-list ')' is only allowed for
830 // normal declarators, not for abstract-declarators.
831 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
832
833 // If there was no identifier specified, either we are in an
834 // abstract-declarator, or we are in a parameter declarator which was found
835 // to be abstract. In abstract-declarators, identifier lists are not valid,
836 // diagnose this.
837 if (!D.getIdentifier())
838 Diag(Tok, diag::ext_ident_list_in_param);
839
Chris Lattner9fab3b92006-08-12 18:25:42 +0000840 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000841 ConsumeToken();
842 while (Tok.getKind() == tok::comma) {
843 // Eat the comma.
844 ConsumeToken();
845
Chris Lattner0be454e2006-08-12 19:30:51 +0000846 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000847 ErrorEmitted = true;
848 break;
849 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000850 }
851
Chris Lattneracd58a32006-08-06 17:24:14 +0000852 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000853 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000854 HasPrototype = false;
855 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000856 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000857 bool ReadArg = false;
858 // Finally, a normal, non-empty parameter type list.
859 while (1) {
860 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000861 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000862
863 // Check to see if this is "void(...)" which is not allowed.
864 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000865 // Otherwise, parse parameter type list. If it starts with an
866 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000867 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000868 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000869 }
870
871 // Consume the ellipsis.
872 ConsumeToken();
873 break;
874 }
875
876 ReadArg = true;
877
878 // Parse the declaration-specifiers.
879 DeclSpec DS;
880 ParseDeclarationSpecifiers(DS);
881
882 // Parse the declarator. This is "PrototypeContext", because we must
883 // accept either 'declarator' or 'abstract-declarator' here.
884 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
885 ParseDeclarator(DeclaratorInfo);
886
887 // TODO: do something with the declarator, if it is valid.
888
889 // If the next token is a comma, consume it and keep reading arguments.
890 if (Tok.getKind() != tok::comma) break;
891
892 // Consume the comma.
893 ConsumeToken();
894 }
895
896 HasPrototype = true;
897 }
898
Chris Lattner9fab3b92006-08-12 18:25:42 +0000899 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000900
Chris Lattner9fab3b92006-08-12 18:25:42 +0000901 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000902
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000903 // Remember that we parsed a function type, and remember the attributes.
904 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000905 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000906
Chris Lattner14776b92006-08-06 22:27:40 +0000907
908 // If we have the closing ')', eat it and we're done.
909 if (Tok.getKind() == tok::r_paren) {
910 ConsumeParen();
911 } else {
912 // If an error happened earlier parsing something else in the proto, don't
913 // issue another error.
914 if (!ErrorEmitted)
915 Diag(Tok, diag::err_expected_rparen);
916 SkipUntil(tok::r_paren);
917 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000918}
Chris Lattneracd58a32006-08-06 17:24:14 +0000919
Chris Lattnere8074e62006-08-06 18:30:15 +0000920
921/// [C90] direct-declarator '[' constant-expression[opt] ']'
922/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
923/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
924/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
925/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
926void Parser::ParseBracketDeclarator(Declarator &D) {
927 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +0000928 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +0000929
930 // If valid, this location is the position where we read the 'static' keyword.
931 SourceLocation StaticLoc;
932 if (Tok.getKind() == tok::kw_static) {
933 StaticLoc = Tok.getLocation();
934 ConsumeToken();
935 }
936
937 // If there is a type-qualifier-list, read it now.
938 DeclSpec DS;
939 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +0000940
941 // If we haven't already read 'static', check to see if there is one after the
942 // type-qualifier-list.
943 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
944 StaticLoc = Tok.getLocation();
945 ConsumeToken();
946 }
947
948 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +0000949 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +0000950 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +0000951 if (Tok.getKind() == tok::star) {
952 // Remember the '*' token, in case we have to un-get it.
953 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +0000954 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +0000955
956 // Check that the ']' token is present to avoid incorrectly parsing
957 // expressions starting with '*' as [*].
958 if (Tok.getKind() == tok::r_square) {
959 if (StaticLoc.isValid())
960 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
961 StaticLoc = SourceLocation(); // Drop the static.
962 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +0000963 } else {
964 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +0000965 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +0000966 // need to reparse it. This handles cases like 'X[*p + 4]'
967 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +0000968 }
Chris Lattner9fab3b92006-08-12 18:25:42 +0000969 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000970 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +0000971 NumElements = ParseAssignmentExpression();
972 }
973
974 // If there was an error parsing the assignment-expression, recover.
975 if (NumElements.isInvalid) {
976 // If the expression was invalid, skip it.
977 SkipUntil(tok::r_square);
978 return;
Chris Lattnere8074e62006-08-06 18:30:15 +0000979 }
980
Chris Lattner9fab3b92006-08-12 18:25:42 +0000981 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
982
Chris Lattnere8074e62006-08-06 18:30:15 +0000983 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
984 // it was not a constant expression.
985 if (!getLang().C99) {
986 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +0000987 if (isStar || StaticLoc.isValid() ||
988 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +0000989 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +0000990 }
Chris Lattner6c7416c2006-08-07 00:19:33 +0000991
992 // Remember that we parsed a pointer type, and remember the type-quals.
993 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
994 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +0000995 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +0000996}
997