blob: b0ea7d0be8d31cafda87db05cb3943f4ae235b1b [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]
Chris Lattnere550a4e2006-08-24 06:37:51 +000026Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027 // 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);
Chris Lattnere550a4e2006-08-24 06:37:51 +000034
35 // TODO: Return something useful as the type, obtained from actions.
36 return 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037}
38
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000039/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
54/// any-word
55/// any-word '(' identifier ')'
56/// any-word '(' identifier ',' nonempty-expr-list ')'
57/// any-word '(' expr-list ')'
58///
59void Parser::ParseAttributes() {
60 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
61 ConsumeToken();
62
63 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
64 "attribute"))
65 return;
66
67 // TODO: Parse the attributes.
68 SkipUntil(tok::r_paren, false);
69}
70
Chris Lattnerf5fbd792006-08-10 23:56:11 +000071
Chris Lattner53361ac2006-08-10 05:19:57 +000072/// ParseDeclaration - Parse a full 'declaration', which consists of
73/// declaration-specifiers, some number of declarators, and a semicolon.
74/// 'Context' should be a Declarator::TheContext value.
75void Parser::ParseDeclaration(unsigned Context) {
76 // Parse the common declaration-specifiers piece.
77 DeclSpec DS;
78 ParseDeclarationSpecifiers(DS);
79
Chris Lattner0e894622006-08-13 19:58:17 +000080 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
81 // declaration-specifiers init-declarator-list[opt] ';'
82 if (Tok.getKind() == tok::semi) {
83 // TODO: emit error on 'int;' or 'const enum foo;'.
84 // if (!DS.isMissingDeclaratorOk()) Diag(...);
85
86 ConsumeToken();
87 return;
88 }
89
Chris Lattner53361ac2006-08-10 05:19:57 +000090 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
91 ParseDeclarator(DeclaratorInfo);
92
93 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
94}
95
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000096/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
97/// parsing 'declaration-specifiers declarator'. This method is split out this
98/// way to handle the ambiguity between top-level function-definitions and
99/// declarations.
100///
101/// declaration: [C99 6.7]
102/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
103/// [!C99] init-declarator-list ';' [TODO]
104/// [OMP] threadprivate-directive [TODO]
105///
106/// init-declarator-list: [C99 6.7]
107/// init-declarator
108/// init-declarator-list ',' init-declarator
109/// init-declarator: [C99 6.7]
110/// declarator
111/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000112/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
113/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000114///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000115Parser::DeclTy *Parser::
116ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
117
118 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
119 // that they can be chained properly if the actions want this.
120 Parser::DeclTy *LastDeclInGroup = 0;
121
Chris Lattner53361ac2006-08-10 05:19:57 +0000122 // At this point, we know that it is not a function definition. Parse the
123 // rest of the init-declarator-list.
124 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000125 // If a simple-asm-expr is present, parse it.
126 if (Tok.getKind() == tok::kw_asm)
127 ParseSimpleAsm();
128
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000129 // If attributes are present, parse them.
130 if (Tok.getKind() == tok::kw___attribute)
131 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000132
Chris Lattner53361ac2006-08-10 05:19:57 +0000133 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000134 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000135 if (Tok.getKind() == tok::equal) {
136 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000137 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000138 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000139 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000140 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000141 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000142 }
143
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000144 // Inform the current actions module that we just parsed a declarator.
Chris Lattnera11999d2006-10-15 22:34:45 +0000145 // TODO: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000146 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
147 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000148
149 // If we don't have a comma, it is either the end of the list (a ';') or an
150 // error, bail out.
151 if (Tok.getKind() != tok::comma)
152 break;
153
154 // Consume the comma.
155 ConsumeToken();
156
157 // Parse the next declarator.
158 D.clear();
159 ParseDeclarator(D);
160 }
161
162 if (Tok.getKind() == tok::semi) {
163 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000164 return LastDeclInGroup;
Chris Lattner53361ac2006-08-10 05:19:57 +0000165 } else {
166 Diag(Tok, diag::err_parse_error);
167 // Skip to end of block or statement
168 SkipUntil(tok::r_brace, true);
169 if (Tok.getKind() == tok::semi)
170 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000171 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000172 }
173}
174
Chris Lattner1890ac82006-08-13 01:16:23 +0000175/// ParseSpecifierQualifierList
176/// specifier-qualifier-list:
177/// type-specifier specifier-qualifier-list[opt]
178/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000179/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000180///
181void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
182 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
183 /// parse declaration-specifiers and complain about extra stuff.
184 SourceLocation Loc = Tok.getLocation();
185 ParseDeclarationSpecifiers(DS);
186
187 // Validate declspec for type-name.
188 unsigned Specs = DS.getParsedSpecifiers();
189 if (Specs == DeclSpec::PQ_None)
190 Diag(Tok, diag::err_typename_requires_specqual);
191
192 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
193 Diag(Loc, diag::err_typename_invalid_storageclass);
194 // Remove storage class.
195 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
196 DS.SCS_thread_specified = false;
197 }
198 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
199 Diag(Loc, diag::err_typename_invalid_functionspec);
200 DS.FS_inline_specified = false;
201 }
202}
Chris Lattner53361ac2006-08-10 05:19:57 +0000203
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000204/// ParseDeclarationSpecifiers
205/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000206/// storage-class-specifier declaration-specifiers[opt]
207/// type-specifier declaration-specifiers[opt]
208/// type-qualifier declaration-specifiers[opt]
209/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000210/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000211///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000212/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000213/// 'typedef'
214/// 'extern'
215/// 'static'
216/// 'auto'
217/// 'register'
218/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000219/// type-specifier: [C99 6.7.2]
220/// 'void'
221/// 'char'
222/// 'short'
223/// 'int'
224/// 'long'
225/// 'float'
226/// 'double'
227/// 'signed'
228/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000229/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000230/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000231/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000232/// [C99] '_Bool'
233/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000234/// [C99] '_Imaginary' // Removed in TC2?
235/// [GNU] '_Decimal32'
236/// [GNU] '_Decimal64'
237/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000238/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000239/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000240/// [OBJC] typedef-name objc-protocol-refs [TODO]
241/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000242/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000243/// 'const'
244/// 'volatile'
245/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000246/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000247/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000248///
249void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
250 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000251 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000252 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000253 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000254 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000255 // typedef-name
256 case tok::identifier:
257 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000258 // a type-specifier. Without this check we misparse:
259 // typedef int X; struct Y { short X; }; as 'short int'.
260 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
261 DS.TypeSpecWidth == DeclSpec::TSW_unspecified &&
262 DS.TypeSpecComplex == DeclSpec::TSC_unspecified &&
263 DS.TypeSpecSign == DeclSpec::TSS_unspecified &&
264 // It has to be available as a typedef too!
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000265 Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
267 break;
268 }
269 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000270 default:
271 // If this is not a declaration specifier token, we're done reading decl
272 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000273 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000274 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000275
276 // GNU attributes support.
277 case tok::kw___attribute:
278 ParseAttributes();
279 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000280
281 // storage-class-specifier
282 case tok::kw_typedef:
283 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
284 break;
285 case tok::kw_extern:
286 if (DS.SCS_thread_specified)
287 Diag(Tok, diag::ext_thread_before, "extern");
288 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
289 break;
290 case tok::kw_static:
291 if (DS.SCS_thread_specified)
292 Diag(Tok, diag::ext_thread_before, "static");
293 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
294 break;
295 case tok::kw_auto:
296 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
297 break;
298 case tok::kw_register:
299 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
300 break;
301 case tok::kw___thread:
302 if (DS.SCS_thread_specified)
303 isInvalid = 2, PrevSpec = "__thread";
304 else
305 DS.SCS_thread_specified = true;
306 break;
307
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000308 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000309 case tok::kw_short:
310 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
311 break;
312 case tok::kw_long:
313 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
314 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
315 } else {
316 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
317 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
318 }
319 break;
320 case tok::kw_signed:
321 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
322 break;
323 case tok::kw_unsigned:
324 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
325 break;
326 case tok::kw__Complex:
327 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
328 break;
329 case tok::kw__Imaginary:
330 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
331 break;
332 case tok::kw_void:
333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
334 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000335 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
337 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000338 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000339 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
340 break;
341 case tok::kw_float:
342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
343 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000344 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
346 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000347 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
349 break;
350 case tok::kw__Decimal32:
351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
352 break;
353 case tok::kw__Decimal64:
354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
355 break;
356 case tok::kw__Decimal128:
357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000358 break;
359
Chris Lattner1890ac82006-08-13 01:16:23 +0000360 case tok::kw_struct:
361 case tok::kw_union:
362 ParseStructUnionSpecifier(DS);
363 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000364 case tok::kw_enum:
365 ParseEnumSpecifier(DS);
366 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000367
368 // type-qualifier
369 case tok::kw_const:
370 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
371 break;
372 case tok::kw_volatile:
373 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
374 break;
375 case tok::kw_restrict:
376 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
377 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000378
379 // function-specifier
380 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000381 // 'inline inline' is ok.
382 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000383 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000384 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000385 // If the specifier combination wasn't legal, issue a diagnostic.
386 if (isInvalid) {
387 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000388 if (isInvalid == 1) // Error.
389 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
390 else // extwarn.
391 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000392 }
393 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000394 }
395}
396
Chris Lattner1890ac82006-08-13 01:16:23 +0000397
398/// ParseStructUnionSpecifier
399/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000400/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000401/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000402/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
403/// '}' attributes[opt]
404/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000405/// struct-or-union:
406/// 'struct'
407/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000408/// struct-contents:
409/// struct-declaration-list
410/// [EXT] empty
411/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000412/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000413/// struct-declaration
414/// struct-declaration-list struct-declaration
415/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
416/// struct-declaration:
417/// specifier-qualifier-list struct-declarator-list ';'
418/// [GNU] __extension__ struct-declaration [TODO]
419/// [GNU] specifier-qualifier-list ';' [TODO]
420/// struct-declarator-list:
421/// struct-declarator
422/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000423/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000424/// struct-declarator:
425/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000426/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000427/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000428/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000429///
430void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
431 assert((Tok.getKind() == tok::kw_struct ||
432 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000433 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000434 bool isUnion = Tok.getKind() == tok::kw_union;
435 ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000436
437 // If attributes exist after tag, parse them.
438 if (Tok.getKind() == tok::kw___attribute)
439 ParseAttributes();
440
Chris Lattner1890ac82006-08-13 01:16:23 +0000441 // Must have either 'struct name' or 'struct {...}'.
442 if (Tok.getKind() != tok::identifier &&
443 Tok.getKind() != tok::l_brace) {
444 Diag(Tok, diag::err_expected_ident_lbrace);
445 return;
446 }
447
448 if (Tok.getKind() == tok::identifier)
449 ConsumeToken();
450
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000451 if (Tok.getKind() == tok::l_brace) {
452 SourceLocation LBraceLoc = Tok.getLocation();
453 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000454
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000455 if (Tok.getKind() == tok::r_brace)
456 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000457
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000458 while (Tok.getKind() != tok::r_brace &&
459 Tok.getKind() != tok::eof) {
460 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000461
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000462 // Parse the common specifier-qualifiers-list piece.
463 DeclSpec DS;
464 SourceLocation SpecQualLoc = Tok.getLocation();
465 ParseSpecifierQualifierList(DS);
466 // TODO: Does specifier-qualifier list correctly check that *something* is
467 // specified?
468
469 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000470
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000471 // If there are no declarators, issue a warning.
472 if (Tok.getKind() == tok::semi) {
473 Diag(SpecQualLoc, diag::w_no_declarators);
474 } else {
475 // Read struct-declarators until we find the semicolon.
476 while (1) {
477 /// struct-declarator: declarator
478 /// struct-declarator: declarator[opt] ':' constant-expression
479 if (Tok.getKind() != tok::colon)
480 ParseDeclarator(DeclaratorInfo);
481
482 if (Tok.getKind() == tok::colon) {
483 ConsumeToken();
484 ExprResult Res = ParseConstantExpression();
485 if (Res.isInvalid) {
486 SkipUntil(tok::semi, true, true);
487 } else {
488 // Process it.
489 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000490 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000491
492 // If attributes exist after the declarator, parse them.
493 if (Tok.getKind() == tok::kw___attribute)
494 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000495
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000496 // TODO: install declarator.
497
498 // If we don't have a comma, it is either the end of the list (a ';')
499 // or an error, bail out.
500 if (Tok.getKind() != tok::comma)
501 break;
502
503 // Consume the comma.
504 ConsumeToken();
505
506 // Parse the next declarator.
507 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000508
509 // Attributes are only allowed on the second declarator.
510 if (Tok.getKind() == tok::kw___attribute)
511 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000512 }
513 }
514
515 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000516 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000517 } else {
518 Diag(Tok, diag::err_expected_semi_decl_list);
519 // Skip to end of block or statement
520 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000521 }
522 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000523
Chris Lattner04f80192006-08-15 04:55:54 +0000524 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000525
526 // If attributes exist after struct contents, parse them.
527 if (Tok.getKind() == tok::kw___attribute)
528 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000529 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000530
531 const char *PrevSpec = 0;
532 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
533 PrevSpec))
534 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000535}
536
537
Chris Lattner3b561a32006-08-13 00:12:11 +0000538/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000539/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000540/// 'enum' identifier[opt] '{' enumerator-list '}'
541/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000542/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
543/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000544/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000545/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000546/// enumerator-list:
547/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000548/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000549/// enumerator:
550/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000551/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000552/// enumeration-constant:
553/// identifier
554///
555void Parser::ParseEnumSpecifier(DeclSpec &DS) {
556 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000557 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000558 ConsumeToken();
559
Chris Lattnere37e2332006-08-15 04:50:22 +0000560 if (Tok.getKind() == tok::kw___attribute)
561 ParseAttributes();
562
Chris Lattner3b561a32006-08-13 00:12:11 +0000563 // Must have either 'enum name' or 'enum {...}'.
564 if (Tok.getKind() != tok::identifier &&
565 Tok.getKind() != tok::l_brace) {
566 Diag(Tok, diag::err_expected_ident_lbrace);
567 return;
568 }
569
570 if (Tok.getKind() == tok::identifier)
571 ConsumeToken();
572
Chris Lattner0fb8b362006-08-14 01:30:12 +0000573 if (Tok.getKind() == tok::l_brace) {
574 SourceLocation LBraceLoc = Tok.getLocation();
575 ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000576
Chris Lattner0fb8b362006-08-14 01:30:12 +0000577 if (Tok.getKind() == tok::r_brace)
578 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
579
580 // Parse the enumerator-list.
581 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000582 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000583
584 if (Tok.getKind() == tok::equal) {
585 ConsumeToken();
586 ExprResult Res = ParseConstantExpression();
587 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
588 }
589
590 if (Tok.getKind() != tok::comma)
591 break;
592 SourceLocation CommaLoc = Tok.getLocation();
593 ConsumeToken();
594
595 if (Tok.getKind() != tok::identifier && !getLang().C99)
596 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000597 }
598
Chris Lattner0fb8b362006-08-14 01:30:12 +0000599 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000600 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000601
602 // If attributes exist after the identifier list, parse them.
603 if (Tok.getKind() == tok::kw___attribute)
604 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000605 }
606 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000607
608
609 const char *PrevSpec = 0;
610 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
611 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000612}
613
614
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000615/// isTypeSpecifierQualifier - Return true if the current token could be the
616/// start of a specifier-qualifier-list.
617bool Parser::isTypeSpecifierQualifier() const {
618 switch (Tok.getKind()) {
619 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000620 // GNU attributes support.
621 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000622 // type-specifiers
623 case tok::kw_short:
624 case tok::kw_long:
625 case tok::kw_signed:
626 case tok::kw_unsigned:
627 case tok::kw__Complex:
628 case tok::kw__Imaginary:
629 case tok::kw_void:
630 case tok::kw_char:
631 case tok::kw_int:
632 case tok::kw_float:
633 case tok::kw_double:
634 case tok::kw__Bool:
635 case tok::kw__Decimal32:
636 case tok::kw__Decimal64:
637 case tok::kw__Decimal128:
638
639 // struct-or-union-specifier
640 case tok::kw_struct:
641 case tok::kw_union:
642 // enum-specifier
643 case tok::kw_enum:
644
645 // type-qualifier
646 case tok::kw_const:
647 case tok::kw_volatile:
648 case tok::kw_restrict:
649 return true;
650
651 // typedef-name
652 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000653 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000654
655 // TODO: Attributes.
656 }
657}
658
Chris Lattneracd58a32006-08-06 17:24:14 +0000659/// isDeclarationSpecifier() - Return true if the current token is part of a
660/// declaration specifier.
661bool Parser::isDeclarationSpecifier() const {
662 switch (Tok.getKind()) {
663 default: return false;
664 // storage-class-specifier
665 case tok::kw_typedef:
666 case tok::kw_extern:
667 case tok::kw_static:
668 case tok::kw_auto:
669 case tok::kw_register:
670 case tok::kw___thread:
671
672 // type-specifiers
673 case tok::kw_short:
674 case tok::kw_long:
675 case tok::kw_signed:
676 case tok::kw_unsigned:
677 case tok::kw__Complex:
678 case tok::kw__Imaginary:
679 case tok::kw_void:
680 case tok::kw_char:
681 case tok::kw_int:
682 case tok::kw_float:
683 case tok::kw_double:
684 case tok::kw__Bool:
685 case tok::kw__Decimal32:
686 case tok::kw__Decimal64:
687 case tok::kw__Decimal128:
688
689 // struct-or-union-specifier
690 case tok::kw_struct:
691 case tok::kw_union:
692 // enum-specifier
693 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000694
Chris Lattneracd58a32006-08-06 17:24:14 +0000695 // type-qualifier
696 case tok::kw_const:
697 case tok::kw_volatile:
698 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000699
Chris Lattneracd58a32006-08-06 17:24:14 +0000700 // function-specifier
701 case tok::kw_inline:
702 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000703
Chris Lattneracd58a32006-08-06 17:24:14 +0000704 // typedef-name
705 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000706 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000707 // TODO: Attributes.
708 }
709}
710
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000711
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000712/// ParseTypeQualifierListOpt
713/// type-qualifier-list: [C99 6.7.5]
714/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000715/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000716/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000717/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000718///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000719void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
720 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000721 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000722 int isInvalid = false;
723 const char *PrevSpec = 0;
724
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000725 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000726 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000727 // If this is not a type-qualifier token, we're done reading type
728 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000729 DS.Finish(StartLoc, Diags, getLang());
730 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000731 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000732 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
733 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000734 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000735 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
736 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000737 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000738 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000739 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000740
741 case tok::kw___attribute:
742 ParseAttributes();
743 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000744 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000745
746 // If the specifier combination wasn't legal, issue a diagnostic.
747 if (isInvalid) {
748 assert(PrevSpec && "Method did not return previous specifier!");
749 if (isInvalid == 1) // Error.
750 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
751 else // extwarn.
752 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
753 }
754 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000755 }
756}
757
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000758
759/// ParseDeclarator - Parse and verify a newly-initialized declarator.
760///
761void Parser::ParseDeclarator(Declarator &D) {
762 /// This implements the 'declarator' production in the C grammar, then checks
763 /// for well-formedness and issues diagnostics.
764 ParseDeclaratorInternal(D);
765
Chris Lattner9fab3b92006-08-12 18:25:42 +0000766 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000767
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000768}
769
770/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000771/// declarator: [C99 6.7.5]
772/// pointer[opt] direct-declarator
773///
774/// pointer: [C99 6.7.5]
775/// '*' type-qualifier-list[opt]
776/// '*' type-qualifier-list[opt] pointer
777///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000778void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000779 if (Tok.getKind() != tok::star)
780 return ParseDirectDeclarator(D);
781
782 // Otherwise, '*' -> pointer.
783 SourceLocation Loc = Tok.getLocation();
784 ConsumeToken(); // Eat the *.
785 DeclSpec DS;
786 ParseTypeQualifierListOpt(DS);
787
788 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000789 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000790
791 // Remember that we parsed a pointer type, and remember the type-quals.
792 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
793}
794
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000795
796/// ParseDirectDeclarator
797/// direct-declarator: [C99 6.7.5]
798/// identifier
799/// '(' declarator ')'
800/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000801/// [C90] direct-declarator '[' constant-expression[opt] ']'
802/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
803/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
804/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
805/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000806/// direct-declarator '(' parameter-type-list ')'
807/// direct-declarator '(' identifier-list[opt] ')'
808/// [GNU] direct-declarator '(' parameter-forward-declarations
809/// parameter-type-list[opt] ')'
810///
Chris Lattneracd58a32006-08-06 17:24:14 +0000811void Parser::ParseDirectDeclarator(Declarator &D) {
812 // Parse the first direct-declarator seen.
813 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
814 assert(Tok.getIdentifierInfo() && "Not an identifier?");
815 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
816 ConsumeToken();
817 } else if (Tok.getKind() == tok::l_paren) {
818 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000819 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000820 // Example: 'char (*X)' or 'int (*XX)(void)'
821 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000822 } else if (D.mayOmitIdentifier()) {
823 // This could be something simple like "int" (in which case the declarator
824 // portion is empty), if an abstract-declarator is allowed.
825 D.SetIdentifier(0, Tok.getLocation());
826 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000827 // Expected identifier or '('.
828 Diag(Tok, diag::err_expected_ident_lparen);
829 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000830 }
831
832 assert(D.isPastIdentifier() &&
833 "Haven't past the location of the identifier yet?");
834
835 while (1) {
836 if (Tok.getKind() == tok::l_paren) {
837 ParseParenDeclarator(D);
838 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000839 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000840 } else {
841 break;
842 }
843 }
844}
845
846/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
847/// either be before the identifier (in which case these are just grouping
848/// parens for precedence) or it may be after the identifier, in which case
849/// these are function arguments.
850///
851/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000852/// parameter-type-list: [C99 6.7.5]
853/// parameter-list
854/// parameter-list ',' '...'
855///
856/// parameter-list: [C99 6.7.5]
857/// parameter-declaration
858/// parameter-list ',' parameter-declaration
859///
860/// parameter-declaration: [C99 6.7.5]
861/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000862/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000863/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000864/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000865///
866/// identifier-list: [C99 6.7.5]
867/// identifier
868/// identifier-list ',' identifier
869///
Chris Lattneracd58a32006-08-06 17:24:14 +0000870void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000871 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000872 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000873
Chris Lattneracd58a32006-08-06 17:24:14 +0000874 // If we haven't past the identifier yet (or where the identifier would be
875 // stored, if this is an abstract declarator), then this is probably just
876 // grouping parens.
877 if (!D.isPastIdentifier()) {
878 // Okay, this is probably a grouping paren. However, if this could be an
879 // abstract-declarator, then this could also be the start of function
880 // arguments (consider 'void()').
881 bool isGrouping;
882
883 if (!D.mayOmitIdentifier()) {
884 // If this can't be an abstract-declarator, this *must* be a grouping
885 // paren, because we haven't seen the identifier yet.
886 isGrouping = true;
887 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
888 isDeclarationSpecifier()) { // 'int(int)' is a function.
889
890 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000891 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000892 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000893 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000894 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000895
896 // If this is a grouping paren, handle:
897 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000898 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000899 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000900 if (Tok.getKind() == tok::kw___attribute)
901 ParseAttributes();
902
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000903 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000904 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000905 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000906 return;
907 }
908
909 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000910 // argument list. Recognize that this declarator will never have an
911 // identifier (and remember where it would have been), then fall through to
912 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000913 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000914 }
915
Chris Lattneracd58a32006-08-06 17:24:14 +0000916 // Okay, this is the parameter list of a function definition, or it is an
917 // identifier list of a K&R-style function.
918
Chris Lattner9fab3b92006-08-12 18:25:42 +0000919 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000920 // arguments to the function scope.
921 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000922 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000923 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000924 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000925 bool ErrorEmitted = false;
926
Chris Lattneracd58a32006-08-06 17:24:14 +0000927 if (Tok.getKind() == tok::r_paren) {
928 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000929 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000930 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000931 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000932 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner8a3e9182006-08-14 15:44:00 +0000933 !Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000934 // Identifier list. Note that '(' identifier-list ')' is only allowed for
935 // normal declarators, not for abstract-declarators.
936 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
937
938 // If there was no identifier specified, either we are in an
939 // abstract-declarator, or we are in a parameter declarator which was found
940 // to be abstract. In abstract-declarators, identifier lists are not valid,
941 // diagnose this.
942 if (!D.getIdentifier())
943 Diag(Tok, diag::ext_ident_list_in_param);
944
Chris Lattner9fab3b92006-08-12 18:25:42 +0000945 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000946 ConsumeToken();
947 while (Tok.getKind() == tok::comma) {
948 // Eat the comma.
949 ConsumeToken();
950
Chris Lattner0be454e2006-08-12 19:30:51 +0000951 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000952 ErrorEmitted = true;
953 break;
954 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000955 }
956
Chris Lattneracd58a32006-08-06 17:24:14 +0000957 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000958 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000959 HasPrototype = false;
960 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000961 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000962 bool ReadArg = false;
963 // Finally, a normal, non-empty parameter type list.
964 while (1) {
965 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000966 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000967
968 // Check to see if this is "void(...)" which is not allowed.
969 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000970 // Otherwise, parse parameter type list. If it starts with an
971 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000972 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000973 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000974 }
975
976 // Consume the ellipsis.
977 ConsumeToken();
978 break;
979 }
980
981 ReadArg = true;
982
983 // Parse the declaration-specifiers.
984 DeclSpec DS;
985 ParseDeclarationSpecifiers(DS);
986
987 // Parse the declarator. This is "PrototypeContext", because we must
988 // accept either 'declarator' or 'abstract-declarator' here.
989 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
990 ParseDeclarator(DeclaratorInfo);
991
Chris Lattnere37e2332006-08-15 04:50:22 +0000992 // Parse GNU attributes, if present.
993 if (Tok.getKind() == tok::kw___attribute)
994 ParseAttributes();
995
Chris Lattneracd58a32006-08-06 17:24:14 +0000996 // TODO: do something with the declarator, if it is valid.
997
998 // If the next token is a comma, consume it and keep reading arguments.
999 if (Tok.getKind() != tok::comma) break;
1000
1001 // Consume the comma.
1002 ConsumeToken();
1003 }
1004
1005 HasPrototype = true;
1006 }
1007
Chris Lattner9fab3b92006-08-12 18:25:42 +00001008 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001009
Chris Lattner9fab3b92006-08-12 18:25:42 +00001010 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +00001011
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001012 // Remember that we parsed a function type, and remember the attributes.
1013 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001014 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001015
Chris Lattner14776b92006-08-06 22:27:40 +00001016
1017 // If we have the closing ')', eat it and we're done.
1018 if (Tok.getKind() == tok::r_paren) {
1019 ConsumeParen();
1020 } else {
1021 // If an error happened earlier parsing something else in the proto, don't
1022 // issue another error.
1023 if (!ErrorEmitted)
1024 Diag(Tok, diag::err_expected_rparen);
1025 SkipUntil(tok::r_paren);
1026 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001027}
Chris Lattneracd58a32006-08-06 17:24:14 +00001028
Chris Lattnere8074e62006-08-06 18:30:15 +00001029
1030/// [C90] direct-declarator '[' constant-expression[opt] ']'
1031/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1032/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1033/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1034/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1035void Parser::ParseBracketDeclarator(Declarator &D) {
1036 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +00001037 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001038
1039 // If valid, this location is the position where we read the 'static' keyword.
1040 SourceLocation StaticLoc;
1041 if (Tok.getKind() == tok::kw_static) {
1042 StaticLoc = Tok.getLocation();
1043 ConsumeToken();
1044 }
1045
1046 // If there is a type-qualifier-list, read it now.
1047 DeclSpec DS;
1048 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001049
1050 // If we haven't already read 'static', check to see if there is one after the
1051 // type-qualifier-list.
1052 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
1053 StaticLoc = Tok.getLocation();
1054 ConsumeToken();
1055 }
1056
1057 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001058 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001059 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001060 if (Tok.getKind() == tok::star) {
1061 // Remember the '*' token, in case we have to un-get it.
1062 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001063 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001064
1065 // Check that the ']' token is present to avoid incorrectly parsing
1066 // expressions starting with '*' as [*].
1067 if (Tok.getKind() == tok::r_square) {
1068 if (StaticLoc.isValid())
1069 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1070 StaticLoc = SourceLocation(); // Drop the static.
1071 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001072 } else {
1073 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001074 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001075 // need to reparse it. This handles cases like 'X[*p + 4]'
1076 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001077 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001078 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001079 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001080 NumElements = ParseAssignmentExpression();
1081 }
1082
1083 // If there was an error parsing the assignment-expression, recover.
1084 if (NumElements.isInvalid) {
1085 // If the expression was invalid, skip it.
1086 SkipUntil(tok::r_square);
1087 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001088 }
1089
Chris Lattner04f80192006-08-15 04:55:54 +00001090 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001091
Chris Lattnere8074e62006-08-06 18:30:15 +00001092 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1093 // it was not a constant expression.
1094 if (!getLang().C99) {
1095 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001096 if (isStar || StaticLoc.isValid() ||
1097 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001098 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001099 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001100
1101 // Remember that we parsed a pointer type, and remember the type-quals.
1102 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1103 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001104 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001105}
1106