blob: 242dc4336ff3991db694aa288b5601a2ad383c98 [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
Chris Lattner5646b3e2006-08-15 05:12:01 +0000245 // a type-specifier. Without this check we misparse:
246 // typedef int X; struct Y { short X; }; as 'short int'.
247 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
248 DS.TypeSpecWidth == DeclSpec::TSW_unspecified &&
249 DS.TypeSpecComplex == DeclSpec::TSC_unspecified &&
250 DS.TypeSpecSign == DeclSpec::TSS_unspecified &&
251 // It has to be available as a typedef too!
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000252 Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
254 break;
255 }
256 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000257 default:
258 // If this is not a declaration specifier token, we're done reading decl
259 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000260 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000261 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000262
263 // GNU attributes support.
264 case tok::kw___attribute:
265 ParseAttributes();
266 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000267
268 // storage-class-specifier
269 case tok::kw_typedef:
270 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
271 break;
272 case tok::kw_extern:
273 if (DS.SCS_thread_specified)
274 Diag(Tok, diag::ext_thread_before, "extern");
275 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
276 break;
277 case tok::kw_static:
278 if (DS.SCS_thread_specified)
279 Diag(Tok, diag::ext_thread_before, "static");
280 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
281 break;
282 case tok::kw_auto:
283 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
284 break;
285 case tok::kw_register:
286 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
287 break;
288 case tok::kw___thread:
289 if (DS.SCS_thread_specified)
290 isInvalid = 2, PrevSpec = "__thread";
291 else
292 DS.SCS_thread_specified = true;
293 break;
294
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000295 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000296 case tok::kw_short:
297 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
298 break;
299 case tok::kw_long:
300 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
301 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
302 } else {
303 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
304 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
305 }
306 break;
307 case tok::kw_signed:
308 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
309 break;
310 case tok::kw_unsigned:
311 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
312 break;
313 case tok::kw__Complex:
314 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
315 break;
316 case tok::kw__Imaginary:
317 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
318 break;
319 case tok::kw_void:
320 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
321 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000322 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
324 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000325 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
327 break;
328 case tok::kw_float:
329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
330 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000331 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
333 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000334 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
336 break;
337 case tok::kw__Decimal32:
338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
339 break;
340 case tok::kw__Decimal64:
341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
342 break;
343 case tok::kw__Decimal128:
344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000345 break;
346
Chris Lattner1890ac82006-08-13 01:16:23 +0000347 case tok::kw_struct:
348 case tok::kw_union:
349 ParseStructUnionSpecifier(DS);
350 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000351 case tok::kw_enum:
352 ParseEnumSpecifier(DS);
353 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000354
355 // type-qualifier
356 case tok::kw_const:
357 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
358 break;
359 case tok::kw_volatile:
360 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
361 break;
362 case tok::kw_restrict:
363 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
364 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000365
366 // function-specifier
367 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000368 // 'inline inline' is ok.
369 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000370 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000371 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000372 // If the specifier combination wasn't legal, issue a diagnostic.
373 if (isInvalid) {
374 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000375 if (isInvalid == 1) // Error.
376 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
377 else // extwarn.
378 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000379 }
380 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000381 }
382}
383
Chris Lattner1890ac82006-08-13 01:16:23 +0000384
385/// ParseStructUnionSpecifier
386/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000387/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000388/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000389/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
390/// '}' attributes[opt]
391/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000392/// struct-or-union:
393/// 'struct'
394/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000395/// struct-contents:
396/// struct-declaration-list
397/// [EXT] empty
398/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000399/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000400/// struct-declaration
401/// struct-declaration-list struct-declaration
402/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
403/// struct-declaration:
404/// specifier-qualifier-list struct-declarator-list ';'
405/// [GNU] __extension__ struct-declaration [TODO]
406/// [GNU] specifier-qualifier-list ';' [TODO]
407/// struct-declarator-list:
408/// struct-declarator
409/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000410/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000411/// struct-declarator:
412/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000413/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000414/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000415/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000416///
417void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
418 assert((Tok.getKind() == tok::kw_struct ||
419 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000420 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000421 bool isUnion = Tok.getKind() == tok::kw_union;
422 ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000423
424 // If attributes exist after tag, parse them.
425 if (Tok.getKind() == tok::kw___attribute)
426 ParseAttributes();
427
Chris Lattner1890ac82006-08-13 01:16:23 +0000428 // Must have either 'struct name' or 'struct {...}'.
429 if (Tok.getKind() != tok::identifier &&
430 Tok.getKind() != tok::l_brace) {
431 Diag(Tok, diag::err_expected_ident_lbrace);
432 return;
433 }
434
435 if (Tok.getKind() == tok::identifier)
436 ConsumeToken();
437
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000438 if (Tok.getKind() == tok::l_brace) {
439 SourceLocation LBraceLoc = Tok.getLocation();
440 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000441
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000442 if (Tok.getKind() == tok::r_brace)
443 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000444
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000445 while (Tok.getKind() != tok::r_brace &&
446 Tok.getKind() != tok::eof) {
447 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000448
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000449 // Parse the common specifier-qualifiers-list piece.
450 DeclSpec DS;
451 SourceLocation SpecQualLoc = Tok.getLocation();
452 ParseSpecifierQualifierList(DS);
453 // TODO: Does specifier-qualifier list correctly check that *something* is
454 // specified?
455
456 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000457
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000458 // If there are no declarators, issue a warning.
459 if (Tok.getKind() == tok::semi) {
460 Diag(SpecQualLoc, diag::w_no_declarators);
461 } else {
462 // Read struct-declarators until we find the semicolon.
463 while (1) {
464 /// struct-declarator: declarator
465 /// struct-declarator: declarator[opt] ':' constant-expression
466 if (Tok.getKind() != tok::colon)
467 ParseDeclarator(DeclaratorInfo);
468
469 if (Tok.getKind() == tok::colon) {
470 ConsumeToken();
471 ExprResult Res = ParseConstantExpression();
472 if (Res.isInvalid) {
473 SkipUntil(tok::semi, true, true);
474 } else {
475 // Process it.
476 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000477 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000478
479 // If attributes exist after the declarator, parse them.
480 if (Tok.getKind() == tok::kw___attribute)
481 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000482
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000483 // TODO: install declarator.
484
485 // If we don't have a comma, it is either the end of the list (a ';')
486 // or an error, bail out.
487 if (Tok.getKind() != tok::comma)
488 break;
489
490 // Consume the comma.
491 ConsumeToken();
492
493 // Parse the next declarator.
494 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000495
496 // Attributes are only allowed on the second declarator.
497 if (Tok.getKind() == tok::kw___attribute)
498 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000499 }
500 }
501
502 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000503 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000504 } else {
505 Diag(Tok, diag::err_expected_semi_decl_list);
506 // Skip to end of block or statement
507 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000508 }
509 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000510
Chris Lattner04f80192006-08-15 04:55:54 +0000511 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000512
513 // If attributes exist after struct contents, parse them.
514 if (Tok.getKind() == tok::kw___attribute)
515 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000516 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000517
518 const char *PrevSpec = 0;
519 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
520 PrevSpec))
521 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000522}
523
524
Chris Lattner3b561a32006-08-13 00:12:11 +0000525/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000526/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000527/// 'enum' identifier[opt] '{' enumerator-list '}'
528/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000529/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
530/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000531/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000532/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000533/// enumerator-list:
534/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000535/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000536/// enumerator:
537/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000538/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000539/// enumeration-constant:
540/// identifier
541///
542void Parser::ParseEnumSpecifier(DeclSpec &DS) {
543 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000544 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000545 ConsumeToken();
546
Chris Lattnere37e2332006-08-15 04:50:22 +0000547 if (Tok.getKind() == tok::kw___attribute)
548 ParseAttributes();
549
Chris Lattner3b561a32006-08-13 00:12:11 +0000550 // Must have either 'enum name' or 'enum {...}'.
551 if (Tok.getKind() != tok::identifier &&
552 Tok.getKind() != tok::l_brace) {
553 Diag(Tok, diag::err_expected_ident_lbrace);
554 return;
555 }
556
557 if (Tok.getKind() == tok::identifier)
558 ConsumeToken();
559
Chris Lattner0fb8b362006-08-14 01:30:12 +0000560 if (Tok.getKind() == tok::l_brace) {
561 SourceLocation LBraceLoc = Tok.getLocation();
562 ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000563
Chris Lattner0fb8b362006-08-14 01:30:12 +0000564 if (Tok.getKind() == tok::r_brace)
565 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
566
567 // Parse the enumerator-list.
568 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000569 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000570
571 if (Tok.getKind() == tok::equal) {
572 ConsumeToken();
573 ExprResult Res = ParseConstantExpression();
574 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
575 }
576
577 if (Tok.getKind() != tok::comma)
578 break;
579 SourceLocation CommaLoc = Tok.getLocation();
580 ConsumeToken();
581
582 if (Tok.getKind() != tok::identifier && !getLang().C99)
583 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000584 }
585
Chris Lattner0fb8b362006-08-14 01:30:12 +0000586 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000587 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000588
589 // If attributes exist after the identifier list, parse them.
590 if (Tok.getKind() == tok::kw___attribute)
591 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000592 }
593 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000594
595
596 const char *PrevSpec = 0;
597 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
598 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000599}
600
601
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000602/// isTypeSpecifierQualifier - Return true if the current token could be the
603/// start of a specifier-qualifier-list.
604bool Parser::isTypeSpecifierQualifier() const {
605 switch (Tok.getKind()) {
606 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000607 // GNU attributes support.
608 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000609 // type-specifiers
610 case tok::kw_short:
611 case tok::kw_long:
612 case tok::kw_signed:
613 case tok::kw_unsigned:
614 case tok::kw__Complex:
615 case tok::kw__Imaginary:
616 case tok::kw_void:
617 case tok::kw_char:
618 case tok::kw_int:
619 case tok::kw_float:
620 case tok::kw_double:
621 case tok::kw__Bool:
622 case tok::kw__Decimal32:
623 case tok::kw__Decimal64:
624 case tok::kw__Decimal128:
625
626 // struct-or-union-specifier
627 case tok::kw_struct:
628 case tok::kw_union:
629 // enum-specifier
630 case tok::kw_enum:
631
632 // type-qualifier
633 case tok::kw_const:
634 case tok::kw_volatile:
635 case tok::kw_restrict:
636 return true;
637
638 // typedef-name
639 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000640 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000641
642 // TODO: Attributes.
643 }
644}
645
Chris Lattneracd58a32006-08-06 17:24:14 +0000646/// isDeclarationSpecifier() - Return true if the current token is part of a
647/// declaration specifier.
648bool Parser::isDeclarationSpecifier() const {
649 switch (Tok.getKind()) {
650 default: return false;
651 // storage-class-specifier
652 case tok::kw_typedef:
653 case tok::kw_extern:
654 case tok::kw_static:
655 case tok::kw_auto:
656 case tok::kw_register:
657 case tok::kw___thread:
658
659 // type-specifiers
660 case tok::kw_short:
661 case tok::kw_long:
662 case tok::kw_signed:
663 case tok::kw_unsigned:
664 case tok::kw__Complex:
665 case tok::kw__Imaginary:
666 case tok::kw_void:
667 case tok::kw_char:
668 case tok::kw_int:
669 case tok::kw_float:
670 case tok::kw_double:
671 case tok::kw__Bool:
672 case tok::kw__Decimal32:
673 case tok::kw__Decimal64:
674 case tok::kw__Decimal128:
675
676 // struct-or-union-specifier
677 case tok::kw_struct:
678 case tok::kw_union:
679 // enum-specifier
680 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000681
Chris Lattneracd58a32006-08-06 17:24:14 +0000682 // type-qualifier
683 case tok::kw_const:
684 case tok::kw_volatile:
685 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000686
Chris Lattneracd58a32006-08-06 17:24:14 +0000687 // function-specifier
688 case tok::kw_inline:
689 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000690
Chris Lattneracd58a32006-08-06 17:24:14 +0000691 // typedef-name
692 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000693 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000694 // TODO: Attributes.
695 }
696}
697
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000698
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000699/// ParseTypeQualifierListOpt
700/// type-qualifier-list: [C99 6.7.5]
701/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000702/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000703/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000704/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000705///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000706void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
707 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000708 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000709 int isInvalid = false;
710 const char *PrevSpec = 0;
711
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000712 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000713 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000714 // If this is not a type-qualifier token, we're done reading type
715 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000716 DS.Finish(StartLoc, Diags, getLang());
717 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000718 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000719 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
720 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000721 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000722 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
723 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000724 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000725 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000726 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000727
728 case tok::kw___attribute:
729 ParseAttributes();
730 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000731 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000732
733 // If the specifier combination wasn't legal, issue a diagnostic.
734 if (isInvalid) {
735 assert(PrevSpec && "Method did not return previous specifier!");
736 if (isInvalid == 1) // Error.
737 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
738 else // extwarn.
739 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
740 }
741 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000742 }
743}
744
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000745
746/// ParseDeclarator - Parse and verify a newly-initialized declarator.
747///
748void Parser::ParseDeclarator(Declarator &D) {
749 /// This implements the 'declarator' production in the C grammar, then checks
750 /// for well-formedness and issues diagnostics.
751 ParseDeclaratorInternal(D);
752
Chris Lattner9fab3b92006-08-12 18:25:42 +0000753 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000754
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000755}
756
757/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000758/// declarator: [C99 6.7.5]
759/// pointer[opt] direct-declarator
760///
761/// pointer: [C99 6.7.5]
762/// '*' type-qualifier-list[opt]
763/// '*' type-qualifier-list[opt] pointer
764///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000765void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000766 if (Tok.getKind() != tok::star)
767 return ParseDirectDeclarator(D);
768
769 // Otherwise, '*' -> pointer.
770 SourceLocation Loc = Tok.getLocation();
771 ConsumeToken(); // Eat the *.
772 DeclSpec DS;
773 ParseTypeQualifierListOpt(DS);
774
775 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000776 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000777
778 // Remember that we parsed a pointer type, and remember the type-quals.
779 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
780}
781
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000782
783/// ParseDirectDeclarator
784/// direct-declarator: [C99 6.7.5]
785/// identifier
786/// '(' declarator ')'
787/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000788/// [C90] direct-declarator '[' constant-expression[opt] ']'
789/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
790/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
791/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
792/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000793/// direct-declarator '(' parameter-type-list ')'
794/// direct-declarator '(' identifier-list[opt] ')'
795/// [GNU] direct-declarator '(' parameter-forward-declarations
796/// parameter-type-list[opt] ')'
797///
Chris Lattneracd58a32006-08-06 17:24:14 +0000798void Parser::ParseDirectDeclarator(Declarator &D) {
799 // Parse the first direct-declarator seen.
800 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
801 assert(Tok.getIdentifierInfo() && "Not an identifier?");
802 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
803 ConsumeToken();
804 } else if (Tok.getKind() == tok::l_paren) {
805 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000806 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000807 // Example: 'char (*X)' or 'int (*XX)(void)'
808 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000809 } else if (D.mayOmitIdentifier()) {
810 // This could be something simple like "int" (in which case the declarator
811 // portion is empty), if an abstract-declarator is allowed.
812 D.SetIdentifier(0, Tok.getLocation());
813 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000814 // Expected identifier or '('.
815 Diag(Tok, diag::err_expected_ident_lparen);
816 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000817 }
818
819 assert(D.isPastIdentifier() &&
820 "Haven't past the location of the identifier yet?");
821
822 while (1) {
823 if (Tok.getKind() == tok::l_paren) {
824 ParseParenDeclarator(D);
825 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000826 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000827 } else {
828 break;
829 }
830 }
831}
832
833/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
834/// either be before the identifier (in which case these are just grouping
835/// parens for precedence) or it may be after the identifier, in which case
836/// these are function arguments.
837///
838/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000839/// parameter-type-list: [C99 6.7.5]
840/// parameter-list
841/// parameter-list ',' '...'
842///
843/// parameter-list: [C99 6.7.5]
844/// parameter-declaration
845/// parameter-list ',' parameter-declaration
846///
847/// parameter-declaration: [C99 6.7.5]
848/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000849/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000850/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000851/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000852///
853/// identifier-list: [C99 6.7.5]
854/// identifier
855/// identifier-list ',' identifier
856///
Chris Lattneracd58a32006-08-06 17:24:14 +0000857void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000858 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000859 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000860
Chris Lattneracd58a32006-08-06 17:24:14 +0000861 // If we haven't past the identifier yet (or where the identifier would be
862 // stored, if this is an abstract declarator), then this is probably just
863 // grouping parens.
864 if (!D.isPastIdentifier()) {
865 // Okay, this is probably a grouping paren. However, if this could be an
866 // abstract-declarator, then this could also be the start of function
867 // arguments (consider 'void()').
868 bool isGrouping;
869
870 if (!D.mayOmitIdentifier()) {
871 // If this can't be an abstract-declarator, this *must* be a grouping
872 // paren, because we haven't seen the identifier yet.
873 isGrouping = true;
874 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
875 isDeclarationSpecifier()) { // 'int(int)' is a function.
876
877 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000878 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000879 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000880 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000881 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000882
883 // If this is a grouping paren, handle:
884 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000885 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000886 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000887 if (Tok.getKind() == tok::kw___attribute)
888 ParseAttributes();
889
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000890 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000891 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000892 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000893 return;
894 }
895
896 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000897 // argument list. Recognize that this declarator will never have an
898 // identifier (and remember where it would have been), then fall through to
899 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000900 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000901 }
902
Chris Lattneracd58a32006-08-06 17:24:14 +0000903 // Okay, this is the parameter list of a function definition, or it is an
904 // identifier list of a K&R-style function.
905
Chris Lattner9fab3b92006-08-12 18:25:42 +0000906 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000907 // arguments to the function scope.
908 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000909 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000910 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000911 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000912 bool ErrorEmitted = false;
913
Chris Lattneracd58a32006-08-06 17:24:14 +0000914 if (Tok.getKind() == tok::r_paren) {
915 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000916 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000917 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000918 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000919 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner8a3e9182006-08-14 15:44:00 +0000920 !Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000921 // Identifier list. Note that '(' identifier-list ')' is only allowed for
922 // normal declarators, not for abstract-declarators.
923 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
924
925 // If there was no identifier specified, either we are in an
926 // abstract-declarator, or we are in a parameter declarator which was found
927 // to be abstract. In abstract-declarators, identifier lists are not valid,
928 // diagnose this.
929 if (!D.getIdentifier())
930 Diag(Tok, diag::ext_ident_list_in_param);
931
Chris Lattner9fab3b92006-08-12 18:25:42 +0000932 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000933 ConsumeToken();
934 while (Tok.getKind() == tok::comma) {
935 // Eat the comma.
936 ConsumeToken();
937
Chris Lattner0be454e2006-08-12 19:30:51 +0000938 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000939 ErrorEmitted = true;
940 break;
941 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000942 }
943
Chris Lattneracd58a32006-08-06 17:24:14 +0000944 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000945 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000946 HasPrototype = false;
947 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000948 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000949 bool ReadArg = false;
950 // Finally, a normal, non-empty parameter type list.
951 while (1) {
952 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000953 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000954
955 // Check to see if this is "void(...)" which is not allowed.
956 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000957 // Otherwise, parse parameter type list. If it starts with an
958 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000959 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000960 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000961 }
962
963 // Consume the ellipsis.
964 ConsumeToken();
965 break;
966 }
967
968 ReadArg = true;
969
970 // Parse the declaration-specifiers.
971 DeclSpec DS;
972 ParseDeclarationSpecifiers(DS);
973
974 // Parse the declarator. This is "PrototypeContext", because we must
975 // accept either 'declarator' or 'abstract-declarator' here.
976 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
977 ParseDeclarator(DeclaratorInfo);
978
Chris Lattnere37e2332006-08-15 04:50:22 +0000979 // Parse GNU attributes, if present.
980 if (Tok.getKind() == tok::kw___attribute)
981 ParseAttributes();
982
Chris Lattneracd58a32006-08-06 17:24:14 +0000983 // TODO: do something with the declarator, if it is valid.
984
985 // If the next token is a comma, consume it and keep reading arguments.
986 if (Tok.getKind() != tok::comma) break;
987
988 // Consume the comma.
989 ConsumeToken();
990 }
991
992 HasPrototype = true;
993 }
994
Chris Lattner9fab3b92006-08-12 18:25:42 +0000995 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000996
Chris Lattner9fab3b92006-08-12 18:25:42 +0000997 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000998
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000999 // Remember that we parsed a function type, and remember the attributes.
1000 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001001 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001002
Chris Lattner14776b92006-08-06 22:27:40 +00001003
1004 // If we have the closing ')', eat it and we're done.
1005 if (Tok.getKind() == tok::r_paren) {
1006 ConsumeParen();
1007 } else {
1008 // If an error happened earlier parsing something else in the proto, don't
1009 // issue another error.
1010 if (!ErrorEmitted)
1011 Diag(Tok, diag::err_expected_rparen);
1012 SkipUntil(tok::r_paren);
1013 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001014}
Chris Lattneracd58a32006-08-06 17:24:14 +00001015
Chris Lattnere8074e62006-08-06 18:30:15 +00001016
1017/// [C90] direct-declarator '[' constant-expression[opt] ']'
1018/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1019/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1020/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1021/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1022void Parser::ParseBracketDeclarator(Declarator &D) {
1023 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +00001024 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001025
1026 // If valid, this location is the position where we read the 'static' keyword.
1027 SourceLocation StaticLoc;
1028 if (Tok.getKind() == tok::kw_static) {
1029 StaticLoc = Tok.getLocation();
1030 ConsumeToken();
1031 }
1032
1033 // If there is a type-qualifier-list, read it now.
1034 DeclSpec DS;
1035 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001036
1037 // If we haven't already read 'static', check to see if there is one after the
1038 // type-qualifier-list.
1039 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
1040 StaticLoc = Tok.getLocation();
1041 ConsumeToken();
1042 }
1043
1044 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001045 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001046 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001047 if (Tok.getKind() == tok::star) {
1048 // Remember the '*' token, in case we have to un-get it.
1049 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001050 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001051
1052 // Check that the ']' token is present to avoid incorrectly parsing
1053 // expressions starting with '*' as [*].
1054 if (Tok.getKind() == tok::r_square) {
1055 if (StaticLoc.isValid())
1056 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1057 StaticLoc = SourceLocation(); // Drop the static.
1058 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001059 } else {
1060 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001061 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001062 // need to reparse it. This handles cases like 'X[*p + 4]'
1063 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001064 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001065 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001066 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001067 NumElements = ParseAssignmentExpression();
1068 }
1069
1070 // If there was an error parsing the assignment-expression, recover.
1071 if (NumElements.isInvalid) {
1072 // If the expression was invalid, skip it.
1073 SkipUntil(tok::r_square);
1074 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001075 }
1076
Chris Lattner04f80192006-08-15 04:55:54 +00001077 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001078
Chris Lattnere8074e62006-08-06 18:30:15 +00001079 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1080 // it was not a constant expression.
1081 if (!getLang().C99) {
1082 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001083 if (isStar || StaticLoc.isValid() ||
1084 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001085 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001086 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001087
1088 // Remember that we parsed a pointer type, and remember the type-quals.
1089 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1090 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001091 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001092}
1093