blob: da1e455a9a8727ec5ce00c51c45bb336e1d4327e [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 Lattner53361ac2006-08-10 05:19:57 +0000115void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
116 // At this point, we know that it is not a function definition. Parse the
117 // rest of the init-declarator-list.
118 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000119 // If a simple-asm-expr is present, parse it.
120 if (Tok.getKind() == tok::kw_asm)
121 ParseSimpleAsm();
122
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000123 // If attributes are present, parse them.
124 if (Tok.getKind() == tok::kw___attribute)
125 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000126
Chris Lattner53361ac2006-08-10 05:19:57 +0000127 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000128 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000129 if (Tok.getKind() == tok::equal) {
130 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000131 Init = ParseInitializer();
132 if (!Init.isInvalid) {
133 SkipUntil(tok::semi);
134 return;
135 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000136 }
137
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000138 // Inform the current actions module that we just parsed a declarator.
139 Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000140
141 // If we don't have a comma, it is either the end of the list (a ';') or an
142 // error, bail out.
143 if (Tok.getKind() != tok::comma)
144 break;
145
146 // Consume the comma.
147 ConsumeToken();
148
149 // Parse the next declarator.
150 D.clear();
151 ParseDeclarator(D);
152 }
153
154 if (Tok.getKind() == tok::semi) {
155 ConsumeToken();
156 } else {
157 Diag(Tok, diag::err_parse_error);
158 // Skip to end of block or statement
159 SkipUntil(tok::r_brace, true);
160 if (Tok.getKind() == tok::semi)
161 ConsumeToken();
162 }
163}
164
Chris Lattner1890ac82006-08-13 01:16:23 +0000165/// ParseSpecifierQualifierList
166/// specifier-qualifier-list:
167/// type-specifier specifier-qualifier-list[opt]
168/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000169/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000170///
171void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
172 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
173 /// parse declaration-specifiers and complain about extra stuff.
174 SourceLocation Loc = Tok.getLocation();
175 ParseDeclarationSpecifiers(DS);
176
177 // Validate declspec for type-name.
178 unsigned Specs = DS.getParsedSpecifiers();
179 if (Specs == DeclSpec::PQ_None)
180 Diag(Tok, diag::err_typename_requires_specqual);
181
182 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
183 Diag(Loc, diag::err_typename_invalid_storageclass);
184 // Remove storage class.
185 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
186 DS.SCS_thread_specified = false;
187 }
188 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
189 Diag(Loc, diag::err_typename_invalid_functionspec);
190 DS.FS_inline_specified = false;
191 }
192}
Chris Lattner53361ac2006-08-10 05:19:57 +0000193
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000194/// ParseDeclarationSpecifiers
195/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000196/// storage-class-specifier declaration-specifiers[opt]
197/// type-specifier declaration-specifiers[opt]
198/// type-qualifier declaration-specifiers[opt]
199/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000200/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000201///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000202/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000203/// 'typedef'
204/// 'extern'
205/// 'static'
206/// 'auto'
207/// 'register'
208/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000209/// type-specifier: [C99 6.7.2]
210/// 'void'
211/// 'char'
212/// 'short'
213/// 'int'
214/// 'long'
215/// 'float'
216/// 'double'
217/// 'signed'
218/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000219/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000220/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000221/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000222/// [C99] '_Bool'
223/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000224/// [C99] '_Imaginary' // Removed in TC2?
225/// [GNU] '_Decimal32'
226/// [GNU] '_Decimal64'
227/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000228/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000229/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000230/// [OBJC] typedef-name objc-protocol-refs [TODO]
231/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000232/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000233/// 'const'
234/// 'volatile'
235/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000236/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000237/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000238///
239void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
240 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000241 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000242 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000243 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000244 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000245 // typedef-name
246 case tok::identifier:
247 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000248 // a type-specifier. Without this check we misparse:
249 // typedef int X; struct Y { short X; }; as 'short int'.
250 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
251 DS.TypeSpecWidth == DeclSpec::TSW_unspecified &&
252 DS.TypeSpecComplex == DeclSpec::TSC_unspecified &&
253 DS.TypeSpecSign == DeclSpec::TSS_unspecified &&
254 // It has to be available as a typedef too!
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000255 Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
256 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
257 break;
258 }
259 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000260 default:
261 // If this is not a declaration specifier token, we're done reading decl
262 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000263 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000264 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000265
266 // GNU attributes support.
267 case tok::kw___attribute:
268 ParseAttributes();
269 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000270
271 // storage-class-specifier
272 case tok::kw_typedef:
273 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
274 break;
275 case tok::kw_extern:
276 if (DS.SCS_thread_specified)
277 Diag(Tok, diag::ext_thread_before, "extern");
278 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
279 break;
280 case tok::kw_static:
281 if (DS.SCS_thread_specified)
282 Diag(Tok, diag::ext_thread_before, "static");
283 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
284 break;
285 case tok::kw_auto:
286 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
287 break;
288 case tok::kw_register:
289 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
290 break;
291 case tok::kw___thread:
292 if (DS.SCS_thread_specified)
293 isInvalid = 2, PrevSpec = "__thread";
294 else
295 DS.SCS_thread_specified = true;
296 break;
297
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000298 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000299 case tok::kw_short:
300 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
301 break;
302 case tok::kw_long:
303 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
304 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
305 } else {
306 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
307 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
308 }
309 break;
310 case tok::kw_signed:
311 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
312 break;
313 case tok::kw_unsigned:
314 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
315 break;
316 case tok::kw__Complex:
317 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
318 break;
319 case tok::kw__Imaginary:
320 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
321 break;
322 case tok::kw_void:
323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
324 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000325 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
327 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000328 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
330 break;
331 case tok::kw_float:
332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
333 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000334 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
336 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000337 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
339 break;
340 case tok::kw__Decimal32:
341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
342 break;
343 case tok::kw__Decimal64:
344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
345 break;
346 case tok::kw__Decimal128:
347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000348 break;
349
Chris Lattner1890ac82006-08-13 01:16:23 +0000350 case tok::kw_struct:
351 case tok::kw_union:
352 ParseStructUnionSpecifier(DS);
353 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000354 case tok::kw_enum:
355 ParseEnumSpecifier(DS);
356 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000357
358 // type-qualifier
359 case tok::kw_const:
360 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
361 break;
362 case tok::kw_volatile:
363 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
364 break;
365 case tok::kw_restrict:
366 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
367 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000368
369 // function-specifier
370 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000371 // 'inline inline' is ok.
372 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000373 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000374 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000375 // If the specifier combination wasn't legal, issue a diagnostic.
376 if (isInvalid) {
377 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000378 if (isInvalid == 1) // Error.
379 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
380 else // extwarn.
381 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000382 }
383 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000384 }
385}
386
Chris Lattner1890ac82006-08-13 01:16:23 +0000387
388/// ParseStructUnionSpecifier
389/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000390/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000391/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000392/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
393/// '}' attributes[opt]
394/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000395/// struct-or-union:
396/// 'struct'
397/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000398/// struct-contents:
399/// struct-declaration-list
400/// [EXT] empty
401/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000402/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000403/// struct-declaration
404/// struct-declaration-list struct-declaration
405/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
406/// struct-declaration:
407/// specifier-qualifier-list struct-declarator-list ';'
408/// [GNU] __extension__ struct-declaration [TODO]
409/// [GNU] specifier-qualifier-list ';' [TODO]
410/// struct-declarator-list:
411/// struct-declarator
412/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000413/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000414/// struct-declarator:
415/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000416/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000417/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000418/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000419///
420void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
421 assert((Tok.getKind() == tok::kw_struct ||
422 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000423 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000424 bool isUnion = Tok.getKind() == tok::kw_union;
425 ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000426
427 // If attributes exist after tag, parse them.
428 if (Tok.getKind() == tok::kw___attribute)
429 ParseAttributes();
430
Chris Lattner1890ac82006-08-13 01:16:23 +0000431 // Must have either 'struct name' or 'struct {...}'.
432 if (Tok.getKind() != tok::identifier &&
433 Tok.getKind() != tok::l_brace) {
434 Diag(Tok, diag::err_expected_ident_lbrace);
435 return;
436 }
437
438 if (Tok.getKind() == tok::identifier)
439 ConsumeToken();
440
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000441 if (Tok.getKind() == tok::l_brace) {
442 SourceLocation LBraceLoc = Tok.getLocation();
443 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000444
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000445 if (Tok.getKind() == tok::r_brace)
446 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000447
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000448 while (Tok.getKind() != tok::r_brace &&
449 Tok.getKind() != tok::eof) {
450 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000451
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000452 // Parse the common specifier-qualifiers-list piece.
453 DeclSpec DS;
454 SourceLocation SpecQualLoc = Tok.getLocation();
455 ParseSpecifierQualifierList(DS);
456 // TODO: Does specifier-qualifier list correctly check that *something* is
457 // specified?
458
459 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000460
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000461 // If there are no declarators, issue a warning.
462 if (Tok.getKind() == tok::semi) {
463 Diag(SpecQualLoc, diag::w_no_declarators);
464 } else {
465 // Read struct-declarators until we find the semicolon.
466 while (1) {
467 /// struct-declarator: declarator
468 /// struct-declarator: declarator[opt] ':' constant-expression
469 if (Tok.getKind() != tok::colon)
470 ParseDeclarator(DeclaratorInfo);
471
472 if (Tok.getKind() == tok::colon) {
473 ConsumeToken();
474 ExprResult Res = ParseConstantExpression();
475 if (Res.isInvalid) {
476 SkipUntil(tok::semi, true, true);
477 } else {
478 // Process it.
479 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000480 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000481
482 // If attributes exist after the declarator, parse them.
483 if (Tok.getKind() == tok::kw___attribute)
484 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000485
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000486 // TODO: install declarator.
487
488 // If we don't have a comma, it is either the end of the list (a ';')
489 // or an error, bail out.
490 if (Tok.getKind() != tok::comma)
491 break;
492
493 // Consume the comma.
494 ConsumeToken();
495
496 // Parse the next declarator.
497 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000498
499 // Attributes are only allowed on the second declarator.
500 if (Tok.getKind() == tok::kw___attribute)
501 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000502 }
503 }
504
505 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000506 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000507 } else {
508 Diag(Tok, diag::err_expected_semi_decl_list);
509 // Skip to end of block or statement
510 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000511 }
512 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000513
Chris Lattner04f80192006-08-15 04:55:54 +0000514 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000515
516 // If attributes exist after struct contents, parse them.
517 if (Tok.getKind() == tok::kw___attribute)
518 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000519 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000520
521 const char *PrevSpec = 0;
522 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
523 PrevSpec))
524 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000525}
526
527
Chris Lattner3b561a32006-08-13 00:12:11 +0000528/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000529/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000530/// 'enum' identifier[opt] '{' enumerator-list '}'
531/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000532/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
533/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000534/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000535/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000536/// enumerator-list:
537/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000538/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000539/// enumerator:
540/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000541/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000542/// enumeration-constant:
543/// identifier
544///
545void Parser::ParseEnumSpecifier(DeclSpec &DS) {
546 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000547 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000548 ConsumeToken();
549
Chris Lattnere37e2332006-08-15 04:50:22 +0000550 if (Tok.getKind() == tok::kw___attribute)
551 ParseAttributes();
552
Chris Lattner3b561a32006-08-13 00:12:11 +0000553 // Must have either 'enum name' or 'enum {...}'.
554 if (Tok.getKind() != tok::identifier &&
555 Tok.getKind() != tok::l_brace) {
556 Diag(Tok, diag::err_expected_ident_lbrace);
557 return;
558 }
559
560 if (Tok.getKind() == tok::identifier)
561 ConsumeToken();
562
Chris Lattner0fb8b362006-08-14 01:30:12 +0000563 if (Tok.getKind() == tok::l_brace) {
564 SourceLocation LBraceLoc = Tok.getLocation();
565 ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000566
Chris Lattner0fb8b362006-08-14 01:30:12 +0000567 if (Tok.getKind() == tok::r_brace)
568 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
569
570 // Parse the enumerator-list.
571 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000572 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000573
574 if (Tok.getKind() == tok::equal) {
575 ConsumeToken();
576 ExprResult Res = ParseConstantExpression();
577 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
578 }
579
580 if (Tok.getKind() != tok::comma)
581 break;
582 SourceLocation CommaLoc = Tok.getLocation();
583 ConsumeToken();
584
585 if (Tok.getKind() != tok::identifier && !getLang().C99)
586 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000587 }
588
Chris Lattner0fb8b362006-08-14 01:30:12 +0000589 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000590 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000591
592 // If attributes exist after the identifier list, parse them.
593 if (Tok.getKind() == tok::kw___attribute)
594 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000595 }
596 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000597
598
599 const char *PrevSpec = 0;
600 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
601 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000602}
603
604
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000605/// isTypeSpecifierQualifier - Return true if the current token could be the
606/// start of a specifier-qualifier-list.
607bool Parser::isTypeSpecifierQualifier() const {
608 switch (Tok.getKind()) {
609 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000610 // GNU attributes support.
611 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000612 // type-specifiers
613 case tok::kw_short:
614 case tok::kw_long:
615 case tok::kw_signed:
616 case tok::kw_unsigned:
617 case tok::kw__Complex:
618 case tok::kw__Imaginary:
619 case tok::kw_void:
620 case tok::kw_char:
621 case tok::kw_int:
622 case tok::kw_float:
623 case tok::kw_double:
624 case tok::kw__Bool:
625 case tok::kw__Decimal32:
626 case tok::kw__Decimal64:
627 case tok::kw__Decimal128:
628
629 // struct-or-union-specifier
630 case tok::kw_struct:
631 case tok::kw_union:
632 // enum-specifier
633 case tok::kw_enum:
634
635 // type-qualifier
636 case tok::kw_const:
637 case tok::kw_volatile:
638 case tok::kw_restrict:
639 return true;
640
641 // typedef-name
642 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000643 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000644
645 // TODO: Attributes.
646 }
647}
648
Chris Lattneracd58a32006-08-06 17:24:14 +0000649/// isDeclarationSpecifier() - Return true if the current token is part of a
650/// declaration specifier.
651bool Parser::isDeclarationSpecifier() const {
652 switch (Tok.getKind()) {
653 default: return false;
654 // storage-class-specifier
655 case tok::kw_typedef:
656 case tok::kw_extern:
657 case tok::kw_static:
658 case tok::kw_auto:
659 case tok::kw_register:
660 case tok::kw___thread:
661
662 // type-specifiers
663 case tok::kw_short:
664 case tok::kw_long:
665 case tok::kw_signed:
666 case tok::kw_unsigned:
667 case tok::kw__Complex:
668 case tok::kw__Imaginary:
669 case tok::kw_void:
670 case tok::kw_char:
671 case tok::kw_int:
672 case tok::kw_float:
673 case tok::kw_double:
674 case tok::kw__Bool:
675 case tok::kw__Decimal32:
676 case tok::kw__Decimal64:
677 case tok::kw__Decimal128:
678
679 // struct-or-union-specifier
680 case tok::kw_struct:
681 case tok::kw_union:
682 // enum-specifier
683 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000684
Chris Lattneracd58a32006-08-06 17:24:14 +0000685 // type-qualifier
686 case tok::kw_const:
687 case tok::kw_volatile:
688 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000689
Chris Lattneracd58a32006-08-06 17:24:14 +0000690 // function-specifier
691 case tok::kw_inline:
692 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000693
Chris Lattneracd58a32006-08-06 17:24:14 +0000694 // typedef-name
695 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000696 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000697 // TODO: Attributes.
698 }
699}
700
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000701
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000702/// ParseTypeQualifierListOpt
703/// type-qualifier-list: [C99 6.7.5]
704/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000705/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000706/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000707/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000708///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000709void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
710 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000711 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000712 int isInvalid = false;
713 const char *PrevSpec = 0;
714
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000715 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000716 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000717 // If this is not a type-qualifier token, we're done reading type
718 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000719 DS.Finish(StartLoc, Diags, getLang());
720 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000721 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000722 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
723 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000724 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000725 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
726 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000727 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000728 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000729 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000730
731 case tok::kw___attribute:
732 ParseAttributes();
733 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000734 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000735
736 // If the specifier combination wasn't legal, issue a diagnostic.
737 if (isInvalid) {
738 assert(PrevSpec && "Method did not return previous specifier!");
739 if (isInvalid == 1) // Error.
740 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
741 else // extwarn.
742 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
743 }
744 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000745 }
746}
747
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000748
749/// ParseDeclarator - Parse and verify a newly-initialized declarator.
750///
751void Parser::ParseDeclarator(Declarator &D) {
752 /// This implements the 'declarator' production in the C grammar, then checks
753 /// for well-formedness and issues diagnostics.
754 ParseDeclaratorInternal(D);
755
Chris Lattner9fab3b92006-08-12 18:25:42 +0000756 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000757
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000758}
759
760/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000761/// declarator: [C99 6.7.5]
762/// pointer[opt] direct-declarator
763///
764/// pointer: [C99 6.7.5]
765/// '*' type-qualifier-list[opt]
766/// '*' type-qualifier-list[opt] pointer
767///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000768void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000769 if (Tok.getKind() != tok::star)
770 return ParseDirectDeclarator(D);
771
772 // Otherwise, '*' -> pointer.
773 SourceLocation Loc = Tok.getLocation();
774 ConsumeToken(); // Eat the *.
775 DeclSpec DS;
776 ParseTypeQualifierListOpt(DS);
777
778 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000779 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000780
781 // Remember that we parsed a pointer type, and remember the type-quals.
782 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
783}
784
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000785
786/// ParseDirectDeclarator
787/// direct-declarator: [C99 6.7.5]
788/// identifier
789/// '(' declarator ')'
790/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000791/// [C90] direct-declarator '[' constant-expression[opt] ']'
792/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
793/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
794/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
795/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000796/// direct-declarator '(' parameter-type-list ')'
797/// direct-declarator '(' identifier-list[opt] ')'
798/// [GNU] direct-declarator '(' parameter-forward-declarations
799/// parameter-type-list[opt] ')'
800///
Chris Lattneracd58a32006-08-06 17:24:14 +0000801void Parser::ParseDirectDeclarator(Declarator &D) {
802 // Parse the first direct-declarator seen.
803 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
804 assert(Tok.getIdentifierInfo() && "Not an identifier?");
805 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
806 ConsumeToken();
807 } else if (Tok.getKind() == tok::l_paren) {
808 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000809 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000810 // Example: 'char (*X)' or 'int (*XX)(void)'
811 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000812 } else if (D.mayOmitIdentifier()) {
813 // This could be something simple like "int" (in which case the declarator
814 // portion is empty), if an abstract-declarator is allowed.
815 D.SetIdentifier(0, Tok.getLocation());
816 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000817 // Expected identifier or '('.
818 Diag(Tok, diag::err_expected_ident_lparen);
819 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000820 }
821
822 assert(D.isPastIdentifier() &&
823 "Haven't past the location of the identifier yet?");
824
825 while (1) {
826 if (Tok.getKind() == tok::l_paren) {
827 ParseParenDeclarator(D);
828 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000829 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000830 } else {
831 break;
832 }
833 }
834}
835
836/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
837/// either be before the identifier (in which case these are just grouping
838/// parens for precedence) or it may be after the identifier, in which case
839/// these are function arguments.
840///
841/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000842/// parameter-type-list: [C99 6.7.5]
843/// parameter-list
844/// parameter-list ',' '...'
845///
846/// parameter-list: [C99 6.7.5]
847/// parameter-declaration
848/// parameter-list ',' parameter-declaration
849///
850/// parameter-declaration: [C99 6.7.5]
851/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000852/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000853/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000854/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000855///
856/// identifier-list: [C99 6.7.5]
857/// identifier
858/// identifier-list ',' identifier
859///
Chris Lattneracd58a32006-08-06 17:24:14 +0000860void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000861 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000862 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000863
Chris Lattneracd58a32006-08-06 17:24:14 +0000864 // If we haven't past the identifier yet (or where the identifier would be
865 // stored, if this is an abstract declarator), then this is probably just
866 // grouping parens.
867 if (!D.isPastIdentifier()) {
868 // Okay, this is probably a grouping paren. However, if this could be an
869 // abstract-declarator, then this could also be the start of function
870 // arguments (consider 'void()').
871 bool isGrouping;
872
873 if (!D.mayOmitIdentifier()) {
874 // If this can't be an abstract-declarator, this *must* be a grouping
875 // paren, because we haven't seen the identifier yet.
876 isGrouping = true;
877 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
878 isDeclarationSpecifier()) { // 'int(int)' is a function.
879
880 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000881 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000882 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000883 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000884 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000885
886 // If this is a grouping paren, handle:
887 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000888 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000889 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000890 if (Tok.getKind() == tok::kw___attribute)
891 ParseAttributes();
892
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000893 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000894 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000895 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000896 return;
897 }
898
899 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000900 // argument list. Recognize that this declarator will never have an
901 // identifier (and remember where it would have been), then fall through to
902 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000903 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000904 }
905
Chris Lattneracd58a32006-08-06 17:24:14 +0000906 // Okay, this is the parameter list of a function definition, or it is an
907 // identifier list of a K&R-style function.
908
Chris Lattner9fab3b92006-08-12 18:25:42 +0000909 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000910 // arguments to the function scope.
911 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000912 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000913 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000914 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000915 bool ErrorEmitted = false;
916
Chris Lattneracd58a32006-08-06 17:24:14 +0000917 if (Tok.getKind() == tok::r_paren) {
918 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000919 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000920 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000921 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000922 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner8a3e9182006-08-14 15:44:00 +0000923 !Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000924 // Identifier list. Note that '(' identifier-list ')' is only allowed for
925 // normal declarators, not for abstract-declarators.
926 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
927
928 // If there was no identifier specified, either we are in an
929 // abstract-declarator, or we are in a parameter declarator which was found
930 // to be abstract. In abstract-declarators, identifier lists are not valid,
931 // diagnose this.
932 if (!D.getIdentifier())
933 Diag(Tok, diag::ext_ident_list_in_param);
934
Chris Lattner9fab3b92006-08-12 18:25:42 +0000935 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000936 ConsumeToken();
937 while (Tok.getKind() == tok::comma) {
938 // Eat the comma.
939 ConsumeToken();
940
Chris Lattner0be454e2006-08-12 19:30:51 +0000941 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000942 ErrorEmitted = true;
943 break;
944 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000945 }
946
Chris Lattneracd58a32006-08-06 17:24:14 +0000947 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000948 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000949 HasPrototype = false;
950 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000951 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000952 bool ReadArg = false;
953 // Finally, a normal, non-empty parameter type list.
954 while (1) {
955 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000956 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000957
958 // Check to see if this is "void(...)" which is not allowed.
959 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000960 // Otherwise, parse parameter type list. If it starts with an
961 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000962 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000963 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000964 }
965
966 // Consume the ellipsis.
967 ConsumeToken();
968 break;
969 }
970
971 ReadArg = true;
972
973 // Parse the declaration-specifiers.
974 DeclSpec DS;
975 ParseDeclarationSpecifiers(DS);
976
977 // Parse the declarator. This is "PrototypeContext", because we must
978 // accept either 'declarator' or 'abstract-declarator' here.
979 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
980 ParseDeclarator(DeclaratorInfo);
981
Chris Lattnere37e2332006-08-15 04:50:22 +0000982 // Parse GNU attributes, if present.
983 if (Tok.getKind() == tok::kw___attribute)
984 ParseAttributes();
985
Chris Lattneracd58a32006-08-06 17:24:14 +0000986 // TODO: do something with the declarator, if it is valid.
987
988 // If the next token is a comma, consume it and keep reading arguments.
989 if (Tok.getKind() != tok::comma) break;
990
991 // Consume the comma.
992 ConsumeToken();
993 }
994
995 HasPrototype = true;
996 }
997
Chris Lattner9fab3b92006-08-12 18:25:42 +0000998 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000999
Chris Lattner9fab3b92006-08-12 18:25:42 +00001000 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +00001001
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001002 // Remember that we parsed a function type, and remember the attributes.
1003 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001004 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001005
Chris Lattner14776b92006-08-06 22:27:40 +00001006
1007 // If we have the closing ')', eat it and we're done.
1008 if (Tok.getKind() == tok::r_paren) {
1009 ConsumeParen();
1010 } else {
1011 // If an error happened earlier parsing something else in the proto, don't
1012 // issue another error.
1013 if (!ErrorEmitted)
1014 Diag(Tok, diag::err_expected_rparen);
1015 SkipUntil(tok::r_paren);
1016 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001017}
Chris Lattneracd58a32006-08-06 17:24:14 +00001018
Chris Lattnere8074e62006-08-06 18:30:15 +00001019
1020/// [C90] direct-declarator '[' constant-expression[opt] ']'
1021/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1022/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1023/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1024/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1025void Parser::ParseBracketDeclarator(Declarator &D) {
1026 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +00001027 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001028
1029 // If valid, this location is the position where we read the 'static' keyword.
1030 SourceLocation StaticLoc;
1031 if (Tok.getKind() == tok::kw_static) {
1032 StaticLoc = Tok.getLocation();
1033 ConsumeToken();
1034 }
1035
1036 // If there is a type-qualifier-list, read it now.
1037 DeclSpec DS;
1038 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001039
1040 // If we haven't already read 'static', check to see if there is one after the
1041 // type-qualifier-list.
1042 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
1043 StaticLoc = Tok.getLocation();
1044 ConsumeToken();
1045 }
1046
1047 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001048 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001049 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001050 if (Tok.getKind() == tok::star) {
1051 // Remember the '*' token, in case we have to un-get it.
1052 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001053 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001054
1055 // Check that the ']' token is present to avoid incorrectly parsing
1056 // expressions starting with '*' as [*].
1057 if (Tok.getKind() == tok::r_square) {
1058 if (StaticLoc.isValid())
1059 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1060 StaticLoc = SourceLocation(); // Drop the static.
1061 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001062 } else {
1063 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001064 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001065 // need to reparse it. This handles cases like 'X[*p + 4]'
1066 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001067 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001068 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001069 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001070 NumElements = ParseAssignmentExpression();
1071 }
1072
1073 // If there was an error parsing the assignment-expression, recover.
1074 if (NumElements.isInvalid) {
1075 // If the expression was invalid, skip it.
1076 SkipUntil(tok::r_square);
1077 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001078 }
1079
Chris Lattner04f80192006-08-15 04:55:54 +00001080 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001081
Chris Lattnere8074e62006-08-06 18:30:15 +00001082 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1083 // it was not a constant expression.
1084 if (!getLang().C99) {
1085 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001086 if (isStar || StaticLoc.isValid() ||
1087 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001088 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001089 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001090
1091 // Remember that we parsed a pointer type, and remember the type-quals.
1092 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1093 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001094 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001095}
1096