blob: 458f67b0aac0b926fb7998885b4517e30fe4d859 [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);
190 // Remove storage class.
191 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
192 DS.SCS_thread_specified = false;
193 }
194 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
195 Diag(Loc, diag::err_typename_invalid_functionspec);
196 DS.FS_inline_specified = false;
197 }
198}
Chris Lattner53361ac2006-08-10 05:19:57 +0000199
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000200/// ParseDeclarationSpecifiers
201/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000202/// storage-class-specifier declaration-specifiers[opt]
203/// type-specifier declaration-specifiers[opt]
204/// type-qualifier declaration-specifiers[opt]
205/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000206/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000207///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000208/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000209/// 'typedef'
210/// 'extern'
211/// 'static'
212/// 'auto'
213/// 'register'
214/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000215/// type-specifier: [C99 6.7.2]
216/// 'void'
217/// 'char'
218/// 'short'
219/// 'int'
220/// 'long'
221/// 'float'
222/// 'double'
223/// 'signed'
224/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000225/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000226/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000227/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000228/// [C99] '_Bool'
229/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000230/// [C99] '_Imaginary' // Removed in TC2?
231/// [GNU] '_Decimal32'
232/// [GNU] '_Decimal64'
233/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000234/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000235/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000236/// [OBJC] typedef-name objc-protocol-refs [TODO]
237/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000238/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000239/// 'const'
240/// 'volatile'
241/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000242/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000243/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000244///
245void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
246 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000247 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000248 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000249 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000250 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000251 // typedef-name
252 case tok::identifier:
253 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000254 // a type-specifier. Without this check we misparse:
255 // typedef int X; struct Y { short X; }; as 'short int'.
256 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
257 DS.TypeSpecWidth == DeclSpec::TSW_unspecified &&
258 DS.TypeSpecComplex == DeclSpec::TSC_unspecified &&
259 DS.TypeSpecSign == DeclSpec::TSS_unspecified &&
260 // It has to be available as a typedef too!
Steve Naroffb419d3a2006-10-27 23:18:49 +0000261 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
263 break;
264 }
265 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000266 default:
267 // If this is not a declaration specifier token, we're done reading decl
268 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000269 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000270 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000271
272 // GNU attributes support.
273 case tok::kw___attribute:
274 ParseAttributes();
Chris Lattnerb95cca02006-10-17 03:01:08 +0000275 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000276
277 // storage-class-specifier
278 case tok::kw_typedef:
279 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
280 break;
281 case tok::kw_extern:
282 if (DS.SCS_thread_specified)
283 Diag(Tok, diag::ext_thread_before, "extern");
284 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
285 break;
286 case tok::kw_static:
287 if (DS.SCS_thread_specified)
288 Diag(Tok, diag::ext_thread_before, "static");
289 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
290 break;
291 case tok::kw_auto:
292 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
293 break;
294 case tok::kw_register:
295 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
296 break;
297 case tok::kw___thread:
298 if (DS.SCS_thread_specified)
299 isInvalid = 2, PrevSpec = "__thread";
300 else
301 DS.SCS_thread_specified = true;
302 break;
303
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000304 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000305 case tok::kw_short:
306 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
307 break;
308 case tok::kw_long:
309 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
310 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
311 } else {
312 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
313 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
314 }
315 break;
316 case tok::kw_signed:
317 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
318 break;
319 case tok::kw_unsigned:
320 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
321 break;
322 case tok::kw__Complex:
323 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
324 break;
325 case tok::kw__Imaginary:
326 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
327 break;
328 case tok::kw_void:
329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
330 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000331 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
333 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000334 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
336 break;
337 case tok::kw_float:
338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
339 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000340 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
342 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000343 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
345 break;
346 case tok::kw__Decimal32:
347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
348 break;
349 case tok::kw__Decimal64:
350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
351 break;
352 case tok::kw__Decimal128:
353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000354 break;
355
Chris Lattner1890ac82006-08-13 01:16:23 +0000356 case tok::kw_struct:
357 case tok::kw_union:
358 ParseStructUnionSpecifier(DS);
359 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000360 case tok::kw_enum:
361 ParseEnumSpecifier(DS);
362 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000363
364 // type-qualifier
365 case tok::kw_const:
366 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
367 break;
368 case tok::kw_volatile:
369 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
370 break;
371 case tok::kw_restrict:
372 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
373 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000374
375 // function-specifier
376 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000377 // 'inline inline' is ok.
378 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000379 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000380 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000381 // If the specifier combination wasn't legal, issue a diagnostic.
382 if (isInvalid) {
383 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000384 if (isInvalid == 1) // Error.
385 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
386 else // extwarn.
387 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000388 }
389 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000390 }
391}
392
Chris Lattner1890ac82006-08-13 01:16:23 +0000393
394/// ParseStructUnionSpecifier
395/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000396/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000397/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000398/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
399/// '}' attributes[opt]
400/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000401/// struct-or-union:
402/// 'struct'
403/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000404/// struct-contents:
405/// struct-declaration-list
406/// [EXT] empty
407/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000408/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000409/// struct-declaration
410/// struct-declaration-list struct-declaration
411/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
412/// struct-declaration:
413/// specifier-qualifier-list struct-declarator-list ';'
414/// [GNU] __extension__ struct-declaration [TODO]
415/// [GNU] specifier-qualifier-list ';' [TODO]
416/// struct-declarator-list:
417/// struct-declarator
418/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000419/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000420/// struct-declarator:
421/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000422/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000423/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000424/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000425///
426void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
427 assert((Tok.getKind() == tok::kw_struct ||
428 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
429 bool isUnion = Tok.getKind() == tok::kw_union;
Chris Lattneraf635312006-10-16 06:06:51 +0000430 SourceLocation Start = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000431
432 // If attributes exist after tag, parse them.
433 if (Tok.getKind() == tok::kw___attribute)
434 ParseAttributes();
435
Chris Lattner1890ac82006-08-13 01:16:23 +0000436 // Must have either 'struct name' or 'struct {...}'.
437 if (Tok.getKind() != tok::identifier &&
438 Tok.getKind() != tok::l_brace) {
439 Diag(Tok, diag::err_expected_ident_lbrace);
440 return;
441 }
442
443 if (Tok.getKind() == tok::identifier)
444 ConsumeToken();
445
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000446 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000447 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000448
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000449 if (Tok.getKind() == tok::r_brace)
450 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000451
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000452 while (Tok.getKind() != tok::r_brace &&
453 Tok.getKind() != tok::eof) {
454 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000455
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000456 // Parse the common specifier-qualifiers-list piece.
457 DeclSpec DS;
458 SourceLocation SpecQualLoc = Tok.getLocation();
459 ParseSpecifierQualifierList(DS);
460 // TODO: Does specifier-qualifier list correctly check that *something* is
461 // specified?
462
463 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000464
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000465 // If there are no declarators, issue a warning.
466 if (Tok.getKind() == tok::semi) {
467 Diag(SpecQualLoc, diag::w_no_declarators);
468 } else {
469 // Read struct-declarators until we find the semicolon.
470 while (1) {
471 /// struct-declarator: declarator
472 /// struct-declarator: declarator[opt] ':' constant-expression
473 if (Tok.getKind() != tok::colon)
474 ParseDeclarator(DeclaratorInfo);
475
476 if (Tok.getKind() == tok::colon) {
477 ConsumeToken();
478 ExprResult Res = ParseConstantExpression();
479 if (Res.isInvalid) {
480 SkipUntil(tok::semi, true, true);
481 } else {
482 // Process it.
483 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000484 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000485
486 // If attributes exist after the declarator, parse them.
487 if (Tok.getKind() == tok::kw___attribute)
488 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000489
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000490 // TODO: install declarator.
491
492 // If we don't have a comma, it is either the end of the list (a ';')
493 // or an error, bail out.
494 if (Tok.getKind() != tok::comma)
495 break;
496
497 // Consume the comma.
498 ConsumeToken();
499
500 // Parse the next declarator.
501 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000502
503 // Attributes are only allowed on the second declarator.
504 if (Tok.getKind() == tok::kw___attribute)
505 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000506 }
507 }
508
509 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000510 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000511 } else {
512 Diag(Tok, diag::err_expected_semi_decl_list);
513 // Skip to end of block or statement
514 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000515 }
516 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000517
Chris Lattner04f80192006-08-15 04:55:54 +0000518 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000519
520 // If attributes exist after struct contents, parse them.
521 if (Tok.getKind() == tok::kw___attribute)
522 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000523 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000524
525 const char *PrevSpec = 0;
526 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
527 PrevSpec))
528 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000529}
530
531
Chris Lattner3b561a32006-08-13 00:12:11 +0000532/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000533/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000534/// 'enum' identifier[opt] '{' enumerator-list '}'
535/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000536/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
537/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000538/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000539/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000540/// enumerator-list:
541/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000542/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000543/// enumerator:
544/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000545/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000546/// enumeration-constant:
547/// identifier
548///
549void Parser::ParseEnumSpecifier(DeclSpec &DS) {
550 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattneraf635312006-10-16 06:06:51 +0000551 SourceLocation Start = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000552
Chris Lattnere37e2332006-08-15 04:50:22 +0000553 if (Tok.getKind() == tok::kw___attribute)
554 ParseAttributes();
555
Chris Lattner3b561a32006-08-13 00:12:11 +0000556 // Must have either 'enum name' or 'enum {...}'.
557 if (Tok.getKind() != tok::identifier &&
558 Tok.getKind() != tok::l_brace) {
559 Diag(Tok, diag::err_expected_ident_lbrace);
560 return;
561 }
562
563 if (Tok.getKind() == tok::identifier)
564 ConsumeToken();
565
Chris Lattner0fb8b362006-08-14 01:30:12 +0000566 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000567 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000568
Chris Lattner0fb8b362006-08-14 01:30:12 +0000569 if (Tok.getKind() == tok::r_brace)
570 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
571
572 // Parse the enumerator-list.
573 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000574 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000575
576 if (Tok.getKind() == tok::equal) {
577 ConsumeToken();
578 ExprResult Res = ParseConstantExpression();
579 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
580 }
581
582 if (Tok.getKind() != tok::comma)
583 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000584 SourceLocation CommaLoc = ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000585
586 if (Tok.getKind() != tok::identifier && !getLang().C99)
587 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000588 }
589
Chris Lattner0fb8b362006-08-14 01:30:12 +0000590 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000591 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000592
593 // If attributes exist after the identifier list, parse them.
594 if (Tok.getKind() == tok::kw___attribute)
595 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000596 }
597 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000598
599
600 const char *PrevSpec = 0;
601 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
602 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000603}
604
605
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000606/// isTypeSpecifierQualifier - Return true if the current token could be the
607/// start of a specifier-qualifier-list.
608bool Parser::isTypeSpecifierQualifier() const {
609 switch (Tok.getKind()) {
610 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000611 // GNU attributes support.
612 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000613 // type-specifiers
614 case tok::kw_short:
615 case tok::kw_long:
616 case tok::kw_signed:
617 case tok::kw_unsigned:
618 case tok::kw__Complex:
619 case tok::kw__Imaginary:
620 case tok::kw_void:
621 case tok::kw_char:
622 case tok::kw_int:
623 case tok::kw_float:
624 case tok::kw_double:
625 case tok::kw__Bool:
626 case tok::kw__Decimal32:
627 case tok::kw__Decimal64:
628 case tok::kw__Decimal128:
629
630 // struct-or-union-specifier
631 case tok::kw_struct:
632 case tok::kw_union:
633 // enum-specifier
634 case tok::kw_enum:
635
636 // type-qualifier
637 case tok::kw_const:
638 case tok::kw_volatile:
639 case tok::kw_restrict:
640 return true;
641
642 // typedef-name
643 case tok::identifier:
Steve Naroffb419d3a2006-10-27 23:18:49 +0000644 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000645
646 // TODO: Attributes.
647 }
648}
649
Chris Lattneracd58a32006-08-06 17:24:14 +0000650/// isDeclarationSpecifier() - Return true if the current token is part of a
651/// declaration specifier.
652bool Parser::isDeclarationSpecifier() const {
653 switch (Tok.getKind()) {
654 default: return false;
655 // storage-class-specifier
656 case tok::kw_typedef:
657 case tok::kw_extern:
658 case tok::kw_static:
659 case tok::kw_auto:
660 case tok::kw_register:
661 case tok::kw___thread:
662
663 // type-specifiers
664 case tok::kw_short:
665 case tok::kw_long:
666 case tok::kw_signed:
667 case tok::kw_unsigned:
668 case tok::kw__Complex:
669 case tok::kw__Imaginary:
670 case tok::kw_void:
671 case tok::kw_char:
672 case tok::kw_int:
673 case tok::kw_float:
674 case tok::kw_double:
675 case tok::kw__Bool:
676 case tok::kw__Decimal32:
677 case tok::kw__Decimal64:
678 case tok::kw__Decimal128:
679
680 // struct-or-union-specifier
681 case tok::kw_struct:
682 case tok::kw_union:
683 // enum-specifier
684 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000685
Chris Lattneracd58a32006-08-06 17:24:14 +0000686 // type-qualifier
687 case tok::kw_const:
688 case tok::kw_volatile:
689 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000690
Chris Lattneracd58a32006-08-06 17:24:14 +0000691 // function-specifier
692 case tok::kw_inline:
693 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000694
Chris Lattneracd58a32006-08-06 17:24:14 +0000695 // typedef-name
696 case tok::identifier:
Steve Naroffb419d3a2006-10-27 23:18:49 +0000697 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000698 // TODO: Attributes.
699 }
700}
701
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000702
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000703/// ParseTypeQualifierListOpt
704/// type-qualifier-list: [C99 6.7.5]
705/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000706/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000707/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000708/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000709///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000710void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
711 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000712 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000713 int isInvalid = false;
714 const char *PrevSpec = 0;
715
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000716 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000717 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000718 // If this is not a type-qualifier token, we're done reading type
719 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000720 DS.Finish(StartLoc, Diags, getLang());
721 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000722 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000723 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
724 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000725 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000726 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
727 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000728 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000729 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000730 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000731
732 case tok::kw___attribute:
733 ParseAttributes();
734 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000735 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000736
737 // If the specifier combination wasn't legal, issue a diagnostic.
738 if (isInvalid) {
739 assert(PrevSpec && "Method did not return previous specifier!");
740 if (isInvalid == 1) // Error.
741 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
742 else // extwarn.
743 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
744 }
745 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000746 }
747}
748
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000749
750/// ParseDeclarator - Parse and verify a newly-initialized declarator.
751///
752void Parser::ParseDeclarator(Declarator &D) {
753 /// This implements the 'declarator' production in the C grammar, then checks
754 /// for well-formedness and issues diagnostics.
755 ParseDeclaratorInternal(D);
756
Chris Lattner9fab3b92006-08-12 18:25:42 +0000757 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000758
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000759}
760
761/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000762/// declarator: [C99 6.7.5]
763/// pointer[opt] direct-declarator
764///
765/// pointer: [C99 6.7.5]
766/// '*' type-qualifier-list[opt]
767/// '*' type-qualifier-list[opt] pointer
768///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000769void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000770 if (Tok.getKind() != tok::star)
771 return ParseDirectDeclarator(D);
772
773 // Otherwise, '*' -> pointer.
Chris Lattneraf635312006-10-16 06:06:51 +0000774 SourceLocation Loc = ConsumeToken(); // Eat the *.
Chris Lattner6c7416c2006-08-07 00:19:33 +0000775 DeclSpec DS;
776 ParseTypeQualifierListOpt(DS);
777
778 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000779 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000780
Chris Lattner6c7416c2006-08-07 00:19:33 +0000781 // Remember that we parsed a pointer type, and remember the type-quals.
782 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
783}
784
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000785
786/// ParseDirectDeclarator
787/// direct-declarator: [C99 6.7.5]
788/// identifier
789/// '(' declarator ')'
790/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000791/// [C90] direct-declarator '[' constant-expression[opt] ']'
792/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
793/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
794/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
795/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000796/// direct-declarator '(' parameter-type-list ')'
797/// direct-declarator '(' identifier-list[opt] ')'
798/// [GNU] direct-declarator '(' parameter-forward-declarations
799/// parameter-type-list[opt] ')'
800///
Chris Lattneracd58a32006-08-06 17:24:14 +0000801void Parser::ParseDirectDeclarator(Declarator &D) {
802 // Parse the first direct-declarator seen.
803 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
804 assert(Tok.getIdentifierInfo() && "Not an identifier?");
805 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
806 ConsumeToken();
807 } else if (Tok.getKind() == tok::l_paren) {
808 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000809 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000810 // Example: 'char (*X)' or 'int (*XX)(void)'
811 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000812 } else if (D.mayOmitIdentifier()) {
813 // This could be something simple like "int" (in which case the declarator
814 // portion is empty), if an abstract-declarator is allowed.
815 D.SetIdentifier(0, Tok.getLocation());
816 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000817 // Expected identifier or '('.
818 Diag(Tok, diag::err_expected_ident_lparen);
819 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000820 }
821
822 assert(D.isPastIdentifier() &&
823 "Haven't past the location of the identifier yet?");
824
825 while (1) {
826 if (Tok.getKind() == tok::l_paren) {
827 ParseParenDeclarator(D);
828 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000829 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000830 } else {
831 break;
832 }
833 }
834}
835
836/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
837/// either be before the identifier (in which case these are just grouping
838/// parens for precedence) or it may be after the identifier, in which case
839/// these are function arguments.
840///
841/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000842/// parameter-type-list: [C99 6.7.5]
843/// parameter-list
844/// parameter-list ',' '...'
845///
846/// parameter-list: [C99 6.7.5]
847/// parameter-declaration
848/// parameter-list ',' parameter-declaration
849///
850/// parameter-declaration: [C99 6.7.5]
851/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000852/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000853/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000854/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000855///
856/// identifier-list: [C99 6.7.5]
857/// identifier
858/// identifier-list ',' identifier
859///
Chris Lattneracd58a32006-08-06 17:24:14 +0000860void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +0000861 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000862
Chris Lattneracd58a32006-08-06 17:24:14 +0000863 // If we haven't past the identifier yet (or where the identifier would be
864 // stored, if this is an abstract declarator), then this is probably just
865 // grouping parens.
866 if (!D.isPastIdentifier()) {
867 // Okay, this is probably a grouping paren. However, if this could be an
868 // abstract-declarator, then this could also be the start of function
869 // arguments (consider 'void()').
870 bool isGrouping;
871
872 if (!D.mayOmitIdentifier()) {
873 // If this can't be an abstract-declarator, this *must* be a grouping
874 // paren, because we haven't seen the identifier yet.
875 isGrouping = true;
876 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
877 isDeclarationSpecifier()) { // 'int(int)' is a function.
878
879 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000880 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000881 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000882 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000883 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000884
885 // If this is a grouping paren, handle:
886 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000887 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000888 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000889 if (Tok.getKind() == tok::kw___attribute)
890 ParseAttributes();
891
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000892 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000893 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000894 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000895 return;
896 }
897
898 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000899 // argument list. Recognize that this declarator will never have an
900 // identifier (and remember where it would have been), then fall through to
901 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000902 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000903 }
904
Chris Lattneracd58a32006-08-06 17:24:14 +0000905 // Okay, this is the parameter list of a function definition, or it is an
906 // identifier list of a K&R-style function.
907
Chris Lattner9fab3b92006-08-12 18:25:42 +0000908 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000909 // arguments to the function scope.
910 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000911 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000912 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000913 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000914 bool ErrorEmitted = false;
915
Chris Lattneracd58a32006-08-06 17:24:14 +0000916 if (Tok.getKind() == tok::r_paren) {
917 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000918 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000919 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000920 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000921 } else if (Tok.getKind() == tok::identifier &&
Steve Naroffb419d3a2006-10-27 23:18:49 +0000922 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000923 // Identifier list. Note that '(' identifier-list ')' is only allowed for
924 // normal declarators, not for abstract-declarators.
925 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
926
927 // If there was no identifier specified, either we are in an
928 // abstract-declarator, or we are in a parameter declarator which was found
929 // to be abstract. In abstract-declarators, identifier lists are not valid,
930 // diagnose this.
931 if (!D.getIdentifier())
932 Diag(Tok, diag::ext_ident_list_in_param);
933
Chris Lattner9fab3b92006-08-12 18:25:42 +0000934 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000935 ConsumeToken();
936 while (Tok.getKind() == tok::comma) {
937 // Eat the comma.
938 ConsumeToken();
939
Chris Lattner0be454e2006-08-12 19:30:51 +0000940 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000941 ErrorEmitted = true;
942 break;
943 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000944 }
945
Chris Lattneracd58a32006-08-06 17:24:14 +0000946 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000947 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000948 HasPrototype = false;
949 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000950 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000951 bool ReadArg = false;
952 // Finally, a normal, non-empty parameter type list.
953 while (1) {
954 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000955 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000956
957 // Check to see if this is "void(...)" which is not allowed.
958 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000959 // Otherwise, parse parameter type list. If it starts with an
960 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000961 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000962 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000963 }
964
965 // Consume the ellipsis.
966 ConsumeToken();
967 break;
968 }
969
970 ReadArg = true;
971
972 // Parse the declaration-specifiers.
973 DeclSpec DS;
974 ParseDeclarationSpecifiers(DS);
975
976 // Parse the declarator. This is "PrototypeContext", because we must
977 // accept either 'declarator' or 'abstract-declarator' here.
978 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
979 ParseDeclarator(DeclaratorInfo);
980
Chris Lattnere37e2332006-08-15 04:50:22 +0000981 // Parse GNU attributes, if present.
982 if (Tok.getKind() == tok::kw___attribute)
983 ParseAttributes();
984
Chris Lattneracd58a32006-08-06 17:24:14 +0000985 // TODO: do something with the declarator, if it is valid.
986
987 // If the next token is a comma, consume it and keep reading arguments.
988 if (Tok.getKind() != tok::comma) break;
989
990 // Consume the comma.
991 ConsumeToken();
992 }
993
994 HasPrototype = true;
995 }
996
Chris Lattner9fab3b92006-08-12 18:25:42 +0000997 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000998
Chris Lattner9fab3b92006-08-12 18:25:42 +0000999 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +00001000
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001001 // Remember that we parsed a function type, and remember the attributes.
1002 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001003 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001004
Chris Lattner14776b92006-08-06 22:27:40 +00001005
1006 // If we have the closing ')', eat it and we're done.
1007 if (Tok.getKind() == tok::r_paren) {
1008 ConsumeParen();
1009 } else {
1010 // If an error happened earlier parsing something else in the proto, don't
1011 // issue another error.
1012 if (!ErrorEmitted)
1013 Diag(Tok, diag::err_expected_rparen);
1014 SkipUntil(tok::r_paren);
1015 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001016}
Chris Lattneracd58a32006-08-06 17:24:14 +00001017
Chris Lattnere8074e62006-08-06 18:30:15 +00001018
1019/// [C90] direct-declarator '[' constant-expression[opt] ']'
1020/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1021/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1022/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1023/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1024void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001025 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001026
1027 // If valid, this location is the position where we read the 'static' keyword.
1028 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001029 if (Tok.getKind() == tok::kw_static)
1030 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001031
1032 // If there is a type-qualifier-list, read it now.
1033 DeclSpec DS;
1034 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001035
1036 // If we haven't already read 'static', check to see if there is one after the
1037 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001038 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1039 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001040
1041 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001042 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001043 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001044 if (Tok.getKind() == tok::star) {
1045 // Remember the '*' token, in case we have to un-get it.
1046 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001047 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001048
1049 // Check that the ']' token is present to avoid incorrectly parsing
1050 // expressions starting with '*' as [*].
1051 if (Tok.getKind() == tok::r_square) {
1052 if (StaticLoc.isValid())
1053 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1054 StaticLoc = SourceLocation(); // Drop the static.
1055 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001056 } else {
1057 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001058 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001059 // need to reparse it. This handles cases like 'X[*p + 4]'
1060 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001061 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001062 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001063 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001064 NumElements = ParseAssignmentExpression();
1065 }
1066
1067 // If there was an error parsing the assignment-expression, recover.
1068 if (NumElements.isInvalid) {
1069 // If the expression was invalid, skip it.
1070 SkipUntil(tok::r_square);
1071 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001072 }
1073
Chris Lattner04f80192006-08-15 04:55:54 +00001074 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001075
Chris Lattnere8074e62006-08-06 18:30:15 +00001076 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1077 // it was not a constant expression.
1078 if (!getLang().C99) {
1079 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001080 if (isStar || StaticLoc.isValid() ||
1081 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001082 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001083 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001084
1085 // Remember that we parsed a pointer type, and remember the type-quals.
1086 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1087 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001088 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001089}
1090