blob: b630d1d0bdaaddbccb59a618e14281cd23a3518f [file] [log] [blame]
Chris Lattnereb8a28f2006-08-10 18:43:39 +00001//===--- Declaration.cpp - Declaration Parsing ----------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattnerb9093cd2006-08-04 04:39:53 +000015#include "clang/Parse/Declarations.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000016using namespace llvm;
17using namespace clang;
18
19//===----------------------------------------------------------------------===//
20// C99 6.7: Declarations.
21//===----------------------------------------------------------------------===//
22
Chris Lattnerf5fbd792006-08-10 23:56:11 +000023/// ParseTypeName
24/// type-name: [C99 6.7.6]
25/// specifier-qualifier-list abstract-declarator[opt]
26void Parser::ParseTypeName() {
27 // Parse the common declaration-specifiers piece.
28 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000029 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030
31 // Parse the abstract-declarator, if present.
32 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
33 ParseDeclarator(DeclaratorInfo);
34}
35
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000036/// ParseAttributes - Parse a non-empty attributes list.
37///
38/// [GNU] attributes:
39/// attribute
40/// attributes attribute
41///
42/// [GNU] attribute:
43/// '__attribute__' '(' '(' attribute-list ')' ')'
44///
45/// [GNU] attribute-list:
46/// attrib
47/// attribute_list ',' attrib
48///
49/// [GNU] attrib:
50/// empty
51/// any-word
52/// any-word '(' identifier ')'
53/// any-word '(' identifier ',' nonempty-expr-list ')'
54/// any-word '(' expr-list ')'
55///
56void Parser::ParseAttributes() {
57 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
58 ConsumeToken();
59
60 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
61 "attribute"))
62 return;
63
64 // TODO: Parse the attributes.
65 SkipUntil(tok::r_paren, false);
66}
67
Chris Lattnerf5fbd792006-08-10 23:56:11 +000068
Chris Lattner53361ac2006-08-10 05:19:57 +000069/// ParseDeclaration - Parse a full 'declaration', which consists of
70/// declaration-specifiers, some number of declarators, and a semicolon.
71/// 'Context' should be a Declarator::TheContext value.
72void Parser::ParseDeclaration(unsigned Context) {
73 // Parse the common declaration-specifiers piece.
74 DeclSpec DS;
75 ParseDeclarationSpecifiers(DS);
76
Chris Lattner0e894622006-08-13 19:58:17 +000077 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
78 // declaration-specifiers init-declarator-list[opt] ';'
79 if (Tok.getKind() == tok::semi) {
80 // TODO: emit error on 'int;' or 'const enum foo;'.
81 // if (!DS.isMissingDeclaratorOk()) Diag(...);
82
83 ConsumeToken();
84 return;
85 }
86
Chris Lattner53361ac2006-08-10 05:19:57 +000087 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
88 ParseDeclarator(DeclaratorInfo);
89
90 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
91}
92
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000093/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
94/// parsing 'declaration-specifiers declarator'. This method is split out this
95/// way to handle the ambiguity between top-level function-definitions and
96/// declarations.
97///
98/// declaration: [C99 6.7]
99/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
100/// [!C99] init-declarator-list ';' [TODO]
101/// [OMP] threadprivate-directive [TODO]
102///
103/// init-declarator-list: [C99 6.7]
104/// init-declarator
105/// init-declarator-list ',' init-declarator
106/// init-declarator: [C99 6.7]
107/// declarator
108/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000109/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
110/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000111///
Chris Lattner53361ac2006-08-10 05:19:57 +0000112void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
113 // At this point, we know that it is not a function definition. Parse the
114 // rest of the init-declarator-list.
115 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000116 // If a simple-asm-expr is present, parse it.
117 if (Tok.getKind() == tok::kw_asm)
118 ParseSimpleAsm();
119
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000120 // If attributes are present, parse them.
121 if (Tok.getKind() == tok::kw___attribute)
122 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000123
Chris Lattner53361ac2006-08-10 05:19:57 +0000124 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000125 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000126 if (Tok.getKind() == tok::equal) {
127 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000128 Init = ParseInitializer();
129 if (!Init.isInvalid) {
130 SkipUntil(tok::semi);
131 return;
132 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000133 }
134
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000135 // Inform the current actions module that we just parsed a declarator.
136 Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000137
138 // If we don't have a comma, it is either the end of the list (a ';') or an
139 // error, bail out.
140 if (Tok.getKind() != tok::comma)
141 break;
142
143 // Consume the comma.
144 ConsumeToken();
145
146 // Parse the next declarator.
147 D.clear();
148 ParseDeclarator(D);
149 }
150
151 if (Tok.getKind() == tok::semi) {
152 ConsumeToken();
153 } else {
154 Diag(Tok, diag::err_parse_error);
155 // Skip to end of block or statement
156 SkipUntil(tok::r_brace, true);
157 if (Tok.getKind() == tok::semi)
158 ConsumeToken();
159 }
160}
161
Chris Lattner1890ac82006-08-13 01:16:23 +0000162/// ParseSpecifierQualifierList
163/// specifier-qualifier-list:
164/// type-specifier specifier-qualifier-list[opt]
165/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000166/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000167///
168void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
169 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
170 /// parse declaration-specifiers and complain about extra stuff.
171 SourceLocation Loc = Tok.getLocation();
172 ParseDeclarationSpecifiers(DS);
173
174 // Validate declspec for type-name.
175 unsigned Specs = DS.getParsedSpecifiers();
176 if (Specs == DeclSpec::PQ_None)
177 Diag(Tok, diag::err_typename_requires_specqual);
178
179 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
180 Diag(Loc, diag::err_typename_invalid_storageclass);
181 // Remove storage class.
182 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
183 DS.SCS_thread_specified = false;
184 }
185 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
186 Diag(Loc, diag::err_typename_invalid_functionspec);
187 DS.FS_inline_specified = false;
188 }
189}
Chris Lattner53361ac2006-08-10 05:19:57 +0000190
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000191/// ParseDeclarationSpecifiers
192/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000193/// storage-class-specifier declaration-specifiers[opt]
194/// type-specifier declaration-specifiers[opt]
195/// type-qualifier declaration-specifiers[opt]
196/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000197/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000198///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000199/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000200/// 'typedef'
201/// 'extern'
202/// 'static'
203/// 'auto'
204/// 'register'
205/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000206/// type-specifier: [C99 6.7.2]
207/// 'void'
208/// 'char'
209/// 'short'
210/// 'int'
211/// 'long'
212/// 'float'
213/// 'double'
214/// 'signed'
215/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000216/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000217/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000218/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000219/// [C99] '_Bool'
220/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000221/// [C99] '_Imaginary' // Removed in TC2?
222/// [GNU] '_Decimal32'
223/// [GNU] '_Decimal64'
224/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000225/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000226/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000227/// [OBJC] typedef-name objc-protocol-refs [TODO]
228/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000229/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000230/// 'const'
231/// 'volatile'
232/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000233/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000234/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000235///
236void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
237 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000238 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000239 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000240 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000241 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000242 // typedef-name
243 case tok::identifier:
244 // This identifier can only be a typedef name if we haven't already seen
245 // a typename. This avoids the virtual method call on things like
246 // 'int foo'.
247 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
248 Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
250 break;
251 }
252 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000253 default:
254 // If this is not a declaration specifier token, we're done reading decl
255 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000256 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000257 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000258
259 // GNU attributes support.
260 case tok::kw___attribute:
261 ParseAttributes();
262 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000263
264 // storage-class-specifier
265 case tok::kw_typedef:
266 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
267 break;
268 case tok::kw_extern:
269 if (DS.SCS_thread_specified)
270 Diag(Tok, diag::ext_thread_before, "extern");
271 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
272 break;
273 case tok::kw_static:
274 if (DS.SCS_thread_specified)
275 Diag(Tok, diag::ext_thread_before, "static");
276 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
277 break;
278 case tok::kw_auto:
279 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
280 break;
281 case tok::kw_register:
282 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
283 break;
284 case tok::kw___thread:
285 if (DS.SCS_thread_specified)
286 isInvalid = 2, PrevSpec = "__thread";
287 else
288 DS.SCS_thread_specified = true;
289 break;
290
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000291 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000292 case tok::kw_short:
293 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
294 break;
295 case tok::kw_long:
296 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
297 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
298 } else {
299 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
300 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
301 }
302 break;
303 case tok::kw_signed:
304 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
305 break;
306 case tok::kw_unsigned:
307 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
308 break;
309 case tok::kw__Complex:
310 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
311 break;
312 case tok::kw__Imaginary:
313 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
314 break;
315 case tok::kw_void:
316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
317 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000318 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
320 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000321 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
323 break;
324 case tok::kw_float:
325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
326 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000327 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
329 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000330 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
332 break;
333 case tok::kw__Decimal32:
334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
335 break;
336 case tok::kw__Decimal64:
337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
338 break;
339 case tok::kw__Decimal128:
340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000341 break;
342
Chris Lattner1890ac82006-08-13 01:16:23 +0000343 case tok::kw_struct:
344 case tok::kw_union:
345 ParseStructUnionSpecifier(DS);
346 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000347 case tok::kw_enum:
348 ParseEnumSpecifier(DS);
349 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000350
351 // type-qualifier
352 case tok::kw_const:
353 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
354 break;
355 case tok::kw_volatile:
356 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
357 break;
358 case tok::kw_restrict:
359 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
360 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000361
362 // function-specifier
363 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000364 // 'inline inline' is ok.
365 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000366 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000367 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000368 // If the specifier combination wasn't legal, issue a diagnostic.
369 if (isInvalid) {
370 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000371 if (isInvalid == 1) // Error.
372 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
373 else // extwarn.
374 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000375 }
376 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000377 }
378}
379
Chris Lattner1890ac82006-08-13 01:16:23 +0000380
381/// ParseStructUnionSpecifier
382/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000383/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000384/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000385/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
386/// '}' attributes[opt]
387/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000388/// struct-or-union:
389/// 'struct'
390/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000391/// struct-contents:
392/// struct-declaration-list
393/// [EXT] empty
394/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000395/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000396/// struct-declaration
397/// struct-declaration-list struct-declaration
398/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
399/// struct-declaration:
400/// specifier-qualifier-list struct-declarator-list ';'
401/// [GNU] __extension__ struct-declaration [TODO]
402/// [GNU] specifier-qualifier-list ';' [TODO]
403/// struct-declarator-list:
404/// struct-declarator
405/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000406/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000407/// struct-declarator:
408/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000409/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000410/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000411/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000412///
413void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
414 assert((Tok.getKind() == tok::kw_struct ||
415 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000416 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000417 bool isUnion = Tok.getKind() == tok::kw_union;
418 ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000419
420 // If attributes exist after tag, parse them.
421 if (Tok.getKind() == tok::kw___attribute)
422 ParseAttributes();
423
Chris Lattner1890ac82006-08-13 01:16:23 +0000424 // Must have either 'struct name' or 'struct {...}'.
425 if (Tok.getKind() != tok::identifier &&
426 Tok.getKind() != tok::l_brace) {
427 Diag(Tok, diag::err_expected_ident_lbrace);
428 return;
429 }
430
431 if (Tok.getKind() == tok::identifier)
432 ConsumeToken();
433
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000434 if (Tok.getKind() == tok::l_brace) {
435 SourceLocation LBraceLoc = Tok.getLocation();
436 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000437
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000438 if (Tok.getKind() == tok::r_brace)
439 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000440
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000441 while (Tok.getKind() != tok::r_brace &&
442 Tok.getKind() != tok::eof) {
443 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000444
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000445 // Parse the common specifier-qualifiers-list piece.
446 DeclSpec DS;
447 SourceLocation SpecQualLoc = Tok.getLocation();
448 ParseSpecifierQualifierList(DS);
449 // TODO: Does specifier-qualifier list correctly check that *something* is
450 // specified?
451
452 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000453
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000454 // If there are no declarators, issue a warning.
455 if (Tok.getKind() == tok::semi) {
456 Diag(SpecQualLoc, diag::w_no_declarators);
457 } else {
458 // Read struct-declarators until we find the semicolon.
459 while (1) {
460 /// struct-declarator: declarator
461 /// struct-declarator: declarator[opt] ':' constant-expression
462 if (Tok.getKind() != tok::colon)
463 ParseDeclarator(DeclaratorInfo);
464
465 if (Tok.getKind() == tok::colon) {
466 ConsumeToken();
467 ExprResult Res = ParseConstantExpression();
468 if (Res.isInvalid) {
469 SkipUntil(tok::semi, true, true);
470 } else {
471 // Process it.
472 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000473 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000474
475 // If attributes exist after the declarator, parse them.
476 if (Tok.getKind() == tok::kw___attribute)
477 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000478
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000479 // TODO: install declarator.
480
481 // If we don't have a comma, it is either the end of the list (a ';')
482 // or an error, bail out.
483 if (Tok.getKind() != tok::comma)
484 break;
485
486 // Consume the comma.
487 ConsumeToken();
488
489 // Parse the next declarator.
490 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000491
492 // Attributes are only allowed on the second declarator.
493 if (Tok.getKind() == tok::kw___attribute)
494 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000495 }
496 }
497
498 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000499 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000500 } else {
501 Diag(Tok, diag::err_expected_semi_decl_list);
502 // Skip to end of block or statement
503 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000504 }
505 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000506
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000507 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace);
Chris Lattnere37e2332006-08-15 04:50:22 +0000508
509 // If attributes exist after struct contents, parse them.
510 if (Tok.getKind() == tok::kw___attribute)
511 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000512 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000513
514 const char *PrevSpec = 0;
515 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
516 PrevSpec))
517 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000518}
519
520
Chris Lattner3b561a32006-08-13 00:12:11 +0000521/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000522/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000523/// 'enum' identifier[opt] '{' enumerator-list '}'
524/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000525/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
526/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000527/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000528/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000529/// enumerator-list:
530/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000531/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000532/// enumerator:
533/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000534/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000535/// enumeration-constant:
536/// identifier
537///
538void Parser::ParseEnumSpecifier(DeclSpec &DS) {
539 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000540 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000541 ConsumeToken();
542
Chris Lattnere37e2332006-08-15 04:50:22 +0000543 if (Tok.getKind() == tok::kw___attribute)
544 ParseAttributes();
545
Chris Lattner3b561a32006-08-13 00:12:11 +0000546 // Must have either 'enum name' or 'enum {...}'.
547 if (Tok.getKind() != tok::identifier &&
548 Tok.getKind() != tok::l_brace) {
549 Diag(Tok, diag::err_expected_ident_lbrace);
550 return;
551 }
552
553 if (Tok.getKind() == tok::identifier)
554 ConsumeToken();
555
Chris Lattner0fb8b362006-08-14 01:30:12 +0000556 if (Tok.getKind() == tok::l_brace) {
557 SourceLocation LBraceLoc = Tok.getLocation();
558 ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000559
Chris Lattner0fb8b362006-08-14 01:30:12 +0000560 if (Tok.getKind() == tok::r_brace)
561 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
562
563 // Parse the enumerator-list.
564 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000565 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000566
567 if (Tok.getKind() == tok::equal) {
568 ConsumeToken();
569 ExprResult Res = ParseConstantExpression();
570 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
571 }
572
573 if (Tok.getKind() != tok::comma)
574 break;
575 SourceLocation CommaLoc = Tok.getLocation();
576 ConsumeToken();
577
578 if (Tok.getKind() != tok::identifier && !getLang().C99)
579 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000580 }
581
Chris Lattner0fb8b362006-08-14 01:30:12 +0000582 // Eat the }.
583 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
584 diag::err_expected_rbrace);
Chris Lattnere37e2332006-08-15 04:50:22 +0000585
586 // If attributes exist after the identifier list, parse them.
587 if (Tok.getKind() == tok::kw___attribute)
588 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000589 }
590 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000591
592
593 const char *PrevSpec = 0;
594 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
595 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000596}
597
598
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000599/// isTypeSpecifierQualifier - Return true if the current token could be the
600/// start of a specifier-qualifier-list.
601bool Parser::isTypeSpecifierQualifier() const {
602 switch (Tok.getKind()) {
603 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000604 // GNU attributes support.
605 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000606 // type-specifiers
607 case tok::kw_short:
608 case tok::kw_long:
609 case tok::kw_signed:
610 case tok::kw_unsigned:
611 case tok::kw__Complex:
612 case tok::kw__Imaginary:
613 case tok::kw_void:
614 case tok::kw_char:
615 case tok::kw_int:
616 case tok::kw_float:
617 case tok::kw_double:
618 case tok::kw__Bool:
619 case tok::kw__Decimal32:
620 case tok::kw__Decimal64:
621 case tok::kw__Decimal128:
622
623 // struct-or-union-specifier
624 case tok::kw_struct:
625 case tok::kw_union:
626 // enum-specifier
627 case tok::kw_enum:
628
629 // type-qualifier
630 case tok::kw_const:
631 case tok::kw_volatile:
632 case tok::kw_restrict:
633 return true;
634
635 // typedef-name
636 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000637 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000638
639 // TODO: Attributes.
640 }
641}
642
Chris Lattneracd58a32006-08-06 17:24:14 +0000643/// isDeclarationSpecifier() - Return true if the current token is part of a
644/// declaration specifier.
645bool Parser::isDeclarationSpecifier() const {
646 switch (Tok.getKind()) {
647 default: return false;
648 // storage-class-specifier
649 case tok::kw_typedef:
650 case tok::kw_extern:
651 case tok::kw_static:
652 case tok::kw_auto:
653 case tok::kw_register:
654 case tok::kw___thread:
655
656 // type-specifiers
657 case tok::kw_short:
658 case tok::kw_long:
659 case tok::kw_signed:
660 case tok::kw_unsigned:
661 case tok::kw__Complex:
662 case tok::kw__Imaginary:
663 case tok::kw_void:
664 case tok::kw_char:
665 case tok::kw_int:
666 case tok::kw_float:
667 case tok::kw_double:
668 case tok::kw__Bool:
669 case tok::kw__Decimal32:
670 case tok::kw__Decimal64:
671 case tok::kw__Decimal128:
672
673 // struct-or-union-specifier
674 case tok::kw_struct:
675 case tok::kw_union:
676 // enum-specifier
677 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000678
Chris Lattneracd58a32006-08-06 17:24:14 +0000679 // type-qualifier
680 case tok::kw_const:
681 case tok::kw_volatile:
682 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000683
Chris Lattneracd58a32006-08-06 17:24:14 +0000684 // function-specifier
685 case tok::kw_inline:
686 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000687
Chris Lattneracd58a32006-08-06 17:24:14 +0000688 // typedef-name
689 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000690 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000691 // TODO: Attributes.
692 }
693}
694
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000695
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000696/// ParseTypeQualifierListOpt
697/// type-qualifier-list: [C99 6.7.5]
698/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000699/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000700/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000701/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000702///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000703void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
704 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000705 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000706 int isInvalid = false;
707 const char *PrevSpec = 0;
708
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000709 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000710 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000711 // If this is not a type-qualifier token, we're done reading type
712 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000713 DS.Finish(StartLoc, Diags, getLang());
714 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000715 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000716 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
717 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000718 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000719 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
720 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000721 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000722 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000723 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000724
725 case tok::kw___attribute:
726 ParseAttributes();
727 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000728 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000729
730 // If the specifier combination wasn't legal, issue a diagnostic.
731 if (isInvalid) {
732 assert(PrevSpec && "Method did not return previous specifier!");
733 if (isInvalid == 1) // Error.
734 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
735 else // extwarn.
736 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
737 }
738 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000739 }
740}
741
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000742
743/// ParseDeclarator - Parse and verify a newly-initialized declarator.
744///
745void Parser::ParseDeclarator(Declarator &D) {
746 /// This implements the 'declarator' production in the C grammar, then checks
747 /// for well-formedness and issues diagnostics.
748 ParseDeclaratorInternal(D);
749
Chris Lattner9fab3b92006-08-12 18:25:42 +0000750 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000751
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000752}
753
754/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000755/// declarator: [C99 6.7.5]
756/// pointer[opt] direct-declarator
757///
758/// pointer: [C99 6.7.5]
759/// '*' type-qualifier-list[opt]
760/// '*' type-qualifier-list[opt] pointer
761///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000762void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000763 if (Tok.getKind() != tok::star)
764 return ParseDirectDeclarator(D);
765
766 // Otherwise, '*' -> pointer.
767 SourceLocation Loc = Tok.getLocation();
768 ConsumeToken(); // Eat the *.
769 DeclSpec DS;
770 ParseTypeQualifierListOpt(DS);
771
772 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000773 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000774
775 // Remember that we parsed a pointer type, and remember the type-quals.
776 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
777}
778
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000779
780/// ParseDirectDeclarator
781/// direct-declarator: [C99 6.7.5]
782/// identifier
783/// '(' declarator ')'
784/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000785/// [C90] direct-declarator '[' constant-expression[opt] ']'
786/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
787/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
788/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
789/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000790/// direct-declarator '(' parameter-type-list ')'
791/// direct-declarator '(' identifier-list[opt] ')'
792/// [GNU] direct-declarator '(' parameter-forward-declarations
793/// parameter-type-list[opt] ')'
794///
Chris Lattneracd58a32006-08-06 17:24:14 +0000795void Parser::ParseDirectDeclarator(Declarator &D) {
796 // Parse the first direct-declarator seen.
797 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
798 assert(Tok.getIdentifierInfo() && "Not an identifier?");
799 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
800 ConsumeToken();
801 } else if (Tok.getKind() == tok::l_paren) {
802 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000803 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000804 // Example: 'char (*X)' or 'int (*XX)(void)'
805 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000806 } else if (D.mayOmitIdentifier()) {
807 // This could be something simple like "int" (in which case the declarator
808 // portion is empty), if an abstract-declarator is allowed.
809 D.SetIdentifier(0, Tok.getLocation());
810 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000811 // Expected identifier or '('.
812 Diag(Tok, diag::err_expected_ident_lparen);
813 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000814 }
815
816 assert(D.isPastIdentifier() &&
817 "Haven't past the location of the identifier yet?");
818
819 while (1) {
820 if (Tok.getKind() == tok::l_paren) {
821 ParseParenDeclarator(D);
822 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000823 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000824 } else {
825 break;
826 }
827 }
828}
829
830/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
831/// either be before the identifier (in which case these are just grouping
832/// parens for precedence) or it may be after the identifier, in which case
833/// these are function arguments.
834///
835/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000836/// parameter-type-list: [C99 6.7.5]
837/// parameter-list
838/// parameter-list ',' '...'
839///
840/// parameter-list: [C99 6.7.5]
841/// parameter-declaration
842/// parameter-list ',' parameter-declaration
843///
844/// parameter-declaration: [C99 6.7.5]
845/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000846/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000847/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000848/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000849///
850/// identifier-list: [C99 6.7.5]
851/// identifier
852/// identifier-list ',' identifier
853///
Chris Lattneracd58a32006-08-06 17:24:14 +0000854void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000855 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000856 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000857
Chris Lattneracd58a32006-08-06 17:24:14 +0000858 // If we haven't past the identifier yet (or where the identifier would be
859 // stored, if this is an abstract declarator), then this is probably just
860 // grouping parens.
861 if (!D.isPastIdentifier()) {
862 // Okay, this is probably a grouping paren. However, if this could be an
863 // abstract-declarator, then this could also be the start of function
864 // arguments (consider 'void()').
865 bool isGrouping;
866
867 if (!D.mayOmitIdentifier()) {
868 // If this can't be an abstract-declarator, this *must* be a grouping
869 // paren, because we haven't seen the identifier yet.
870 isGrouping = true;
871 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
872 isDeclarationSpecifier()) { // 'int(int)' is a function.
873
874 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000875 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000876 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000877 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000878 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000879
880 // If this is a grouping paren, handle:
881 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000882 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000883 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000884 if (Tok.getKind() == tok::kw___attribute)
885 ParseAttributes();
886
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000887 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000888 // Match the ')'.
889 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
890 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000891 return;
892 }
893
894 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000895 // argument list. Recognize that this declarator will never have an
896 // identifier (and remember where it would have been), then fall through to
897 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000898 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000899 }
900
Chris Lattneracd58a32006-08-06 17:24:14 +0000901 // Okay, this is the parameter list of a function definition, or it is an
902 // identifier list of a K&R-style function.
903
Chris Lattner9fab3b92006-08-12 18:25:42 +0000904 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000905 // arguments to the function scope.
906 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000907 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000908 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000909 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000910 bool ErrorEmitted = false;
911
Chris Lattneracd58a32006-08-06 17:24:14 +0000912 if (Tok.getKind() == tok::r_paren) {
913 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000914 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000915 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000916 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000917 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner8a3e9182006-08-14 15:44:00 +0000918 !Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000919 // Identifier list. Note that '(' identifier-list ')' is only allowed for
920 // normal declarators, not for abstract-declarators.
921 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
922
923 // If there was no identifier specified, either we are in an
924 // abstract-declarator, or we are in a parameter declarator which was found
925 // to be abstract. In abstract-declarators, identifier lists are not valid,
926 // diagnose this.
927 if (!D.getIdentifier())
928 Diag(Tok, diag::ext_ident_list_in_param);
929
Chris Lattner9fab3b92006-08-12 18:25:42 +0000930 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000931 ConsumeToken();
932 while (Tok.getKind() == tok::comma) {
933 // Eat the comma.
934 ConsumeToken();
935
Chris Lattner0be454e2006-08-12 19:30:51 +0000936 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000937 ErrorEmitted = true;
938 break;
939 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000940 }
941
Chris Lattneracd58a32006-08-06 17:24:14 +0000942 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000943 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000944 HasPrototype = false;
945 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000946 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000947 bool ReadArg = false;
948 // Finally, a normal, non-empty parameter type list.
949 while (1) {
950 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000951 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000952
953 // Check to see if this is "void(...)" which is not allowed.
954 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000955 // Otherwise, parse parameter type list. If it starts with an
956 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000957 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000958 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000959 }
960
961 // Consume the ellipsis.
962 ConsumeToken();
963 break;
964 }
965
966 ReadArg = true;
967
968 // Parse the declaration-specifiers.
969 DeclSpec DS;
970 ParseDeclarationSpecifiers(DS);
971
972 // Parse the declarator. This is "PrototypeContext", because we must
973 // accept either 'declarator' or 'abstract-declarator' here.
974 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
975 ParseDeclarator(DeclaratorInfo);
976
Chris Lattnere37e2332006-08-15 04:50:22 +0000977 // Parse GNU attributes, if present.
978 if (Tok.getKind() == tok::kw___attribute)
979 ParseAttributes();
980
Chris Lattneracd58a32006-08-06 17:24:14 +0000981 // TODO: do something with the declarator, if it is valid.
982
983 // If the next token is a comma, consume it and keep reading arguments.
984 if (Tok.getKind() != tok::comma) break;
985
986 // Consume the comma.
987 ConsumeToken();
988 }
989
990 HasPrototype = true;
991 }
992
Chris Lattner9fab3b92006-08-12 18:25:42 +0000993 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000994
Chris Lattner9fab3b92006-08-12 18:25:42 +0000995 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000996
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000997 // Remember that we parsed a function type, and remember the attributes.
998 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000999 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001000
Chris Lattner14776b92006-08-06 22:27:40 +00001001
1002 // If we have the closing ')', eat it and we're done.
1003 if (Tok.getKind() == tok::r_paren) {
1004 ConsumeParen();
1005 } else {
1006 // If an error happened earlier parsing something else in the proto, don't
1007 // issue another error.
1008 if (!ErrorEmitted)
1009 Diag(Tok, diag::err_expected_rparen);
1010 SkipUntil(tok::r_paren);
1011 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001012}
Chris Lattneracd58a32006-08-06 17:24:14 +00001013
Chris Lattnere8074e62006-08-06 18:30:15 +00001014
1015/// [C90] direct-declarator '[' constant-expression[opt] ']'
1016/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1017/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1018/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1019/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1020void Parser::ParseBracketDeclarator(Declarator &D) {
1021 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +00001022 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001023
1024 // If valid, this location is the position where we read the 'static' keyword.
1025 SourceLocation StaticLoc;
1026 if (Tok.getKind() == tok::kw_static) {
1027 StaticLoc = Tok.getLocation();
1028 ConsumeToken();
1029 }
1030
1031 // If there is a type-qualifier-list, read it now.
1032 DeclSpec DS;
1033 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001034
1035 // If we haven't already read 'static', check to see if there is one after the
1036 // type-qualifier-list.
1037 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
1038 StaticLoc = Tok.getLocation();
1039 ConsumeToken();
1040 }
1041
1042 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001043 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001044 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001045 if (Tok.getKind() == tok::star) {
1046 // Remember the '*' token, in case we have to un-get it.
1047 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001048 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001049
1050 // Check that the ']' token is present to avoid incorrectly parsing
1051 // expressions starting with '*' as [*].
1052 if (Tok.getKind() == tok::r_square) {
1053 if (StaticLoc.isValid())
1054 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1055 StaticLoc = SourceLocation(); // Drop the static.
1056 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001057 } else {
1058 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001059 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001060 // need to reparse it. This handles cases like 'X[*p + 4]'
1061 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001062 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001063 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001064 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001065 NumElements = ParseAssignmentExpression();
1066 }
1067
1068 // If there was an error parsing the assignment-expression, recover.
1069 if (NumElements.isInvalid) {
1070 // If the expression was invalid, skip it.
1071 SkipUntil(tok::r_square);
1072 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001073 }
1074
Chris Lattner9fab3b92006-08-12 18:25:42 +00001075 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
1076
Chris Lattnere8074e62006-08-06 18:30:15 +00001077 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1078 // it was not a constant expression.
1079 if (!getLang().C99) {
1080 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001081 if (isStar || StaticLoc.isValid() ||
1082 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001083 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001084 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001085
1086 // Remember that we parsed a pointer type, and remember the type-quals.
1087 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1088 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001089 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001090}
1091