blob: 36e53e08de600e1aceb2a7f36cf8b0940f8a53b9 [file] [log] [blame]
Chris Lattnereb8a28f2006-08-10 18:43:39 +00001//===--- Declaration.cpp - Declaration Parsing ----------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattnerb9093cd2006-08-04 04:39:53 +000015#include "clang/Parse/Declarations.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000016using namespace llvm;
17using namespace clang;
18
19//===----------------------------------------------------------------------===//
20// C99 6.7: Declarations.
21//===----------------------------------------------------------------------===//
22
Chris Lattnerf5fbd792006-08-10 23:56:11 +000023/// ParseTypeName
24/// type-name: [C99 6.7.6]
25/// specifier-qualifier-list abstract-declarator[opt]
26void Parser::ParseTypeName() {
27 // Parse the common declaration-specifiers piece.
28 DeclSpec DS;
29 SourceLocation Loc = Tok.getLocation();
30 ParseDeclarationSpecifiers(DS);
31
32 // Validate declspec for type-name.
33 unsigned Specs = DS.getParsedSpecifiers();
34 if (Specs == DeclSpec::PQ_None)
35 Diag(Tok, diag::err_typename_requires_specqual);
36
37 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
38 Diag(Loc, diag::err_typename_invalid_storageclass);
39 // Remove storage class.
40 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
41 DS.SCS_thread_specified = false;
42 }
43 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
44 Diag(Loc, diag::err_typename_invalid_functionspec);
45 DS.FS_inline_specified = false;
46 }
47
48 // Parse the abstract-declarator, if present.
49 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
50 ParseDeclarator(DeclaratorInfo);
51}
52
53
Chris Lattner53361ac2006-08-10 05:19:57 +000054/// ParseDeclaration - Parse a full 'declaration', which consists of
55/// declaration-specifiers, some number of declarators, and a semicolon.
56/// 'Context' should be a Declarator::TheContext value.
57void Parser::ParseDeclaration(unsigned Context) {
58 // Parse the common declaration-specifiers piece.
59 DeclSpec DS;
60 ParseDeclarationSpecifiers(DS);
61
62 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
63 ParseDeclarator(DeclaratorInfo);
64
65 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
66}
67
68void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
69 // At this point, we know that it is not a function definition. Parse the
70 // rest of the init-declarator-list.
71 while (1) {
72 // must be: decl-spec[opt] declarator init-declarator-list
73 // Parse declarator '=' initializer.
74 if (Tok.getKind() == tok::equal) {
75 ConsumeToken();
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000076 ParseInitializer();
Chris Lattner53361ac2006-08-10 05:19:57 +000077 }
78
Chris Lattner53361ac2006-08-10 05:19:57 +000079 // TODO: install declarator.
80
81 // If we don't have a comma, it is either the end of the list (a ';') or an
82 // error, bail out.
83 if (Tok.getKind() != tok::comma)
84 break;
85
86 // Consume the comma.
87 ConsumeToken();
88
89 // Parse the next declarator.
90 D.clear();
91 ParseDeclarator(D);
92 }
93
94 if (Tok.getKind() == tok::semi) {
95 ConsumeToken();
96 } else {
97 Diag(Tok, diag::err_parse_error);
98 // Skip to end of block or statement
99 SkipUntil(tok::r_brace, true);
100 if (Tok.getKind() == tok::semi)
101 ConsumeToken();
102 }
103}
104
105
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000106/// ParseDeclarationSpecifiers
107/// declaration-specifiers: [C99 6.7]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000108/// storage-class-specifier declaration-specifiers [opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000109/// type-specifier declaration-specifiers [opt]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000110/// type-qualifier declaration-specifiers [opt]
111/// [C99] function-specifier declaration-specifiers [opt]
112/// [GNU] attributes declaration-specifiers [opt] [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000113///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000114/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000115/// 'typedef'
116/// 'extern'
117/// 'static'
118/// 'auto'
119/// 'register'
120/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000121/// type-specifier: [C99 6.7.2]
122/// 'void'
123/// 'char'
124/// 'short'
125/// 'int'
126/// 'long'
127/// 'float'
128/// 'double'
129/// 'signed'
130/// 'unsigned'
Chris Lattner8e90ef62006-08-05 03:30:45 +0000131/// struct-or-union-specifier [TODO]
132/// enum-specifier [TODO]
133/// typedef-name [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000134/// [C99] '_Bool'
135/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000136/// [C99] '_Imaginary' // Removed in TC2?
137/// [GNU] '_Decimal32'
138/// [GNU] '_Decimal64'
139/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000140/// [GNU] typeof-specifier [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000141/// [OBJC] class-name objc-protocol-refs [opt] [TODO]
142/// [OBJC] typedef-name objc-protocol-refs [TODO]
143/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000144/// type-qualifier:
145/// const
146/// volatile
147/// [C99] restrict
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000148/// function-specifier: [C99 6.7.4]
149/// [C99] inline
150///
151void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
152 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000153 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000154 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000155 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000156 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000157 default:
158 // If this is not a declaration specifier token, we're done reading decl
159 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000160 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000161 return;
Chris Lattner9fab3b92006-08-12 18:25:42 +0000162 // FIXME: Handle struct/union/enum tags.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000163
164 // storage-class-specifier
165 case tok::kw_typedef:
166 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
167 break;
168 case tok::kw_extern:
169 if (DS.SCS_thread_specified)
170 Diag(Tok, diag::ext_thread_before, "extern");
171 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
172 break;
173 case tok::kw_static:
174 if (DS.SCS_thread_specified)
175 Diag(Tok, diag::ext_thread_before, "static");
176 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
177 break;
178 case tok::kw_auto:
179 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
180 break;
181 case tok::kw_register:
182 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
183 break;
184 case tok::kw___thread:
185 if (DS.SCS_thread_specified)
186 isInvalid = 2, PrevSpec = "__thread";
187 else
188 DS.SCS_thread_specified = true;
189 break;
190
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000191 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000192 case tok::kw_short:
193 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
194 break;
195 case tok::kw_long:
196 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
197 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
198 } else {
199 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
200 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
201 }
202 break;
203 case tok::kw_signed:
204 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
205 break;
206 case tok::kw_unsigned:
207 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
208 break;
209 case tok::kw__Complex:
210 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
211 break;
212 case tok::kw__Imaginary:
213 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
214 break;
215 case tok::kw_void:
216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
217 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000218 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000219 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
220 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000221 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000222 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
223 break;
224 case tok::kw_float:
225 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
226 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000227 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000228 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
229 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000230 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000231 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
232 break;
233 case tok::kw__Decimal32:
234 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
235 break;
236 case tok::kw__Decimal64:
237 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
238 break;
239 case tok::kw__Decimal128:
240 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000241 break;
242
243 //case tok::kw_struct:
244 //case tok::kw_union:
245 //case tok::kw_enum:
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000246
Chris Lattneracd58a32006-08-06 17:24:14 +0000247 //case tok::identifier:
248 // TODO: handle typedef names.
249
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000250 // type-qualifier
251 case tok::kw_const:
252 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
253 break;
254 case tok::kw_volatile:
255 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
256 break;
257 case tok::kw_restrict:
258 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
259 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000260
261 // function-specifier
262 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000263 // 'inline inline' is ok.
264 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000265 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000266 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000267 // If the specifier combination wasn't legal, issue a diagnostic.
268 if (isInvalid) {
269 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000270 if (isInvalid == 1) // Error.
271 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
272 else // extwarn.
273 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000274 }
275 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000276 }
277}
278
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000279/// isTypeSpecifierQualifier - Return true if the current token could be the
280/// start of a specifier-qualifier-list.
281bool Parser::isTypeSpecifierQualifier() const {
282 switch (Tok.getKind()) {
283 default: return false;
284 // type-specifiers
285 case tok::kw_short:
286 case tok::kw_long:
287 case tok::kw_signed:
288 case tok::kw_unsigned:
289 case tok::kw__Complex:
290 case tok::kw__Imaginary:
291 case tok::kw_void:
292 case tok::kw_char:
293 case tok::kw_int:
294 case tok::kw_float:
295 case tok::kw_double:
296 case tok::kw__Bool:
297 case tok::kw__Decimal32:
298 case tok::kw__Decimal64:
299 case tok::kw__Decimal128:
300
301 // struct-or-union-specifier
302 case tok::kw_struct:
303 case tok::kw_union:
304 // enum-specifier
305 case tok::kw_enum:
306
307 // type-qualifier
308 case tok::kw_const:
309 case tok::kw_volatile:
310 case tok::kw_restrict:
311 return true;
312
313 // typedef-name
314 case tok::identifier:
315 // FIXME: if this is a typedef return true.
316 return false;
317
318 // TODO: Attributes.
319 }
320}
321
Chris Lattneracd58a32006-08-06 17:24:14 +0000322/// isDeclarationSpecifier() - Return true if the current token is part of a
323/// declaration specifier.
324bool Parser::isDeclarationSpecifier() const {
325 switch (Tok.getKind()) {
326 default: return false;
327 // storage-class-specifier
328 case tok::kw_typedef:
329 case tok::kw_extern:
330 case tok::kw_static:
331 case tok::kw_auto:
332 case tok::kw_register:
333 case tok::kw___thread:
334
335 // type-specifiers
336 case tok::kw_short:
337 case tok::kw_long:
338 case tok::kw_signed:
339 case tok::kw_unsigned:
340 case tok::kw__Complex:
341 case tok::kw__Imaginary:
342 case tok::kw_void:
343 case tok::kw_char:
344 case tok::kw_int:
345 case tok::kw_float:
346 case tok::kw_double:
347 case tok::kw__Bool:
348 case tok::kw__Decimal32:
349 case tok::kw__Decimal64:
350 case tok::kw__Decimal128:
351
352 // struct-or-union-specifier
353 case tok::kw_struct:
354 case tok::kw_union:
355 // enum-specifier
356 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000357
Chris Lattneracd58a32006-08-06 17:24:14 +0000358 // type-qualifier
359 case tok::kw_const:
360 case tok::kw_volatile:
361 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000362
Chris Lattneracd58a32006-08-06 17:24:14 +0000363 // function-specifier
364 case tok::kw_inline:
365 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000366
Chris Lattneracd58a32006-08-06 17:24:14 +0000367 // typedef-name
368 case tok::identifier:
369 // FIXME: if this is a typedef return true.
370 return false;
371 // TODO: Attributes.
372 }
373}
374
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000375
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000376/// ParseTypeQualifierListOpt
377/// type-qualifier-list: [C99 6.7.5]
378/// type-qualifier
379/// [GNU] attributes [TODO]
380/// type-qualifier-list type-qualifier
381/// [GNU] type-qualifier-list attributes [TODO]
382///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000383void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
384 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000385 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000386 int isInvalid = false;
387 const char *PrevSpec = 0;
388
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000389 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000390 default:
391 // If this is not a declaration specifier token, we're done reading decl
392 // specifiers. First verify that DeclSpec's are consistent.
393 DS.Finish(StartLoc, Diags, getLang());
394 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000395 // TODO: attributes.
396 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000397 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
398 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000399 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000400 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
401 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000402 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000403 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000404 break;
405 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000406
407 // If the specifier combination wasn't legal, issue a diagnostic.
408 if (isInvalid) {
409 assert(PrevSpec && "Method did not return previous specifier!");
410 if (isInvalid == 1) // Error.
411 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
412 else // extwarn.
413 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
414 }
415 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000416 }
417}
418
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000419
420/// ParseDeclarator - Parse and verify a newly-initialized declarator.
421///
422void Parser::ParseDeclarator(Declarator &D) {
423 /// This implements the 'declarator' production in the C grammar, then checks
424 /// for well-formedness and issues diagnostics.
425 ParseDeclaratorInternal(D);
426
Chris Lattner9fab3b92006-08-12 18:25:42 +0000427 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000428
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000429}
430
431/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000432/// declarator: [C99 6.7.5]
433/// pointer[opt] direct-declarator
434///
435/// pointer: [C99 6.7.5]
436/// '*' type-qualifier-list[opt]
437/// '*' type-qualifier-list[opt] pointer
438///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000439void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000440 if (Tok.getKind() != tok::star)
441 return ParseDirectDeclarator(D);
442
443 // Otherwise, '*' -> pointer.
444 SourceLocation Loc = Tok.getLocation();
445 ConsumeToken(); // Eat the *.
446 DeclSpec DS;
447 ParseTypeQualifierListOpt(DS);
448
449 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000450 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000451
452 // Remember that we parsed a pointer type, and remember the type-quals.
453 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
454}
455
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000456
457/// ParseDirectDeclarator
458/// direct-declarator: [C99 6.7.5]
459/// identifier
460/// '(' declarator ')'
461/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000462/// [C90] direct-declarator '[' constant-expression[opt] ']'
463/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
464/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
465/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
466/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000467/// direct-declarator '(' parameter-type-list ')'
468/// direct-declarator '(' identifier-list[opt] ')'
469/// [GNU] direct-declarator '(' parameter-forward-declarations
470/// parameter-type-list[opt] ')'
471///
Chris Lattneracd58a32006-08-06 17:24:14 +0000472void Parser::ParseDirectDeclarator(Declarator &D) {
473 // Parse the first direct-declarator seen.
474 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
475 assert(Tok.getIdentifierInfo() && "Not an identifier?");
476 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
477 ConsumeToken();
478 } else if (Tok.getKind() == tok::l_paren) {
479 // direct-declarator: '(' declarator ')'
480 // direct-declarator: '(' attributes declarator ')' [TODO]
481 // Example: 'char (*X)' or 'int (*XX)(void)'
482 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000483 } else if (D.mayOmitIdentifier()) {
484 // This could be something simple like "int" (in which case the declarator
485 // portion is empty), if an abstract-declarator is allowed.
486 D.SetIdentifier(0, Tok.getLocation());
487 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000488 // Expected identifier or '('.
489 Diag(Tok, diag::err_expected_ident_lparen);
490 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000491 }
492
493 assert(D.isPastIdentifier() &&
494 "Haven't past the location of the identifier yet?");
495
496 while (1) {
497 if (Tok.getKind() == tok::l_paren) {
498 ParseParenDeclarator(D);
499 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000500 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000501 } else {
502 break;
503 }
504 }
505}
506
507/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
508/// either be before the identifier (in which case these are just grouping
509/// parens for precedence) or it may be after the identifier, in which case
510/// these are function arguments.
511///
512/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000513/// parameter-type-list: [C99 6.7.5]
514/// parameter-list
515/// parameter-list ',' '...'
516///
517/// parameter-list: [C99 6.7.5]
518/// parameter-declaration
519/// parameter-list ',' parameter-declaration
520///
521/// parameter-declaration: [C99 6.7.5]
522/// declaration-specifiers declarator
Chris Lattneracd58a32006-08-06 17:24:14 +0000523/// [GNU] declaration-specifiers declarator attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000524/// declaration-specifiers abstract-declarator[opt]
Chris Lattneracd58a32006-08-06 17:24:14 +0000525/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000526///
527/// identifier-list: [C99 6.7.5]
528/// identifier
529/// identifier-list ',' identifier
530///
Chris Lattneracd58a32006-08-06 17:24:14 +0000531void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000532 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000533 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000534
Chris Lattneracd58a32006-08-06 17:24:14 +0000535 // If we haven't past the identifier yet (or where the identifier would be
536 // stored, if this is an abstract declarator), then this is probably just
537 // grouping parens.
538 if (!D.isPastIdentifier()) {
539 // Okay, this is probably a grouping paren. However, if this could be an
540 // abstract-declarator, then this could also be the start of function
541 // arguments (consider 'void()').
542 bool isGrouping;
543
544 if (!D.mayOmitIdentifier()) {
545 // If this can't be an abstract-declarator, this *must* be a grouping
546 // paren, because we haven't seen the identifier yet.
547 isGrouping = true;
548 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
549 isDeclarationSpecifier()) { // 'int(int)' is a function.
550
551 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000552 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000553 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000554 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000555 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000556
557 // If this is a grouping paren, handle:
558 // direct-declarator: '(' declarator ')'
559 // direct-declarator: '(' attributes declarator ')' [TODO]
560 if (isGrouping) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000561 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000562 // Match the ')'.
563 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
564 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000565 return;
566 }
567
568 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000569 // argument list. Recognize that this declarator will never have an
570 // identifier (and remember where it would have been), then fall through to
571 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000572 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000573 }
574
Chris Lattneracd58a32006-08-06 17:24:14 +0000575 // Okay, this is the parameter list of a function definition, or it is an
576 // identifier list of a K&R-style function.
577
Chris Lattner9fab3b92006-08-12 18:25:42 +0000578 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000579 // arguments to the function scope.
580 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000581 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000582 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000583 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000584 bool ErrorEmitted = false;
585
Chris Lattneracd58a32006-08-06 17:24:14 +0000586 if (Tok.getKind() == tok::r_paren) {
587 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000588 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000589 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000590 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000591 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000592 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000593 // Identifier list. Note that '(' identifier-list ')' is only allowed for
594 // normal declarators, not for abstract-declarators.
595 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
596
597 // If there was no identifier specified, either we are in an
598 // abstract-declarator, or we are in a parameter declarator which was found
599 // to be abstract. In abstract-declarators, identifier lists are not valid,
600 // diagnose this.
601 if (!D.getIdentifier())
602 Diag(Tok, diag::ext_ident_list_in_param);
603
Chris Lattner9fab3b92006-08-12 18:25:42 +0000604 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000605 ConsumeToken();
606 while (Tok.getKind() == tok::comma) {
607 // Eat the comma.
608 ConsumeToken();
609
Chris Lattner14776b92006-08-06 22:27:40 +0000610 if (Tok.getKind() != tok::identifier) {
611 // If not identifier, diagnose the error.
612 Diag(Tok, diag::err_expected_ident);
613 ErrorEmitted = true;
614 break;
615 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000616
617 // Eat the id.
Chris Lattner9fab3b92006-08-12 18:25:42 +0000618 // TODO: remember it!
Chris Lattneracd58a32006-08-06 17:24:14 +0000619 ConsumeToken();
620 }
621
Chris Lattneracd58a32006-08-06 17:24:14 +0000622 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000623 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000624 HasPrototype = false;
625 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000626 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000627 bool ReadArg = false;
628 // Finally, a normal, non-empty parameter type list.
629 while (1) {
630 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000631 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000632
633 // Check to see if this is "void(...)" which is not allowed.
634 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000635 // Otherwise, parse parameter type list. If it starts with an
636 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000637 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000638 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000639 }
640
641 // Consume the ellipsis.
642 ConsumeToken();
643 break;
644 }
645
646 ReadArg = true;
647
648 // Parse the declaration-specifiers.
649 DeclSpec DS;
650 ParseDeclarationSpecifiers(DS);
651
652 // Parse the declarator. This is "PrototypeContext", because we must
653 // accept either 'declarator' or 'abstract-declarator' here.
654 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
655 ParseDeclarator(DeclaratorInfo);
656
657 // TODO: do something with the declarator, if it is valid.
658
659 // If the next token is a comma, consume it and keep reading arguments.
660 if (Tok.getKind() != tok::comma) break;
661
662 // Consume the comma.
663 ConsumeToken();
664 }
665
666 HasPrototype = true;
667 }
668
Chris Lattner9fab3b92006-08-12 18:25:42 +0000669 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000670
Chris Lattner9fab3b92006-08-12 18:25:42 +0000671 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000672
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000673 // Remember that we parsed a function type, and remember the attributes.
674 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000675 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000676
Chris Lattner14776b92006-08-06 22:27:40 +0000677
678 // If we have the closing ')', eat it and we're done.
679 if (Tok.getKind() == tok::r_paren) {
680 ConsumeParen();
681 } else {
682 // If an error happened earlier parsing something else in the proto, don't
683 // issue another error.
684 if (!ErrorEmitted)
685 Diag(Tok, diag::err_expected_rparen);
686 SkipUntil(tok::r_paren);
687 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000688}
Chris Lattneracd58a32006-08-06 17:24:14 +0000689
Chris Lattnere8074e62006-08-06 18:30:15 +0000690
691/// [C90] direct-declarator '[' constant-expression[opt] ']'
692/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
693/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
694/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
695/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
696void Parser::ParseBracketDeclarator(Declarator &D) {
697 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +0000698 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +0000699
700 // If valid, this location is the position where we read the 'static' keyword.
701 SourceLocation StaticLoc;
702 if (Tok.getKind() == tok::kw_static) {
703 StaticLoc = Tok.getLocation();
704 ConsumeToken();
705 }
706
707 // If there is a type-qualifier-list, read it now.
708 DeclSpec DS;
709 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +0000710
711 // If we haven't already read 'static', check to see if there is one after the
712 // type-qualifier-list.
713 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
714 StaticLoc = Tok.getLocation();
715 ConsumeToken();
716 }
717
718 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +0000719 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +0000720 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +0000721 if (Tok.getKind() == tok::star) {
722 // Remember the '*' token, in case we have to un-get it.
723 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +0000724 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +0000725
726 // Check that the ']' token is present to avoid incorrectly parsing
727 // expressions starting with '*' as [*].
728 if (Tok.getKind() == tok::r_square) {
729 if (StaticLoc.isValid())
730 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
731 StaticLoc = SourceLocation(); // Drop the static.
732 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +0000733 } else {
734 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +0000735 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +0000736 // need to reparse it. This handles cases like 'X[*p + 4]'
737 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +0000738 }
Chris Lattner9fab3b92006-08-12 18:25:42 +0000739 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000740 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +0000741 NumElements = ParseAssignmentExpression();
742 }
743
744 // If there was an error parsing the assignment-expression, recover.
745 if (NumElements.isInvalid) {
746 // If the expression was invalid, skip it.
747 SkipUntil(tok::r_square);
748 return;
Chris Lattnere8074e62006-08-06 18:30:15 +0000749 }
750
Chris Lattner9fab3b92006-08-12 18:25:42 +0000751 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
752
Chris Lattnere8074e62006-08-06 18:30:15 +0000753 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
754 // it was not a constant expression.
755 if (!getLang().C99) {
756 // TODO: check C90 array constant exprness.
Chris Lattner62591722006-08-12 18:40:58 +0000757 if (isStar || StaticLoc.isValid() || 0/*FIXME: NumElts is constantexpr*/)
Chris Lattner8a39edc2006-08-06 18:33:32 +0000758 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +0000759 }
Chris Lattner6c7416c2006-08-07 00:19:33 +0000760
761 // Remember that we parsed a pointer type, and remember the type-quals.
762 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
763 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +0000764 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +0000765}
766