blob: 469306f92df31669a2ef001e5f71a8cac5a18804 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.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 Lattner289ab7b2006-11-08 06:54:53 +000015#include "clang/Parse/SemaDeclSpec.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000016using namespace llvm;
17using namespace clang;
18
19//===----------------------------------------------------------------------===//
20// C99 6.7: Declarations.
21//===----------------------------------------------------------------------===//
22
Chris Lattnerf5fbd792006-08-10 23:56:11 +000023/// ParseTypeName
24/// type-name: [C99 6.7.6]
25/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000026Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027 // Parse the common declaration-specifiers piece.
28 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000029 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030
31 // Parse the abstract-declarator, if present.
32 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
33 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000034
35 // TODO: Return something useful as the type, obtained from actions.
36 return 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037}
38
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000039/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
54/// any-word
55/// any-word '(' identifier ')'
56/// any-word '(' identifier ',' nonempty-expr-list ')'
57/// any-word '(' expr-list ')'
58///
59void Parser::ParseAttributes() {
60 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
61 ConsumeToken();
62
63 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
64 "attribute"))
65 return;
66
67 // TODO: Parse the attributes.
68 SkipUntil(tok::r_paren, false);
69}
70
Chris Lattnerf5fbd792006-08-10 23:56:11 +000071
Chris Lattner53361ac2006-08-10 05:19:57 +000072/// ParseDeclaration - Parse a full 'declaration', which consists of
73/// declaration-specifiers, some number of declarators, and a semicolon.
74/// 'Context' should be a Declarator::TheContext value.
75void Parser::ParseDeclaration(unsigned Context) {
76 // Parse the common declaration-specifiers piece.
77 DeclSpec DS;
78 ParseDeclarationSpecifiers(DS);
79
Chris Lattner0e894622006-08-13 19:58:17 +000080 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
81 // declaration-specifiers init-declarator-list[opt] ';'
82 if (Tok.getKind() == tok::semi) {
83 // TODO: emit error on 'int;' or 'const enum foo;'.
84 // if (!DS.isMissingDeclaratorOk()) Diag(...);
85
Chris Lattner289ab7b2006-11-08 06:54:53 +000086 // TODO: Register 'struct foo;' with the type system as an opaque struct.
87 // TODO: Check that we don't already have 'union foo;' or something else
88 // that conflicts.
89
Chris Lattner0e894622006-08-13 19:58:17 +000090 ConsumeToken();
91 return;
92 }
93
Chris Lattner53361ac2006-08-10 05:19:57 +000094 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
95 ParseDeclarator(DeclaratorInfo);
96
97 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
98}
99
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000100/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
101/// parsing 'declaration-specifiers declarator'. This method is split out this
102/// way to handle the ambiguity between top-level function-definitions and
103/// declarations.
104///
105/// declaration: [C99 6.7]
106/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
107/// [!C99] init-declarator-list ';' [TODO]
108/// [OMP] threadprivate-directive [TODO]
109///
110/// init-declarator-list: [C99 6.7]
111/// init-declarator
112/// init-declarator-list ',' init-declarator
113/// init-declarator: [C99 6.7]
114/// declarator
115/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000116/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
117/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000118///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000119Parser::DeclTy *Parser::
120ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
121
122 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
123 // that they can be chained properly if the actions want this.
124 Parser::DeclTy *LastDeclInGroup = 0;
125
Chris Lattner53361ac2006-08-10 05:19:57 +0000126 // At this point, we know that it is not a function definition. Parse the
127 // rest of the init-declarator-list.
128 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000129 // If a simple-asm-expr is present, parse it.
130 if (Tok.getKind() == tok::kw_asm)
131 ParseSimpleAsm();
132
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000133 // If attributes are present, parse them.
134 if (Tok.getKind() == tok::kw___attribute)
135 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000136
Chris Lattner53361ac2006-08-10 05:19:57 +0000137 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000138 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000139 if (Tok.getKind() == tok::equal) {
140 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000141 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000142 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000143 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000144 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000145 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000146 }
147
Chris Lattner697e5d62006-11-09 06:32:27 +0000148 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000149 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000150 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
151 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000152
153 // If we don't have a comma, it is either the end of the list (a ';') or an
154 // error, bail out.
155 if (Tok.getKind() != tok::comma)
156 break;
157
158 // Consume the comma.
159 ConsumeToken();
160
161 // Parse the next declarator.
162 D.clear();
163 ParseDeclarator(D);
164 }
165
166 if (Tok.getKind() == tok::semi) {
167 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000168 return LastDeclInGroup;
Chris Lattner53361ac2006-08-10 05:19:57 +0000169 } else {
170 Diag(Tok, diag::err_parse_error);
171 // Skip to end of block or statement
172 SkipUntil(tok::r_brace, true);
173 if (Tok.getKind() == tok::semi)
174 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000175 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000176 }
177}
178
Chris Lattner1890ac82006-08-13 01:16:23 +0000179/// ParseSpecifierQualifierList
180/// specifier-qualifier-list:
181/// type-specifier specifier-qualifier-list[opt]
182/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000183/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000184///
185void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
186 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
187 /// parse declaration-specifiers and complain about extra stuff.
188 SourceLocation Loc = Tok.getLocation();
189 ParseDeclarationSpecifiers(DS);
190
191 // Validate declspec for type-name.
192 unsigned Specs = DS.getParsedSpecifiers();
193 if (Specs == DeclSpec::PQ_None)
194 Diag(Tok, diag::err_typename_requires_specqual);
195
196 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
197 Diag(Loc, diag::err_typename_invalid_storageclass);
198 // Remove storage class.
199 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
200 DS.SCS_thread_specified = false;
201 }
202 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
203 Diag(Loc, diag::err_typename_invalid_functionspec);
204 DS.FS_inline_specified = false;
205 }
206}
Chris Lattner53361ac2006-08-10 05:19:57 +0000207
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000208/// ParseDeclarationSpecifiers
209/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000210/// storage-class-specifier declaration-specifiers[opt]
211/// type-specifier declaration-specifiers[opt]
212/// type-qualifier declaration-specifiers[opt]
213/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000214/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000215///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000216/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000217/// 'typedef'
218/// 'extern'
219/// 'static'
220/// 'auto'
221/// 'register'
222/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000223/// type-specifier: [C99 6.7.2]
224/// 'void'
225/// 'char'
226/// 'short'
227/// 'int'
228/// 'long'
229/// 'float'
230/// 'double'
231/// 'signed'
232/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000233/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000234/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000235/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000236/// [C99] '_Bool'
237/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000238/// [C99] '_Imaginary' // Removed in TC2?
239/// [GNU] '_Decimal32'
240/// [GNU] '_Decimal64'
241/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000242/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000243/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000244/// [OBJC] typedef-name objc-protocol-refs [TODO]
245/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000246/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000247/// 'const'
248/// 'volatile'
249/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000250/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000251/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000252///
253void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
254 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000255 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000256 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000257 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000258 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000259 // typedef-name
260 case tok::identifier:
261 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000262 // a type-specifier. Without this check we misparse:
263 // typedef int X; struct Y { short X; }; as 'short int'.
264 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
265 DS.TypeSpecWidth == DeclSpec::TSW_unspecified &&
266 DS.TypeSpecComplex == DeclSpec::TSC_unspecified &&
267 DS.TypeSpecSign == DeclSpec::TSS_unspecified &&
268 // It has to be available as a typedef too!
Steve Naroffb419d3a2006-10-27 23:18:49 +0000269 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000270 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
271 break;
272 }
273 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000274 default:
275 // If this is not a declaration specifier token, we're done reading decl
276 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000277 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000278 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000279
280 // GNU attributes support.
281 case tok::kw___attribute:
282 ParseAttributes();
Chris Lattnerb95cca02006-10-17 03:01:08 +0000283 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000284
285 // storage-class-specifier
286 case tok::kw_typedef:
287 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
288 break;
289 case tok::kw_extern:
290 if (DS.SCS_thread_specified)
291 Diag(Tok, diag::ext_thread_before, "extern");
292 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
293 break;
294 case tok::kw_static:
295 if (DS.SCS_thread_specified)
296 Diag(Tok, diag::ext_thread_before, "static");
297 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
298 break;
299 case tok::kw_auto:
300 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
301 break;
302 case tok::kw_register:
303 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
304 break;
305 case tok::kw___thread:
306 if (DS.SCS_thread_specified)
307 isInvalid = 2, PrevSpec = "__thread";
308 else
309 DS.SCS_thread_specified = true;
310 break;
311
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000312 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000313 case tok::kw_short:
314 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
315 break;
316 case tok::kw_long:
317 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
318 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
319 } else {
320 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
321 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
322 }
323 break;
324 case tok::kw_signed:
325 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
326 break;
327 case tok::kw_unsigned:
328 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
329 break;
330 case tok::kw__Complex:
331 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
332 break;
333 case tok::kw__Imaginary:
334 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
335 break;
336 case tok::kw_void:
337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
338 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000339 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
341 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000342 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000343 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
344 break;
345 case tok::kw_float:
346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
347 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000348 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
350 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000351 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000352 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
353 break;
354 case tok::kw__Decimal32:
355 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
356 break;
357 case tok::kw__Decimal64:
358 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
359 break;
360 case tok::kw__Decimal128:
361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000362 break;
363
Chris Lattner1890ac82006-08-13 01:16:23 +0000364 case tok::kw_struct:
365 case tok::kw_union:
366 ParseStructUnionSpecifier(DS);
367 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000368 case tok::kw_enum:
369 ParseEnumSpecifier(DS);
370 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000371
372 // type-qualifier
373 case tok::kw_const:
374 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
375 break;
376 case tok::kw_volatile:
377 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
378 break;
379 case tok::kw_restrict:
380 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
381 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000382
383 // function-specifier
384 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000385 // 'inline inline' is ok.
386 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000387 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000388 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000389 // If the specifier combination wasn't legal, issue a diagnostic.
390 if (isInvalid) {
391 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000392 if (isInvalid == 1) // Error.
393 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
394 else // extwarn.
395 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000396 }
397 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000398 }
399}
400
Chris Lattner1890ac82006-08-13 01:16:23 +0000401
402/// ParseStructUnionSpecifier
403/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000404/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000405/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000406/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
407/// '}' attributes[opt]
408/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000409/// struct-or-union:
410/// 'struct'
411/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000412/// struct-contents:
413/// struct-declaration-list
414/// [EXT] empty
415/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000416/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000417/// struct-declaration
418/// struct-declaration-list struct-declaration
419/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
420/// struct-declaration:
421/// specifier-qualifier-list struct-declarator-list ';'
422/// [GNU] __extension__ struct-declaration [TODO]
423/// [GNU] specifier-qualifier-list ';' [TODO]
424/// struct-declarator-list:
425/// struct-declarator
426/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000427/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000428/// struct-declarator:
429/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000430/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000431/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000432/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000433///
434void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
435 assert((Tok.getKind() == tok::kw_struct ||
436 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
437 bool isUnion = Tok.getKind() == tok::kw_union;
Chris Lattneraf635312006-10-16 06:06:51 +0000438 SourceLocation Start = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000439
440 // If attributes exist after tag, parse them.
441 if (Tok.getKind() == tok::kw___attribute)
442 ParseAttributes();
443
Chris Lattner1890ac82006-08-13 01:16:23 +0000444 // Must have either 'struct name' or 'struct {...}'.
445 if (Tok.getKind() != tok::identifier &&
446 Tok.getKind() != tok::l_brace) {
447 Diag(Tok, diag::err_expected_ident_lbrace);
448 return;
449 }
450
451 if (Tok.getKind() == tok::identifier)
452 ConsumeToken();
453
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000454 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000455 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000456
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000457 if (Tok.getKind() == tok::r_brace)
458 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000459
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000460 while (Tok.getKind() != tok::r_brace &&
461 Tok.getKind() != tok::eof) {
462 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000463
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000464 // Parse the common specifier-qualifiers-list piece.
465 DeclSpec DS;
466 SourceLocation SpecQualLoc = Tok.getLocation();
467 ParseSpecifierQualifierList(DS);
468 // TODO: Does specifier-qualifier list correctly check that *something* is
469 // specified?
470
471 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000472
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000473 // If there are no declarators, issue a warning.
474 if (Tok.getKind() == tok::semi) {
475 Diag(SpecQualLoc, diag::w_no_declarators);
476 } else {
477 // Read struct-declarators until we find the semicolon.
478 while (1) {
479 /// struct-declarator: declarator
480 /// struct-declarator: declarator[opt] ':' constant-expression
481 if (Tok.getKind() != tok::colon)
482 ParseDeclarator(DeclaratorInfo);
483
484 if (Tok.getKind() == tok::colon) {
485 ConsumeToken();
486 ExprResult Res = ParseConstantExpression();
487 if (Res.isInvalid) {
488 SkipUntil(tok::semi, true, true);
489 } else {
490 // Process it.
491 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000492 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000493
494 // If attributes exist after the declarator, parse them.
495 if (Tok.getKind() == tok::kw___attribute)
496 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000497
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000498 // TODO: install declarator.
499
500 // If we don't have a comma, it is either the end of the list (a ';')
501 // or an error, bail out.
502 if (Tok.getKind() != tok::comma)
503 break;
504
505 // Consume the comma.
506 ConsumeToken();
507
508 // Parse the next declarator.
509 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000510
511 // Attributes are only allowed on the second declarator.
512 if (Tok.getKind() == tok::kw___attribute)
513 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000514 }
515 }
516
517 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000518 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000519 } else {
520 Diag(Tok, diag::err_expected_semi_decl_list);
521 // Skip to end of block or statement
522 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000523 }
524 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000525
Chris Lattner04f80192006-08-15 04:55:54 +0000526 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000527
528 // If attributes exist after struct contents, parse them.
529 if (Tok.getKind() == tok::kw___attribute)
530 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000531 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000532
533 const char *PrevSpec = 0;
534 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
535 PrevSpec))
536 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000537}
538
539
Chris Lattner3b561a32006-08-13 00:12:11 +0000540/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000541/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000542/// 'enum' identifier[opt] '{' enumerator-list '}'
543/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000544/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
545/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000546/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000547/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000548/// enumerator-list:
549/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000550/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000551/// enumerator:
552/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000553/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000554/// enumeration-constant:
555/// identifier
556///
557void Parser::ParseEnumSpecifier(DeclSpec &DS) {
558 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattneraf635312006-10-16 06:06:51 +0000559 SourceLocation Start = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000560
Chris Lattnere37e2332006-08-15 04:50:22 +0000561 if (Tok.getKind() == tok::kw___attribute)
562 ParseAttributes();
563
Chris Lattner3b561a32006-08-13 00:12:11 +0000564 // Must have either 'enum name' or 'enum {...}'.
565 if (Tok.getKind() != tok::identifier &&
566 Tok.getKind() != tok::l_brace) {
567 Diag(Tok, diag::err_expected_ident_lbrace);
568 return;
569 }
570
571 if (Tok.getKind() == tok::identifier)
572 ConsumeToken();
573
Chris Lattner0fb8b362006-08-14 01:30:12 +0000574 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000575 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000576
Chris Lattner0fb8b362006-08-14 01:30:12 +0000577 if (Tok.getKind() == tok::r_brace)
578 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
579
580 // Parse the enumerator-list.
581 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000582 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000583
584 if (Tok.getKind() == tok::equal) {
585 ConsumeToken();
586 ExprResult Res = ParseConstantExpression();
587 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
588 }
589
590 if (Tok.getKind() != tok::comma)
591 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000592 SourceLocation CommaLoc = ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000593
594 if (Tok.getKind() != tok::identifier && !getLang().C99)
595 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000596 }
597
Chris Lattner0fb8b362006-08-14 01:30:12 +0000598 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000599 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000600
601 // If attributes exist after the identifier list, parse them.
602 if (Tok.getKind() == tok::kw___attribute)
603 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000604 }
605 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000606
607
608 const char *PrevSpec = 0;
609 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
610 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000611}
612
613
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000614/// isTypeSpecifierQualifier - Return true if the current token could be the
615/// start of a specifier-qualifier-list.
616bool Parser::isTypeSpecifierQualifier() const {
617 switch (Tok.getKind()) {
618 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000619 // GNU attributes support.
620 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000621 // type-specifiers
622 case tok::kw_short:
623 case tok::kw_long:
624 case tok::kw_signed:
625 case tok::kw_unsigned:
626 case tok::kw__Complex:
627 case tok::kw__Imaginary:
628 case tok::kw_void:
629 case tok::kw_char:
630 case tok::kw_int:
631 case tok::kw_float:
632 case tok::kw_double:
633 case tok::kw__Bool:
634 case tok::kw__Decimal32:
635 case tok::kw__Decimal64:
636 case tok::kw__Decimal128:
637
638 // struct-or-union-specifier
639 case tok::kw_struct:
640 case tok::kw_union:
641 // enum-specifier
642 case tok::kw_enum:
643
644 // type-qualifier
645 case tok::kw_const:
646 case tok::kw_volatile:
647 case tok::kw_restrict:
648 return true;
649
650 // typedef-name
651 case tok::identifier:
Steve Naroffb419d3a2006-10-27 23:18:49 +0000652 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000653
654 // TODO: Attributes.
655 }
656}
657
Chris Lattneracd58a32006-08-06 17:24:14 +0000658/// isDeclarationSpecifier() - Return true if the current token is part of a
659/// declaration specifier.
660bool Parser::isDeclarationSpecifier() const {
661 switch (Tok.getKind()) {
662 default: return false;
663 // storage-class-specifier
664 case tok::kw_typedef:
665 case tok::kw_extern:
666 case tok::kw_static:
667 case tok::kw_auto:
668 case tok::kw_register:
669 case tok::kw___thread:
670
671 // type-specifiers
672 case tok::kw_short:
673 case tok::kw_long:
674 case tok::kw_signed:
675 case tok::kw_unsigned:
676 case tok::kw__Complex:
677 case tok::kw__Imaginary:
678 case tok::kw_void:
679 case tok::kw_char:
680 case tok::kw_int:
681 case tok::kw_float:
682 case tok::kw_double:
683 case tok::kw__Bool:
684 case tok::kw__Decimal32:
685 case tok::kw__Decimal64:
686 case tok::kw__Decimal128:
687
688 // struct-or-union-specifier
689 case tok::kw_struct:
690 case tok::kw_union:
691 // enum-specifier
692 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000693
Chris Lattneracd58a32006-08-06 17:24:14 +0000694 // type-qualifier
695 case tok::kw_const:
696 case tok::kw_volatile:
697 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000698
Chris Lattneracd58a32006-08-06 17:24:14 +0000699 // function-specifier
700 case tok::kw_inline:
701 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000702
Chris Lattneracd58a32006-08-06 17:24:14 +0000703 // typedef-name
704 case tok::identifier:
Steve Naroffb419d3a2006-10-27 23:18:49 +0000705 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000706 // TODO: Attributes.
707 }
708}
709
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000710
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000711/// ParseTypeQualifierListOpt
712/// type-qualifier-list: [C99 6.7.5]
713/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000714/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000715/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000716/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000717///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000718void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
719 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000720 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000721 int isInvalid = false;
722 const char *PrevSpec = 0;
723
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000724 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000725 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000726 // If this is not a type-qualifier token, we're done reading type
727 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000728 DS.Finish(StartLoc, Diags, getLang());
729 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000730 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000731 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
732 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000733 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000734 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
735 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000736 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000737 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000738 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000739
740 case tok::kw___attribute:
741 ParseAttributes();
742 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000743 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000744
745 // If the specifier combination wasn't legal, issue a diagnostic.
746 if (isInvalid) {
747 assert(PrevSpec && "Method did not return previous specifier!");
748 if (isInvalid == 1) // Error.
749 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
750 else // extwarn.
751 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
752 }
753 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000754 }
755}
756
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000757
758/// ParseDeclarator - Parse and verify a newly-initialized declarator.
759///
760void Parser::ParseDeclarator(Declarator &D) {
761 /// This implements the 'declarator' production in the C grammar, then checks
762 /// for well-formedness and issues diagnostics.
763 ParseDeclaratorInternal(D);
764
Chris Lattner9fab3b92006-08-12 18:25:42 +0000765 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000766
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000767}
768
769/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000770/// declarator: [C99 6.7.5]
771/// pointer[opt] direct-declarator
772///
773/// pointer: [C99 6.7.5]
774/// '*' type-qualifier-list[opt]
775/// '*' type-qualifier-list[opt] pointer
776///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000777void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000778 if (Tok.getKind() != tok::star)
779 return ParseDirectDeclarator(D);
780
781 // Otherwise, '*' -> pointer.
Chris Lattneraf635312006-10-16 06:06:51 +0000782 SourceLocation Loc = ConsumeToken(); // Eat the *.
Chris Lattner6c7416c2006-08-07 00:19:33 +0000783 DeclSpec DS;
784 ParseTypeQualifierListOpt(DS);
785
786 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000787 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000788
789 // Remember that we parsed a pointer type, and remember the type-quals.
790 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
791}
792
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000793
794/// ParseDirectDeclarator
795/// direct-declarator: [C99 6.7.5]
796/// identifier
797/// '(' declarator ')'
798/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000799/// [C90] direct-declarator '[' constant-expression[opt] ']'
800/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
801/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
802/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
803/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000804/// direct-declarator '(' parameter-type-list ')'
805/// direct-declarator '(' identifier-list[opt] ')'
806/// [GNU] direct-declarator '(' parameter-forward-declarations
807/// parameter-type-list[opt] ')'
808///
Chris Lattneracd58a32006-08-06 17:24:14 +0000809void Parser::ParseDirectDeclarator(Declarator &D) {
810 // Parse the first direct-declarator seen.
811 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
812 assert(Tok.getIdentifierInfo() && "Not an identifier?");
813 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
814 ConsumeToken();
815 } else if (Tok.getKind() == tok::l_paren) {
816 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000817 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000818 // Example: 'char (*X)' or 'int (*XX)(void)'
819 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000820 } else if (D.mayOmitIdentifier()) {
821 // This could be something simple like "int" (in which case the declarator
822 // portion is empty), if an abstract-declarator is allowed.
823 D.SetIdentifier(0, Tok.getLocation());
824 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000825 // Expected identifier or '('.
826 Diag(Tok, diag::err_expected_ident_lparen);
827 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000828 }
829
830 assert(D.isPastIdentifier() &&
831 "Haven't past the location of the identifier yet?");
832
833 while (1) {
834 if (Tok.getKind() == tok::l_paren) {
835 ParseParenDeclarator(D);
836 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000837 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000838 } else {
839 break;
840 }
841 }
842}
843
844/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
845/// either be before the identifier (in which case these are just grouping
846/// parens for precedence) or it may be after the identifier, in which case
847/// these are function arguments.
848///
849/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000850/// parameter-type-list: [C99 6.7.5]
851/// parameter-list
852/// parameter-list ',' '...'
853///
854/// parameter-list: [C99 6.7.5]
855/// parameter-declaration
856/// parameter-list ',' parameter-declaration
857///
858/// parameter-declaration: [C99 6.7.5]
859/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000860/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000861/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000862/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000863///
864/// identifier-list: [C99 6.7.5]
865/// identifier
866/// identifier-list ',' identifier
867///
Chris Lattneracd58a32006-08-06 17:24:14 +0000868void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +0000869 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000870
Chris Lattneracd58a32006-08-06 17:24:14 +0000871 // If we haven't past the identifier yet (or where the identifier would be
872 // stored, if this is an abstract declarator), then this is probably just
873 // grouping parens.
874 if (!D.isPastIdentifier()) {
875 // Okay, this is probably a grouping paren. However, if this could be an
876 // abstract-declarator, then this could also be the start of function
877 // arguments (consider 'void()').
878 bool isGrouping;
879
880 if (!D.mayOmitIdentifier()) {
881 // If this can't be an abstract-declarator, this *must* be a grouping
882 // paren, because we haven't seen the identifier yet.
883 isGrouping = true;
884 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
885 isDeclarationSpecifier()) { // 'int(int)' is a function.
886
887 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000888 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000889 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000890 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000891 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000892
893 // If this is a grouping paren, handle:
894 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000895 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000896 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000897 if (Tok.getKind() == tok::kw___attribute)
898 ParseAttributes();
899
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000900 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000901 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000902 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000903 return;
904 }
905
906 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000907 // argument list. Recognize that this declarator will never have an
908 // identifier (and remember where it would have been), then fall through to
909 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000910 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000911 }
912
Chris Lattneracd58a32006-08-06 17:24:14 +0000913 // Okay, this is the parameter list of a function definition, or it is an
914 // identifier list of a K&R-style function.
915
Chris Lattner9fab3b92006-08-12 18:25:42 +0000916 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000917 // arguments to the function scope.
918 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000919 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000920 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000921 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000922 bool ErrorEmitted = false;
923
Chris Lattneracd58a32006-08-06 17:24:14 +0000924 if (Tok.getKind() == tok::r_paren) {
925 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000926 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000927 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000928 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000929 } else if (Tok.getKind() == tok::identifier &&
Steve Naroffb419d3a2006-10-27 23:18:49 +0000930 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000931 // Identifier list. Note that '(' identifier-list ')' is only allowed for
932 // normal declarators, not for abstract-declarators.
933 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
934
935 // If there was no identifier specified, either we are in an
936 // abstract-declarator, or we are in a parameter declarator which was found
937 // to be abstract. In abstract-declarators, identifier lists are not valid,
938 // diagnose this.
939 if (!D.getIdentifier())
940 Diag(Tok, diag::ext_ident_list_in_param);
941
Chris Lattner9fab3b92006-08-12 18:25:42 +0000942 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000943 ConsumeToken();
944 while (Tok.getKind() == tok::comma) {
945 // Eat the comma.
946 ConsumeToken();
947
Chris Lattner0be454e2006-08-12 19:30:51 +0000948 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000949 ErrorEmitted = true;
950 break;
951 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000952 }
953
Chris Lattneracd58a32006-08-06 17:24:14 +0000954 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000955 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000956 HasPrototype = false;
957 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000958 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000959 bool ReadArg = false;
960 // Finally, a normal, non-empty parameter type list.
961 while (1) {
962 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000963 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000964
965 // Check to see if this is "void(...)" which is not allowed.
966 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000967 // Otherwise, parse parameter type list. If it starts with an
968 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000969 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000970 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000971 }
972
973 // Consume the ellipsis.
974 ConsumeToken();
975 break;
976 }
977
978 ReadArg = true;
979
980 // Parse the declaration-specifiers.
981 DeclSpec DS;
982 ParseDeclarationSpecifiers(DS);
983
984 // Parse the declarator. This is "PrototypeContext", because we must
985 // accept either 'declarator' or 'abstract-declarator' here.
986 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
987 ParseDeclarator(DeclaratorInfo);
988
Chris Lattnere37e2332006-08-15 04:50:22 +0000989 // Parse GNU attributes, if present.
990 if (Tok.getKind() == tok::kw___attribute)
991 ParseAttributes();
992
Chris Lattneracd58a32006-08-06 17:24:14 +0000993 // TODO: do something with the declarator, if it is valid.
994
995 // If the next token is a comma, consume it and keep reading arguments.
996 if (Tok.getKind() != tok::comma) break;
997
998 // Consume the comma.
999 ConsumeToken();
1000 }
1001
1002 HasPrototype = true;
1003 }
1004
Chris Lattner9fab3b92006-08-12 18:25:42 +00001005 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001006
Chris Lattner9fab3b92006-08-12 18:25:42 +00001007 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +00001008
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001009 // Remember that we parsed a function type, and remember the attributes.
1010 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001011 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001012
Chris Lattner14776b92006-08-06 22:27:40 +00001013
1014 // If we have the closing ')', eat it and we're done.
1015 if (Tok.getKind() == tok::r_paren) {
1016 ConsumeParen();
1017 } else {
1018 // If an error happened earlier parsing something else in the proto, don't
1019 // issue another error.
1020 if (!ErrorEmitted)
1021 Diag(Tok, diag::err_expected_rparen);
1022 SkipUntil(tok::r_paren);
1023 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001024}
Chris Lattneracd58a32006-08-06 17:24:14 +00001025
Chris Lattnere8074e62006-08-06 18:30:15 +00001026
1027/// [C90] direct-declarator '[' constant-expression[opt] ']'
1028/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1029/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1030/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1031/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1032void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001033 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001034
1035 // If valid, this location is the position where we read the 'static' keyword.
1036 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001037 if (Tok.getKind() == tok::kw_static)
1038 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001039
1040 // If there is a type-qualifier-list, read it now.
1041 DeclSpec DS;
1042 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001043
1044 // If we haven't already read 'static', check to see if there is one after the
1045 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001046 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1047 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001048
1049 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001050 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001051 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001052 if (Tok.getKind() == tok::star) {
1053 // Remember the '*' token, in case we have to un-get it.
1054 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001055 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001056
1057 // Check that the ']' token is present to avoid incorrectly parsing
1058 // expressions starting with '*' as [*].
1059 if (Tok.getKind() == tok::r_square) {
1060 if (StaticLoc.isValid())
1061 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1062 StaticLoc = SourceLocation(); // Drop the static.
1063 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001064 } else {
1065 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001066 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001067 // need to reparse it. This handles cases like 'X[*p + 4]'
1068 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001069 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001070 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001071 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001072 NumElements = ParseAssignmentExpression();
1073 }
1074
1075 // If there was an error parsing the assignment-expression, recover.
1076 if (NumElements.isInvalid) {
1077 // If the expression was invalid, skip it.
1078 SkipUntil(tok::r_square);
1079 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001080 }
1081
Chris Lattner04f80192006-08-15 04:55:54 +00001082 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001083
Chris Lattnere8074e62006-08-06 18:30:15 +00001084 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1085 // it was not a constant expression.
1086 if (!getLang().C99) {
1087 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001088 if (isStar || StaticLoc.isValid() ||
1089 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001090 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001091 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001092
1093 // Remember that we parsed a pointer type, and remember the type-quals.
1094 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1095 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001096 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001097}
1098