blob: 1584154cdcb3e847e229e0cfe89ab9f9aa5ac60f [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;
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);
34}
35
36
Chris Lattner53361ac2006-08-10 05:19:57 +000037/// ParseDeclaration - Parse a full 'declaration', which consists of
38/// declaration-specifiers, some number of declarators, and a semicolon.
39/// 'Context' should be a Declarator::TheContext value.
40void Parser::ParseDeclaration(unsigned Context) {
41 // Parse the common declaration-specifiers piece.
42 DeclSpec DS;
43 ParseDeclarationSpecifiers(DS);
44
Chris Lattner0e894622006-08-13 19:58:17 +000045 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
46 // declaration-specifiers init-declarator-list[opt] ';'
47 if (Tok.getKind() == tok::semi) {
48 // TODO: emit error on 'int;' or 'const enum foo;'.
49 // if (!DS.isMissingDeclaratorOk()) Diag(...);
50
51 ConsumeToken();
52 return;
53 }
54
Chris Lattner53361ac2006-08-10 05:19:57 +000055 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
56 ParseDeclarator(DeclaratorInfo);
57
58 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
59}
60
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000061/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
62/// parsing 'declaration-specifiers declarator'. This method is split out this
63/// way to handle the ambiguity between top-level function-definitions and
64/// declarations.
65///
66/// declaration: [C99 6.7]
67/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
68/// [!C99] init-declarator-list ';' [TODO]
69/// [OMP] threadprivate-directive [TODO]
70///
71/// init-declarator-list: [C99 6.7]
72/// init-declarator
73/// init-declarator-list ',' init-declarator
74/// init-declarator: [C99 6.7]
75/// declarator
76/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +000077/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
78/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000079///
Chris Lattner53361ac2006-08-10 05:19:57 +000080void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
81 // At this point, we know that it is not a function definition. Parse the
82 // rest of the init-declarator-list.
83 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +000084 // If a simple-asm-expr is present, parse it.
85 if (Tok.getKind() == tok::kw_asm)
86 ParseSimpleAsm();
87
88 // TODO: parse attributes.
89
Chris Lattner53361ac2006-08-10 05:19:57 +000090 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000091 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +000092 if (Tok.getKind() == tok::equal) {
93 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000094 Init = ParseInitializer();
95 if (!Init.isInvalid) {
96 SkipUntil(tok::semi);
97 return;
98 }
Chris Lattner53361ac2006-08-10 05:19:57 +000099 }
100
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000101 // Inform the current actions module that we just parsed a declarator.
102 Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000103
104 // If we don't have a comma, it is either the end of the list (a ';') or an
105 // error, bail out.
106 if (Tok.getKind() != tok::comma)
107 break;
108
109 // Consume the comma.
110 ConsumeToken();
111
112 // Parse the next declarator.
113 D.clear();
114 ParseDeclarator(D);
115 }
116
117 if (Tok.getKind() == tok::semi) {
118 ConsumeToken();
119 } else {
120 Diag(Tok, diag::err_parse_error);
121 // Skip to end of block or statement
122 SkipUntil(tok::r_brace, true);
123 if (Tok.getKind() == tok::semi)
124 ConsumeToken();
125 }
126}
127
Chris Lattner1890ac82006-08-13 01:16:23 +0000128/// ParseSpecifierQualifierList
129/// specifier-qualifier-list:
130/// type-specifier specifier-qualifier-list[opt]
131/// type-qualifier specifier-qualifier-list[opt]
132///
133void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
134 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
135 /// parse declaration-specifiers and complain about extra stuff.
136 SourceLocation Loc = Tok.getLocation();
137 ParseDeclarationSpecifiers(DS);
138
139 // Validate declspec for type-name.
140 unsigned Specs = DS.getParsedSpecifiers();
141 if (Specs == DeclSpec::PQ_None)
142 Diag(Tok, diag::err_typename_requires_specqual);
143
144 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
145 Diag(Loc, diag::err_typename_invalid_storageclass);
146 // Remove storage class.
147 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
148 DS.SCS_thread_specified = false;
149 }
150 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
151 Diag(Loc, diag::err_typename_invalid_functionspec);
152 DS.FS_inline_specified = false;
153 }
154}
Chris Lattner53361ac2006-08-10 05:19:57 +0000155
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000156/// ParseDeclarationSpecifiers
157/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000158/// storage-class-specifier declaration-specifiers[opt]
159/// type-specifier declaration-specifiers[opt]
160/// type-qualifier declaration-specifiers[opt]
161/// [C99] function-specifier declaration-specifiers[opt]
162/// [GNU] attributes declaration-specifiers[opt] [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000163///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000164/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000165/// 'typedef'
166/// 'extern'
167/// 'static'
168/// 'auto'
169/// 'register'
170/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000171/// type-specifier: [C99 6.7.2]
172/// 'void'
173/// 'char'
174/// 'short'
175/// 'int'
176/// 'long'
177/// 'float'
178/// 'double'
179/// 'signed'
180/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000181/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000182/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000183/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000184/// [C99] '_Bool'
185/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000186/// [C99] '_Imaginary' // Removed in TC2?
187/// [GNU] '_Decimal32'
188/// [GNU] '_Decimal64'
189/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000190/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000191/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000192/// [OBJC] typedef-name objc-protocol-refs [TODO]
193/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000194/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000195/// 'const'
196/// 'volatile'
197/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000198/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000199/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000200///
201void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
202 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000203 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000204 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000205 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000206 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000207 // typedef-name
208 case tok::identifier:
209 // This identifier can only be a typedef name if we haven't already seen
210 // a typename. This avoids the virtual method call on things like
211 // 'int foo'.
212 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
213 Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
214 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
215 break;
216 }
217 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000218 default:
219 // If this is not a declaration specifier token, we're done reading decl
220 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000221 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000222 return;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000223
224 // storage-class-specifier
225 case tok::kw_typedef:
226 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
227 break;
228 case tok::kw_extern:
229 if (DS.SCS_thread_specified)
230 Diag(Tok, diag::ext_thread_before, "extern");
231 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
232 break;
233 case tok::kw_static:
234 if (DS.SCS_thread_specified)
235 Diag(Tok, diag::ext_thread_before, "static");
236 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
237 break;
238 case tok::kw_auto:
239 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
240 break;
241 case tok::kw_register:
242 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
243 break;
244 case tok::kw___thread:
245 if (DS.SCS_thread_specified)
246 isInvalid = 2, PrevSpec = "__thread";
247 else
248 DS.SCS_thread_specified = true;
249 break;
250
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000251 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000252 case tok::kw_short:
253 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
254 break;
255 case tok::kw_long:
256 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
257 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
258 } else {
259 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
260 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
261 }
262 break;
263 case tok::kw_signed:
264 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
265 break;
266 case tok::kw_unsigned:
267 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
268 break;
269 case tok::kw__Complex:
270 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
271 break;
272 case tok::kw__Imaginary:
273 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
274 break;
275 case tok::kw_void:
276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
277 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000278 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000279 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
280 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000281 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000282 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
283 break;
284 case tok::kw_float:
285 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
286 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000287 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
289 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000290 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
292 break;
293 case tok::kw__Decimal32:
294 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
295 break;
296 case tok::kw__Decimal64:
297 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
298 break;
299 case tok::kw__Decimal128:
300 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000301 break;
302
Chris Lattner1890ac82006-08-13 01:16:23 +0000303 case tok::kw_struct:
304 case tok::kw_union:
305 ParseStructUnionSpecifier(DS);
306 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000307 case tok::kw_enum:
308 ParseEnumSpecifier(DS);
309 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000310
311 // type-qualifier
312 case tok::kw_const:
313 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
314 break;
315 case tok::kw_volatile:
316 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
317 break;
318 case tok::kw_restrict:
319 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
320 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000321
322 // function-specifier
323 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000324 // 'inline inline' is ok.
325 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000326 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000327 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000328 // If the specifier combination wasn't legal, issue a diagnostic.
329 if (isInvalid) {
330 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000331 if (isInvalid == 1) // Error.
332 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
333 else // extwarn.
334 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000335 }
336 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000337 }
338}
339
Chris Lattner1890ac82006-08-13 01:16:23 +0000340
341/// ParseStructUnionSpecifier
342/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000343/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000344/// struct-or-union identifier
345/// struct-or-union:
346/// 'struct'
347/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000348/// struct-contents:
349/// struct-declaration-list
350/// [EXT] empty
351/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000352/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000353/// struct-declaration
354/// struct-declaration-list struct-declaration
355/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
356/// struct-declaration:
357/// specifier-qualifier-list struct-declarator-list ';'
358/// [GNU] __extension__ struct-declaration [TODO]
359/// [GNU] specifier-qualifier-list ';' [TODO]
360/// struct-declarator-list:
361/// struct-declarator
362/// struct-declarator-list ',' struct-declarator
363/// struct-declarator:
364/// declarator
365/// declarator[opt] ':' constant-expression
Chris Lattner1890ac82006-08-13 01:16:23 +0000366///
367void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
368 assert((Tok.getKind() == tok::kw_struct ||
369 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000370 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000371 bool isUnion = Tok.getKind() == tok::kw_union;
372 ConsumeToken();
373
374 // Must have either 'struct name' or 'struct {...}'.
375 if (Tok.getKind() != tok::identifier &&
376 Tok.getKind() != tok::l_brace) {
377 Diag(Tok, diag::err_expected_ident_lbrace);
378 return;
379 }
380
381 if (Tok.getKind() == tok::identifier)
382 ConsumeToken();
383
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000384 if (Tok.getKind() == tok::l_brace) {
385 SourceLocation LBraceLoc = Tok.getLocation();
386 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000387
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000388 if (Tok.getKind() == tok::r_brace)
389 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000390
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000391 while (Tok.getKind() != tok::r_brace &&
392 Tok.getKind() != tok::eof) {
393 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000394
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000395 // Parse the common specifier-qualifiers-list piece.
396 DeclSpec DS;
397 SourceLocation SpecQualLoc = Tok.getLocation();
398 ParseSpecifierQualifierList(DS);
399 // TODO: Does specifier-qualifier list correctly check that *something* is
400 // specified?
401
402 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000403
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000404 // If there are no declarators, issue a warning.
405 if (Tok.getKind() == tok::semi) {
406 Diag(SpecQualLoc, diag::w_no_declarators);
407 } else {
408 // Read struct-declarators until we find the semicolon.
409 while (1) {
410 /// struct-declarator: declarator
411 /// struct-declarator: declarator[opt] ':' constant-expression
412 if (Tok.getKind() != tok::colon)
413 ParseDeclarator(DeclaratorInfo);
414
415 if (Tok.getKind() == tok::colon) {
416 ConsumeToken();
417 ExprResult Res = ParseConstantExpression();
418 if (Res.isInvalid) {
419 SkipUntil(tok::semi, true, true);
420 } else {
421 // Process it.
422 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000423 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000424
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000425 // TODO: install declarator.
426
427 // If we don't have a comma, it is either the end of the list (a ';')
428 // or an error, bail out.
429 if (Tok.getKind() != tok::comma)
430 break;
431
432 // Consume the comma.
433 ConsumeToken();
434
435 // Parse the next declarator.
436 DeclaratorInfo.clear();
437 }
438 }
439
440 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000441 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000442 } else {
443 Diag(Tok, diag::err_expected_semi_decl_list);
444 // Skip to end of block or statement
445 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000446 }
447 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000448
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000449 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace);
450 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000451
452 const char *PrevSpec = 0;
453 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
454 PrevSpec))
455 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000456}
457
458
Chris Lattner3b561a32006-08-13 00:12:11 +0000459/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000460/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000461/// 'enum' identifier[opt] '{' enumerator-list '}'
462/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000463/// [GNU] 'enum' identifier[opt] '{' enumerator-list '}' attributes [TODO]
464/// [GNU] 'enum' identifier[opt] '{' enumerator-list ',' '}' attributes [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000465/// 'enum' identifier
466/// enumerator-list:
467/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000468/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000469/// enumerator:
470/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000471/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000472/// enumeration-constant:
473/// identifier
474///
475void Parser::ParseEnumSpecifier(DeclSpec &DS) {
476 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000477 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000478 ConsumeToken();
479
480 // Must have either 'enum name' or 'enum {...}'.
481 if (Tok.getKind() != tok::identifier &&
482 Tok.getKind() != tok::l_brace) {
483 Diag(Tok, diag::err_expected_ident_lbrace);
484 return;
485 }
486
487 if (Tok.getKind() == tok::identifier)
488 ConsumeToken();
489
Chris Lattner0fb8b362006-08-14 01:30:12 +0000490 if (Tok.getKind() == tok::l_brace) {
491 SourceLocation LBraceLoc = Tok.getLocation();
492 ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000493
Chris Lattner0fb8b362006-08-14 01:30:12 +0000494 if (Tok.getKind() == tok::r_brace)
495 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
496
497 // Parse the enumerator-list.
498 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000499 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000500
501 if (Tok.getKind() == tok::equal) {
502 ConsumeToken();
503 ExprResult Res = ParseConstantExpression();
504 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
505 }
506
507 if (Tok.getKind() != tok::comma)
508 break;
509 SourceLocation CommaLoc = Tok.getLocation();
510 ConsumeToken();
511
512 if (Tok.getKind() != tok::identifier && !getLang().C99)
513 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000514 }
515
Chris Lattner0fb8b362006-08-14 01:30:12 +0000516 // Eat the }.
517 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
518 diag::err_expected_rbrace);
Chris Lattner3b561a32006-08-13 00:12:11 +0000519 }
520 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000521
522
523 const char *PrevSpec = 0;
524 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
525 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000526}
527
528
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000529/// isTypeSpecifierQualifier - Return true if the current token could be the
530/// start of a specifier-qualifier-list.
531bool Parser::isTypeSpecifierQualifier() const {
532 switch (Tok.getKind()) {
533 default: return false;
534 // type-specifiers
535 case tok::kw_short:
536 case tok::kw_long:
537 case tok::kw_signed:
538 case tok::kw_unsigned:
539 case tok::kw__Complex:
540 case tok::kw__Imaginary:
541 case tok::kw_void:
542 case tok::kw_char:
543 case tok::kw_int:
544 case tok::kw_float:
545 case tok::kw_double:
546 case tok::kw__Bool:
547 case tok::kw__Decimal32:
548 case tok::kw__Decimal64:
549 case tok::kw__Decimal128:
550
551 // struct-or-union-specifier
552 case tok::kw_struct:
553 case tok::kw_union:
554 // enum-specifier
555 case tok::kw_enum:
556
557 // type-qualifier
558 case tok::kw_const:
559 case tok::kw_volatile:
560 case tok::kw_restrict:
561 return true;
562
563 // typedef-name
564 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000565 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000566
567 // TODO: Attributes.
568 }
569}
570
Chris Lattneracd58a32006-08-06 17:24:14 +0000571/// isDeclarationSpecifier() - Return true if the current token is part of a
572/// declaration specifier.
573bool Parser::isDeclarationSpecifier() const {
574 switch (Tok.getKind()) {
575 default: return false;
576 // storage-class-specifier
577 case tok::kw_typedef:
578 case tok::kw_extern:
579 case tok::kw_static:
580 case tok::kw_auto:
581 case tok::kw_register:
582 case tok::kw___thread:
583
584 // type-specifiers
585 case tok::kw_short:
586 case tok::kw_long:
587 case tok::kw_signed:
588 case tok::kw_unsigned:
589 case tok::kw__Complex:
590 case tok::kw__Imaginary:
591 case tok::kw_void:
592 case tok::kw_char:
593 case tok::kw_int:
594 case tok::kw_float:
595 case tok::kw_double:
596 case tok::kw__Bool:
597 case tok::kw__Decimal32:
598 case tok::kw__Decimal64:
599 case tok::kw__Decimal128:
600
601 // struct-or-union-specifier
602 case tok::kw_struct:
603 case tok::kw_union:
604 // enum-specifier
605 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000606
Chris Lattneracd58a32006-08-06 17:24:14 +0000607 // type-qualifier
608 case tok::kw_const:
609 case tok::kw_volatile:
610 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000611
Chris Lattneracd58a32006-08-06 17:24:14 +0000612 // function-specifier
613 case tok::kw_inline:
614 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000615
Chris Lattneracd58a32006-08-06 17:24:14 +0000616 // typedef-name
617 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000618 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000619 // TODO: Attributes.
620 }
621}
622
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000623
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000624/// ParseTypeQualifierListOpt
625/// type-qualifier-list: [C99 6.7.5]
626/// type-qualifier
627/// [GNU] attributes [TODO]
628/// type-qualifier-list type-qualifier
629/// [GNU] type-qualifier-list attributes [TODO]
630///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000631void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
632 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000633 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000634 int isInvalid = false;
635 const char *PrevSpec = 0;
636
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000637 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000638 default:
639 // If this is not a declaration specifier token, we're done reading decl
640 // specifiers. First verify that DeclSpec's are consistent.
641 DS.Finish(StartLoc, Diags, getLang());
642 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000643 // TODO: attributes.
644 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000645 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
646 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000647 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000648 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
649 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000650 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000651 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000652 break;
653 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000654
655 // If the specifier combination wasn't legal, issue a diagnostic.
656 if (isInvalid) {
657 assert(PrevSpec && "Method did not return previous specifier!");
658 if (isInvalid == 1) // Error.
659 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
660 else // extwarn.
661 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
662 }
663 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000664 }
665}
666
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000667
668/// ParseDeclarator - Parse and verify a newly-initialized declarator.
669///
670void Parser::ParseDeclarator(Declarator &D) {
671 /// This implements the 'declarator' production in the C grammar, then checks
672 /// for well-formedness and issues diagnostics.
673 ParseDeclaratorInternal(D);
674
Chris Lattner9fab3b92006-08-12 18:25:42 +0000675 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000676
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000677}
678
679/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000680/// declarator: [C99 6.7.5]
681/// pointer[opt] direct-declarator
682///
683/// pointer: [C99 6.7.5]
684/// '*' type-qualifier-list[opt]
685/// '*' type-qualifier-list[opt] pointer
686///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000687void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000688 if (Tok.getKind() != tok::star)
689 return ParseDirectDeclarator(D);
690
691 // Otherwise, '*' -> pointer.
692 SourceLocation Loc = Tok.getLocation();
693 ConsumeToken(); // Eat the *.
694 DeclSpec DS;
695 ParseTypeQualifierListOpt(DS);
696
697 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000698 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000699
700 // Remember that we parsed a pointer type, and remember the type-quals.
701 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
702}
703
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000704
705/// ParseDirectDeclarator
706/// direct-declarator: [C99 6.7.5]
707/// identifier
708/// '(' declarator ')'
709/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000710/// [C90] direct-declarator '[' constant-expression[opt] ']'
711/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
712/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
713/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
714/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000715/// direct-declarator '(' parameter-type-list ')'
716/// direct-declarator '(' identifier-list[opt] ')'
717/// [GNU] direct-declarator '(' parameter-forward-declarations
718/// parameter-type-list[opt] ')'
719///
Chris Lattneracd58a32006-08-06 17:24:14 +0000720void Parser::ParseDirectDeclarator(Declarator &D) {
721 // Parse the first direct-declarator seen.
722 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
723 assert(Tok.getIdentifierInfo() && "Not an identifier?");
724 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
725 ConsumeToken();
726 } else if (Tok.getKind() == tok::l_paren) {
727 // direct-declarator: '(' declarator ')'
728 // direct-declarator: '(' attributes declarator ')' [TODO]
729 // Example: 'char (*X)' or 'int (*XX)(void)'
730 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000731 } else if (D.mayOmitIdentifier()) {
732 // This could be something simple like "int" (in which case the declarator
733 // portion is empty), if an abstract-declarator is allowed.
734 D.SetIdentifier(0, Tok.getLocation());
735 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000736 // Expected identifier or '('.
737 Diag(Tok, diag::err_expected_ident_lparen);
738 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000739 }
740
741 assert(D.isPastIdentifier() &&
742 "Haven't past the location of the identifier yet?");
743
744 while (1) {
745 if (Tok.getKind() == tok::l_paren) {
746 ParseParenDeclarator(D);
747 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000748 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000749 } else {
750 break;
751 }
752 }
753}
754
755/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
756/// either be before the identifier (in which case these are just grouping
757/// parens for precedence) or it may be after the identifier, in which case
758/// these are function arguments.
759///
760/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000761/// parameter-type-list: [C99 6.7.5]
762/// parameter-list
763/// parameter-list ',' '...'
764///
765/// parameter-list: [C99 6.7.5]
766/// parameter-declaration
767/// parameter-list ',' parameter-declaration
768///
769/// parameter-declaration: [C99 6.7.5]
770/// declaration-specifiers declarator
Chris Lattneracd58a32006-08-06 17:24:14 +0000771/// [GNU] declaration-specifiers declarator attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000772/// declaration-specifiers abstract-declarator[opt]
Chris Lattneracd58a32006-08-06 17:24:14 +0000773/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000774///
775/// identifier-list: [C99 6.7.5]
776/// identifier
777/// identifier-list ',' identifier
778///
Chris Lattneracd58a32006-08-06 17:24:14 +0000779void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000780 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000781 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000782
Chris Lattneracd58a32006-08-06 17:24:14 +0000783 // If we haven't past the identifier yet (or where the identifier would be
784 // stored, if this is an abstract declarator), then this is probably just
785 // grouping parens.
786 if (!D.isPastIdentifier()) {
787 // Okay, this is probably a grouping paren. However, if this could be an
788 // abstract-declarator, then this could also be the start of function
789 // arguments (consider 'void()').
790 bool isGrouping;
791
792 if (!D.mayOmitIdentifier()) {
793 // If this can't be an abstract-declarator, this *must* be a grouping
794 // paren, because we haven't seen the identifier yet.
795 isGrouping = true;
796 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
797 isDeclarationSpecifier()) { // 'int(int)' is a function.
798
799 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000800 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000801 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000802 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000803 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000804
805 // If this is a grouping paren, handle:
806 // direct-declarator: '(' declarator ')'
807 // direct-declarator: '(' attributes declarator ')' [TODO]
808 if (isGrouping) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000809 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000810 // Match the ')'.
811 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
812 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000813 return;
814 }
815
816 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000817 // argument list. Recognize that this declarator will never have an
818 // identifier (and remember where it would have been), then fall through to
819 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000820 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000821 }
822
Chris Lattneracd58a32006-08-06 17:24:14 +0000823 // Okay, this is the parameter list of a function definition, or it is an
824 // identifier list of a K&R-style function.
825
Chris Lattner9fab3b92006-08-12 18:25:42 +0000826 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000827 // arguments to the function scope.
828 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000829 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000830 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000831 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000832 bool ErrorEmitted = false;
833
Chris Lattneracd58a32006-08-06 17:24:14 +0000834 if (Tok.getKind() == tok::r_paren) {
835 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000836 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000837 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000838 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000839 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner8a3e9182006-08-14 15:44:00 +0000840 !Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000841 // Identifier list. Note that '(' identifier-list ')' is only allowed for
842 // normal declarators, not for abstract-declarators.
843 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
844
845 // If there was no identifier specified, either we are in an
846 // abstract-declarator, or we are in a parameter declarator which was found
847 // to be abstract. In abstract-declarators, identifier lists are not valid,
848 // diagnose this.
849 if (!D.getIdentifier())
850 Diag(Tok, diag::ext_ident_list_in_param);
851
Chris Lattner9fab3b92006-08-12 18:25:42 +0000852 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000853 ConsumeToken();
854 while (Tok.getKind() == tok::comma) {
855 // Eat the comma.
856 ConsumeToken();
857
Chris Lattner0be454e2006-08-12 19:30:51 +0000858 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000859 ErrorEmitted = true;
860 break;
861 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000862 }
863
Chris Lattneracd58a32006-08-06 17:24:14 +0000864 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000865 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000866 HasPrototype = false;
867 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000868 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000869 bool ReadArg = false;
870 // Finally, a normal, non-empty parameter type list.
871 while (1) {
872 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000873 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000874
875 // Check to see if this is "void(...)" which is not allowed.
876 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000877 // Otherwise, parse parameter type list. If it starts with an
878 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000879 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000880 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000881 }
882
883 // Consume the ellipsis.
884 ConsumeToken();
885 break;
886 }
887
888 ReadArg = true;
889
890 // Parse the declaration-specifiers.
891 DeclSpec DS;
892 ParseDeclarationSpecifiers(DS);
893
894 // Parse the declarator. This is "PrototypeContext", because we must
895 // accept either 'declarator' or 'abstract-declarator' here.
896 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
897 ParseDeclarator(DeclaratorInfo);
898
899 // TODO: do something with the declarator, if it is valid.
900
901 // If the next token is a comma, consume it and keep reading arguments.
902 if (Tok.getKind() != tok::comma) break;
903
904 // Consume the comma.
905 ConsumeToken();
906 }
907
908 HasPrototype = true;
909 }
910
Chris Lattner9fab3b92006-08-12 18:25:42 +0000911 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000912
Chris Lattner9fab3b92006-08-12 18:25:42 +0000913 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000914
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000915 // Remember that we parsed a function type, and remember the attributes.
916 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000917 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000918
Chris Lattner14776b92006-08-06 22:27:40 +0000919
920 // If we have the closing ')', eat it and we're done.
921 if (Tok.getKind() == tok::r_paren) {
922 ConsumeParen();
923 } else {
924 // If an error happened earlier parsing something else in the proto, don't
925 // issue another error.
926 if (!ErrorEmitted)
927 Diag(Tok, diag::err_expected_rparen);
928 SkipUntil(tok::r_paren);
929 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000930}
Chris Lattneracd58a32006-08-06 17:24:14 +0000931
Chris Lattnere8074e62006-08-06 18:30:15 +0000932
933/// [C90] direct-declarator '[' constant-expression[opt] ']'
934/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
935/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
936/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
937/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
938void Parser::ParseBracketDeclarator(Declarator &D) {
939 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +0000940 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +0000941
942 // If valid, this location is the position where we read the 'static' keyword.
943 SourceLocation StaticLoc;
944 if (Tok.getKind() == tok::kw_static) {
945 StaticLoc = Tok.getLocation();
946 ConsumeToken();
947 }
948
949 // If there is a type-qualifier-list, read it now.
950 DeclSpec DS;
951 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +0000952
953 // If we haven't already read 'static', check to see if there is one after the
954 // type-qualifier-list.
955 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
956 StaticLoc = Tok.getLocation();
957 ConsumeToken();
958 }
959
960 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +0000961 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +0000962 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +0000963 if (Tok.getKind() == tok::star) {
964 // Remember the '*' token, in case we have to un-get it.
965 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +0000966 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +0000967
968 // Check that the ']' token is present to avoid incorrectly parsing
969 // expressions starting with '*' as [*].
970 if (Tok.getKind() == tok::r_square) {
971 if (StaticLoc.isValid())
972 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
973 StaticLoc = SourceLocation(); // Drop the static.
974 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +0000975 } else {
976 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +0000977 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +0000978 // need to reparse it. This handles cases like 'X[*p + 4]'
979 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +0000980 }
Chris Lattner9fab3b92006-08-12 18:25:42 +0000981 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000982 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +0000983 NumElements = ParseAssignmentExpression();
984 }
985
986 // If there was an error parsing the assignment-expression, recover.
987 if (NumElements.isInvalid) {
988 // If the expression was invalid, skip it.
989 SkipUntil(tok::r_square);
990 return;
Chris Lattnere8074e62006-08-06 18:30:15 +0000991 }
992
Chris Lattner9fab3b92006-08-12 18:25:42 +0000993 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
994
Chris Lattnere8074e62006-08-06 18:30:15 +0000995 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
996 // it was not a constant expression.
997 if (!getLang().C99) {
998 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +0000999 if (isStar || StaticLoc.isValid() ||
1000 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001001 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001002 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001003
1004 // Remember that we parsed a pointer type, and remember the type-quals.
1005 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1006 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001007 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001008}
1009