blob: 0629cfb52e5adef6b00f93c241e3141feeb89462 [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 Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.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
Chris Lattner558cb292006-11-19 01:31:06 +000035 return Actions.ParseTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036}
37
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000038/// ParseAttributes - Parse a non-empty attributes list.
39///
40/// [GNU] attributes:
41/// attribute
42/// attributes attribute
43///
44/// [GNU] attribute:
45/// '__attribute__' '(' '(' attribute-list ')' ')'
46///
47/// [GNU] attribute-list:
48/// attrib
49/// attribute_list ',' attrib
50///
51/// [GNU] attrib:
52/// empty
53/// any-word
54/// any-word '(' identifier ')'
55/// any-word '(' identifier ',' nonempty-expr-list ')'
56/// any-word '(' expr-list ')'
57///
58void Parser::ParseAttributes() {
59 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
60 ConsumeToken();
61
62 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
63 "attribute"))
64 return;
65
66 // TODO: Parse the attributes.
67 SkipUntil(tok::r_paren, false);
68}
69
Chris Lattnerf5fbd792006-08-10 23:56:11 +000070
Chris Lattner53361ac2006-08-10 05:19:57 +000071/// ParseDeclaration - Parse a full 'declaration', which consists of
72/// declaration-specifiers, some number of declarators, and a semicolon.
73/// 'Context' should be a Declarator::TheContext value.
Chris Lattner302b4be2006-11-19 02:31:38 +000074Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +000075 // Parse the common declaration-specifiers piece.
76 DeclSpec DS;
77 ParseDeclarationSpecifiers(DS);
78
Chris Lattner0e894622006-08-13 19:58:17 +000079 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
80 // declaration-specifiers init-declarator-list[opt] ';'
81 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +000082 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +000083 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +000084 }
85
Chris Lattner53361ac2006-08-10 05:19:57 +000086 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
87 ParseDeclarator(DeclaratorInfo);
88
Chris Lattner302b4be2006-11-19 02:31:38 +000089 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +000090}
91
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000092/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
93/// parsing 'declaration-specifiers declarator'. This method is split out this
94/// way to handle the ambiguity between top-level function-definitions and
95/// declarations.
96///
97/// declaration: [C99 6.7]
98/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
99/// [!C99] init-declarator-list ';' [TODO]
100/// [OMP] threadprivate-directive [TODO]
101///
102/// init-declarator-list: [C99 6.7]
103/// init-declarator
104/// init-declarator-list ',' init-declarator
105/// init-declarator: [C99 6.7]
106/// declarator
107/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000108/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
109/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000110///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000111Parser::DeclTy *Parser::
112ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
113
114 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
115 // that they can be chained properly if the actions want this.
116 Parser::DeclTy *LastDeclInGroup = 0;
117
Chris Lattner53361ac2006-08-10 05:19:57 +0000118 // At this point, we know that it is not a function definition. Parse the
119 // rest of the init-declarator-list.
120 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000121 // If a simple-asm-expr is present, parse it.
122 if (Tok.getKind() == tok::kw_asm)
123 ParseSimpleAsm();
124
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000125 // If attributes are present, parse them.
126 if (Tok.getKind() == tok::kw___attribute)
127 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000128
Chris Lattner53361ac2006-08-10 05:19:57 +0000129 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000130 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000131 if (Tok.getKind() == tok::equal) {
132 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000133 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000134 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000135 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000136 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000137 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000138 }
139
Chris Lattner697e5d62006-11-09 06:32:27 +0000140 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000141 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000142 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
143 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000144
145 // If we don't have a comma, it is either the end of the list (a ';') or an
146 // error, bail out.
147 if (Tok.getKind() != tok::comma)
148 break;
149
150 // Consume the comma.
151 ConsumeToken();
152
153 // Parse the next declarator.
154 D.clear();
155 ParseDeclarator(D);
156 }
157
158 if (Tok.getKind() == tok::semi) {
159 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000160 return LastDeclInGroup;
Chris Lattner53361ac2006-08-10 05:19:57 +0000161 } else {
162 Diag(Tok, diag::err_parse_error);
163 // Skip to end of block or statement
164 SkipUntil(tok::r_brace, true);
165 if (Tok.getKind() == tok::semi)
166 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000167 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000168 }
169}
170
Chris Lattner1890ac82006-08-13 01:16:23 +0000171/// ParseSpecifierQualifierList
172/// specifier-qualifier-list:
173/// type-specifier specifier-qualifier-list[opt]
174/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000175/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000176///
177void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
178 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
179 /// parse declaration-specifiers and complain about extra stuff.
180 SourceLocation Loc = Tok.getLocation();
181 ParseDeclarationSpecifiers(DS);
182
183 // Validate declspec for type-name.
184 unsigned Specs = DS.getParsedSpecifiers();
185 if (Specs == DeclSpec::PQ_None)
186 Diag(Tok, diag::err_typename_requires_specqual);
187
188 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
189 Diag(Loc, diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000190 // FIXME: better loc info for this!
Chris Lattner1890ac82006-08-13 01:16:23 +0000191 // Remove storage class.
Chris Lattnera925dc62006-11-28 04:33:46 +0000192 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000193 }
194 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattnera925dc62006-11-28 04:33:46 +0000195 // FIXME: better loc info for this!
Chris Lattner1890ac82006-08-13 01:16:23 +0000196 Diag(Loc, diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000197 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000198 }
199}
Chris Lattner53361ac2006-08-10 05:19:57 +0000200
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000201/// ParseDeclarationSpecifiers
202/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000203/// storage-class-specifier declaration-specifiers[opt]
204/// type-specifier declaration-specifiers[opt]
205/// type-qualifier declaration-specifiers[opt]
206/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000207/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000208///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000209/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000210/// 'typedef'
211/// 'extern'
212/// 'static'
213/// 'auto'
214/// 'register'
215/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000216/// type-specifier: [C99 6.7.2]
217/// 'void'
218/// 'char'
219/// 'short'
220/// 'int'
221/// 'long'
222/// 'float'
223/// 'double'
224/// 'signed'
225/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000226/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000227/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000228/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000229/// [C99] '_Bool'
230/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000231/// [C99] '_Imaginary' // Removed in TC2?
232/// [GNU] '_Decimal32'
233/// [GNU] '_Decimal64'
234/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000235/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000236/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000237/// [OBJC] typedef-name objc-protocol-refs [TODO]
238/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000239/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000240/// 'const'
241/// 'volatile'
242/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000243/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000244/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000245///
246void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
247 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000248 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000249 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000250 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000251 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000252 // typedef-name
253 case tok::identifier:
254 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000255 // a type-specifier. Without this check we misparse:
256 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000257 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000258 // It has to be available as a typedef too!
259 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
260 CurScope)) {
261 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec,
262 TypeRep);
263 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000264 break;
265 }
266 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000267 default:
268 // If this is not a declaration specifier token, we're done reading decl
269 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000270 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000271 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000272
273 // GNU attributes support.
274 case tok::kw___attribute:
275 ParseAttributes();
Chris Lattnerb95cca02006-10-17 03:01:08 +0000276 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000277
278 // storage-class-specifier
279 case tok::kw_typedef:
280 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
281 break;
282 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000283 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000284 Diag(Tok, diag::ext_thread_before, "extern");
285 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
286 break;
287 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000288 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000289 Diag(Tok, diag::ext_thread_before, "static");
290 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
291 break;
292 case tok::kw_auto:
293 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
294 break;
295 case tok::kw_register:
296 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
297 break;
298 case tok::kw___thread:
Chris Lattner353f5742006-11-28 04:50:12 +0000299 isInvalid = DS.SetStorageClassSpecThread(PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000300 break;
301
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000302 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000303 case tok::kw_short:
304 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
305 break;
306 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000307 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000308 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000309 else
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000310 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000311 break;
312 case tok::kw_signed:
313 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
314 break;
315 case tok::kw_unsigned:
316 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
317 break;
318 case tok::kw__Complex:
319 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
320 break;
321 case tok::kw__Imaginary:
322 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
323 break;
324 case tok::kw_void:
325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
326 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000327 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
329 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000330 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
332 break;
333 case tok::kw_float:
334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
335 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000336 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
338 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000339 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
341 break;
342 case tok::kw__Decimal32:
343 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
344 break;
345 case tok::kw__Decimal64:
346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
347 break;
348 case tok::kw__Decimal128:
349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000350 break;
351
Chris Lattner1890ac82006-08-13 01:16:23 +0000352 case tok::kw_struct:
353 case tok::kw_union:
354 ParseStructUnionSpecifier(DS);
355 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000356 case tok::kw_enum:
357 ParseEnumSpecifier(DS);
358 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000359
360 // type-qualifier
361 case tok::kw_const:
362 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
363 break;
364 case tok::kw_volatile:
365 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
366 break;
367 case tok::kw_restrict:
368 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
369 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000370
371 // function-specifier
372 case tok::kw_inline:
Chris Lattnera925dc62006-11-28 04:33:46 +0000373 isInvalid = DS.SetFunctionSpecInline(PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000374 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000375 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000376 // If the specifier combination wasn't legal, issue a diagnostic.
377 if (isInvalid) {
378 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000379 if (isInvalid == 1) // Error.
380 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
381 else // extwarn.
382 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000383 }
384 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000385 }
386}
387
Chris Lattner1890ac82006-08-13 01:16:23 +0000388
389/// ParseStructUnionSpecifier
390/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000391/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000392/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000393/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
394/// '}' attributes[opt]
395/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000396/// struct-or-union:
397/// 'struct'
398/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000399/// struct-contents:
400/// struct-declaration-list
401/// [EXT] empty
402/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000403/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000404/// struct-declaration
405/// struct-declaration-list struct-declaration
406/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
407/// struct-declaration:
408/// specifier-qualifier-list struct-declarator-list ';'
409/// [GNU] __extension__ struct-declaration [TODO]
410/// [GNU] specifier-qualifier-list ';' [TODO]
411/// struct-declarator-list:
412/// struct-declarator
413/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000414/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000415/// struct-declarator:
416/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000417/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000418/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000419/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000420///
421void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
422 assert((Tok.getKind() == tok::kw_struct ||
423 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
424 bool isUnion = Tok.getKind() == tok::kw_union;
Chris Lattneraf635312006-10-16 06:06:51 +0000425 SourceLocation Start = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000426
427 // If attributes exist after tag, parse them.
428 if (Tok.getKind() == tok::kw___attribute)
429 ParseAttributes();
430
Chris Lattner1890ac82006-08-13 01:16:23 +0000431 // Must have either 'struct name' or 'struct {...}'.
432 if (Tok.getKind() != tok::identifier &&
433 Tok.getKind() != tok::l_brace) {
434 Diag(Tok, diag::err_expected_ident_lbrace);
435 return;
436 }
437
438 if (Tok.getKind() == tok::identifier)
439 ConsumeToken();
440
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000441 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000442 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000443
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000444 if (Tok.getKind() == tok::r_brace)
445 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000446
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000447 while (Tok.getKind() != tok::r_brace &&
448 Tok.getKind() != tok::eof) {
449 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000450
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000451 // Parse the common specifier-qualifiers-list piece.
452 DeclSpec DS;
453 SourceLocation SpecQualLoc = Tok.getLocation();
454 ParseSpecifierQualifierList(DS);
455 // TODO: Does specifier-qualifier list correctly check that *something* is
456 // specified?
457
458 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000459
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000460 // If there are no declarators, issue a warning.
461 if (Tok.getKind() == tok::semi) {
462 Diag(SpecQualLoc, diag::w_no_declarators);
463 } else {
464 // Read struct-declarators until we find the semicolon.
465 while (1) {
466 /// struct-declarator: declarator
467 /// struct-declarator: declarator[opt] ':' constant-expression
468 if (Tok.getKind() != tok::colon)
469 ParseDeclarator(DeclaratorInfo);
470
471 if (Tok.getKind() == tok::colon) {
472 ConsumeToken();
473 ExprResult Res = ParseConstantExpression();
474 if (Res.isInvalid) {
475 SkipUntil(tok::semi, true, true);
476 } else {
477 // Process it.
478 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000479 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000480
481 // If attributes exist after the declarator, parse them.
482 if (Tok.getKind() == tok::kw___attribute)
483 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000484
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000485 // TODO: install declarator.
486
487 // If we don't have a comma, it is either the end of the list (a ';')
488 // or an error, bail out.
489 if (Tok.getKind() != tok::comma)
490 break;
491
492 // Consume the comma.
493 ConsumeToken();
494
495 // Parse the next declarator.
496 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000497
498 // Attributes are only allowed on the second declarator.
499 if (Tok.getKind() == tok::kw___attribute)
500 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000501 }
502 }
503
504 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000505 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000506 } else {
507 Diag(Tok, diag::err_expected_semi_decl_list);
508 // Skip to end of block or statement
509 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000510 }
511 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000512
Chris Lattner04f80192006-08-15 04:55:54 +0000513 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000514
515 // If attributes exist after struct contents, parse them.
516 if (Tok.getKind() == tok::kw___attribute)
517 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000518 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000519
520 const char *PrevSpec = 0;
521 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
522 PrevSpec))
523 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000524}
525
526
Chris Lattner3b561a32006-08-13 00:12:11 +0000527/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000528/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000529/// 'enum' identifier[opt] '{' enumerator-list '}'
530/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000531/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
532/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000533/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000534/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000535/// enumerator-list:
536/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000537/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000538/// enumerator:
539/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000540/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000541/// enumeration-constant:
542/// identifier
543///
544void Parser::ParseEnumSpecifier(DeclSpec &DS) {
545 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattneraf635312006-10-16 06:06:51 +0000546 SourceLocation Start = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000547
Chris Lattnere37e2332006-08-15 04:50:22 +0000548 if (Tok.getKind() == tok::kw___attribute)
549 ParseAttributes();
550
Chris Lattner3b561a32006-08-13 00:12:11 +0000551 // Must have either 'enum name' or 'enum {...}'.
552 if (Tok.getKind() != tok::identifier &&
553 Tok.getKind() != tok::l_brace) {
554 Diag(Tok, diag::err_expected_ident_lbrace);
555 return;
556 }
557
558 if (Tok.getKind() == tok::identifier)
559 ConsumeToken();
560
Chris Lattner0fb8b362006-08-14 01:30:12 +0000561 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000562 SourceLocation LBraceLoc = 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;
Chris Lattneraf635312006-10-16 06:06:51 +0000579 SourceLocation CommaLoc = ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000580
581 if (Tok.getKind() != tok::identifier && !getLang().C99)
582 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000583 }
584
Chris Lattner0fb8b362006-08-14 01:30:12 +0000585 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000586 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000587
588 // If attributes exist after the identifier list, parse them.
589 if (Tok.getKind() == tok::kw___attribute)
590 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000591 }
592 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000593
594
595 const char *PrevSpec = 0;
596 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
597 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000598}
599
600
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000601/// isTypeSpecifierQualifier - Return true if the current token could be the
602/// start of a specifier-qualifier-list.
603bool Parser::isTypeSpecifierQualifier() const {
604 switch (Tok.getKind()) {
605 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000606 // GNU attributes support.
607 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000608 // type-specifiers
609 case tok::kw_short:
610 case tok::kw_long:
611 case tok::kw_signed:
612 case tok::kw_unsigned:
613 case tok::kw__Complex:
614 case tok::kw__Imaginary:
615 case tok::kw_void:
616 case tok::kw_char:
617 case tok::kw_int:
618 case tok::kw_float:
619 case tok::kw_double:
620 case tok::kw__Bool:
621 case tok::kw__Decimal32:
622 case tok::kw__Decimal64:
623 case tok::kw__Decimal128:
624
625 // struct-or-union-specifier
626 case tok::kw_struct:
627 case tok::kw_union:
628 // enum-specifier
629 case tok::kw_enum:
630
631 // type-qualifier
632 case tok::kw_const:
633 case tok::kw_volatile:
634 case tok::kw_restrict:
635 return true;
636
637 // typedef-name
638 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000639 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000640
641 // TODO: Attributes.
642 }
643}
644
Chris Lattneracd58a32006-08-06 17:24:14 +0000645/// isDeclarationSpecifier() - Return true if the current token is part of a
646/// declaration specifier.
647bool Parser::isDeclarationSpecifier() const {
648 switch (Tok.getKind()) {
649 default: return false;
650 // storage-class-specifier
651 case tok::kw_typedef:
652 case tok::kw_extern:
653 case tok::kw_static:
654 case tok::kw_auto:
655 case tok::kw_register:
656 case tok::kw___thread:
657
658 // type-specifiers
659 case tok::kw_short:
660 case tok::kw_long:
661 case tok::kw_signed:
662 case tok::kw_unsigned:
663 case tok::kw__Complex:
664 case tok::kw__Imaginary:
665 case tok::kw_void:
666 case tok::kw_char:
667 case tok::kw_int:
668 case tok::kw_float:
669 case tok::kw_double:
670 case tok::kw__Bool:
671 case tok::kw__Decimal32:
672 case tok::kw__Decimal64:
673 case tok::kw__Decimal128:
674
675 // struct-or-union-specifier
676 case tok::kw_struct:
677 case tok::kw_union:
678 // enum-specifier
679 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000680
Chris Lattneracd58a32006-08-06 17:24:14 +0000681 // type-qualifier
682 case tok::kw_const:
683 case tok::kw_volatile:
684 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000685
Chris Lattneracd58a32006-08-06 17:24:14 +0000686 // function-specifier
687 case tok::kw_inline:
688 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000689
Chris Lattneracd58a32006-08-06 17:24:14 +0000690 // typedef-name
691 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000692 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000693 // TODO: Attributes.
694 }
695}
696
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000697
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000698/// ParseTypeQualifierListOpt
699/// type-qualifier-list: [C99 6.7.5]
700/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000701/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000702/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000703/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000704///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000705void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
706 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000707 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000708 int isInvalid = false;
709 const char *PrevSpec = 0;
710
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000711 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000712 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000713 // If this is not a type-qualifier token, we're done reading type
714 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000715 DS.Finish(StartLoc, Diags, getLang());
716 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000717 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000718 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
719 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000720 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000721 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
722 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000723 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000724 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000725 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000726
727 case tok::kw___attribute:
728 ParseAttributes();
729 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000730 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000731
732 // If the specifier combination wasn't legal, issue a diagnostic.
733 if (isInvalid) {
734 assert(PrevSpec && "Method did not return previous specifier!");
735 if (isInvalid == 1) // Error.
736 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
737 else // extwarn.
738 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
739 }
740 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000741 }
742}
743
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000744
745/// ParseDeclarator - Parse and verify a newly-initialized declarator.
746///
747void Parser::ParseDeclarator(Declarator &D) {
748 /// This implements the 'declarator' production in the C grammar, then checks
749 /// for well-formedness and issues diagnostics.
750 ParseDeclaratorInternal(D);
751
Chris Lattner9fab3b92006-08-12 18:25:42 +0000752 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000753
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000754}
755
756/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000757/// declarator: [C99 6.7.5]
758/// pointer[opt] direct-declarator
759///
760/// pointer: [C99 6.7.5]
761/// '*' type-qualifier-list[opt]
762/// '*' type-qualifier-list[opt] pointer
763///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000764void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000765 if (Tok.getKind() != tok::star)
766 return ParseDirectDeclarator(D);
767
768 // Otherwise, '*' -> pointer.
Chris Lattneraf635312006-10-16 06:06:51 +0000769 SourceLocation Loc = ConsumeToken(); // Eat the *.
Chris Lattner6c7416c2006-08-07 00:19:33 +0000770 DeclSpec DS;
771 ParseTypeQualifierListOpt(DS);
772
773 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000774 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000775
Chris Lattner6c7416c2006-08-07 00:19:33 +0000776 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnera925dc62006-11-28 04:33:46 +0000777 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.getTypeQualifiers(), Loc));
Chris Lattner6c7416c2006-08-07 00:19:33 +0000778}
779
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000780
781/// ParseDirectDeclarator
782/// direct-declarator: [C99 6.7.5]
783/// identifier
784/// '(' declarator ')'
785/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000786/// [C90] direct-declarator '[' constant-expression[opt] ']'
787/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
788/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
789/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
790/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000791/// direct-declarator '(' parameter-type-list ')'
792/// direct-declarator '(' identifier-list[opt] ')'
793/// [GNU] direct-declarator '(' parameter-forward-declarations
794/// parameter-type-list[opt] ')'
795///
Chris Lattneracd58a32006-08-06 17:24:14 +0000796void Parser::ParseDirectDeclarator(Declarator &D) {
797 // Parse the first direct-declarator seen.
798 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
799 assert(Tok.getIdentifierInfo() && "Not an identifier?");
800 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
801 ConsumeToken();
802 } else if (Tok.getKind() == tok::l_paren) {
803 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000804 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000805 // Example: 'char (*X)' or 'int (*XX)(void)'
806 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000807 } else if (D.mayOmitIdentifier()) {
808 // This could be something simple like "int" (in which case the declarator
809 // portion is empty), if an abstract-declarator is allowed.
810 D.SetIdentifier(0, Tok.getLocation());
811 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000812 // Expected identifier or '('.
813 Diag(Tok, diag::err_expected_ident_lparen);
814 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000815 }
816
817 assert(D.isPastIdentifier() &&
818 "Haven't past the location of the identifier yet?");
819
820 while (1) {
821 if (Tok.getKind() == tok::l_paren) {
822 ParseParenDeclarator(D);
823 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000824 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000825 } else {
826 break;
827 }
828 }
829}
830
831/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
832/// either be before the identifier (in which case these are just grouping
833/// parens for precedence) or it may be after the identifier, in which case
834/// these are function arguments.
835///
836/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000837/// parameter-type-list: [C99 6.7.5]
838/// parameter-list
839/// parameter-list ',' '...'
840///
841/// parameter-list: [C99 6.7.5]
842/// parameter-declaration
843/// parameter-list ',' parameter-declaration
844///
845/// parameter-declaration: [C99 6.7.5]
846/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000847/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000848/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000849/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000850///
851/// identifier-list: [C99 6.7.5]
852/// identifier
853/// identifier-list ',' identifier
854///
Chris Lattneracd58a32006-08-06 17:24:14 +0000855void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +0000856 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000857
Chris Lattneracd58a32006-08-06 17:24:14 +0000858 // If we haven't past the identifier yet (or where the identifier would be
859 // stored, if this is an abstract declarator), then this is probably just
860 // grouping parens.
861 if (!D.isPastIdentifier()) {
862 // Okay, this is probably a grouping paren. However, if this could be an
863 // abstract-declarator, then this could also be the start of function
864 // arguments (consider 'void()').
865 bool isGrouping;
866
867 if (!D.mayOmitIdentifier()) {
868 // If this can't be an abstract-declarator, this *must* be a grouping
869 // paren, because we haven't seen the identifier yet.
870 isGrouping = true;
871 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
872 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000873 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
874 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +0000875 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000876 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000877 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000878 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000879 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000880
881 // If this is a grouping paren, handle:
882 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000883 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000884 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000885 if (Tok.getKind() == tok::kw___attribute)
886 ParseAttributes();
887
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000888 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000889 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000890 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000891 return;
892 }
893
894 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000895 // argument list. Recognize that this declarator will never have an
896 // identifier (and remember where it would have been), then fall through to
897 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000898 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000899 }
900
Chris Lattneracd58a32006-08-06 17:24:14 +0000901 // Okay, this is the parameter list of a function definition, or it is an
902 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000903 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000904 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000905 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000906 bool ErrorEmitted = false;
907
Chris Lattneracd58a32006-08-06 17:24:14 +0000908 if (Tok.getKind() == tok::r_paren) {
909 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000910 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000911 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000912 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000913 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000914 // K&R identifier lists can't have typedefs as identifiers, per
915 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000916 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000917 // Identifier list. Note that '(' identifier-list ')' is only allowed for
918 // normal declarators, not for abstract-declarators.
919 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
920
921 // If there was no identifier specified, either we are in an
922 // abstract-declarator, or we are in a parameter declarator which was found
923 // to be abstract. In abstract-declarators, identifier lists are not valid,
924 // diagnose this.
925 if (!D.getIdentifier())
926 Diag(Tok, diag::ext_ident_list_in_param);
927
Chris Lattner9fab3b92006-08-12 18:25:42 +0000928 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000929 ConsumeToken();
930 while (Tok.getKind() == tok::comma) {
931 // Eat the comma.
932 ConsumeToken();
933
Chris Lattner0be454e2006-08-12 19:30:51 +0000934 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000935 ErrorEmitted = true;
936 break;
937 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000938 }
939
Chris Lattneracd58a32006-08-06 17:24:14 +0000940 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000941 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000942 HasPrototype = false;
943 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +0000944 // Finally, a normal, non-empty parameter type list.
945
946 // Enter function-declaration scope, limiting any declarators for arguments
947 // to the function scope.
948 EnterScope(0);
949
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000950 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000951 bool ReadArg = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000952 while (1) {
953 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000954 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000955
956 // Check to see if this is "void(...)" which is not allowed.
957 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000958 // Otherwise, parse parameter type list. If it starts with an
959 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000960 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000961 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000962 }
963
964 // Consume the ellipsis.
965 ConsumeToken();
966 break;
967 }
968
969 ReadArg = true;
970
971 // Parse the declaration-specifiers.
972 DeclSpec DS;
973 ParseDeclarationSpecifiers(DS);
974
975 // Parse the declarator. This is "PrototypeContext", because we must
976 // accept either 'declarator' or 'abstract-declarator' here.
977 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
978 ParseDeclarator(DeclaratorInfo);
979
Chris Lattnere37e2332006-08-15 04:50:22 +0000980 // Parse GNU attributes, if present.
981 if (Tok.getKind() == tok::kw___attribute)
982 ParseAttributes();
983
Chris Lattner43e956c2006-11-28 04:05:37 +0000984 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000985 switch (DS.getStorageClassSpec()) {
Chris Lattner43e956c2006-11-28 04:05:37 +0000986 case DeclSpec::SCS_unspecified:
987 case DeclSpec::SCS_register:
988 break;
989 case DeclSpec::SCS_auto:
990 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
991 default:
992 // FIXME: Get better loc info from declspecs!
993 Diag(DeclaratorInfo.getIdentifierLoc(),
994 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +0000995 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +0000996 break;
997 }
998
999 // Inform the actions module about the parameter declarator, so it gets
1000 // added to the current scope.
1001 Actions.ParseDeclarator(CurScope, DeclaratorInfo, 0, 0);
Chris Lattneracd58a32006-08-06 17:24:14 +00001002
1003 // If the next token is a comma, consume it and keep reading arguments.
1004 if (Tok.getKind() != tok::comma) break;
1005
1006 // Consume the comma.
1007 ConsumeToken();
1008 }
1009
1010 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001011
1012 // Leave prototype scope.
1013 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001014 }
1015
Chris Lattner9fab3b92006-08-12 18:25:42 +00001016 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +00001017
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001018 // Remember that we parsed a function type, and remember the attributes.
1019 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001020 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001021
Chris Lattner14776b92006-08-06 22:27:40 +00001022
1023 // If we have the closing ')', eat it and we're done.
1024 if (Tok.getKind() == tok::r_paren) {
1025 ConsumeParen();
1026 } else {
1027 // If an error happened earlier parsing something else in the proto, don't
1028 // issue another error.
1029 if (!ErrorEmitted)
1030 Diag(Tok, diag::err_expected_rparen);
1031 SkipUntil(tok::r_paren);
1032 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001033}
Chris Lattneracd58a32006-08-06 17:24:14 +00001034
Chris Lattnere8074e62006-08-06 18:30:15 +00001035
1036/// [C90] direct-declarator '[' constant-expression[opt] ']'
1037/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1038/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1039/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1040/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1041void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001042 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001043
1044 // If valid, this location is the position where we read the 'static' keyword.
1045 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001046 if (Tok.getKind() == tok::kw_static)
1047 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001048
1049 // If there is a type-qualifier-list, read it now.
1050 DeclSpec DS;
1051 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001052
1053 // If we haven't already read 'static', check to see if there is one after the
1054 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001055 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1056 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001057
1058 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001059 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001060 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001061 if (Tok.getKind() == tok::star) {
1062 // Remember the '*' token, in case we have to un-get it.
1063 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001064 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001065
1066 // Check that the ']' token is present to avoid incorrectly parsing
1067 // expressions starting with '*' as [*].
1068 if (Tok.getKind() == tok::r_square) {
1069 if (StaticLoc.isValid())
1070 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1071 StaticLoc = SourceLocation(); // Drop the static.
1072 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001073 } else {
1074 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001075 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001076 // need to reparse it. This handles cases like 'X[*p + 4]'
1077 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001078 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001079 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001080 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001081 NumElements = ParseAssignmentExpression();
1082 }
1083
1084 // If there was an error parsing the assignment-expression, recover.
1085 if (NumElements.isInvalid) {
1086 // If the expression was invalid, skip it.
1087 SkipUntil(tok::r_square);
1088 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001089 }
1090
Chris Lattner04f80192006-08-15 04:55:54 +00001091 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001092
Chris Lattnere8074e62006-08-06 18:30:15 +00001093 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1094 // it was not a constant expression.
1095 if (!getLang().C99) {
1096 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001097 if (isStar || StaticLoc.isValid() ||
1098 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001099 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001100 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001101
1102 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnera925dc62006-11-28 04:33:46 +00001103 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.getTypeQualifiers(),
Chris Lattner6c7416c2006-08-07 00:19:33 +00001104 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001105 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001106}
1107