blob: 22f7f4f77d5b3e2eb190bec42566e3851d2c4d1c [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
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000036/// ParseAttributes - Parse a non-empty attributes list.
37///
38/// [GNU] attributes:
39/// attribute
40/// attributes attribute
41///
42/// [GNU] attribute:
43/// '__attribute__' '(' '(' attribute-list ')' ')'
44///
45/// [GNU] attribute-list:
46/// attrib
47/// attribute_list ',' attrib
48///
49/// [GNU] attrib:
50/// empty
51/// any-word
52/// any-word '(' identifier ')'
53/// any-word '(' identifier ',' nonempty-expr-list ')'
54/// any-word '(' expr-list ')'
55///
56void Parser::ParseAttributes() {
57 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
58 ConsumeToken();
59
60 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
61 "attribute"))
62 return;
63
64 // TODO: Parse the attributes.
65 SkipUntil(tok::r_paren, false);
66}
67
Chris Lattnerf5fbd792006-08-10 23:56:11 +000068
Chris Lattner53361ac2006-08-10 05:19:57 +000069/// ParseDeclaration - Parse a full 'declaration', which consists of
70/// declaration-specifiers, some number of declarators, and a semicolon.
71/// 'Context' should be a Declarator::TheContext value.
72void Parser::ParseDeclaration(unsigned Context) {
73 // Parse the common declaration-specifiers piece.
74 DeclSpec DS;
75 ParseDeclarationSpecifiers(DS);
76
Chris Lattner0e894622006-08-13 19:58:17 +000077 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
78 // declaration-specifiers init-declarator-list[opt] ';'
79 if (Tok.getKind() == tok::semi) {
80 // TODO: emit error on 'int;' or 'const enum foo;'.
81 // if (!DS.isMissingDeclaratorOk()) Diag(...);
82
83 ConsumeToken();
84 return;
85 }
86
Chris Lattner53361ac2006-08-10 05:19:57 +000087 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
88 ParseDeclarator(DeclaratorInfo);
89
90 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
91}
92
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000093/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
94/// parsing 'declaration-specifiers declarator'. This method is split out this
95/// way to handle the ambiguity between top-level function-definitions and
96/// declarations.
97///
98/// declaration: [C99 6.7]
99/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
100/// [!C99] init-declarator-list ';' [TODO]
101/// [OMP] threadprivate-directive [TODO]
102///
103/// init-declarator-list: [C99 6.7]
104/// init-declarator
105/// init-declarator-list ',' init-declarator
106/// init-declarator: [C99 6.7]
107/// declarator
108/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000109/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
110/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000111///
Chris Lattner53361ac2006-08-10 05:19:57 +0000112void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
113 // At this point, we know that it is not a function definition. Parse the
114 // rest of the init-declarator-list.
115 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000116 // If a simple-asm-expr is present, parse it.
117 if (Tok.getKind() == tok::kw_asm)
118 ParseSimpleAsm();
119
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000120 // If attributes are present, parse them.
121 if (Tok.getKind() == tok::kw___attribute)
122 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000123
Chris Lattner53361ac2006-08-10 05:19:57 +0000124 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000125 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000126 if (Tok.getKind() == tok::equal) {
127 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000128 Init = ParseInitializer();
129 if (!Init.isInvalid) {
130 SkipUntil(tok::semi);
131 return;
132 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000133 }
134
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000135 // Inform the current actions module that we just parsed a declarator.
136 Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000137
138 // If we don't have a comma, it is either the end of the list (a ';') or an
139 // error, bail out.
140 if (Tok.getKind() != tok::comma)
141 break;
142
143 // Consume the comma.
144 ConsumeToken();
145
146 // Parse the next declarator.
147 D.clear();
148 ParseDeclarator(D);
149 }
150
151 if (Tok.getKind() == tok::semi) {
152 ConsumeToken();
153 } else {
154 Diag(Tok, diag::err_parse_error);
155 // Skip to end of block or statement
156 SkipUntil(tok::r_brace, true);
157 if (Tok.getKind() == tok::semi)
158 ConsumeToken();
159 }
160}
161
Chris Lattner1890ac82006-08-13 01:16:23 +0000162/// ParseSpecifierQualifierList
163/// specifier-qualifier-list:
164/// type-specifier specifier-qualifier-list[opt]
165/// type-qualifier specifier-qualifier-list[opt]
166///
167void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
168 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
169 /// parse declaration-specifiers and complain about extra stuff.
170 SourceLocation Loc = Tok.getLocation();
171 ParseDeclarationSpecifiers(DS);
172
173 // Validate declspec for type-name.
174 unsigned Specs = DS.getParsedSpecifiers();
175 if (Specs == DeclSpec::PQ_None)
176 Diag(Tok, diag::err_typename_requires_specqual);
177
178 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
179 Diag(Loc, diag::err_typename_invalid_storageclass);
180 // Remove storage class.
181 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
182 DS.SCS_thread_specified = false;
183 }
184 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
185 Diag(Loc, diag::err_typename_invalid_functionspec);
186 DS.FS_inline_specified = false;
187 }
188}
Chris Lattner53361ac2006-08-10 05:19:57 +0000189
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000190/// ParseDeclarationSpecifiers
191/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000192/// storage-class-specifier declaration-specifiers[opt]
193/// type-specifier declaration-specifiers[opt]
194/// type-qualifier declaration-specifiers[opt]
195/// [C99] function-specifier declaration-specifiers[opt]
196/// [GNU] attributes declaration-specifiers[opt] [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000197///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000198/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000199/// 'typedef'
200/// 'extern'
201/// 'static'
202/// 'auto'
203/// 'register'
204/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000205/// type-specifier: [C99 6.7.2]
206/// 'void'
207/// 'char'
208/// 'short'
209/// 'int'
210/// 'long'
211/// 'float'
212/// 'double'
213/// 'signed'
214/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000215/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000216/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000217/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000218/// [C99] '_Bool'
219/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000220/// [C99] '_Imaginary' // Removed in TC2?
221/// [GNU] '_Decimal32'
222/// [GNU] '_Decimal64'
223/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000224/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000225/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000226/// [OBJC] typedef-name objc-protocol-refs [TODO]
227/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000228/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000229/// 'const'
230/// 'volatile'
231/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000232/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000233/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000234///
235void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
236 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000237 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000238 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000239 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000240 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000241 // typedef-name
242 case tok::identifier:
243 // This identifier can only be a typedef name if we haven't already seen
244 // a typename. This avoids the virtual method call on things like
245 // 'int foo'.
246 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
247 Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
248 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
249 break;
250 }
251 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000252 default:
253 // If this is not a declaration specifier token, we're done reading decl
254 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000255 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000256 return;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000257
258 // storage-class-specifier
259 case tok::kw_typedef:
260 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
261 break;
262 case tok::kw_extern:
263 if (DS.SCS_thread_specified)
264 Diag(Tok, diag::ext_thread_before, "extern");
265 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
266 break;
267 case tok::kw_static:
268 if (DS.SCS_thread_specified)
269 Diag(Tok, diag::ext_thread_before, "static");
270 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
271 break;
272 case tok::kw_auto:
273 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
274 break;
275 case tok::kw_register:
276 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
277 break;
278 case tok::kw___thread:
279 if (DS.SCS_thread_specified)
280 isInvalid = 2, PrevSpec = "__thread";
281 else
282 DS.SCS_thread_specified = true;
283 break;
284
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000285 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000286 case tok::kw_short:
287 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
288 break;
289 case tok::kw_long:
290 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
291 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
292 } else {
293 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
294 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
295 }
296 break;
297 case tok::kw_signed:
298 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
299 break;
300 case tok::kw_unsigned:
301 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
302 break;
303 case tok::kw__Complex:
304 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
305 break;
306 case tok::kw__Imaginary:
307 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
308 break;
309 case tok::kw_void:
310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
311 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000312 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
314 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000315 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
317 break;
318 case tok::kw_float:
319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
320 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000321 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
323 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000324 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
326 break;
327 case tok::kw__Decimal32:
328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
329 break;
330 case tok::kw__Decimal64:
331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
332 break;
333 case tok::kw__Decimal128:
334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000335 break;
336
Chris Lattner1890ac82006-08-13 01:16:23 +0000337 case tok::kw_struct:
338 case tok::kw_union:
339 ParseStructUnionSpecifier(DS);
340 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000341 case tok::kw_enum:
342 ParseEnumSpecifier(DS);
343 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000344
345 // type-qualifier
346 case tok::kw_const:
347 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
348 break;
349 case tok::kw_volatile:
350 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
351 break;
352 case tok::kw_restrict:
353 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
354 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000355
356 // function-specifier
357 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000358 // 'inline inline' is ok.
359 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000360 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000361 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000362 // If the specifier combination wasn't legal, issue a diagnostic.
363 if (isInvalid) {
364 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000365 if (isInvalid == 1) // Error.
366 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
367 else // extwarn.
368 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000369 }
370 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000371 }
372}
373
Chris Lattner1890ac82006-08-13 01:16:23 +0000374
375/// ParseStructUnionSpecifier
376/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000377/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000378/// struct-or-union identifier
379/// struct-or-union:
380/// 'struct'
381/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000382/// struct-contents:
383/// struct-declaration-list
384/// [EXT] empty
385/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000386/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000387/// struct-declaration
388/// struct-declaration-list struct-declaration
389/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
390/// struct-declaration:
391/// specifier-qualifier-list struct-declarator-list ';'
392/// [GNU] __extension__ struct-declaration [TODO]
393/// [GNU] specifier-qualifier-list ';' [TODO]
394/// struct-declarator-list:
395/// struct-declarator
396/// struct-declarator-list ',' struct-declarator
397/// struct-declarator:
398/// declarator
399/// declarator[opt] ':' constant-expression
Chris Lattner1890ac82006-08-13 01:16:23 +0000400///
401void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
402 assert((Tok.getKind() == tok::kw_struct ||
403 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000404 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000405 bool isUnion = Tok.getKind() == tok::kw_union;
406 ConsumeToken();
407
408 // Must have either 'struct name' or 'struct {...}'.
409 if (Tok.getKind() != tok::identifier &&
410 Tok.getKind() != tok::l_brace) {
411 Diag(Tok, diag::err_expected_ident_lbrace);
412 return;
413 }
414
415 if (Tok.getKind() == tok::identifier)
416 ConsumeToken();
417
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000418 if (Tok.getKind() == tok::l_brace) {
419 SourceLocation LBraceLoc = Tok.getLocation();
420 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000421
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000422 if (Tok.getKind() == tok::r_brace)
423 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000424
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000425 while (Tok.getKind() != tok::r_brace &&
426 Tok.getKind() != tok::eof) {
427 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000428
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000429 // Parse the common specifier-qualifiers-list piece.
430 DeclSpec DS;
431 SourceLocation SpecQualLoc = Tok.getLocation();
432 ParseSpecifierQualifierList(DS);
433 // TODO: Does specifier-qualifier list correctly check that *something* is
434 // specified?
435
436 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000437
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000438 // If there are no declarators, issue a warning.
439 if (Tok.getKind() == tok::semi) {
440 Diag(SpecQualLoc, diag::w_no_declarators);
441 } else {
442 // Read struct-declarators until we find the semicolon.
443 while (1) {
444 /// struct-declarator: declarator
445 /// struct-declarator: declarator[opt] ':' constant-expression
446 if (Tok.getKind() != tok::colon)
447 ParseDeclarator(DeclaratorInfo);
448
449 if (Tok.getKind() == tok::colon) {
450 ConsumeToken();
451 ExprResult Res = ParseConstantExpression();
452 if (Res.isInvalid) {
453 SkipUntil(tok::semi, true, true);
454 } else {
455 // Process it.
456 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000457 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000458
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000459 // TODO: install declarator.
460
461 // If we don't have a comma, it is either the end of the list (a ';')
462 // or an error, bail out.
463 if (Tok.getKind() != tok::comma)
464 break;
465
466 // Consume the comma.
467 ConsumeToken();
468
469 // Parse the next declarator.
470 DeclaratorInfo.clear();
471 }
472 }
473
474 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000475 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000476 } else {
477 Diag(Tok, diag::err_expected_semi_decl_list);
478 // Skip to end of block or statement
479 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000480 }
481 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000482
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000483 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace);
484 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000485
486 const char *PrevSpec = 0;
487 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
488 PrevSpec))
489 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000490}
491
492
Chris Lattner3b561a32006-08-13 00:12:11 +0000493/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000494/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000495/// 'enum' identifier[opt] '{' enumerator-list '}'
496/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000497/// [GNU] 'enum' identifier[opt] '{' enumerator-list '}' attributes [TODO]
498/// [GNU] 'enum' identifier[opt] '{' enumerator-list ',' '}' attributes [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000499/// 'enum' identifier
500/// enumerator-list:
501/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000502/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000503/// enumerator:
504/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000505/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000506/// enumeration-constant:
507/// identifier
508///
509void Parser::ParseEnumSpecifier(DeclSpec &DS) {
510 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000511 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000512 ConsumeToken();
513
514 // Must have either 'enum name' or 'enum {...}'.
515 if (Tok.getKind() != tok::identifier &&
516 Tok.getKind() != tok::l_brace) {
517 Diag(Tok, diag::err_expected_ident_lbrace);
518 return;
519 }
520
521 if (Tok.getKind() == tok::identifier)
522 ConsumeToken();
523
Chris Lattner0fb8b362006-08-14 01:30:12 +0000524 if (Tok.getKind() == tok::l_brace) {
525 SourceLocation LBraceLoc = Tok.getLocation();
526 ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000527
Chris Lattner0fb8b362006-08-14 01:30:12 +0000528 if (Tok.getKind() == tok::r_brace)
529 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
530
531 // Parse the enumerator-list.
532 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000533 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000534
535 if (Tok.getKind() == tok::equal) {
536 ConsumeToken();
537 ExprResult Res = ParseConstantExpression();
538 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
539 }
540
541 if (Tok.getKind() != tok::comma)
542 break;
543 SourceLocation CommaLoc = Tok.getLocation();
544 ConsumeToken();
545
546 if (Tok.getKind() != tok::identifier && !getLang().C99)
547 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000548 }
549
Chris Lattner0fb8b362006-08-14 01:30:12 +0000550 // Eat the }.
551 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
552 diag::err_expected_rbrace);
Chris Lattner3b561a32006-08-13 00:12:11 +0000553 }
554 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000555
556
557 const char *PrevSpec = 0;
558 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
559 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000560}
561
562
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000563/// isTypeSpecifierQualifier - Return true if the current token could be the
564/// start of a specifier-qualifier-list.
565bool Parser::isTypeSpecifierQualifier() const {
566 switch (Tok.getKind()) {
567 default: return false;
568 // type-specifiers
569 case tok::kw_short:
570 case tok::kw_long:
571 case tok::kw_signed:
572 case tok::kw_unsigned:
573 case tok::kw__Complex:
574 case tok::kw__Imaginary:
575 case tok::kw_void:
576 case tok::kw_char:
577 case tok::kw_int:
578 case tok::kw_float:
579 case tok::kw_double:
580 case tok::kw__Bool:
581 case tok::kw__Decimal32:
582 case tok::kw__Decimal64:
583 case tok::kw__Decimal128:
584
585 // struct-or-union-specifier
586 case tok::kw_struct:
587 case tok::kw_union:
588 // enum-specifier
589 case tok::kw_enum:
590
591 // type-qualifier
592 case tok::kw_const:
593 case tok::kw_volatile:
594 case tok::kw_restrict:
595 return true;
596
597 // typedef-name
598 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000599 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000600
601 // TODO: Attributes.
602 }
603}
604
Chris Lattneracd58a32006-08-06 17:24:14 +0000605/// isDeclarationSpecifier() - Return true if the current token is part of a
606/// declaration specifier.
607bool Parser::isDeclarationSpecifier() const {
608 switch (Tok.getKind()) {
609 default: return false;
610 // storage-class-specifier
611 case tok::kw_typedef:
612 case tok::kw_extern:
613 case tok::kw_static:
614 case tok::kw_auto:
615 case tok::kw_register:
616 case tok::kw___thread:
617
618 // type-specifiers
619 case tok::kw_short:
620 case tok::kw_long:
621 case tok::kw_signed:
622 case tok::kw_unsigned:
623 case tok::kw__Complex:
624 case tok::kw__Imaginary:
625 case tok::kw_void:
626 case tok::kw_char:
627 case tok::kw_int:
628 case tok::kw_float:
629 case tok::kw_double:
630 case tok::kw__Bool:
631 case tok::kw__Decimal32:
632 case tok::kw__Decimal64:
633 case tok::kw__Decimal128:
634
635 // struct-or-union-specifier
636 case tok::kw_struct:
637 case tok::kw_union:
638 // enum-specifier
639 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000640
Chris Lattneracd58a32006-08-06 17:24:14 +0000641 // type-qualifier
642 case tok::kw_const:
643 case tok::kw_volatile:
644 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000645
Chris Lattneracd58a32006-08-06 17:24:14 +0000646 // function-specifier
647 case tok::kw_inline:
648 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000649
Chris Lattneracd58a32006-08-06 17:24:14 +0000650 // typedef-name
651 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000652 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000653 // TODO: Attributes.
654 }
655}
656
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000657
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000658/// ParseTypeQualifierListOpt
659/// type-qualifier-list: [C99 6.7.5]
660/// type-qualifier
661/// [GNU] attributes [TODO]
662/// type-qualifier-list type-qualifier
663/// [GNU] type-qualifier-list attributes [TODO]
664///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000665void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
666 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000667 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000668 int isInvalid = false;
669 const char *PrevSpec = 0;
670
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000671 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000672 default:
673 // If this is not a declaration specifier token, we're done reading decl
674 // specifiers. First verify that DeclSpec's are consistent.
675 DS.Finish(StartLoc, Diags, getLang());
676 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000677 // TODO: attributes.
678 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000679 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
680 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000681 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000682 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
683 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000684 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000685 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000686 break;
687 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000688
689 // If the specifier combination wasn't legal, issue a diagnostic.
690 if (isInvalid) {
691 assert(PrevSpec && "Method did not return previous specifier!");
692 if (isInvalid == 1) // Error.
693 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
694 else // extwarn.
695 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
696 }
697 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000698 }
699}
700
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000701
702/// ParseDeclarator - Parse and verify a newly-initialized declarator.
703///
704void Parser::ParseDeclarator(Declarator &D) {
705 /// This implements the 'declarator' production in the C grammar, then checks
706 /// for well-formedness and issues diagnostics.
707 ParseDeclaratorInternal(D);
708
Chris Lattner9fab3b92006-08-12 18:25:42 +0000709 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000710
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000711}
712
713/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000714/// declarator: [C99 6.7.5]
715/// pointer[opt] direct-declarator
716///
717/// pointer: [C99 6.7.5]
718/// '*' type-qualifier-list[opt]
719/// '*' type-qualifier-list[opt] pointer
720///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000721void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000722 if (Tok.getKind() != tok::star)
723 return ParseDirectDeclarator(D);
724
725 // Otherwise, '*' -> pointer.
726 SourceLocation Loc = Tok.getLocation();
727 ConsumeToken(); // Eat the *.
728 DeclSpec DS;
729 ParseTypeQualifierListOpt(DS);
730
731 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000732 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000733
734 // Remember that we parsed a pointer type, and remember the type-quals.
735 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
736}
737
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000738
739/// ParseDirectDeclarator
740/// direct-declarator: [C99 6.7.5]
741/// identifier
742/// '(' declarator ')'
743/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000744/// [C90] direct-declarator '[' constant-expression[opt] ']'
745/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
746/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
747/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
748/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000749/// direct-declarator '(' parameter-type-list ')'
750/// direct-declarator '(' identifier-list[opt] ')'
751/// [GNU] direct-declarator '(' parameter-forward-declarations
752/// parameter-type-list[opt] ')'
753///
Chris Lattneracd58a32006-08-06 17:24:14 +0000754void Parser::ParseDirectDeclarator(Declarator &D) {
755 // Parse the first direct-declarator seen.
756 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
757 assert(Tok.getIdentifierInfo() && "Not an identifier?");
758 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
759 ConsumeToken();
760 } else if (Tok.getKind() == tok::l_paren) {
761 // direct-declarator: '(' declarator ')'
762 // direct-declarator: '(' attributes declarator ')' [TODO]
763 // Example: 'char (*X)' or 'int (*XX)(void)'
764 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000765 } else if (D.mayOmitIdentifier()) {
766 // This could be something simple like "int" (in which case the declarator
767 // portion is empty), if an abstract-declarator is allowed.
768 D.SetIdentifier(0, Tok.getLocation());
769 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000770 // Expected identifier or '('.
771 Diag(Tok, diag::err_expected_ident_lparen);
772 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000773 }
774
775 assert(D.isPastIdentifier() &&
776 "Haven't past the location of the identifier yet?");
777
778 while (1) {
779 if (Tok.getKind() == tok::l_paren) {
780 ParseParenDeclarator(D);
781 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000782 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000783 } else {
784 break;
785 }
786 }
787}
788
789/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
790/// either be before the identifier (in which case these are just grouping
791/// parens for precedence) or it may be after the identifier, in which case
792/// these are function arguments.
793///
794/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000795/// parameter-type-list: [C99 6.7.5]
796/// parameter-list
797/// parameter-list ',' '...'
798///
799/// parameter-list: [C99 6.7.5]
800/// parameter-declaration
801/// parameter-list ',' parameter-declaration
802///
803/// parameter-declaration: [C99 6.7.5]
804/// declaration-specifiers declarator
Chris Lattneracd58a32006-08-06 17:24:14 +0000805/// [GNU] declaration-specifiers declarator attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000806/// declaration-specifiers abstract-declarator[opt]
Chris Lattneracd58a32006-08-06 17:24:14 +0000807/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000808///
809/// identifier-list: [C99 6.7.5]
810/// identifier
811/// identifier-list ',' identifier
812///
Chris Lattneracd58a32006-08-06 17:24:14 +0000813void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000814 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000815 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000816
Chris Lattneracd58a32006-08-06 17:24:14 +0000817 // If we haven't past the identifier yet (or where the identifier would be
818 // stored, if this is an abstract declarator), then this is probably just
819 // grouping parens.
820 if (!D.isPastIdentifier()) {
821 // Okay, this is probably a grouping paren. However, if this could be an
822 // abstract-declarator, then this could also be the start of function
823 // arguments (consider 'void()').
824 bool isGrouping;
825
826 if (!D.mayOmitIdentifier()) {
827 // If this can't be an abstract-declarator, this *must* be a grouping
828 // paren, because we haven't seen the identifier yet.
829 isGrouping = true;
830 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
831 isDeclarationSpecifier()) { // 'int(int)' is a function.
832
833 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000834 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000835 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000836 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000837 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000838
839 // If this is a grouping paren, handle:
840 // direct-declarator: '(' declarator ')'
841 // direct-declarator: '(' attributes declarator ')' [TODO]
842 if (isGrouping) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000843 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000844 // Match the ')'.
845 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
846 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000847 return;
848 }
849
850 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000851 // argument list. Recognize that this declarator will never have an
852 // identifier (and remember where it would have been), then fall through to
853 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000854 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000855 }
856
Chris Lattneracd58a32006-08-06 17:24:14 +0000857 // Okay, this is the parameter list of a function definition, or it is an
858 // identifier list of a K&R-style function.
859
Chris Lattner9fab3b92006-08-12 18:25:42 +0000860 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000861 // arguments to the function scope.
862 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000863 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000864 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000865 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000866 bool ErrorEmitted = false;
867
Chris Lattneracd58a32006-08-06 17:24:14 +0000868 if (Tok.getKind() == tok::r_paren) {
869 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000870 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000871 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000872 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000873 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner8a3e9182006-08-14 15:44:00 +0000874 !Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000875 // Identifier list. Note that '(' identifier-list ')' is only allowed for
876 // normal declarators, not for abstract-declarators.
877 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
878
879 // If there was no identifier specified, either we are in an
880 // abstract-declarator, or we are in a parameter declarator which was found
881 // to be abstract. In abstract-declarators, identifier lists are not valid,
882 // diagnose this.
883 if (!D.getIdentifier())
884 Diag(Tok, diag::ext_ident_list_in_param);
885
Chris Lattner9fab3b92006-08-12 18:25:42 +0000886 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000887 ConsumeToken();
888 while (Tok.getKind() == tok::comma) {
889 // Eat the comma.
890 ConsumeToken();
891
Chris Lattner0be454e2006-08-12 19:30:51 +0000892 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000893 ErrorEmitted = true;
894 break;
895 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000896 }
897
Chris Lattneracd58a32006-08-06 17:24:14 +0000898 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000899 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000900 HasPrototype = false;
901 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000902 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000903 bool ReadArg = false;
904 // Finally, a normal, non-empty parameter type list.
905 while (1) {
906 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000907 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000908
909 // Check to see if this is "void(...)" which is not allowed.
910 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000911 // Otherwise, parse parameter type list. If it starts with an
912 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000913 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000914 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000915 }
916
917 // Consume the ellipsis.
918 ConsumeToken();
919 break;
920 }
921
922 ReadArg = true;
923
924 // Parse the declaration-specifiers.
925 DeclSpec DS;
926 ParseDeclarationSpecifiers(DS);
927
928 // Parse the declarator. This is "PrototypeContext", because we must
929 // accept either 'declarator' or 'abstract-declarator' here.
930 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
931 ParseDeclarator(DeclaratorInfo);
932
933 // TODO: do something with the declarator, if it is valid.
934
935 // If the next token is a comma, consume it and keep reading arguments.
936 if (Tok.getKind() != tok::comma) break;
937
938 // Consume the comma.
939 ConsumeToken();
940 }
941
942 HasPrototype = true;
943 }
944
Chris Lattner9fab3b92006-08-12 18:25:42 +0000945 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000946
Chris Lattner9fab3b92006-08-12 18:25:42 +0000947 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000948
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000949 // Remember that we parsed a function type, and remember the attributes.
950 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000951 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000952
Chris Lattner14776b92006-08-06 22:27:40 +0000953
954 // If we have the closing ')', eat it and we're done.
955 if (Tok.getKind() == tok::r_paren) {
956 ConsumeParen();
957 } else {
958 // If an error happened earlier parsing something else in the proto, don't
959 // issue another error.
960 if (!ErrorEmitted)
961 Diag(Tok, diag::err_expected_rparen);
962 SkipUntil(tok::r_paren);
963 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000964}
Chris Lattneracd58a32006-08-06 17:24:14 +0000965
Chris Lattnere8074e62006-08-06 18:30:15 +0000966
967/// [C90] direct-declarator '[' constant-expression[opt] ']'
968/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
969/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
970/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
971/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
972void Parser::ParseBracketDeclarator(Declarator &D) {
973 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +0000974 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +0000975
976 // If valid, this location is the position where we read the 'static' keyword.
977 SourceLocation StaticLoc;
978 if (Tok.getKind() == tok::kw_static) {
979 StaticLoc = Tok.getLocation();
980 ConsumeToken();
981 }
982
983 // If there is a type-qualifier-list, read it now.
984 DeclSpec DS;
985 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +0000986
987 // If we haven't already read 'static', check to see if there is one after the
988 // type-qualifier-list.
989 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
990 StaticLoc = Tok.getLocation();
991 ConsumeToken();
992 }
993
994 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +0000995 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +0000996 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +0000997 if (Tok.getKind() == tok::star) {
998 // Remember the '*' token, in case we have to un-get it.
999 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001000 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001001
1002 // Check that the ']' token is present to avoid incorrectly parsing
1003 // expressions starting with '*' as [*].
1004 if (Tok.getKind() == tok::r_square) {
1005 if (StaticLoc.isValid())
1006 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1007 StaticLoc = SourceLocation(); // Drop the static.
1008 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001009 } else {
1010 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001011 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001012 // need to reparse it. This handles cases like 'X[*p + 4]'
1013 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001014 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001015 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001016 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001017 NumElements = ParseAssignmentExpression();
1018 }
1019
1020 // If there was an error parsing the assignment-expression, recover.
1021 if (NumElements.isInvalid) {
1022 // If the expression was invalid, skip it.
1023 SkipUntil(tok::r_square);
1024 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001025 }
1026
Chris Lattner9fab3b92006-08-12 18:25:42 +00001027 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
1028
Chris Lattnere8074e62006-08-06 18:30:15 +00001029 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1030 // it was not a constant expression.
1031 if (!getLang().C99) {
1032 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001033 if (isStar || StaticLoc.isValid() ||
1034 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001035 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001036 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001037
1038 // Remember that we parsed a pointer type, and remember the type-quals.
1039 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1040 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001041 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001042}
1043