blob: 087801c4e4eb2f76f61f5cde3b91622c5f410e3d [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 &&
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000259 DS.TypeSpecSign == DeclSpec::TSS_unspecified) {
260 // It has to be available as a typedef too!
261 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
262 CurScope)) {
263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec,
264 TypeRep);
265 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000266 break;
267 }
268 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000269 default:
270 // If this is not a declaration specifier token, we're done reading decl
271 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000272 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000273 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000274
275 // GNU attributes support.
276 case tok::kw___attribute:
277 ParseAttributes();
Chris Lattnerb95cca02006-10-17 03:01:08 +0000278 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000279
280 // storage-class-specifier
281 case tok::kw_typedef:
282 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
283 break;
284 case tok::kw_extern:
285 if (DS.SCS_thread_specified)
286 Diag(Tok, diag::ext_thread_before, "extern");
287 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
288 break;
289 case tok::kw_static:
290 if (DS.SCS_thread_specified)
291 Diag(Tok, diag::ext_thread_before, "static");
292 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
293 break;
294 case tok::kw_auto:
295 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
296 break;
297 case tok::kw_register:
298 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
299 break;
300 case tok::kw___thread:
301 if (DS.SCS_thread_specified)
302 isInvalid = 2, PrevSpec = "__thread";
303 else
304 DS.SCS_thread_specified = true;
305 break;
306
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000307 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000308 case tok::kw_short:
309 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
310 break;
311 case tok::kw_long:
312 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
313 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
314 } else {
315 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
316 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
317 }
318 break;
319 case tok::kw_signed:
320 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
321 break;
322 case tok::kw_unsigned:
323 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
324 break;
325 case tok::kw__Complex:
326 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
327 break;
328 case tok::kw__Imaginary:
329 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
330 break;
331 case tok::kw_void:
332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
333 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000334 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
336 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000337 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
339 break;
340 case tok::kw_float:
341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
342 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000343 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
345 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000346 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
348 break;
349 case tok::kw__Decimal32:
350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
351 break;
352 case tok::kw__Decimal64:
353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
354 break;
355 case tok::kw__Decimal128:
356 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000357 break;
358
Chris Lattner1890ac82006-08-13 01:16:23 +0000359 case tok::kw_struct:
360 case tok::kw_union:
361 ParseStructUnionSpecifier(DS);
362 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000363 case tok::kw_enum:
364 ParseEnumSpecifier(DS);
365 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000366
367 // type-qualifier
368 case tok::kw_const:
369 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
370 break;
371 case tok::kw_volatile:
372 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
373 break;
374 case tok::kw_restrict:
375 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
376 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000377
378 // function-specifier
379 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000380 // 'inline inline' is ok.
381 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000382 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000383 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000384 // If the specifier combination wasn't legal, issue a diagnostic.
385 if (isInvalid) {
386 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000387 if (isInvalid == 1) // Error.
388 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
389 else // extwarn.
390 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000391 }
392 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000393 }
394}
395
Chris Lattner1890ac82006-08-13 01:16:23 +0000396
397/// ParseStructUnionSpecifier
398/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000399/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000400/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000401/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
402/// '}' attributes[opt]
403/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000404/// struct-or-union:
405/// 'struct'
406/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000407/// struct-contents:
408/// struct-declaration-list
409/// [EXT] empty
410/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000411/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000412/// struct-declaration
413/// struct-declaration-list struct-declaration
414/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
415/// struct-declaration:
416/// specifier-qualifier-list struct-declarator-list ';'
417/// [GNU] __extension__ struct-declaration [TODO]
418/// [GNU] specifier-qualifier-list ';' [TODO]
419/// struct-declarator-list:
420/// struct-declarator
421/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000422/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000423/// struct-declarator:
424/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000425/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000426/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000427/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000428///
429void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
430 assert((Tok.getKind() == tok::kw_struct ||
431 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
432 bool isUnion = Tok.getKind() == tok::kw_union;
Chris Lattneraf635312006-10-16 06:06:51 +0000433 SourceLocation Start = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000434
435 // If attributes exist after tag, parse them.
436 if (Tok.getKind() == tok::kw___attribute)
437 ParseAttributes();
438
Chris Lattner1890ac82006-08-13 01:16:23 +0000439 // Must have either 'struct name' or 'struct {...}'.
440 if (Tok.getKind() != tok::identifier &&
441 Tok.getKind() != tok::l_brace) {
442 Diag(Tok, diag::err_expected_ident_lbrace);
443 return;
444 }
445
446 if (Tok.getKind() == tok::identifier)
447 ConsumeToken();
448
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000449 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000450 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000451
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000452 if (Tok.getKind() == tok::r_brace)
453 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000454
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000455 while (Tok.getKind() != tok::r_brace &&
456 Tok.getKind() != tok::eof) {
457 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000458
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000459 // Parse the common specifier-qualifiers-list piece.
460 DeclSpec DS;
461 SourceLocation SpecQualLoc = Tok.getLocation();
462 ParseSpecifierQualifierList(DS);
463 // TODO: Does specifier-qualifier list correctly check that *something* is
464 // specified?
465
466 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000467
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000468 // If there are no declarators, issue a warning.
469 if (Tok.getKind() == tok::semi) {
470 Diag(SpecQualLoc, diag::w_no_declarators);
471 } else {
472 // Read struct-declarators until we find the semicolon.
473 while (1) {
474 /// struct-declarator: declarator
475 /// struct-declarator: declarator[opt] ':' constant-expression
476 if (Tok.getKind() != tok::colon)
477 ParseDeclarator(DeclaratorInfo);
478
479 if (Tok.getKind() == tok::colon) {
480 ConsumeToken();
481 ExprResult Res = ParseConstantExpression();
482 if (Res.isInvalid) {
483 SkipUntil(tok::semi, true, true);
484 } else {
485 // Process it.
486 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000487 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000488
489 // If attributes exist after the declarator, parse them.
490 if (Tok.getKind() == tok::kw___attribute)
491 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000492
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000493 // TODO: install declarator.
494
495 // If we don't have a comma, it is either the end of the list (a ';')
496 // or an error, bail out.
497 if (Tok.getKind() != tok::comma)
498 break;
499
500 // Consume the comma.
501 ConsumeToken();
502
503 // Parse the next declarator.
504 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000505
506 // Attributes are only allowed on the second declarator.
507 if (Tok.getKind() == tok::kw___attribute)
508 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000509 }
510 }
511
512 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000513 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000514 } else {
515 Diag(Tok, diag::err_expected_semi_decl_list);
516 // Skip to end of block or statement
517 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000518 }
519 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000520
Chris Lattner04f80192006-08-15 04:55:54 +0000521 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000522
523 // If attributes exist after struct contents, parse them.
524 if (Tok.getKind() == tok::kw___attribute)
525 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000526 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000527
528 const char *PrevSpec = 0;
529 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
530 PrevSpec))
531 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000532}
533
534
Chris Lattner3b561a32006-08-13 00:12:11 +0000535/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000536/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000537/// 'enum' identifier[opt] '{' enumerator-list '}'
538/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000539/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
540/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000541/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000542/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000543/// enumerator-list:
544/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000545/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000546/// enumerator:
547/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000548/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000549/// enumeration-constant:
550/// identifier
551///
552void Parser::ParseEnumSpecifier(DeclSpec &DS) {
553 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattneraf635312006-10-16 06:06:51 +0000554 SourceLocation Start = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000555
Chris Lattnere37e2332006-08-15 04:50:22 +0000556 if (Tok.getKind() == tok::kw___attribute)
557 ParseAttributes();
558
Chris Lattner3b561a32006-08-13 00:12:11 +0000559 // Must have either 'enum name' or 'enum {...}'.
560 if (Tok.getKind() != tok::identifier &&
561 Tok.getKind() != tok::l_brace) {
562 Diag(Tok, diag::err_expected_ident_lbrace);
563 return;
564 }
565
566 if (Tok.getKind() == tok::identifier)
567 ConsumeToken();
568
Chris Lattner0fb8b362006-08-14 01:30:12 +0000569 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000570 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000571
Chris Lattner0fb8b362006-08-14 01:30:12 +0000572 if (Tok.getKind() == tok::r_brace)
573 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
574
575 // Parse the enumerator-list.
576 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000577 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000578
579 if (Tok.getKind() == tok::equal) {
580 ConsumeToken();
581 ExprResult Res = ParseConstantExpression();
582 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
583 }
584
585 if (Tok.getKind() != tok::comma)
586 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000587 SourceLocation CommaLoc = ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000588
589 if (Tok.getKind() != tok::identifier && !getLang().C99)
590 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000591 }
592
Chris Lattner0fb8b362006-08-14 01:30:12 +0000593 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000594 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000595
596 // If attributes exist after the identifier list, parse them.
597 if (Tok.getKind() == tok::kw___attribute)
598 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000599 }
600 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000601
602
603 const char *PrevSpec = 0;
604 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
605 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000606}
607
608
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000609/// isTypeSpecifierQualifier - Return true if the current token could be the
610/// start of a specifier-qualifier-list.
611bool Parser::isTypeSpecifierQualifier() const {
612 switch (Tok.getKind()) {
613 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000614 // GNU attributes support.
615 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000616 // type-specifiers
617 case tok::kw_short:
618 case tok::kw_long:
619 case tok::kw_signed:
620 case tok::kw_unsigned:
621 case tok::kw__Complex:
622 case tok::kw__Imaginary:
623 case tok::kw_void:
624 case tok::kw_char:
625 case tok::kw_int:
626 case tok::kw_float:
627 case tok::kw_double:
628 case tok::kw__Bool:
629 case tok::kw__Decimal32:
630 case tok::kw__Decimal64:
631 case tok::kw__Decimal128:
632
633 // struct-or-union-specifier
634 case tok::kw_struct:
635 case tok::kw_union:
636 // enum-specifier
637 case tok::kw_enum:
638
639 // type-qualifier
640 case tok::kw_const:
641 case tok::kw_volatile:
642 case tok::kw_restrict:
643 return true;
644
645 // typedef-name
646 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000647 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000648
649 // TODO: Attributes.
650 }
651}
652
Chris Lattneracd58a32006-08-06 17:24:14 +0000653/// isDeclarationSpecifier() - Return true if the current token is part of a
654/// declaration specifier.
655bool Parser::isDeclarationSpecifier() const {
656 switch (Tok.getKind()) {
657 default: return false;
658 // storage-class-specifier
659 case tok::kw_typedef:
660 case tok::kw_extern:
661 case tok::kw_static:
662 case tok::kw_auto:
663 case tok::kw_register:
664 case tok::kw___thread:
665
666 // type-specifiers
667 case tok::kw_short:
668 case tok::kw_long:
669 case tok::kw_signed:
670 case tok::kw_unsigned:
671 case tok::kw__Complex:
672 case tok::kw__Imaginary:
673 case tok::kw_void:
674 case tok::kw_char:
675 case tok::kw_int:
676 case tok::kw_float:
677 case tok::kw_double:
678 case tok::kw__Bool:
679 case tok::kw__Decimal32:
680 case tok::kw__Decimal64:
681 case tok::kw__Decimal128:
682
683 // struct-or-union-specifier
684 case tok::kw_struct:
685 case tok::kw_union:
686 // enum-specifier
687 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000688
Chris Lattneracd58a32006-08-06 17:24:14 +0000689 // type-qualifier
690 case tok::kw_const:
691 case tok::kw_volatile:
692 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000693
Chris Lattneracd58a32006-08-06 17:24:14 +0000694 // function-specifier
695 case tok::kw_inline:
696 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000697
Chris Lattneracd58a32006-08-06 17:24:14 +0000698 // typedef-name
699 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000700 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000701 // TODO: Attributes.
702 }
703}
704
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000705
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000706/// ParseTypeQualifierListOpt
707/// type-qualifier-list: [C99 6.7.5]
708/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000709/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000710/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000711/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000712///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000713void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
714 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000715 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000716 int isInvalid = false;
717 const char *PrevSpec = 0;
718
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000719 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000720 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000721 // If this is not a type-qualifier token, we're done reading type
722 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000723 DS.Finish(StartLoc, Diags, getLang());
724 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000725 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000726 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
727 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000728 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000729 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
730 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000731 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000732 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000733 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000734
735 case tok::kw___attribute:
736 ParseAttributes();
737 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000738 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000739
740 // If the specifier combination wasn't legal, issue a diagnostic.
741 if (isInvalid) {
742 assert(PrevSpec && "Method did not return previous specifier!");
743 if (isInvalid == 1) // Error.
744 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
745 else // extwarn.
746 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
747 }
748 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000749 }
750}
751
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000752
753/// ParseDeclarator - Parse and verify a newly-initialized declarator.
754///
755void Parser::ParseDeclarator(Declarator &D) {
756 /// This implements the 'declarator' production in the C grammar, then checks
757 /// for well-formedness and issues diagnostics.
758 ParseDeclaratorInternal(D);
759
Chris Lattner9fab3b92006-08-12 18:25:42 +0000760 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000761
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000762}
763
764/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000765/// declarator: [C99 6.7.5]
766/// pointer[opt] direct-declarator
767///
768/// pointer: [C99 6.7.5]
769/// '*' type-qualifier-list[opt]
770/// '*' type-qualifier-list[opt] pointer
771///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000772void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000773 if (Tok.getKind() != tok::star)
774 return ParseDirectDeclarator(D);
775
776 // Otherwise, '*' -> pointer.
Chris Lattneraf635312006-10-16 06:06:51 +0000777 SourceLocation Loc = ConsumeToken(); // Eat the *.
Chris Lattner6c7416c2006-08-07 00:19:33 +0000778 DeclSpec DS;
779 ParseTypeQualifierListOpt(DS);
780
781 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000782 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000783
Chris Lattner6c7416c2006-08-07 00:19:33 +0000784 // Remember that we parsed a pointer type, and remember the type-quals.
785 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
786}
787
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000788
789/// ParseDirectDeclarator
790/// direct-declarator: [C99 6.7.5]
791/// identifier
792/// '(' declarator ')'
793/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000794/// [C90] direct-declarator '[' constant-expression[opt] ']'
795/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
796/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
797/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
798/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000799/// direct-declarator '(' parameter-type-list ')'
800/// direct-declarator '(' identifier-list[opt] ')'
801/// [GNU] direct-declarator '(' parameter-forward-declarations
802/// parameter-type-list[opt] ')'
803///
Chris Lattneracd58a32006-08-06 17:24:14 +0000804void Parser::ParseDirectDeclarator(Declarator &D) {
805 // Parse the first direct-declarator seen.
806 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
807 assert(Tok.getIdentifierInfo() && "Not an identifier?");
808 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
809 ConsumeToken();
810 } else if (Tok.getKind() == tok::l_paren) {
811 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000812 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000813 // Example: 'char (*X)' or 'int (*XX)(void)'
814 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000815 } else if (D.mayOmitIdentifier()) {
816 // This could be something simple like "int" (in which case the declarator
817 // portion is empty), if an abstract-declarator is allowed.
818 D.SetIdentifier(0, Tok.getLocation());
819 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000820 // Expected identifier or '('.
821 Diag(Tok, diag::err_expected_ident_lparen);
822 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000823 }
824
825 assert(D.isPastIdentifier() &&
826 "Haven't past the location of the identifier yet?");
827
828 while (1) {
829 if (Tok.getKind() == tok::l_paren) {
830 ParseParenDeclarator(D);
831 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000832 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000833 } else {
834 break;
835 }
836 }
837}
838
839/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
840/// either be before the identifier (in which case these are just grouping
841/// parens for precedence) or it may be after the identifier, in which case
842/// these are function arguments.
843///
844/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000845/// parameter-type-list: [C99 6.7.5]
846/// parameter-list
847/// parameter-list ',' '...'
848///
849/// parameter-list: [C99 6.7.5]
850/// parameter-declaration
851/// parameter-list ',' parameter-declaration
852///
853/// parameter-declaration: [C99 6.7.5]
854/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000855/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000856/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000857/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000858///
859/// identifier-list: [C99 6.7.5]
860/// identifier
861/// identifier-list ',' identifier
862///
Chris Lattneracd58a32006-08-06 17:24:14 +0000863void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +0000864 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000865
Chris Lattneracd58a32006-08-06 17:24:14 +0000866 // If we haven't past the identifier yet (or where the identifier would be
867 // stored, if this is an abstract declarator), then this is probably just
868 // grouping parens.
869 if (!D.isPastIdentifier()) {
870 // Okay, this is probably a grouping paren. However, if this could be an
871 // abstract-declarator, then this could also be the start of function
872 // arguments (consider 'void()').
873 bool isGrouping;
874
875 if (!D.mayOmitIdentifier()) {
876 // If this can't be an abstract-declarator, this *must* be a grouping
877 // paren, because we haven't seen the identifier yet.
878 isGrouping = true;
879 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
880 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000881 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
882 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +0000883 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000884 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000885 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000886 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000887 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000888
889 // If this is a grouping paren, handle:
890 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000891 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000892 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000893 if (Tok.getKind() == tok::kw___attribute)
894 ParseAttributes();
895
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000896 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000897 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000898 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000899 return;
900 }
901
902 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000903 // argument list. Recognize that this declarator will never have an
904 // identifier (and remember where it would have been), then fall through to
905 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000906 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000907 }
908
Chris Lattneracd58a32006-08-06 17:24:14 +0000909 // Okay, this is the parameter list of a function definition, or it is an
910 // identifier list of a K&R-style function.
911
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000912
913
Chris Lattner9fab3b92006-08-12 18:25:42 +0000914 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000915 // arguments to the function scope.
916 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000917 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000918 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000919 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000920 bool ErrorEmitted = false;
921
Chris Lattneracd58a32006-08-06 17:24:14 +0000922 if (Tok.getKind() == tok::r_paren) {
923 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000924 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000925 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000926 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000927 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000928 // K&R identifier lists can't have typedefs as identifiers, per
929 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000930 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000931 // Identifier list. Note that '(' identifier-list ')' is only allowed for
932 // normal declarators, not for abstract-declarators.
933 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
934
935 // If there was no identifier specified, either we are in an
936 // abstract-declarator, or we are in a parameter declarator which was found
937 // to be abstract. In abstract-declarators, identifier lists are not valid,
938 // diagnose this.
939 if (!D.getIdentifier())
940 Diag(Tok, diag::ext_ident_list_in_param);
941
Chris Lattner9fab3b92006-08-12 18:25:42 +0000942 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000943 ConsumeToken();
944 while (Tok.getKind() == tok::comma) {
945 // Eat the comma.
946 ConsumeToken();
947
Chris Lattner0be454e2006-08-12 19:30:51 +0000948 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000949 ErrorEmitted = true;
950 break;
951 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000952 }
953
Chris Lattneracd58a32006-08-06 17:24:14 +0000954 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000955 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000956 HasPrototype = false;
957 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000958 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000959 bool ReadArg = false;
960 // Finally, a normal, non-empty parameter type list.
961 while (1) {
962 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000963 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000964
965 // Check to see if this is "void(...)" which is not allowed.
966 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000967 // Otherwise, parse parameter type list. If it starts with an
968 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000969 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000970 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000971 }
972
973 // Consume the ellipsis.
974 ConsumeToken();
975 break;
976 }
977
978 ReadArg = true;
979
980 // Parse the declaration-specifiers.
981 DeclSpec DS;
982 ParseDeclarationSpecifiers(DS);
983
984 // Parse the declarator. This is "PrototypeContext", because we must
985 // accept either 'declarator' or 'abstract-declarator' here.
986 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
987 ParseDeclarator(DeclaratorInfo);
988
Chris Lattnere37e2332006-08-15 04:50:22 +0000989 // Parse GNU attributes, if present.
990 if (Tok.getKind() == tok::kw___attribute)
991 ParseAttributes();
992
Chris Lattneracd58a32006-08-06 17:24:14 +0000993 // TODO: do something with the declarator, if it is valid.
994
995 // If the next token is a comma, consume it and keep reading arguments.
996 if (Tok.getKind() != tok::comma) break;
997
998 // Consume the comma.
999 ConsumeToken();
1000 }
1001
1002 HasPrototype = true;
1003 }
1004
Chris Lattner9fab3b92006-08-12 18:25:42 +00001005 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001006
Chris Lattner9fab3b92006-08-12 18:25:42 +00001007 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +00001008
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001009 // Remember that we parsed a function type, and remember the attributes.
1010 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001011 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001012
Chris Lattner14776b92006-08-06 22:27:40 +00001013
1014 // If we have the closing ')', eat it and we're done.
1015 if (Tok.getKind() == tok::r_paren) {
1016 ConsumeParen();
1017 } else {
1018 // If an error happened earlier parsing something else in the proto, don't
1019 // issue another error.
1020 if (!ErrorEmitted)
1021 Diag(Tok, diag::err_expected_rparen);
1022 SkipUntil(tok::r_paren);
1023 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001024}
Chris Lattneracd58a32006-08-06 17:24:14 +00001025
Chris Lattnere8074e62006-08-06 18:30:15 +00001026
1027/// [C90] direct-declarator '[' constant-expression[opt] ']'
1028/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1029/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1030/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1031/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1032void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001033 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001034
1035 // If valid, this location is the position where we read the 'static' keyword.
1036 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001037 if (Tok.getKind() == tok::kw_static)
1038 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001039
1040 // If there is a type-qualifier-list, read it now.
1041 DeclSpec DS;
1042 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001043
1044 // If we haven't already read 'static', check to see if there is one after the
1045 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001046 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1047 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001048
1049 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001050 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001051 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001052 if (Tok.getKind() == tok::star) {
1053 // Remember the '*' token, in case we have to un-get it.
1054 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001055 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001056
1057 // Check that the ']' token is present to avoid incorrectly parsing
1058 // expressions starting with '*' as [*].
1059 if (Tok.getKind() == tok::r_square) {
1060 if (StaticLoc.isValid())
1061 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1062 StaticLoc = SourceLocation(); // Drop the static.
1063 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001064 } else {
1065 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001066 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001067 // need to reparse it. This handles cases like 'X[*p + 4]'
1068 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001069 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001070 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001071 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001072 NumElements = ParseAssignmentExpression();
1073 }
1074
1075 // If there was an error parsing the assignment-expression, recover.
1076 if (NumElements.isInvalid) {
1077 // If the expression was invalid, skip it.
1078 SkipUntil(tok::r_square);
1079 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001080 }
1081
Chris Lattner04f80192006-08-15 04:55:54 +00001082 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001083
Chris Lattnere8074e62006-08-06 18:30:15 +00001084 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1085 // it was not a constant expression.
1086 if (!getLang().C99) {
1087 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001088 if (isStar || StaticLoc.isValid() ||
1089 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001090 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001091 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001092
1093 // Remember that we parsed a pointer type, and remember the type-quals.
1094 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1095 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001096 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001097}
1098