blob: 014e59fec9fbf2c8948c3373c5ce561d04941c60 [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 Lattner3b561a32006-08-13 00:12:11 +0000108/// storage-class-specifier declaration-specifiers[opt]
109/// type-specifier declaration-specifiers[opt]
110/// 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]
Chris Lattner3b561a32006-08-13 00:12:11 +0000132/// enum-specifier
Chris Lattner8e90ef62006-08-05 03:30:45 +0000133/// 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 Lattner3b561a32006-08-13 00:12:11 +0000141/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000142/// [OBJC] typedef-name objc-protocol-refs [TODO]
143/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000144/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000145/// 'const'
146/// 'volatile'
147/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000148/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000149/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000150///
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 Lattner3b561a32006-08-13 00:12:11 +0000162 // FIXME: Handle struct/union 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:
Chris Lattner3b561a32006-08-13 00:12:11 +0000245 case tok::kw_enum:
246 ParseEnumSpecifier(DS);
247 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000248
Chris Lattneracd58a32006-08-06 17:24:14 +0000249 //case tok::identifier:
250 // TODO: handle typedef names.
251
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000252 // type-qualifier
253 case tok::kw_const:
254 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
255 break;
256 case tok::kw_volatile:
257 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
258 break;
259 case tok::kw_restrict:
260 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
261 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000262
263 // function-specifier
264 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000265 // 'inline inline' is ok.
266 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000267 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000268 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000269 // If the specifier combination wasn't legal, issue a diagnostic.
270 if (isInvalid) {
271 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000272 if (isInvalid == 1) // Error.
273 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
274 else // extwarn.
275 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000276 }
277 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000278 }
279}
280
Chris Lattner3b561a32006-08-13 00:12:11 +0000281/// ParseEnumSpecifier
282/// enum-specifier:
283/// 'enum' identifier[opt] '{' enumerator-list '}'
284/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
285/// 'enum' identifier
286/// enumerator-list:
287/// enumerator
288/// enumerator-list , enumerator
289/// enumerator:
290/// enumeration-constant
291/// enumeration-constant = constant-expression
292/// enumeration-constant:
293/// identifier
294///
295void Parser::ParseEnumSpecifier(DeclSpec &DS) {
296 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
297 ConsumeToken();
298
299 // Must have either 'enum name' or 'enum {...}'.
300 if (Tok.getKind() != tok::identifier &&
301 Tok.getKind() != tok::l_brace) {
302 Diag(Tok, diag::err_expected_ident_lbrace);
303 return;
304 }
305
306 if (Tok.getKind() == tok::identifier)
307 ConsumeToken();
308
309 if (Tok.getKind() == tok::l_brace) {
310 SourceLocation LBraceLoc = Tok.getLocation();
311 ConsumeBrace();
312
313 // Parse the enumerator-list.
314 while (Tok.getKind() == tok::identifier) {
315 ConsumeToken();
316
317 if (Tok.getKind() == tok::equal) {
318 ConsumeToken();
319 ExprResult Res = ParseConstantExpression();
320 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
321 }
322
323 if (Tok.getKind() != tok::comma)
324 break;
325 SourceLocation CommaLoc = Tok.getLocation();
326 ConsumeToken();
327
328 if (Tok.getKind() != tok::identifier && !getLang().C99)
329 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
330 }
331
332 // Eat the }.
333 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
334 diag::err_expected_rbrace);
335 }
336 // TODO: semantic analysis on the declspec for enums.
337}
338
339
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000340/// isTypeSpecifierQualifier - Return true if the current token could be the
341/// start of a specifier-qualifier-list.
342bool Parser::isTypeSpecifierQualifier() const {
343 switch (Tok.getKind()) {
344 default: return false;
345 // type-specifiers
346 case tok::kw_short:
347 case tok::kw_long:
348 case tok::kw_signed:
349 case tok::kw_unsigned:
350 case tok::kw__Complex:
351 case tok::kw__Imaginary:
352 case tok::kw_void:
353 case tok::kw_char:
354 case tok::kw_int:
355 case tok::kw_float:
356 case tok::kw_double:
357 case tok::kw__Bool:
358 case tok::kw__Decimal32:
359 case tok::kw__Decimal64:
360 case tok::kw__Decimal128:
361
362 // struct-or-union-specifier
363 case tok::kw_struct:
364 case tok::kw_union:
365 // enum-specifier
366 case tok::kw_enum:
367
368 // type-qualifier
369 case tok::kw_const:
370 case tok::kw_volatile:
371 case tok::kw_restrict:
372 return true;
373
374 // typedef-name
375 case tok::identifier:
376 // FIXME: if this is a typedef return true.
377 return false;
378
379 // TODO: Attributes.
380 }
381}
382
Chris Lattneracd58a32006-08-06 17:24:14 +0000383/// isDeclarationSpecifier() - Return true if the current token is part of a
384/// declaration specifier.
385bool Parser::isDeclarationSpecifier() const {
386 switch (Tok.getKind()) {
387 default: return false;
388 // storage-class-specifier
389 case tok::kw_typedef:
390 case tok::kw_extern:
391 case tok::kw_static:
392 case tok::kw_auto:
393 case tok::kw_register:
394 case tok::kw___thread:
395
396 // type-specifiers
397 case tok::kw_short:
398 case tok::kw_long:
399 case tok::kw_signed:
400 case tok::kw_unsigned:
401 case tok::kw__Complex:
402 case tok::kw__Imaginary:
403 case tok::kw_void:
404 case tok::kw_char:
405 case tok::kw_int:
406 case tok::kw_float:
407 case tok::kw_double:
408 case tok::kw__Bool:
409 case tok::kw__Decimal32:
410 case tok::kw__Decimal64:
411 case tok::kw__Decimal128:
412
413 // struct-or-union-specifier
414 case tok::kw_struct:
415 case tok::kw_union:
416 // enum-specifier
417 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000418
Chris Lattneracd58a32006-08-06 17:24:14 +0000419 // type-qualifier
420 case tok::kw_const:
421 case tok::kw_volatile:
422 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000423
Chris Lattneracd58a32006-08-06 17:24:14 +0000424 // function-specifier
425 case tok::kw_inline:
426 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000427
Chris Lattneracd58a32006-08-06 17:24:14 +0000428 // typedef-name
429 case tok::identifier:
430 // FIXME: if this is a typedef return true.
431 return false;
432 // TODO: Attributes.
433 }
434}
435
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000436
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000437/// ParseTypeQualifierListOpt
438/// type-qualifier-list: [C99 6.7.5]
439/// type-qualifier
440/// [GNU] attributes [TODO]
441/// type-qualifier-list type-qualifier
442/// [GNU] type-qualifier-list attributes [TODO]
443///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000444void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
445 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000446 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000447 int isInvalid = false;
448 const char *PrevSpec = 0;
449
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000450 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000451 default:
452 // If this is not a declaration specifier token, we're done reading decl
453 // specifiers. First verify that DeclSpec's are consistent.
454 DS.Finish(StartLoc, Diags, getLang());
455 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000456 // TODO: attributes.
457 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000458 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
459 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000460 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000461 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
462 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000463 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000464 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000465 break;
466 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000467
468 // If the specifier combination wasn't legal, issue a diagnostic.
469 if (isInvalid) {
470 assert(PrevSpec && "Method did not return previous specifier!");
471 if (isInvalid == 1) // Error.
472 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
473 else // extwarn.
474 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
475 }
476 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000477 }
478}
479
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000480
481/// ParseDeclarator - Parse and verify a newly-initialized declarator.
482///
483void Parser::ParseDeclarator(Declarator &D) {
484 /// This implements the 'declarator' production in the C grammar, then checks
485 /// for well-formedness and issues diagnostics.
486 ParseDeclaratorInternal(D);
487
Chris Lattner9fab3b92006-08-12 18:25:42 +0000488 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000489
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000490}
491
492/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000493/// declarator: [C99 6.7.5]
494/// pointer[opt] direct-declarator
495///
496/// pointer: [C99 6.7.5]
497/// '*' type-qualifier-list[opt]
498/// '*' type-qualifier-list[opt] pointer
499///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000500void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000501 if (Tok.getKind() != tok::star)
502 return ParseDirectDeclarator(D);
503
504 // Otherwise, '*' -> pointer.
505 SourceLocation Loc = Tok.getLocation();
506 ConsumeToken(); // Eat the *.
507 DeclSpec DS;
508 ParseTypeQualifierListOpt(DS);
509
510 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000511 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000512
513 // Remember that we parsed a pointer type, and remember the type-quals.
514 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
515}
516
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000517
518/// ParseDirectDeclarator
519/// direct-declarator: [C99 6.7.5]
520/// identifier
521/// '(' declarator ')'
522/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000523/// [C90] direct-declarator '[' constant-expression[opt] ']'
524/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
525/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
526/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
527/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000528/// direct-declarator '(' parameter-type-list ')'
529/// direct-declarator '(' identifier-list[opt] ')'
530/// [GNU] direct-declarator '(' parameter-forward-declarations
531/// parameter-type-list[opt] ')'
532///
Chris Lattneracd58a32006-08-06 17:24:14 +0000533void Parser::ParseDirectDeclarator(Declarator &D) {
534 // Parse the first direct-declarator seen.
535 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
536 assert(Tok.getIdentifierInfo() && "Not an identifier?");
537 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
538 ConsumeToken();
539 } else if (Tok.getKind() == tok::l_paren) {
540 // direct-declarator: '(' declarator ')'
541 // direct-declarator: '(' attributes declarator ')' [TODO]
542 // Example: 'char (*X)' or 'int (*XX)(void)'
543 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000544 } else if (D.mayOmitIdentifier()) {
545 // This could be something simple like "int" (in which case the declarator
546 // portion is empty), if an abstract-declarator is allowed.
547 D.SetIdentifier(0, Tok.getLocation());
548 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000549 // Expected identifier or '('.
550 Diag(Tok, diag::err_expected_ident_lparen);
551 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000552 }
553
554 assert(D.isPastIdentifier() &&
555 "Haven't past the location of the identifier yet?");
556
557 while (1) {
558 if (Tok.getKind() == tok::l_paren) {
559 ParseParenDeclarator(D);
560 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000561 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000562 } else {
563 break;
564 }
565 }
566}
567
568/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
569/// either be before the identifier (in which case these are just grouping
570/// parens for precedence) or it may be after the identifier, in which case
571/// these are function arguments.
572///
573/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000574/// parameter-type-list: [C99 6.7.5]
575/// parameter-list
576/// parameter-list ',' '...'
577///
578/// parameter-list: [C99 6.7.5]
579/// parameter-declaration
580/// parameter-list ',' parameter-declaration
581///
582/// parameter-declaration: [C99 6.7.5]
583/// declaration-specifiers declarator
Chris Lattneracd58a32006-08-06 17:24:14 +0000584/// [GNU] declaration-specifiers declarator attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000585/// declaration-specifiers abstract-declarator[opt]
Chris Lattneracd58a32006-08-06 17:24:14 +0000586/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000587///
588/// identifier-list: [C99 6.7.5]
589/// identifier
590/// identifier-list ',' identifier
591///
Chris Lattneracd58a32006-08-06 17:24:14 +0000592void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000593 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000594 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000595
Chris Lattneracd58a32006-08-06 17:24:14 +0000596 // If we haven't past the identifier yet (or where the identifier would be
597 // stored, if this is an abstract declarator), then this is probably just
598 // grouping parens.
599 if (!D.isPastIdentifier()) {
600 // Okay, this is probably a grouping paren. However, if this could be an
601 // abstract-declarator, then this could also be the start of function
602 // arguments (consider 'void()').
603 bool isGrouping;
604
605 if (!D.mayOmitIdentifier()) {
606 // If this can't be an abstract-declarator, this *must* be a grouping
607 // paren, because we haven't seen the identifier yet.
608 isGrouping = true;
609 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
610 isDeclarationSpecifier()) { // 'int(int)' is a function.
611
612 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000613 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000614 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000615 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000616 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000617
618 // If this is a grouping paren, handle:
619 // direct-declarator: '(' declarator ')'
620 // direct-declarator: '(' attributes declarator ')' [TODO]
621 if (isGrouping) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000622 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000623 // Match the ')'.
624 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
625 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000626 return;
627 }
628
629 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000630 // argument list. Recognize that this declarator will never have an
631 // identifier (and remember where it would have been), then fall through to
632 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000633 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000634 }
635
Chris Lattneracd58a32006-08-06 17:24:14 +0000636 // Okay, this is the parameter list of a function definition, or it is an
637 // identifier list of a K&R-style function.
638
Chris Lattner9fab3b92006-08-12 18:25:42 +0000639 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000640 // arguments to the function scope.
641 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000642 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000643 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000644 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000645 bool ErrorEmitted = false;
646
Chris Lattneracd58a32006-08-06 17:24:14 +0000647 if (Tok.getKind() == tok::r_paren) {
648 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000649 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000650 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000651 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000652 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000653 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000654 // Identifier list. Note that '(' identifier-list ')' is only allowed for
655 // normal declarators, not for abstract-declarators.
656 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
657
658 // If there was no identifier specified, either we are in an
659 // abstract-declarator, or we are in a parameter declarator which was found
660 // to be abstract. In abstract-declarators, identifier lists are not valid,
661 // diagnose this.
662 if (!D.getIdentifier())
663 Diag(Tok, diag::ext_ident_list_in_param);
664
Chris Lattner9fab3b92006-08-12 18:25:42 +0000665 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000666 ConsumeToken();
667 while (Tok.getKind() == tok::comma) {
668 // Eat the comma.
669 ConsumeToken();
670
Chris Lattner0be454e2006-08-12 19:30:51 +0000671 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000672 ErrorEmitted = true;
673 break;
674 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000675 }
676
Chris Lattneracd58a32006-08-06 17:24:14 +0000677 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000678 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000679 HasPrototype = false;
680 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000681 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000682 bool ReadArg = false;
683 // Finally, a normal, non-empty parameter type list.
684 while (1) {
685 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000686 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000687
688 // Check to see if this is "void(...)" which is not allowed.
689 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000690 // Otherwise, parse parameter type list. If it starts with an
691 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000692 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000693 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000694 }
695
696 // Consume the ellipsis.
697 ConsumeToken();
698 break;
699 }
700
701 ReadArg = true;
702
703 // Parse the declaration-specifiers.
704 DeclSpec DS;
705 ParseDeclarationSpecifiers(DS);
706
707 // Parse the declarator. This is "PrototypeContext", because we must
708 // accept either 'declarator' or 'abstract-declarator' here.
709 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
710 ParseDeclarator(DeclaratorInfo);
711
712 // TODO: do something with the declarator, if it is valid.
713
714 // If the next token is a comma, consume it and keep reading arguments.
715 if (Tok.getKind() != tok::comma) break;
716
717 // Consume the comma.
718 ConsumeToken();
719 }
720
721 HasPrototype = true;
722 }
723
Chris Lattner9fab3b92006-08-12 18:25:42 +0000724 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000725
Chris Lattner9fab3b92006-08-12 18:25:42 +0000726 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000727
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000728 // Remember that we parsed a function type, and remember the attributes.
729 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000730 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000731
Chris Lattner14776b92006-08-06 22:27:40 +0000732
733 // If we have the closing ')', eat it and we're done.
734 if (Tok.getKind() == tok::r_paren) {
735 ConsumeParen();
736 } else {
737 // If an error happened earlier parsing something else in the proto, don't
738 // issue another error.
739 if (!ErrorEmitted)
740 Diag(Tok, diag::err_expected_rparen);
741 SkipUntil(tok::r_paren);
742 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000743}
Chris Lattneracd58a32006-08-06 17:24:14 +0000744
Chris Lattnere8074e62006-08-06 18:30:15 +0000745
746/// [C90] direct-declarator '[' constant-expression[opt] ']'
747/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
748/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
749/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
750/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
751void Parser::ParseBracketDeclarator(Declarator &D) {
752 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +0000753 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +0000754
755 // If valid, this location is the position where we read the 'static' keyword.
756 SourceLocation StaticLoc;
757 if (Tok.getKind() == tok::kw_static) {
758 StaticLoc = Tok.getLocation();
759 ConsumeToken();
760 }
761
762 // If there is a type-qualifier-list, read it now.
763 DeclSpec DS;
764 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +0000765
766 // If we haven't already read 'static', check to see if there is one after the
767 // type-qualifier-list.
768 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
769 StaticLoc = Tok.getLocation();
770 ConsumeToken();
771 }
772
773 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +0000774 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +0000775 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +0000776 if (Tok.getKind() == tok::star) {
777 // Remember the '*' token, in case we have to un-get it.
778 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +0000779 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +0000780
781 // Check that the ']' token is present to avoid incorrectly parsing
782 // expressions starting with '*' as [*].
783 if (Tok.getKind() == tok::r_square) {
784 if (StaticLoc.isValid())
785 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
786 StaticLoc = SourceLocation(); // Drop the static.
787 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +0000788 } else {
789 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +0000790 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +0000791 // need to reparse it. This handles cases like 'X[*p + 4]'
792 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +0000793 }
Chris Lattner9fab3b92006-08-12 18:25:42 +0000794 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000795 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +0000796 NumElements = ParseAssignmentExpression();
797 }
798
799 // If there was an error parsing the assignment-expression, recover.
800 if (NumElements.isInvalid) {
801 // If the expression was invalid, skip it.
802 SkipUntil(tok::r_square);
803 return;
Chris Lattnere8074e62006-08-06 18:30:15 +0000804 }
805
Chris Lattner9fab3b92006-08-12 18:25:42 +0000806 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
807
Chris Lattnere8074e62006-08-06 18:30:15 +0000808 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
809 // it was not a constant expression.
810 if (!getLang().C99) {
811 // TODO: check C90 array constant exprness.
Chris Lattner62591722006-08-12 18:40:58 +0000812 if (isStar || StaticLoc.isValid() || 0/*FIXME: NumElts is constantexpr*/)
Chris Lattner8a39edc2006-08-06 18:33:32 +0000813 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +0000814 }
Chris Lattner6c7416c2006-08-07 00:19:33 +0000815
816 // Remember that we parsed a pointer type, and remember the type-quals.
817 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
818 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +0000819 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +0000820}
821