blob: a11f02b98b631fa19e2b17f4d1c5881438d453a7 [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
77///
Chris Lattner53361ac2006-08-10 05:19:57 +000078void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
79 // At this point, we know that it is not a function definition. Parse the
80 // rest of the init-declarator-list.
81 while (1) {
82 // must be: decl-spec[opt] declarator init-declarator-list
83 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000084 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +000085 if (Tok.getKind() == tok::equal) {
86 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000087 Init = ParseInitializer();
88 if (!Init.isInvalid) {
89 SkipUntil(tok::semi);
90 return;
91 }
Chris Lattner53361ac2006-08-10 05:19:57 +000092 }
93
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000094 // Inform the current actions module that we just parsed a declarator.
95 Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +000096
97 // If we don't have a comma, it is either the end of the list (a ';') or an
98 // error, bail out.
99 if (Tok.getKind() != tok::comma)
100 break;
101
102 // Consume the comma.
103 ConsumeToken();
104
105 // Parse the next declarator.
106 D.clear();
107 ParseDeclarator(D);
108 }
109
110 if (Tok.getKind() == tok::semi) {
111 ConsumeToken();
112 } else {
113 Diag(Tok, diag::err_parse_error);
114 // Skip to end of block or statement
115 SkipUntil(tok::r_brace, true);
116 if (Tok.getKind() == tok::semi)
117 ConsumeToken();
118 }
119}
120
Chris Lattner1890ac82006-08-13 01:16:23 +0000121/// ParseSpecifierQualifierList
122/// specifier-qualifier-list:
123/// type-specifier specifier-qualifier-list[opt]
124/// type-qualifier specifier-qualifier-list[opt]
125///
126void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
127 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
128 /// parse declaration-specifiers and complain about extra stuff.
129 SourceLocation Loc = Tok.getLocation();
130 ParseDeclarationSpecifiers(DS);
131
132 // Validate declspec for type-name.
133 unsigned Specs = DS.getParsedSpecifiers();
134 if (Specs == DeclSpec::PQ_None)
135 Diag(Tok, diag::err_typename_requires_specqual);
136
137 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
138 Diag(Loc, diag::err_typename_invalid_storageclass);
139 // Remove storage class.
140 DS.StorageClassSpec = DeclSpec::SCS_unspecified;
141 DS.SCS_thread_specified = false;
142 }
143 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
144 Diag(Loc, diag::err_typename_invalid_functionspec);
145 DS.FS_inline_specified = false;
146 }
147}
Chris Lattner53361ac2006-08-10 05:19:57 +0000148
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000149/// ParseDeclarationSpecifiers
150/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000151/// storage-class-specifier declaration-specifiers[opt]
152/// type-specifier declaration-specifiers[opt]
153/// type-qualifier declaration-specifiers[opt]
154/// [C99] function-specifier declaration-specifiers[opt]
155/// [GNU] attributes declaration-specifiers[opt] [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000156///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000157/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000158/// 'typedef'
159/// 'extern'
160/// 'static'
161/// 'auto'
162/// 'register'
163/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000164/// type-specifier: [C99 6.7.2]
165/// 'void'
166/// 'char'
167/// 'short'
168/// 'int'
169/// 'long'
170/// 'float'
171/// 'double'
172/// 'signed'
173/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000174/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000175/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000176/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000177/// [C99] '_Bool'
178/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000179/// [C99] '_Imaginary' // Removed in TC2?
180/// [GNU] '_Decimal32'
181/// [GNU] '_Decimal64'
182/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000183/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000184/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000185/// [OBJC] typedef-name objc-protocol-refs [TODO]
186/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000187/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000188/// 'const'
189/// 'volatile'
190/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000191/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000192/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000193///
194void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
195 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000196 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000197 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000198 const char *PrevSpec = 0;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000199 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000200 // typedef-name
201 case tok::identifier:
202 // This identifier can only be a typedef name if we haven't already seen
203 // a typename. This avoids the virtual method call on things like
204 // 'int foo'.
205 if (DS.TypeSpecType == DeclSpec::TST_unspecified &&
206 Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) {
207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec);
208 break;
209 }
210 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000211 default:
212 // If this is not a declaration specifier token, we're done reading decl
213 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner839713c2006-08-04 06:15:52 +0000214 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000215 return;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000216
217 // storage-class-specifier
218 case tok::kw_typedef:
219 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
220 break;
221 case tok::kw_extern:
222 if (DS.SCS_thread_specified)
223 Diag(Tok, diag::ext_thread_before, "extern");
224 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
225 break;
226 case tok::kw_static:
227 if (DS.SCS_thread_specified)
228 Diag(Tok, diag::ext_thread_before, "static");
229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
230 break;
231 case tok::kw_auto:
232 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
233 break;
234 case tok::kw_register:
235 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
236 break;
237 case tok::kw___thread:
238 if (DS.SCS_thread_specified)
239 isInvalid = 2, PrevSpec = "__thread";
240 else
241 DS.SCS_thread_specified = true;
242 break;
243
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000244 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000245 case tok::kw_short:
246 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
247 break;
248 case tok::kw_long:
249 if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
250 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
251 } else {
252 DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
253 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
254 }
255 break;
256 case tok::kw_signed:
257 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
258 break;
259 case tok::kw_unsigned:
260 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
261 break;
262 case tok::kw__Complex:
263 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
264 break;
265 case tok::kw__Imaginary:
266 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
267 break;
268 case tok::kw_void:
269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
270 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000271 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
273 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000274 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
276 break;
277 case tok::kw_float:
278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
279 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000280 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
282 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000283 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
285 break;
286 case tok::kw__Decimal32:
287 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
288 break;
289 case tok::kw__Decimal64:
290 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
291 break;
292 case tok::kw__Decimal128:
293 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000294 break;
295
Chris Lattner1890ac82006-08-13 01:16:23 +0000296 case tok::kw_struct:
297 case tok::kw_union:
298 ParseStructUnionSpecifier(DS);
299 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000300 case tok::kw_enum:
301 ParseEnumSpecifier(DS);
302 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000303
304 // type-qualifier
305 case tok::kw_const:
306 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
307 break;
308 case tok::kw_volatile:
309 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
310 break;
311 case tok::kw_restrict:
312 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
313 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000314
315 // function-specifier
316 case tok::kw_inline:
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000317 // 'inline inline' is ok.
318 DS.FS_inline_specified = true;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000319 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000320 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000321 // If the specifier combination wasn't legal, issue a diagnostic.
322 if (isInvalid) {
323 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000324 if (isInvalid == 1) // Error.
325 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
326 else // extwarn.
327 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000328 }
329 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000330 }
331}
332
Chris Lattner1890ac82006-08-13 01:16:23 +0000333
334/// ParseStructUnionSpecifier
335/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000336/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000337/// struct-or-union identifier
338/// struct-or-union:
339/// 'struct'
340/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000341/// struct-contents:
342/// struct-declaration-list
343/// [EXT] empty
344/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000345/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000346/// struct-declaration
347/// struct-declaration-list struct-declaration
348/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
349/// struct-declaration:
350/// specifier-qualifier-list struct-declarator-list ';'
351/// [GNU] __extension__ struct-declaration [TODO]
352/// [GNU] specifier-qualifier-list ';' [TODO]
353/// struct-declarator-list:
354/// struct-declarator
355/// struct-declarator-list ',' struct-declarator
356/// struct-declarator:
357/// declarator
358/// declarator[opt] ':' constant-expression
Chris Lattner1890ac82006-08-13 01:16:23 +0000359///
360void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
361 assert((Tok.getKind() == tok::kw_struct ||
362 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000363 SourceLocation Start = Tok.getLocation();
Chris Lattner1890ac82006-08-13 01:16:23 +0000364 bool isUnion = Tok.getKind() == tok::kw_union;
365 ConsumeToken();
366
367 // Must have either 'struct name' or 'struct {...}'.
368 if (Tok.getKind() != tok::identifier &&
369 Tok.getKind() != tok::l_brace) {
370 Diag(Tok, diag::err_expected_ident_lbrace);
371 return;
372 }
373
374 if (Tok.getKind() == tok::identifier)
375 ConsumeToken();
376
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000377 if (Tok.getKind() == tok::l_brace) {
378 SourceLocation LBraceLoc = Tok.getLocation();
379 ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000380
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000381 if (Tok.getKind() == tok::r_brace)
382 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000383
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000384 while (Tok.getKind() != tok::r_brace &&
385 Tok.getKind() != tok::eof) {
386 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000387
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000388 // Parse the common specifier-qualifiers-list piece.
389 DeclSpec DS;
390 SourceLocation SpecQualLoc = Tok.getLocation();
391 ParseSpecifierQualifierList(DS);
392 // TODO: Does specifier-qualifier list correctly check that *something* is
393 // specified?
394
395 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000396
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000397 // If there are no declarators, issue a warning.
398 if (Tok.getKind() == tok::semi) {
399 Diag(SpecQualLoc, diag::w_no_declarators);
400 } else {
401 // Read struct-declarators until we find the semicolon.
402 while (1) {
403 /// struct-declarator: declarator
404 /// struct-declarator: declarator[opt] ':' constant-expression
405 if (Tok.getKind() != tok::colon)
406 ParseDeclarator(DeclaratorInfo);
407
408 if (Tok.getKind() == tok::colon) {
409 ConsumeToken();
410 ExprResult Res = ParseConstantExpression();
411 if (Res.isInvalid) {
412 SkipUntil(tok::semi, true, true);
413 } else {
414 // Process it.
415 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000416 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000417
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000418 // TODO: install declarator.
419
420 // If we don't have a comma, it is either the end of the list (a ';')
421 // or an error, bail out.
422 if (Tok.getKind() != tok::comma)
423 break;
424
425 // Consume the comma.
426 ConsumeToken();
427
428 // Parse the next declarator.
429 DeclaratorInfo.clear();
430 }
431 }
432
433 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000434 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000435 } else {
436 Diag(Tok, diag::err_expected_semi_decl_list);
437 // Skip to end of block or statement
438 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000439 }
440 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000441
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000442 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace);
443 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000444
445 const char *PrevSpec = 0;
446 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
447 PrevSpec))
448 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000449}
450
451
Chris Lattner3b561a32006-08-13 00:12:11 +0000452/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000453/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000454/// 'enum' identifier[opt] '{' enumerator-list '}'
455/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000456/// [GNU] 'enum' identifier[opt] '{' enumerator-list '}' attributes [TODO]
457/// [GNU] 'enum' identifier[opt] '{' enumerator-list ',' '}' attributes [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000458/// 'enum' identifier
459/// enumerator-list:
460/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000461/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000462/// enumerator:
463/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000464/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000465/// enumeration-constant:
466/// identifier
467///
468void Parser::ParseEnumSpecifier(DeclSpec &DS) {
469 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerda72c822006-08-13 22:16:42 +0000470 SourceLocation Start = Tok.getLocation();
Chris Lattner3b561a32006-08-13 00:12:11 +0000471 ConsumeToken();
472
473 // Must have either 'enum name' or 'enum {...}'.
474 if (Tok.getKind() != tok::identifier &&
475 Tok.getKind() != tok::l_brace) {
476 Diag(Tok, diag::err_expected_ident_lbrace);
477 return;
478 }
479
480 if (Tok.getKind() == tok::identifier)
481 ConsumeToken();
482
Chris Lattner1890ac82006-08-13 01:16:23 +0000483 if (Tok.getKind() != tok::l_brace)
484 return;
485
486 SourceLocation LBraceLoc = Tok.getLocation();
487 ConsumeBrace();
488
489 if (Tok.getKind() == tok::r_brace)
490 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
491
492 // Parse the enumerator-list.
493 while (Tok.getKind() == tok::identifier) {
494 ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000495
Chris Lattner1890ac82006-08-13 01:16:23 +0000496 if (Tok.getKind() == tok::equal) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000497 ConsumeToken();
Chris Lattner1890ac82006-08-13 01:16:23 +0000498 ExprResult Res = ParseConstantExpression();
499 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
Chris Lattner3b561a32006-08-13 00:12:11 +0000500 }
501
Chris Lattner1890ac82006-08-13 01:16:23 +0000502 if (Tok.getKind() != tok::comma)
503 break;
504 SourceLocation CommaLoc = Tok.getLocation();
505 ConsumeToken();
506
507 if (Tok.getKind() != tok::identifier && !getLang().C99)
508 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000509 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000510
511 // Eat the }.
512 MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",
513 diag::err_expected_rbrace);
Chris Lattner3b561a32006-08-13 00:12:11 +0000514 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000515
516
517 const char *PrevSpec = 0;
518 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
519 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000520}
521
522
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000523/// isTypeSpecifierQualifier - Return true if the current token could be the
524/// start of a specifier-qualifier-list.
525bool Parser::isTypeSpecifierQualifier() const {
526 switch (Tok.getKind()) {
527 default: return false;
528 // type-specifiers
529 case tok::kw_short:
530 case tok::kw_long:
531 case tok::kw_signed:
532 case tok::kw_unsigned:
533 case tok::kw__Complex:
534 case tok::kw__Imaginary:
535 case tok::kw_void:
536 case tok::kw_char:
537 case tok::kw_int:
538 case tok::kw_float:
539 case tok::kw_double:
540 case tok::kw__Bool:
541 case tok::kw__Decimal32:
542 case tok::kw__Decimal64:
543 case tok::kw__Decimal128:
544
545 // struct-or-union-specifier
546 case tok::kw_struct:
547 case tok::kw_union:
548 // enum-specifier
549 case tok::kw_enum:
550
551 // type-qualifier
552 case tok::kw_const:
553 case tok::kw_volatile:
554 case tok::kw_restrict:
555 return true;
556
557 // typedef-name
558 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000559 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000560
561 // TODO: Attributes.
562 }
563}
564
Chris Lattneracd58a32006-08-06 17:24:14 +0000565/// isDeclarationSpecifier() - Return true if the current token is part of a
566/// declaration specifier.
567bool Parser::isDeclarationSpecifier() const {
568 switch (Tok.getKind()) {
569 default: return false;
570 // storage-class-specifier
571 case tok::kw_typedef:
572 case tok::kw_extern:
573 case tok::kw_static:
574 case tok::kw_auto:
575 case tok::kw_register:
576 case tok::kw___thread:
577
578 // type-specifiers
579 case tok::kw_short:
580 case tok::kw_long:
581 case tok::kw_signed:
582 case tok::kw_unsigned:
583 case tok::kw__Complex:
584 case tok::kw__Imaginary:
585 case tok::kw_void:
586 case tok::kw_char:
587 case tok::kw_int:
588 case tok::kw_float:
589 case tok::kw_double:
590 case tok::kw__Bool:
591 case tok::kw__Decimal32:
592 case tok::kw__Decimal64:
593 case tok::kw__Decimal128:
594
595 // struct-or-union-specifier
596 case tok::kw_struct:
597 case tok::kw_union:
598 // enum-specifier
599 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000600
Chris Lattneracd58a32006-08-06 17:24:14 +0000601 // type-qualifier
602 case tok::kw_const:
603 case tok::kw_volatile:
604 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000605
Chris Lattneracd58a32006-08-06 17:24:14 +0000606 // function-specifier
607 case tok::kw_inline:
608 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000609
Chris Lattneracd58a32006-08-06 17:24:14 +0000610 // typedef-name
611 case tok::identifier:
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000612 return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattneracd58a32006-08-06 17:24:14 +0000613 // TODO: Attributes.
614 }
615}
616
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000617
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000618/// ParseTypeQualifierListOpt
619/// type-qualifier-list: [C99 6.7.5]
620/// type-qualifier
621/// [GNU] attributes [TODO]
622/// type-qualifier-list type-qualifier
623/// [GNU] type-qualifier-list attributes [TODO]
624///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000625void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
626 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000627 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000628 int isInvalid = false;
629 const char *PrevSpec = 0;
630
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000631 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000632 default:
633 // If this is not a declaration specifier token, we're done reading decl
634 // specifiers. First verify that DeclSpec's are consistent.
635 DS.Finish(StartLoc, Diags, getLang());
636 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000637 // TODO: attributes.
638 case tok::kw_const:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000639 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
640 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000641 case tok::kw_volatile:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000642 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
643 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000644 case tok::kw_restrict:
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000645 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000646 break;
647 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000648
649 // If the specifier combination wasn't legal, issue a diagnostic.
650 if (isInvalid) {
651 assert(PrevSpec && "Method did not return previous specifier!");
652 if (isInvalid == 1) // Error.
653 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
654 else // extwarn.
655 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
656 }
657 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000658 }
659}
660
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000661
662/// ParseDeclarator - Parse and verify a newly-initialized declarator.
663///
664void Parser::ParseDeclarator(Declarator &D) {
665 /// This implements the 'declarator' production in the C grammar, then checks
666 /// for well-formedness and issues diagnostics.
667 ParseDeclaratorInternal(D);
668
Chris Lattner9fab3b92006-08-12 18:25:42 +0000669 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000670
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000671}
672
673/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000674/// declarator: [C99 6.7.5]
675/// pointer[opt] direct-declarator
676///
677/// pointer: [C99 6.7.5]
678/// '*' type-qualifier-list[opt]
679/// '*' type-qualifier-list[opt] pointer
680///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000681void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000682 if (Tok.getKind() != tok::star)
683 return ParseDirectDeclarator(D);
684
685 // Otherwise, '*' -> pointer.
686 SourceLocation Loc = Tok.getLocation();
687 ConsumeToken(); // Eat the *.
688 DeclSpec DS;
689 ParseTypeQualifierListOpt(DS);
690
691 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000692 ParseDeclaratorInternal(D);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000693
694 // Remember that we parsed a pointer type, and remember the type-quals.
695 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
696}
697
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000698
699/// ParseDirectDeclarator
700/// direct-declarator: [C99 6.7.5]
701/// identifier
702/// '(' declarator ')'
703/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000704/// [C90] direct-declarator '[' constant-expression[opt] ']'
705/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
706/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
707/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
708/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000709/// direct-declarator '(' parameter-type-list ')'
710/// direct-declarator '(' identifier-list[opt] ')'
711/// [GNU] direct-declarator '(' parameter-forward-declarations
712/// parameter-type-list[opt] ')'
713///
Chris Lattneracd58a32006-08-06 17:24:14 +0000714void Parser::ParseDirectDeclarator(Declarator &D) {
715 // Parse the first direct-declarator seen.
716 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
717 assert(Tok.getIdentifierInfo() && "Not an identifier?");
718 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
719 ConsumeToken();
720 } else if (Tok.getKind() == tok::l_paren) {
721 // direct-declarator: '(' declarator ')'
722 // direct-declarator: '(' attributes declarator ')' [TODO]
723 // Example: 'char (*X)' or 'int (*XX)(void)'
724 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000725 } else if (D.mayOmitIdentifier()) {
726 // This could be something simple like "int" (in which case the declarator
727 // portion is empty), if an abstract-declarator is allowed.
728 D.SetIdentifier(0, Tok.getLocation());
729 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000730 // Expected identifier or '('.
731 Diag(Tok, diag::err_expected_ident_lparen);
732 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000733 }
734
735 assert(D.isPastIdentifier() &&
736 "Haven't past the location of the identifier yet?");
737
738 while (1) {
739 if (Tok.getKind() == tok::l_paren) {
740 ParseParenDeclarator(D);
741 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000742 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000743 } else {
744 break;
745 }
746 }
747}
748
749/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
750/// either be before the identifier (in which case these are just grouping
751/// parens for precedence) or it may be after the identifier, in which case
752/// these are function arguments.
753///
754/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000755/// parameter-type-list: [C99 6.7.5]
756/// parameter-list
757/// parameter-list ',' '...'
758///
759/// parameter-list: [C99 6.7.5]
760/// parameter-declaration
761/// parameter-list ',' parameter-declaration
762///
763/// parameter-declaration: [C99 6.7.5]
764/// declaration-specifiers declarator
Chris Lattneracd58a32006-08-06 17:24:14 +0000765/// [GNU] declaration-specifiers declarator attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000766/// declaration-specifiers abstract-declarator[opt]
Chris Lattneracd58a32006-08-06 17:24:14 +0000767/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000768///
769/// identifier-list: [C99 6.7.5]
770/// identifier
771/// identifier-list ',' identifier
772///
Chris Lattneracd58a32006-08-06 17:24:14 +0000773void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000774 SourceLocation StartLoc = Tok.getLocation();
Chris Lattneracd58a32006-08-06 17:24:14 +0000775 ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000776
Chris Lattneracd58a32006-08-06 17:24:14 +0000777 // If we haven't past the identifier yet (or where the identifier would be
778 // stored, if this is an abstract declarator), then this is probably just
779 // grouping parens.
780 if (!D.isPastIdentifier()) {
781 // Okay, this is probably a grouping paren. However, if this could be an
782 // abstract-declarator, then this could also be the start of function
783 // arguments (consider 'void()').
784 bool isGrouping;
785
786 if (!D.mayOmitIdentifier()) {
787 // If this can't be an abstract-declarator, this *must* be a grouping
788 // paren, because we haven't seen the identifier yet.
789 isGrouping = true;
790 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
791 isDeclarationSpecifier()) { // 'int(int)' is a function.
792
793 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000794 } else {
Chris Lattnera3507222006-08-07 00:33:37 +0000795 // Otherwise, this is a grouping paren, e.g. 'int (*X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000796 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000797 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000798
799 // If this is a grouping paren, handle:
800 // direct-declarator: '(' declarator ')'
801 // direct-declarator: '(' attributes declarator ')' [TODO]
802 if (isGrouping) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000803 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000804 // Match the ')'.
805 MatchRHSPunctuation(tok::r_paren, StartLoc, "(",
806 diag::err_expected_rparen);
Chris Lattneracd58a32006-08-06 17:24:14 +0000807 return;
808 }
809
810 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000811 // argument list. Recognize that this declarator will never have an
812 // identifier (and remember where it would have been), then fall through to
813 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000814 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000815 }
816
Chris Lattneracd58a32006-08-06 17:24:14 +0000817 // Okay, this is the parameter list of a function definition, or it is an
818 // identifier list of a K&R-style function.
819
Chris Lattner9fab3b92006-08-12 18:25:42 +0000820 // TODO: enter function-declaration scope, limiting any declarators for
Chris Lattneracd58a32006-08-06 17:24:14 +0000821 // arguments to the function scope.
822 // NOTE: better to only create a scope if not '()'
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000823 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000824 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000825 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000826 bool ErrorEmitted = false;
827
Chris Lattneracd58a32006-08-06 17:24:14 +0000828 if (Tok.getKind() == tok::r_paren) {
829 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000830 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000831 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000832 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000833 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000834 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000835 // Identifier list. Note that '(' identifier-list ')' is only allowed for
836 // normal declarators, not for abstract-declarators.
837 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
838
839 // If there was no identifier specified, either we are in an
840 // abstract-declarator, or we are in a parameter declarator which was found
841 // to be abstract. In abstract-declarators, identifier lists are not valid,
842 // diagnose this.
843 if (!D.getIdentifier())
844 Diag(Tok, diag::ext_ident_list_in_param);
845
Chris Lattner9fab3b92006-08-12 18:25:42 +0000846 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000847 ConsumeToken();
848 while (Tok.getKind() == tok::comma) {
849 // Eat the comma.
850 ConsumeToken();
851
Chris Lattner0be454e2006-08-12 19:30:51 +0000852 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000853 ErrorEmitted = true;
854 break;
855 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000856 }
857
Chris Lattneracd58a32006-08-06 17:24:14 +0000858 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000859 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000860 HasPrototype = false;
861 } else {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000862 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000863 bool ReadArg = false;
864 // Finally, a normal, non-empty parameter type list.
865 while (1) {
866 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000867 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000868
869 // Check to see if this is "void(...)" which is not allowed.
870 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000871 // Otherwise, parse parameter type list. If it starts with an
872 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000873 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000874 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000875 }
876
877 // Consume the ellipsis.
878 ConsumeToken();
879 break;
880 }
881
882 ReadArg = true;
883
884 // Parse the declaration-specifiers.
885 DeclSpec DS;
886 ParseDeclarationSpecifiers(DS);
887
888 // Parse the declarator. This is "PrototypeContext", because we must
889 // accept either 'declarator' or 'abstract-declarator' here.
890 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
891 ParseDeclarator(DeclaratorInfo);
892
893 // TODO: do something with the declarator, if it is valid.
894
895 // If the next token is a comma, consume it and keep reading arguments.
896 if (Tok.getKind() != tok::comma) break;
897
898 // Consume the comma.
899 ConsumeToken();
900 }
901
902 HasPrototype = true;
903 }
904
Chris Lattner9fab3b92006-08-12 18:25:42 +0000905 // TODO: pop the scope.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000906
Chris Lattner9fab3b92006-08-12 18:25:42 +0000907 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +0000908
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000909 // Remember that we parsed a function type, and remember the attributes.
910 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +0000911 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000912
Chris Lattner14776b92006-08-06 22:27:40 +0000913
914 // If we have the closing ')', eat it and we're done.
915 if (Tok.getKind() == tok::r_paren) {
916 ConsumeParen();
917 } else {
918 // If an error happened earlier parsing something else in the proto, don't
919 // issue another error.
920 if (!ErrorEmitted)
921 Diag(Tok, diag::err_expected_rparen);
922 SkipUntil(tok::r_paren);
923 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000924}
Chris Lattneracd58a32006-08-06 17:24:14 +0000925
Chris Lattnere8074e62006-08-06 18:30:15 +0000926
927/// [C90] direct-declarator '[' constant-expression[opt] ']'
928/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
929/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
930/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
931/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
932void Parser::ParseBracketDeclarator(Declarator &D) {
933 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnereec40f92006-08-06 21:55:29 +0000934 ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +0000935
936 // If valid, this location is the position where we read the 'static' keyword.
937 SourceLocation StaticLoc;
938 if (Tok.getKind() == tok::kw_static) {
939 StaticLoc = Tok.getLocation();
940 ConsumeToken();
941 }
942
943 // If there is a type-qualifier-list, read it now.
944 DeclSpec DS;
945 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +0000946
947 // If we haven't already read 'static', check to see if there is one after the
948 // type-qualifier-list.
949 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
950 StaticLoc = Tok.getLocation();
951 ConsumeToken();
952 }
953
954 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +0000955 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +0000956 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +0000957 if (Tok.getKind() == tok::star) {
958 // Remember the '*' token, in case we have to un-get it.
959 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +0000960 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +0000961
962 // Check that the ']' token is present to avoid incorrectly parsing
963 // expressions starting with '*' as [*].
964 if (Tok.getKind() == tok::r_square) {
965 if (StaticLoc.isValid())
966 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
967 StaticLoc = SourceLocation(); // Drop the static.
968 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +0000969 } else {
970 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +0000971 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +0000972 // need to reparse it. This handles cases like 'X[*p + 4]'
973 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +0000974 }
Chris Lattner9fab3b92006-08-12 18:25:42 +0000975 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000976 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +0000977 NumElements = ParseAssignmentExpression();
978 }
979
980 // If there was an error parsing the assignment-expression, recover.
981 if (NumElements.isInvalid) {
982 // If the expression was invalid, skip it.
983 SkipUntil(tok::r_square);
984 return;
Chris Lattnere8074e62006-08-06 18:30:15 +0000985 }
986
Chris Lattner9fab3b92006-08-12 18:25:42 +0000987 MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare);
988
Chris Lattnere8074e62006-08-06 18:30:15 +0000989 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
990 // it was not a constant expression.
991 if (!getLang().C99) {
992 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +0000993 if (isStar || StaticLoc.isValid() ||
994 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +0000995 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +0000996 }
Chris Lattner6c7416c2006-08-07 00:19:33 +0000997
998 // Remember that we parsed a pointer type, and remember the type-quals.
999 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
1000 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001001 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001002}
1003