blob: 48e4e30b97348e255d4ac0673ec1a2785e600fff [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
61void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
62 // At this point, we know that it is not a function definition. Parse the
63 // rest of the init-declarator-list.
64 while (1) {
65 // must be: decl-spec[opt] declarator init-declarator-list
66 // Parse declarator '=' initializer.
67 if (Tok.getKind() == tok::equal) {
68 ConsumeToken();
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000069 ParseInitializer();
Chris Lattner53361ac2006-08-10 05:19:57 +000070 }
71
Chris Lattner53361ac2006-08-10 05:19:57 +000072 // TODO: install declarator.
73
74 // If we don't have a comma, it is either the end of the list (a ';') or an
75 // error, bail out.
76 if (Tok.getKind() != tok::comma)
77 break;
78
79 // Consume the comma.
80 ConsumeToken();
81
82 // Parse the next declarator.
83 D.clear();
84 ParseDeclarator(D);
85 }
86
87 if (Tok.getKind() == tok::semi) {
88 ConsumeToken();
89 } else {
90 Diag(Tok, diag::err_parse_error);
91 // Skip to end of block or statement
92 SkipUntil(tok::r_brace, true);
93 if (Tok.getKind() == tok::semi)
94 ConsumeToken();
95 }
96}
97
Chris Lattner1890ac82006-08-13 01:16:23 +000098/// ParseSpecifierQualifierList
99/// specifier-qualifier-list:
100/// type-specifier specifier-qualifier-list[opt]
101/// type-qualifier specifier-qualifier-list[opt]
102///
103void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
104 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
105 /// parse declaration-specifiers and complain about extra stuff.
106 SourceLocation Loc = Tok.getLocation();
107 ParseDeclarationSpecifiers(DS);
108
109 // Validate declspec for type-name.
110 unsigned Specs = DS.getParsedSpecifiers();
111 if (Specs == DeclSpec::PQ_None)
112 Diag(Tok, diag::err_typename_requires_specqual);
113
114 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
115 Diag(Loc, diag::err_typename_invalid_storageclass);
116 // Remove storage class.
117 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
118 DS.SCS_thread_specified = false;
119 }
120 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
121 Diag(Loc, diag::err_typename_invalid_functionspec);
122 DS.FS_inline_specified = false;
123 }
124}
Chris Lattner53361ac2006-08-10 05:19:57 +0000125
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000126/// ParseDeclarationSpecifiers
127/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000128/// storage-class-specifier declaration-specifiers[opt]
129/// type-specifier declaration-specifiers[opt]
130/// type-qualifier declaration-specifiers[opt]
131/// [C99] function-specifier declaration-specifiers[opt]
132/// [GNU] attributes declaration-specifiers[opt] [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000133///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000134/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000135/// 'typedef'
136/// 'extern'
137/// 'static'
138/// 'auto'
139/// 'register'
140/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000141/// type-specifier: [C99 6.7.2]
142/// 'void'
143/// 'char'
144/// 'short'
145/// 'int'
146/// 'long'
147/// 'float'
148/// 'double'
149/// 'signed'
150/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000151/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000152/// enum-specifier
Chris Lattner8e90ef62006-08-05 03:30:45 +0000153/// typedef-name [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000154/// [C99] '_Bool'
155/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000156/// [C99] '_Imaginary' // Removed in TC2?
157/// [GNU] '_Decimal32'
158/// [GNU] '_Decimal64'
159/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000160/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000161/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000162/// [OBJC] typedef-name objc-protocol-refs [TODO]
163/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000164/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000165/// 'const'
166/// 'volatile'
167/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000168/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000169/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000170///
171void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
172 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000173 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000174 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000175 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000176 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000177 default:
178 // If this is not a declaration specifier token, we're done reading decl
179 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000180 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000181 return;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000182
183 // storage-class-specifier
184 case tok::kw_typedef:
185 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
186 break;
187 case tok::kw_extern:
188 if (DS.SCS_thread_specified)
189 Diag(Tok, diag::ext_thread_before, "extern");
190 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
191 break;
192 case tok::kw_static:
193 if (DS.SCS_thread_specified)
194 Diag(Tok, diag::ext_thread_before, "static");
195 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
196 break;
197 case tok::kw_auto:
198 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
199 break;
200 case tok::kw_register:
201 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
202 break;
203 case tok::kw___thread:
204 if (DS.SCS_thread_specified)
205 isInvalid = 2, PrevSpec = "__thread";
206 else
207 DS.SCS_thread_specified = true;
208 break;
209
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000210 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000211 case tok::kw_short:
212 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
213 break;
214 case tok::kw_long:
215 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
216 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
217 } else {
218 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
219 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
220 }
221 break;
222 case tok::kw_signed:
223 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
224 break;
225 case tok::kw_unsigned:
226 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
227 break;
228 case tok::kw__Complex:
229 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
230 break;
231 case tok::kw__Imaginary:
232 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
233 break;
234 case tok::kw_void:
235 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
236 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000237 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000238 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
239 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000240 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000241 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
242 break;
243 case tok::kw_float:
244 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
245 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000246 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000247 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
248 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000249 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000250 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
251 break;
252 case tok::kw__Decimal32:
253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
254 break;
255 case tok::kw__Decimal64:
256 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
257 break;
258 case tok::kw__Decimal128:
259 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000260 break;
261
Chris Lattner1890ac82006-08-13 01:16:23 +0000262 case tok::kw_struct:
263 case tok::kw_union:
264 ParseStructUnionSpecifier(DS);
265 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000266 case tok::kw_enum:
267 ParseEnumSpecifier(DS);
268 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000269
Chris Lattneracd58a32006-08-06 17:24:14 +0000270 //case tok::identifier:
271 // TODO: handle typedef names.
272
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000273 // type-qualifier
274 case tok::kw_const:
275 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
276 break;
277 case tok::kw_volatile:
278 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
279 break;
280 case tok::kw_restrict:
281 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
282 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000283
284 // function-specifier
285 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000286 // 'inline inline' is ok.
287 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000288 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000289 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000290 // If the specifier combination wasn't legal, issue a diagnostic.
291 if (isInvalid) {
292 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000293 if (isInvalid == 1) // Error.
294 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
295 else // extwarn.
296 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000297 }
298 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000299 }
300}
301
Chris Lattner1890ac82006-08-13 01:16:23 +0000302
303/// ParseStructUnionSpecifier
304/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000305/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000306/// struct-or-union identifier
307/// struct-or-union:
308/// 'struct'
309/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000310/// struct-contents:
311/// struct-declaration-list
312/// [EXT] empty
313/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000314/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000315/// struct-declaration
316/// struct-declaration-list struct-declaration
317/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
318/// struct-declaration:
319/// specifier-qualifier-list struct-declarator-list ';'
320/// [GNU] __extension__ struct-declaration [TODO]
321/// [GNU] specifier-qualifier-list ';' [TODO]
322/// struct-declarator-list:
323/// struct-declarator
324/// struct-declarator-list ',' struct-declarator
325/// struct-declarator:
326/// declarator
327/// declarator[opt] ':' constant-expression
Chris Lattner1890ac82006-08-13 01:16:23 +0000328///
329void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
330 assert((Tok.getKind() == tok::kw_struct ||
331 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000332 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000333 bool isUnion = Tok.getKind() == tok::kw_union;
334 ConsumeToken();
335
336 // Must have either 'struct name' or 'struct {...}'.
337 if (Tok.getKind() != tok::identifier &&
338 Tok.getKind() != tok::l_brace) {
339 Diag(Tok, diag::err_expected_ident_lbrace);
340 return;
341 }
342
343 if (Tok.getKind() == tok::identifier)
344 ConsumeToken();
345
346 if (Tok.getKind() != tok::l_brace)
347 return;
348
349 SourceLocation LBraceLoc = Tok.getLocation();
350 ConsumeBrace();
351
352 if (Tok.getKind() == tok::r_brace)
353 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union" : "struct");
354
355 while (Tok.getKind() != tok::r_brace &&
356 Tok.getKind() != tok::eof) {
357 // Each iteration of this loop reads one struct-declaration.
358
359 // Parse the common specifier-qualifiers-list piece.
360 DeclSpec DS;
361 SourceLocation SpecQualLoc = Tok.getLocation();
362 ParseSpecifierQualifierList(DS);
363 // TODO: Does specifier-qualifier list correctly check that *something* is
364 // specified?
365
366 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
367
368 // If there are no declarators, issue a warning.
369 if (Tok.getKind() == tok::semi) {
370 Diag(SpecQualLoc, diag::w_no_declarators);
371 } else {
372 // Read struct-declarators until we find the semicolon.
373 while (1) {
374 /// struct-declarator: declarator
375 /// struct-declarator: declarator[opt] ':' constant-expression
376 if (Tok.getKind() != tok::colon)
377 ParseDeclarator(DeclaratorInfo);
378
379 if (Tok.getKind() == tok::colon) {
380 ConsumeToken();
381 ExprResult Res = ParseConstantExpression();
382 if (Res.isInvalid) {
383 SkipUntil(tok::semi, true, true);
384 } else {
385 // Process it.
386 }
387 }
388
389 // TODO: install declarator.
390
391 // If we don't have a comma, it is either the end of the list (a ';') or
392 // an error, bail out.
393 if (Tok.getKind() != tok::comma)
394 break;
395
396 // Consume the comma.
397 ConsumeToken();
398
399 // Parse the next declarator.
400 DeclaratorInfo.clear();
401 }
402 }
403
404 if (Tok.getKind() == tok::semi) {
405 ConsumeToken();
406 } else {
407 Diag(Tok, diag::err_expected_semi_decl_list);
408 // Skip to end of block or statement
409 SkipUntil(tok::r_brace, true, true);
410 }
411 }
412
413 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace);
Chris Lattnerda72c822006-08-13 22:16:42 +0000414
415 const char *PrevSpec = 0;
416 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
417 PrevSpec))
418 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000419}
420
421
Chris Lattner3b561a32006-08-13 00:12:11 +0000422/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000423/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000424/// 'enum' identifier[opt] '{' enumerator-list '}'
425/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000426/// [GNU] 'enum' identifier[opt] '{' enumerator-list '}' attributes [TODO]
427/// [GNU] 'enum' identifier[opt] '{' enumerator-list ',' '}' attributes [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000428/// 'enum' identifier
429/// enumerator-list:
430/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000431/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000432/// enumerator:
433/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000434/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000435/// enumeration-constant:
436/// identifier
437///
438void Parser::ParseEnumSpecifier(DeclSpec &DS) {
439 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000440 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000441 ConsumeToken();
442
443 // Must have either 'enum name' or 'enum {...}'.
444 if (Tok.getKind() != tok::identifier &&
445 Tok.getKind() != tok::l_brace) {
446 Diag(Tok, diag::err_expected_ident_lbrace);
447 return;
448 }
449
450 if (Tok.getKind() == tok::identifier)
451 ConsumeToken();
452
Chris Lattner1890ac82006-08-13 01:16:23 +0000453 if (Tok.getKind() != tok::l_brace)
454 return;
455
456 SourceLocation LBraceLoc = Tok.getLocation();
457 ConsumeBrace();
458
459 if (Tok.getKind() == tok::r_brace)
460 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
461
462 // Parse the enumerator-list.
463 while (Tok.getKind() == tok::identifier) {
464 ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000465
Chris Lattner1890ac82006-08-13 01:16:23 +0000466 if (Tok.getKind() == tok::equal) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000467 ConsumeToken();
Chris Lattner1890ac82006-08-13 01:16:23 +0000468 ExprResult Res = ParseConstantExpression();
469 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
Chris Lattner3b561a32006-08-13 00:12:11 +0000470 }
471
Chris Lattner1890ac82006-08-13 01:16:23 +0000472 if (Tok.getKind() != tok::comma)
473 break;
474 SourceLocation CommaLoc = Tok.getLocation();
475 ConsumeToken();
476
477 if (Tok.getKind() != tok::identifier && !getLang().C99)
478 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000479 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000480
481 // Eat the }.
482 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
483 diag::err_expected_rbrace);
Chris Lattner3b561a32006-08-13 00:12:11 +0000484 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000485
486
487 const char *PrevSpec = 0;
488 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
489 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000490}
491
492
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000493/// isTypeSpecifierQualifier - Return true if the current token could be the
494/// start of a specifier-qualifier-list.
495bool Parser::isTypeSpecifierQualifier() const {
496 switch (Tok.getKind()) {
497 default: return false;
498 // type-specifiers
499 case tok::kw_short:
500 case tok::kw_long:
501 case tok::kw_signed:
502 case tok::kw_unsigned:
503 case tok::kw__Complex:
504 case tok::kw__Imaginary:
505 case tok::kw_void:
506 case tok::kw_char:
507 case tok::kw_int:
508 case tok::kw_float:
509 case tok::kw_double:
510 case tok::kw__Bool:
511 case tok::kw__Decimal32:
512 case tok::kw__Decimal64:
513 case tok::kw__Decimal128:
514
515 // struct-or-union-specifier
516 case tok::kw_struct:
517 case tok::kw_union:
518 // enum-specifier
519 case tok::kw_enum:
520
521 // type-qualifier
522 case tok::kw_const:
523 case tok::kw_volatile:
524 case tok::kw_restrict:
525 return true;
526
527 // typedef-name
528 case tok::identifier:
529 // FIXME: if this is a typedef return true.
530 return false;
531
532 // TODO: Attributes.
533 }
534}
535
Chris Lattneracd58a32006-08-06 17:24:14 +0000536/// isDeclarationSpecifier() - Return true if the current token is part of a
537/// declaration specifier.
538bool Parser::isDeclarationSpecifier() const {
539 switch (Tok.getKind()) {
540 default: return false;
541 // storage-class-specifier
542 case tok::kw_typedef:
543 case tok::kw_extern:
544 case tok::kw_static:
545 case tok::kw_auto:
546 case tok::kw_register:
547 case tok::kw___thread:
548
549 // type-specifiers
550 case tok::kw_short:
551 case tok::kw_long:
552 case tok::kw_signed:
553 case tok::kw_unsigned:
554 case tok::kw__Complex:
555 case tok::kw__Imaginary:
556 case tok::kw_void:
557 case tok::kw_char:
558 case tok::kw_int:
559 case tok::kw_float:
560 case tok::kw_double:
561 case tok::kw__Bool:
562 case tok::kw__Decimal32:
563 case tok::kw__Decimal64:
564 case tok::kw__Decimal128:
565
566 // struct-or-union-specifier
567 case tok::kw_struct:
568 case tok::kw_union:
569 // enum-specifier
570 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000571
Chris Lattneracd58a32006-08-06 17:24:14 +0000572 // type-qualifier
573 case tok::kw_const:
574 case tok::kw_volatile:
575 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000576
Chris Lattneracd58a32006-08-06 17:24:14 +0000577 // function-specifier
578 case tok::kw_inline:
579 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000580
Chris Lattneracd58a32006-08-06 17:24:14 +0000581 // typedef-name
582 case tok::identifier:
583 // FIXME: if this is a typedef return true.
584 return false;
585 // TODO: Attributes.
586 }
587}
588
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000589
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000590/// ParseTypeQualifierListOpt
591/// type-qualifier-list: [C99 6.7.5]
592/// type-qualifier
593/// [GNU] attributes [TODO]
594/// type-qualifier-list type-qualifier
595/// [GNU] type-qualifier-list attributes [TODO]
596///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000597void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
598 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000599 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000600 int isInvalid = false;
601 const char *PrevSpec = 0;
602
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000603 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000604 default:
605 // If this is not a declaration specifier token, we're done reading decl
606 // specifiers. First verify that DeclSpec's are consistent.
607 DS.Finish(StartLoc, Diags, getLang());
608 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000609 // TODO: attributes.
610 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000611 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
612 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000613 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000614 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
615 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000616 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000617 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000618 break;
619 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000620
621 // If the specifier combination wasn't legal, issue a diagnostic.
622 if (isInvalid) {
623 assert(PrevSpec && "Method did not return previous specifier!");
624 if (isInvalid == 1) // Error.
625 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
626 else // extwarn.
627 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
628 }
629 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000630 }
631}
632
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000633
634/// ParseDeclarator - Parse and verify a newly-initialized declarator.
635///
636void Parser::ParseDeclarator(Declarator &D) {
637 /// This implements the 'declarator' production in the C grammar, then checks
638 /// for well-formedness and issues diagnostics.
639 ParseDeclaratorInternal(D);
640
Chris Lattner9fab3b92006-08-12 18:25:42 +0000641 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000642
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000643}
644
645/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000646/// declarator: [C99 6.7.5]
647/// pointer[opt] direct-declarator
648///
649/// pointer: [C99 6.7.5]
650/// '*' type-qualifier-list[opt]
651/// '*' type-qualifier-list[opt] pointer
652///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000653void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000654 if (Tok.getKind() != tok::star)
655 return ParseDirectDeclarator(D);
656
657 // Otherwise, '*' -> pointer.
658 SourceLocation Loc = Tok.getLocation();
659 ConsumeToken(); // Eat the *.
660 DeclSpec DS;
661 ParseTypeQualifierListOpt(DS);
662
663 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000664 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000665
666 // Remember that we parsed a pointer type, and remember the type-quals.
667 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
668}
669
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000670
671/// ParseDirectDeclarator
672/// direct-declarator: [C99 6.7.5]
673/// identifier
674/// '(' declarator ')'
675/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000676/// [C90] direct-declarator '[' constant-expression[opt] ']'
677/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
678/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
679/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
680/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000681/// direct-declarator '(' parameter-type-list ')'
682/// direct-declarator '(' identifier-list[opt] ')'
683/// [GNU] direct-declarator '(' parameter-forward-declarations
684/// parameter-type-list[opt] ')'
685///
Chris Lattneracd58a32006-08-06 17:24:14 +0000686void Parser::ParseDirectDeclarator(Declarator &D) {
687 // Parse the first direct-declarator seen.
688 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
689 assert(Tok.getIdentifierInfo() && "Not an identifier?");
690 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
691 ConsumeToken();
692 } else if (Tok.getKind() == tok::l_paren) {
693 // direct-declarator: '(' declarator ')'
694 // direct-declarator: '(' attributes declarator ')' [TODO]
695 // Example: 'char (*X)' or 'int (*XX)(void)'
696 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000697 } else if (D.mayOmitIdentifier()) {
698 // This could be something simple like "int" (in which case the declarator
699 // portion is empty), if an abstract-declarator is allowed.
700 D.SetIdentifier(0, Tok.getLocation());
701 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000702 // Expected identifier or '('.
703 Diag(Tok, diag::err_expected_ident_lparen);
704 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000705 }
706
707 assert(D.isPastIdentifier() &&
708 "Haven't past the location of the identifier yet?");
709
710 while (1) {
711 if (Tok.getKind() == tok::l_paren) {
712 ParseParenDeclarator(D);
713 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000714 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000715 } else {
716 break;
717 }
718 }
719}
720
721/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
722/// either be before the identifier (in which case these are just grouping
723/// parens for precedence) or it may be after the identifier, in which case
724/// these are function arguments.
725///
726/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000727/// parameter-type-list: [C99 6.7.5]
728/// parameter-list
729/// parameter-list ',' '...'
730///
731/// parameter-list: [C99 6.7.5]
732/// parameter-declaration
733/// parameter-list ',' parameter-declaration
734///
735/// parameter-declaration: [C99 6.7.5]
736/// declaration-specifiers declarator
Chris Lattneracd58a32006-08-06 17:24:14 +0000737/// [GNU] declaration-specifiers declarator attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000738/// declaration-specifiers abstract-declarator[opt]
Chris Lattneracd58a32006-08-06 17:24:14 +0000739/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000740///
741/// identifier-list: [C99 6.7.5]
742/// identifier
743/// identifier-list ',' identifier
744///
Chris Lattneracd58a32006-08-06 17:24:14 +0000745void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000746 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000747 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000748
Chris Lattneracd58a32006-08-06 17:24:14 +0000749 // If we haven't past the identifier yet (or where the identifier would be
750 // stored, if this is an abstract declarator), then this is probably just
751 // grouping parens.
752 if (!D.isPastIdentifier()) {
753 // Okay, this is probably a grouping paren. However, if this could be an
754 // abstract-declarator, then this could also be the start of function
755 // arguments (consider 'void()').
756 bool isGrouping;
757
758 if (!D.mayOmitIdentifier()) {
759 // If this can't be an abstract-declarator, this *must* be a grouping
760 // paren, because we haven't seen the identifier yet.
761 isGrouping = true;
762 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
763 isDeclarationSpecifier()) { // 'int(int)' is a function.
764
765 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000766 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000767 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000768 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000769 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000770
771 // If this is a grouping paren, handle:
772 // direct-declarator: '(' declarator ')'
773 // direct-declarator: '(' attributes declarator ')' [TODO]
774 if (isGrouping) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000775 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000776 // Match the ')'.
777 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
778 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000779 return;
780 }
781
782 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000783 // argument list. Recognize that this declarator will never have an
784 // identifier (and remember where it would have been), then fall through to
785 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000786 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000787 }
788
Chris Lattneracd58a32006-08-06 17:24:14 +0000789 // Okay, this is the parameter list of a function definition, or it is an
790 // identifier list of a K&R-style function.
791
Chris Lattner9fab3b92006-08-12 18:25:42 +0000792 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000793 // arguments to the function scope.
794 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000795 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000796 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000797 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000798 bool ErrorEmitted = false;
799
Chris Lattneracd58a32006-08-06 17:24:14 +0000800 if (Tok.getKind() == tok::r_paren) {
801 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000802 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000803 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000804 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000805 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000806 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000807 // Identifier list. Note that '(' identifier-list ')' is only allowed for
808 // normal declarators, not for abstract-declarators.
809 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
810
811 // If there was no identifier specified, either we are in an
812 // abstract-declarator, or we are in a parameter declarator which was found
813 // to be abstract. In abstract-declarators, identifier lists are not valid,
814 // diagnose this.
815 if (!D.getIdentifier())
816 Diag(Tok, diag::ext_ident_list_in_param);
817
Chris Lattner9fab3b92006-08-12 18:25:42 +0000818 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000819 ConsumeToken();
820 while (Tok.getKind() == tok::comma) {
821 // Eat the comma.
822 ConsumeToken();
823
Chris Lattner0be454e2006-08-12 19:30:51 +0000824 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000825 ErrorEmitted = true;
826 break;
827 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000828 }
829
Chris Lattneracd58a32006-08-06 17:24:14 +0000830 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000831 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000832 HasPrototype = false;
833 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000834 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000835 bool ReadArg = false;
836 // Finally, a normal, non-empty parameter type list.
837 while (1) {
838 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000839 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000840
841 // Check to see if this is "void(...)" which is not allowed.
842 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000843 // Otherwise, parse parameter type list. If it starts with an
844 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000845 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000846 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000847 }
848
849 // Consume the ellipsis.
850 ConsumeToken();
851 break;
852 }
853
854 ReadArg = true;
855
856 // Parse the declaration-specifiers.
857 DeclSpec DS;
858 ParseDeclarationSpecifiers(DS);
859
860 // Parse the declarator. This is "PrototypeContext", because we must
861 // accept either 'declarator' or 'abstract-declarator' here.
862 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
863 ParseDeclarator(DeclaratorInfo);
864
865 // TODO: do something with the declarator, if it is valid.
866
867 // If the next token is a comma, consume it and keep reading arguments.
868 if (Tok.getKind() != tok::comma) break;
869
870 // Consume the comma.
871 ConsumeToken();
872 }
873
874 HasPrototype = true;
875 }
876
Chris Lattner9fab3b92006-08-12 18:25:42 +0000877 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000878
Chris Lattner9fab3b92006-08-12 18:25:42 +0000879 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000880
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000881 // Remember that we parsed a function type, and remember the attributes.
882 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000883 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000884
Chris Lattner14776b92006-08-06 22:27:40 +0000885
886 // If we have the closing ')', eat it and we're done.
887 if (Tok.getKind() == tok::r_paren) {
888 ConsumeParen();
889 } else {
890 // If an error happened earlier parsing something else in the proto, don't
891 // issue another error.
892 if (!ErrorEmitted)
893 Diag(Tok, diag::err_expected_rparen);
894 SkipUntil(tok::r_paren);
895 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000896}
Chris Lattneracd58a32006-08-06 17:24:14 +0000897
Chris Lattnere8074e62006-08-06 18:30:15 +0000898
899/// [C90] direct-declarator '[' constant-expression[opt] ']'
900/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
901/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
902/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
903/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
904void Parser::ParseBracketDeclarator(Declarator &D) {
905 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +0000906 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +0000907
908 // If valid, this location is the position where we read the 'static' keyword.
909 SourceLocation StaticLoc;
910 if (Tok.getKind() == tok::kw_static) {
911 StaticLoc = Tok.getLocation();
912 ConsumeToken();
913 }
914
915 // If there is a type-qualifier-list, read it now.
916 DeclSpec DS;
917 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +0000918
919 // If we haven't already read 'static', check to see if there is one after the
920 // type-qualifier-list.
921 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
922 StaticLoc = Tok.getLocation();
923 ConsumeToken();
924 }
925
926 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +0000927 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +0000928 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +0000929 if (Tok.getKind() == tok::star) {
930 // Remember the '*' token, in case we have to un-get it.
931 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +0000932 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +0000933
934 // Check that the ']' token is present to avoid incorrectly parsing
935 // expressions starting with '*' as [*].
936 if (Tok.getKind() == tok::r_square) {
937 if (StaticLoc.isValid())
938 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
939 StaticLoc = SourceLocation(); // Drop the static.
940 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +0000941 } else {
942 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +0000943 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +0000944 // need to reparse it. This handles cases like 'X[*p + 4]'
945 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +0000946 }
Chris Lattner9fab3b92006-08-12 18:25:42 +0000947 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000948 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +0000949 NumElements = ParseAssignmentExpression();
950 }
951
952 // If there was an error parsing the assignment-expression, recover.
953 if (NumElements.isInvalid) {
954 // If the expression was invalid, skip it.
955 SkipUntil(tok::r_square);
956 return;
Chris Lattnere8074e62006-08-06 18:30:15 +0000957 }
958
Chris Lattner9fab3b92006-08-12 18:25:42 +0000959 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
960
Chris Lattnere8074e62006-08-06 18:30:15 +0000961 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
962 // it was not a constant expression.
963 if (!getLang().C99) {
964 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +0000965 if (isStar || StaticLoc.isValid() ||
966 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +0000967 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +0000968 }
Chris Lattner6c7416c2006-08-07 00:19:33 +0000969
970 // Remember that we parsed a pointer type, and remember the type-quals.
971 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
972 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +0000973 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +0000974}
975