blob: 8c9d40bff7b76f39e9dd10aa2905b11f787c6142 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000016using namespace llvm;
17using namespace clang;
18
19//===----------------------------------------------------------------------===//
20// C99 6.7: Declarations.
21//===----------------------------------------------------------------------===//
22
Chris Lattnerf5fbd792006-08-10 23:56:11 +000023/// ParseTypeName
24/// type-name: [C99 6.7.6]
25/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000026Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027 // Parse the common declaration-specifiers piece.
28 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000029 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030
31 // Parse the abstract-declarator, if present.
32 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
33 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000034
Chris Lattner558cb292006-11-19 01:31:06 +000035 return Actions.ParseTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036}
37
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000038/// ParseAttributes - Parse a non-empty attributes list.
39///
40/// [GNU] attributes:
41/// attribute
42/// attributes attribute
43///
44/// [GNU] attribute:
45/// '__attribute__' '(' '(' attribute-list ')' ')'
46///
47/// [GNU] attribute-list:
48/// attrib
49/// attribute_list ',' attrib
50///
51/// [GNU] attrib:
52/// empty
53/// any-word
54/// any-word '(' identifier ')'
55/// any-word '(' identifier ',' nonempty-expr-list ')'
56/// any-word '(' expr-list ')'
57///
58void Parser::ParseAttributes() {
59 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
60 ConsumeToken();
61
62 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
63 "attribute"))
64 return;
65
66 // TODO: Parse the attributes.
67 SkipUntil(tok::r_paren, false);
68}
69
Chris Lattnerf5fbd792006-08-10 23:56:11 +000070
Chris Lattner53361ac2006-08-10 05:19:57 +000071/// ParseDeclaration - Parse a full 'declaration', which consists of
72/// declaration-specifiers, some number of declarators, and a semicolon.
73/// 'Context' should be a Declarator::TheContext value.
Chris Lattner302b4be2006-11-19 02:31:38 +000074Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +000075 // Parse the common declaration-specifiers piece.
76 DeclSpec DS;
77 ParseDeclarationSpecifiers(DS);
78
Chris Lattner0e894622006-08-13 19:58:17 +000079 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
80 // declaration-specifiers init-declarator-list[opt] ';'
81 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +000082 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +000083 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +000084 }
85
Chris Lattner53361ac2006-08-10 05:19:57 +000086 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
87 ParseDeclarator(DeclaratorInfo);
88
Chris Lattner302b4be2006-11-19 02:31:38 +000089 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +000090}
91
Chris Lattnerf0f3baa2006-08-14 00:15:20 +000092/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
93/// parsing 'declaration-specifiers declarator'. This method is split out this
94/// way to handle the ambiguity between top-level function-definitions and
95/// declarations.
96///
97/// declaration: [C99 6.7]
98/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
99/// [!C99] init-declarator-list ';' [TODO]
100/// [OMP] threadprivate-directive [TODO]
101///
102/// init-declarator-list: [C99 6.7]
103/// init-declarator
104/// init-declarator-list ',' init-declarator
105/// init-declarator: [C99 6.7]
106/// declarator
107/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000108/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
109/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000110///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000111Parser::DeclTy *Parser::
112ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
113
114 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
115 // that they can be chained properly if the actions want this.
116 Parser::DeclTy *LastDeclInGroup = 0;
117
Chris Lattner53361ac2006-08-10 05:19:57 +0000118 // At this point, we know that it is not a function definition. Parse the
119 // rest of the init-declarator-list.
120 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000121 // If a simple-asm-expr is present, parse it.
122 if (Tok.getKind() == tok::kw_asm)
123 ParseSimpleAsm();
124
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000125 // If attributes are present, parse them.
126 if (Tok.getKind() == tok::kw___attribute)
127 ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000128
Chris Lattner53361ac2006-08-10 05:19:57 +0000129 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000130 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000131 if (Tok.getKind() == tok::equal) {
132 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000133 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000134 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000135 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000136 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000137 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000138 }
139
Chris Lattner697e5d62006-11-09 06:32:27 +0000140 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000141 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000142 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
143 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000144
145 // If we don't have a comma, it is either the end of the list (a ';') or an
146 // error, bail out.
147 if (Tok.getKind() != tok::comma)
148 break;
149
150 // Consume the comma.
151 ConsumeToken();
152
153 // Parse the next declarator.
154 D.clear();
155 ParseDeclarator(D);
156 }
157
158 if (Tok.getKind() == tok::semi) {
159 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000160 return LastDeclInGroup;
Chris Lattner53361ac2006-08-10 05:19:57 +0000161 } else {
162 Diag(Tok, diag::err_parse_error);
163 // Skip to end of block or statement
164 SkipUntil(tok::r_brace, true);
165 if (Tok.getKind() == tok::semi)
166 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000167 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000168 }
169}
170
Chris Lattner1890ac82006-08-13 01:16:23 +0000171/// ParseSpecifierQualifierList
172/// specifier-qualifier-list:
173/// type-specifier specifier-qualifier-list[opt]
174/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000175/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000176///
177void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
178 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
179 /// parse declaration-specifiers and complain about extra stuff.
180 SourceLocation Loc = Tok.getLocation();
181 ParseDeclarationSpecifiers(DS);
182
183 // Validate declspec for type-name.
184 unsigned Specs = DS.getParsedSpecifiers();
185 if (Specs == DeclSpec::PQ_None)
186 Diag(Tok, diag::err_typename_requires_specqual);
187
Chris Lattner1b22eed2006-11-28 05:12:07 +0000188 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000189 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000190 if (DS.getStorageClassSpecLoc().isValid())
191 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
192 else
193 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000194 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000195 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000196
197 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000198 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000199 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000200 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000201 }
202}
Chris Lattner53361ac2006-08-10 05:19:57 +0000203
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000204/// ParseDeclarationSpecifiers
205/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000206/// storage-class-specifier declaration-specifiers[opt]
207/// type-specifier declaration-specifiers[opt]
208/// type-qualifier declaration-specifiers[opt]
209/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000210/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000211///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000212/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000213/// 'typedef'
214/// 'extern'
215/// 'static'
216/// 'auto'
217/// 'register'
218/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000219/// type-specifier: [C99 6.7.2]
220/// 'void'
221/// 'char'
222/// 'short'
223/// 'int'
224/// 'long'
225/// 'float'
226/// 'double'
227/// 'signed'
228/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000229/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000230/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000231/// typedef-name
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000232/// [C99] '_Bool'
233/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000234/// [C99] '_Imaginary' // Removed in TC2?
235/// [GNU] '_Decimal32'
236/// [GNU] '_Decimal64'
237/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000238/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000239/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000240/// [OBJC] typedef-name objc-protocol-refs [TODO]
241/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000242/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000243/// 'const'
244/// 'volatile'
245/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000246/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000247/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000248///
249void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner4d8f8732006-11-28 05:05:08 +0000250 // FIXME: Remove this.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000251 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000252 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000253 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000254 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000255 SourceLocation Loc = Tok.getLocation();
256
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000257 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000258 // typedef-name
259 case tok::identifier:
260 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000261 // a type-specifier. Without this check we misparse:
262 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000263 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000264 // It has to be available as a typedef too!
265 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
266 CurScope)) {
267 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec,
268 TypeRep);
269 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000270 break;
271 }
272 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000273 default:
274 // If this is not a declaration specifier token, we're done reading decl
275 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattner4d8f8732006-11-28 05:05:08 +0000276 // FIXME: Remove StartLoc.
Chris Lattner839713c2006-08-04 06:15:52 +0000277 DS.Finish(StartLoc, Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000278 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000279
280 // GNU attributes support.
281 case tok::kw___attribute:
282 ParseAttributes();
Chris Lattnerb95cca02006-10-17 03:01:08 +0000283 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000284
285 // storage-class-specifier
286 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000287 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000288 break;
289 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000290 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000291 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000292 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000293 break;
294 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000295 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000296 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000297 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000298 break;
299 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000300 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000301 break;
302 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000303 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000304 break;
305 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000306 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000307 break;
308
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000309 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000310 case tok::kw_short:
311 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
312 break;
313 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000314 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000315 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000316 else
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000317 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000318 break;
319 case tok::kw_signed:
320 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
321 break;
322 case tok::kw_unsigned:
323 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
324 break;
325 case tok::kw__Complex:
326 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
327 break;
328 case tok::kw__Imaginary:
329 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
330 break;
331 case tok::kw_void:
332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
333 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000334 case tok::kw_char:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
336 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000337 case tok::kw_int:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
339 break;
340 case tok::kw_float:
341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
342 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000343 case tok::kw_double:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
345 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000346 case tok::kw__Bool:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
348 break;
349 case tok::kw__Decimal32:
350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
351 break;
352 case tok::kw__Decimal64:
353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
354 break;
355 case tok::kw__Decimal128:
356 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000357 break;
358
Chris Lattner1890ac82006-08-13 01:16:23 +0000359 case tok::kw_struct:
360 case tok::kw_union:
361 ParseStructUnionSpecifier(DS);
362 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000363 case tok::kw_enum:
364 ParseEnumSpecifier(DS);
365 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000366
367 // type-qualifier
368 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000369 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
370 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000371 break;
372 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000373 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
374 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000375 break;
376 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000377 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
378 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000379 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000380
381 // function-specifier
382 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000383 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000384 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000385 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000386 // If the specifier combination wasn't legal, issue a diagnostic.
387 if (isInvalid) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000388 // FIXME: emit a matching caret at the previous illegal spec combination.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000389 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000390 if (isInvalid == 1) // Error.
391 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
392 else // extwarn.
393 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000394 }
395 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000396 }
397}
398
Chris Lattner1890ac82006-08-13 01:16:23 +0000399
400/// ParseStructUnionSpecifier
401/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000402/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000403/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000404/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
405/// '}' attributes[opt]
406/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000407/// struct-or-union:
408/// 'struct'
409/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000410/// struct-contents:
411/// struct-declaration-list
412/// [EXT] empty
413/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000414/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000415/// struct-declaration
416/// struct-declaration-list struct-declaration
417/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
418/// struct-declaration:
419/// specifier-qualifier-list struct-declarator-list ';'
420/// [GNU] __extension__ struct-declaration [TODO]
421/// [GNU] specifier-qualifier-list ';' [TODO]
422/// struct-declarator-list:
423/// struct-declarator
424/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000425/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000426/// struct-declarator:
427/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000428/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000429/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000430/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000431///
432void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
433 assert((Tok.getKind() == tok::kw_struct ||
434 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
435 bool isUnion = Tok.getKind() == tok::kw_union;
Chris Lattneraf635312006-10-16 06:06:51 +0000436 SourceLocation Start = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000437
438 // If attributes exist after tag, parse them.
439 if (Tok.getKind() == tok::kw___attribute)
440 ParseAttributes();
441
Chris Lattner1890ac82006-08-13 01:16:23 +0000442 // Must have either 'struct name' or 'struct {...}'.
443 if (Tok.getKind() != tok::identifier &&
444 Tok.getKind() != tok::l_brace) {
445 Diag(Tok, diag::err_expected_ident_lbrace);
446 return;
447 }
448
449 if (Tok.getKind() == tok::identifier)
450 ConsumeToken();
451
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000452 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000453 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000454
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000455 if (Tok.getKind() == tok::r_brace)
456 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000457
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000458 while (Tok.getKind() != tok::r_brace &&
459 Tok.getKind() != tok::eof) {
460 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000461
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000462 // Parse the common specifier-qualifiers-list piece.
463 DeclSpec DS;
464 SourceLocation SpecQualLoc = Tok.getLocation();
465 ParseSpecifierQualifierList(DS);
466 // TODO: Does specifier-qualifier list correctly check that *something* is
467 // specified?
468
469 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000470
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000471 // If there are no declarators, issue a warning.
472 if (Tok.getKind() == tok::semi) {
473 Diag(SpecQualLoc, diag::w_no_declarators);
474 } else {
475 // Read struct-declarators until we find the semicolon.
476 while (1) {
477 /// struct-declarator: declarator
478 /// struct-declarator: declarator[opt] ':' constant-expression
479 if (Tok.getKind() != tok::colon)
480 ParseDeclarator(DeclaratorInfo);
481
482 if (Tok.getKind() == tok::colon) {
483 ConsumeToken();
484 ExprResult Res = ParseConstantExpression();
485 if (Res.isInvalid) {
486 SkipUntil(tok::semi, true, true);
487 } else {
488 // Process it.
489 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000490 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000491
492 // If attributes exist after the declarator, parse them.
493 if (Tok.getKind() == tok::kw___attribute)
494 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000495
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000496 // TODO: install declarator.
497
498 // If we don't have a comma, it is either the end of the list (a ';')
499 // or an error, bail out.
500 if (Tok.getKind() != tok::comma)
501 break;
502
503 // Consume the comma.
504 ConsumeToken();
505
506 // Parse the next declarator.
507 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000508
509 // Attributes are only allowed on the second declarator.
510 if (Tok.getKind() == tok::kw___attribute)
511 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000512 }
513 }
514
515 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000516 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000517 } else {
518 Diag(Tok, diag::err_expected_semi_decl_list);
519 // Skip to end of block or statement
520 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000521 }
522 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000523
Chris Lattner04f80192006-08-15 04:55:54 +0000524 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000525
526 // If attributes exist after struct contents, parse them.
527 if (Tok.getKind() == tok::kw___attribute)
528 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000529 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000530
531 const char *PrevSpec = 0;
532 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
533 PrevSpec))
534 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000535}
536
537
Chris Lattner3b561a32006-08-13 00:12:11 +0000538/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000539/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000540/// 'enum' identifier[opt] '{' enumerator-list '}'
541/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000542/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
543/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000544/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000545/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000546/// enumerator-list:
547/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000548/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000549/// enumerator:
550/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000551/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000552/// enumeration-constant:
553/// identifier
554///
555void Parser::ParseEnumSpecifier(DeclSpec &DS) {
556 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattneraf635312006-10-16 06:06:51 +0000557 SourceLocation Start = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000558
Chris Lattnere37e2332006-08-15 04:50:22 +0000559 if (Tok.getKind() == tok::kw___attribute)
560 ParseAttributes();
561
Chris Lattner3b561a32006-08-13 00:12:11 +0000562 // Must have either 'enum name' or 'enum {...}'.
563 if (Tok.getKind() != tok::identifier &&
564 Tok.getKind() != tok::l_brace) {
565 Diag(Tok, diag::err_expected_ident_lbrace);
566 return;
567 }
568
569 if (Tok.getKind() == tok::identifier)
570 ConsumeToken();
571
Chris Lattner0fb8b362006-08-14 01:30:12 +0000572 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000573 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000574
Chris Lattner0fb8b362006-08-14 01:30:12 +0000575 if (Tok.getKind() == tok::r_brace)
576 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
577
578 // Parse the enumerator-list.
579 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000580 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000581
582 if (Tok.getKind() == tok::equal) {
583 ConsumeToken();
584 ExprResult Res = ParseConstantExpression();
585 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
586 }
587
588 if (Tok.getKind() != tok::comma)
589 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000590 SourceLocation CommaLoc = ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000591
592 if (Tok.getKind() != tok::identifier && !getLang().C99)
593 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000594 }
595
Chris Lattner0fb8b362006-08-14 01:30:12 +0000596 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000597 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000598
599 // If attributes exist after the identifier list, parse them.
600 if (Tok.getKind() == tok::kw___attribute)
601 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000602 }
603 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000604
605
606 const char *PrevSpec = 0;
607 if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec))
608 Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000609}
610
611
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000612/// isTypeSpecifierQualifier - Return true if the current token could be the
613/// start of a specifier-qualifier-list.
614bool Parser::isTypeSpecifierQualifier() const {
615 switch (Tok.getKind()) {
616 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000617 // GNU attributes support.
618 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000619 // type-specifiers
620 case tok::kw_short:
621 case tok::kw_long:
622 case tok::kw_signed:
623 case tok::kw_unsigned:
624 case tok::kw__Complex:
625 case tok::kw__Imaginary:
626 case tok::kw_void:
627 case tok::kw_char:
628 case tok::kw_int:
629 case tok::kw_float:
630 case tok::kw_double:
631 case tok::kw__Bool:
632 case tok::kw__Decimal32:
633 case tok::kw__Decimal64:
634 case tok::kw__Decimal128:
635
636 // struct-or-union-specifier
637 case tok::kw_struct:
638 case tok::kw_union:
639 // enum-specifier
640 case tok::kw_enum:
641
642 // type-qualifier
643 case tok::kw_const:
644 case tok::kw_volatile:
645 case tok::kw_restrict:
646 return true;
647
648 // typedef-name
649 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000650 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000651
652 // TODO: Attributes.
653 }
654}
655
Chris Lattneracd58a32006-08-06 17:24:14 +0000656/// isDeclarationSpecifier() - Return true if the current token is part of a
657/// declaration specifier.
658bool Parser::isDeclarationSpecifier() const {
659 switch (Tok.getKind()) {
660 default: return false;
661 // storage-class-specifier
662 case tok::kw_typedef:
663 case tok::kw_extern:
664 case tok::kw_static:
665 case tok::kw_auto:
666 case tok::kw_register:
667 case tok::kw___thread:
668
669 // type-specifiers
670 case tok::kw_short:
671 case tok::kw_long:
672 case tok::kw_signed:
673 case tok::kw_unsigned:
674 case tok::kw__Complex:
675 case tok::kw__Imaginary:
676 case tok::kw_void:
677 case tok::kw_char:
678 case tok::kw_int:
679 case tok::kw_float:
680 case tok::kw_double:
681 case tok::kw__Bool:
682 case tok::kw__Decimal32:
683 case tok::kw__Decimal64:
684 case tok::kw__Decimal128:
685
686 // struct-or-union-specifier
687 case tok::kw_struct:
688 case tok::kw_union:
689 // enum-specifier
690 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000691
Chris Lattneracd58a32006-08-06 17:24:14 +0000692 // type-qualifier
693 case tok::kw_const:
694 case tok::kw_volatile:
695 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000696
Chris Lattneracd58a32006-08-06 17:24:14 +0000697 // function-specifier
698 case tok::kw_inline:
699 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000700
Chris Lattneracd58a32006-08-06 17:24:14 +0000701 // typedef-name
702 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000703 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000704 // TODO: Attributes.
705 }
706}
707
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000708
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000709/// ParseTypeQualifierListOpt
710/// type-qualifier-list: [C99 6.7.5]
711/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000712/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000713/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000714/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000715///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000716void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
717 SourceLocation StartLoc = Tok.getLocation();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000718 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000719 int isInvalid = false;
720 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000721 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000722
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000723 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000724 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000725 // If this is not a type-qualifier token, we're done reading type
726 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000727 DS.Finish(StartLoc, Diags, getLang());
728 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000729 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000730 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
731 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000732 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000733 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000734 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
735 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000736 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000737 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000738 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
739 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000740 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000741
742 case tok::kw___attribute:
743 ParseAttributes();
744 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000745 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000746
747 // If the specifier combination wasn't legal, issue a diagnostic.
748 if (isInvalid) {
749 assert(PrevSpec && "Method did not return previous specifier!");
750 if (isInvalid == 1) // Error.
751 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
752 else // extwarn.
753 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
754 }
755 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000756 }
757}
758
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000759
760/// ParseDeclarator - Parse and verify a newly-initialized declarator.
761///
762void Parser::ParseDeclarator(Declarator &D) {
763 /// This implements the 'declarator' production in the C grammar, then checks
764 /// for well-formedness and issues diagnostics.
765 ParseDeclaratorInternal(D);
766
Chris Lattner9fab3b92006-08-12 18:25:42 +0000767 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000768
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000769}
770
771/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000772/// declarator: [C99 6.7.5]
773/// pointer[opt] direct-declarator
774///
775/// pointer: [C99 6.7.5]
776/// '*' type-qualifier-list[opt]
777/// '*' type-qualifier-list[opt] pointer
778///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000779void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000780 if (Tok.getKind() != tok::star)
781 return ParseDirectDeclarator(D);
782
783 // Otherwise, '*' -> pointer.
Chris Lattneraf635312006-10-16 06:06:51 +0000784 SourceLocation Loc = ConsumeToken(); // Eat the *.
Chris Lattner6c7416c2006-08-07 00:19:33 +0000785 DeclSpec DS;
786 ParseTypeQualifierListOpt(DS);
787
788 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000789 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000790
Chris Lattner6c7416c2006-08-07 00:19:33 +0000791 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnera925dc62006-11-28 04:33:46 +0000792 D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.getTypeQualifiers(), Loc));
Chris Lattner6c7416c2006-08-07 00:19:33 +0000793}
794
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000795
796/// ParseDirectDeclarator
797/// direct-declarator: [C99 6.7.5]
798/// identifier
799/// '(' declarator ')'
800/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000801/// [C90] direct-declarator '[' constant-expression[opt] ']'
802/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
803/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
804/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
805/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000806/// direct-declarator '(' parameter-type-list ')'
807/// direct-declarator '(' identifier-list[opt] ')'
808/// [GNU] direct-declarator '(' parameter-forward-declarations
809/// parameter-type-list[opt] ')'
810///
Chris Lattneracd58a32006-08-06 17:24:14 +0000811void Parser::ParseDirectDeclarator(Declarator &D) {
812 // Parse the first direct-declarator seen.
813 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
814 assert(Tok.getIdentifierInfo() && "Not an identifier?");
815 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
816 ConsumeToken();
817 } else if (Tok.getKind() == tok::l_paren) {
818 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000819 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000820 // Example: 'char (*X)' or 'int (*XX)(void)'
821 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000822 } else if (D.mayOmitIdentifier()) {
823 // This could be something simple like "int" (in which case the declarator
824 // portion is empty), if an abstract-declarator is allowed.
825 D.SetIdentifier(0, Tok.getLocation());
826 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000827 // Expected identifier or '('.
828 Diag(Tok, diag::err_expected_ident_lparen);
829 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000830 }
831
832 assert(D.isPastIdentifier() &&
833 "Haven't past the location of the identifier yet?");
834
835 while (1) {
836 if (Tok.getKind() == tok::l_paren) {
837 ParseParenDeclarator(D);
838 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000839 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000840 } else {
841 break;
842 }
843 }
844}
845
846/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
847/// either be before the identifier (in which case these are just grouping
848/// parens for precedence) or it may be after the identifier, in which case
849/// these are function arguments.
850///
851/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000852/// parameter-type-list: [C99 6.7.5]
853/// parameter-list
854/// parameter-list ',' '...'
855///
856/// parameter-list: [C99 6.7.5]
857/// parameter-declaration
858/// parameter-list ',' parameter-declaration
859///
860/// parameter-declaration: [C99 6.7.5]
861/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000862/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000863/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000864/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000865///
866/// identifier-list: [C99 6.7.5]
867/// identifier
868/// identifier-list ',' identifier
869///
Chris Lattneracd58a32006-08-06 17:24:14 +0000870void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +0000871 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000872
Chris Lattneracd58a32006-08-06 17:24:14 +0000873 // If we haven't past the identifier yet (or where the identifier would be
874 // stored, if this is an abstract declarator), then this is probably just
875 // grouping parens.
876 if (!D.isPastIdentifier()) {
877 // Okay, this is probably a grouping paren. However, if this could be an
878 // abstract-declarator, then this could also be the start of function
879 // arguments (consider 'void()').
880 bool isGrouping;
881
882 if (!D.mayOmitIdentifier()) {
883 // If this can't be an abstract-declarator, this *must* be a grouping
884 // paren, because we haven't seen the identifier yet.
885 isGrouping = true;
886 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
887 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000888 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
889 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +0000890 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000891 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000892 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000893 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000894 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000895
896 // If this is a grouping paren, handle:
897 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000898 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000899 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000900 if (Tok.getKind() == tok::kw___attribute)
901 ParseAttributes();
902
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000903 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000904 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000905 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000906 return;
907 }
908
909 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000910 // argument list. Recognize that this declarator will never have an
911 // identifier (and remember where it would have been), then fall through to
912 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000913 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000914 }
915
Chris Lattneracd58a32006-08-06 17:24:14 +0000916 // Okay, this is the parameter list of a function definition, or it is an
917 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000918 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000919 bool HasPrototype;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000920 bool IsEmpty = false;
Chris Lattner14776b92006-08-06 22:27:40 +0000921 bool ErrorEmitted = false;
922
Chris Lattneracd58a32006-08-06 17:24:14 +0000923 if (Tok.getKind() == tok::r_paren) {
924 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000925 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000926 HasPrototype = false;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000927 IsEmpty = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000928 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000929 // K&R identifier lists can't have typedefs as identifiers, per
930 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000931 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000932 // Identifier list. Note that '(' identifier-list ')' is only allowed for
933 // normal declarators, not for abstract-declarators.
934 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
935
936 // If there was no identifier specified, either we are in an
937 // abstract-declarator, or we are in a parameter declarator which was found
938 // to be abstract. In abstract-declarators, identifier lists are not valid,
939 // diagnose this.
940 if (!D.getIdentifier())
941 Diag(Tok, diag::ext_ident_list_in_param);
942
Chris Lattner9fab3b92006-08-12 18:25:42 +0000943 // TODO: Remember token.
Chris Lattneracd58a32006-08-06 17:24:14 +0000944 ConsumeToken();
945 while (Tok.getKind() == tok::comma) {
946 // Eat the comma.
947 ConsumeToken();
948
Chris Lattner0be454e2006-08-12 19:30:51 +0000949 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) {
Chris Lattner14776b92006-08-06 22:27:40 +0000950 ErrorEmitted = true;
951 break;
952 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000953 }
954
Chris Lattneracd58a32006-08-06 17:24:14 +0000955 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000956 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000957 HasPrototype = false;
958 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +0000959 // Finally, a normal, non-empty parameter type list.
960
961 // Enter function-declaration scope, limiting any declarators for arguments
962 // to the function scope.
963 EnterScope(0);
964
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000965 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000966 bool ReadArg = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000967 while (1) {
968 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000969 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000970
971 // Check to see if this is "void(...)" which is not allowed.
972 if (!ReadArg) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000973 // Otherwise, parse parameter type list. If it starts with an
974 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000975 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000976 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000977 }
978
979 // Consume the ellipsis.
980 ConsumeToken();
981 break;
982 }
983
984 ReadArg = true;
985
986 // Parse the declaration-specifiers.
987 DeclSpec DS;
988 ParseDeclarationSpecifiers(DS);
989
990 // Parse the declarator. This is "PrototypeContext", because we must
991 // accept either 'declarator' or 'abstract-declarator' here.
992 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
993 ParseDeclarator(DeclaratorInfo);
994
Chris Lattnere37e2332006-08-15 04:50:22 +0000995 // Parse GNU attributes, if present.
996 if (Tok.getKind() == tok::kw___attribute)
997 ParseAttributes();
998
Chris Lattner43e956c2006-11-28 04:05:37 +0000999 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattnerf055d432006-11-28 04:28:12 +00001000 switch (DS.getStorageClassSpec()) {
Chris Lattner43e956c2006-11-28 04:05:37 +00001001 case DeclSpec::SCS_unspecified:
1002 case DeclSpec::SCS_register:
1003 break;
1004 case DeclSpec::SCS_auto:
1005 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1006 default:
Chris Lattner4d8f8732006-11-28 05:05:08 +00001007 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001008 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001009 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001010 break;
1011 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001012 if (DS.isThreadSpecified()) {
1013 Diag(DS.getThreadSpecLoc(),
1014 diag::err_invalid_storage_class_in_func_decl);
1015 DS.ClearStorageClassSpecs();
1016 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001017
1018 // Inform the actions module about the parameter declarator, so it gets
1019 // added to the current scope.
1020 Actions.ParseDeclarator(CurScope, DeclaratorInfo, 0, 0);
Chris Lattneracd58a32006-08-06 17:24:14 +00001021
1022 // If the next token is a comma, consume it and keep reading arguments.
1023 if (Tok.getKind() != tok::comma) break;
1024
1025 // Consume the comma.
1026 ConsumeToken();
1027 }
1028
1029 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001030
1031 // Leave prototype scope.
1032 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001033 }
1034
Chris Lattner9fab3b92006-08-12 18:25:42 +00001035 // TODO: capture argument info.
Chris Lattneracd58a32006-08-06 17:24:14 +00001036
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001037 // Remember that we parsed a function type, and remember the attributes.
1038 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic,
Chris Lattnerfff824f2006-08-07 06:31:38 +00001039 IsEmpty, StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001040
Chris Lattner14776b92006-08-06 22:27:40 +00001041
1042 // If we have the closing ')', eat it and we're done.
1043 if (Tok.getKind() == tok::r_paren) {
1044 ConsumeParen();
1045 } else {
1046 // If an error happened earlier parsing something else in the proto, don't
1047 // issue another error.
1048 if (!ErrorEmitted)
1049 Diag(Tok, diag::err_expected_rparen);
1050 SkipUntil(tok::r_paren);
1051 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001052}
Chris Lattneracd58a32006-08-06 17:24:14 +00001053
Chris Lattnere8074e62006-08-06 18:30:15 +00001054
1055/// [C90] direct-declarator '[' constant-expression[opt] ']'
1056/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1057/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1058/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1059/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1060void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001061 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001062
1063 // If valid, this location is the position where we read the 'static' keyword.
1064 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001065 if (Tok.getKind() == tok::kw_static)
1066 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001067
1068 // If there is a type-qualifier-list, read it now.
1069 DeclSpec DS;
1070 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001071
1072 // If we haven't already read 'static', check to see if there is one after the
1073 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001074 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1075 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001076
1077 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001078 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001079 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001080 if (Tok.getKind() == tok::star) {
1081 // Remember the '*' token, in case we have to un-get it.
1082 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001083 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001084
1085 // Check that the ']' token is present to avoid incorrectly parsing
1086 // expressions starting with '*' as [*].
1087 if (Tok.getKind() == tok::r_square) {
1088 if (StaticLoc.isValid())
1089 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1090 StaticLoc = SourceLocation(); // Drop the static.
1091 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001092 } else {
1093 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001094 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001095 // need to reparse it. This handles cases like 'X[*p + 4]'
1096 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001097 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001098 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001099 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001100 NumElements = ParseAssignmentExpression();
1101 }
1102
1103 // If there was an error parsing the assignment-expression, recover.
1104 if (NumElements.isInvalid) {
1105 // If the expression was invalid, skip it.
1106 SkipUntil(tok::r_square);
1107 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001108 }
1109
Chris Lattner04f80192006-08-15 04:55:54 +00001110 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001111
Chris Lattnere8074e62006-08-06 18:30:15 +00001112 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1113 // it was not a constant expression.
1114 if (!getLang().C99) {
1115 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001116 if (isStar || StaticLoc.isValid() ||
1117 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001118 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001119 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001120
1121 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnera925dc62006-11-28 04:33:46 +00001122 D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.getTypeQualifiers(),
Chris Lattner6c7416c2006-08-07 00:19:33 +00001123 StaticLoc.isValid(), isStar,
Chris Lattner62591722006-08-12 18:40:58 +00001124 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001125}
1126