blob: 57b2e16de47160960989346b949c3d48e52e99d7 [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 Lattnerc0acd3d2006-07-31 05:13:43 +0000250 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000251 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000252 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000253 SourceLocation Loc = Tok.getLocation();
254
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000255 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000256 // typedef-name
257 case tok::identifier:
258 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000259 // a type-specifier. Without this check we misparse:
260 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000261 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000262 // It has to be available as a typedef too!
263 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
264 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000265 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000266 TypeRep);
Chris Lattneredc9e392006-12-02 06:21:46 +0000267 break;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000268 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000269 }
270 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000271 default:
272 // If this is not a declaration specifier token, we're done reading decl
273 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000274 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000275 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000276
277 // GNU attributes support.
278 case tok::kw___attribute:
279 ParseAttributes();
Chris Lattnerb95cca02006-10-17 03:01:08 +0000280 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000281
282 // storage-class-specifier
283 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000284 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000285 break;
286 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000287 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000288 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000289 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000290 break;
291 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000292 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000293 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000294 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000295 break;
296 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000297 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000298 break;
299 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000300 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000301 break;
302 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000303 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000304 break;
305
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000306 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000307 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000308 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000309 break;
310 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000311 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000312 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000313 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000314 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000315 break;
316 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000317 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000318 break;
319 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000320 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000321 break;
322 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000323 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000324 break;
325 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000326 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000327 break;
328 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000330 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000331 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000333 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000334 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000336 break;
337 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000339 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000340 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000342 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000343 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000345 break;
346 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000348 break;
349 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000351 break;
352 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000354 break;
355
Chris Lattner1890ac82006-08-13 01:16:23 +0000356 case tok::kw_struct:
357 case tok::kw_union:
358 ParseStructUnionSpecifier(DS);
359 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000360 case tok::kw_enum:
361 ParseEnumSpecifier(DS);
362 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000363
364 // type-qualifier
365 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000366 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
367 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000368 break;
369 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000370 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
371 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000372 break;
373 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000374 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
375 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000376 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000377
378 // function-specifier
379 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000380 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000381 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000382 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000383 // If the specifier combination wasn't legal, issue a diagnostic.
384 if (isInvalid) {
385 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000386 if (isInvalid == 1) // Error.
387 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
388 else // extwarn.
389 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000390 }
391 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000392 }
393}
394
Chris Lattner1890ac82006-08-13 01:16:23 +0000395
396/// ParseStructUnionSpecifier
397/// struct-or-union-specifier: [C99 6.7.2.1]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000398/// struct-or-union identifier[opt] '{' struct-contents '}'
Chris Lattner1890ac82006-08-13 01:16:23 +0000399/// struct-or-union identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000400/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
401/// '}' attributes[opt]
402/// [GNU] struct-or-union attributes[opt] identifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000403/// struct-or-union:
404/// 'struct'
405/// 'union'
Chris Lattner476c3ad2006-08-13 22:09:58 +0000406/// struct-contents:
407/// struct-declaration-list
408/// [EXT] empty
409/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
Chris Lattner1890ac82006-08-13 01:16:23 +0000410/// struct-declaration-list:
Chris Lattner476c3ad2006-08-13 22:09:58 +0000411/// struct-declaration
412/// struct-declaration-list struct-declaration
413/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
414/// struct-declaration:
415/// specifier-qualifier-list struct-declarator-list ';'
416/// [GNU] __extension__ struct-declaration [TODO]
417/// [GNU] specifier-qualifier-list ';' [TODO]
418/// struct-declarator-list:
419/// struct-declarator
420/// struct-declarator-list ',' struct-declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000421/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
Chris Lattner476c3ad2006-08-13 22:09:58 +0000422/// struct-declarator:
423/// declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000424/// [GNU] declarator attributes[opt]
Chris Lattner476c3ad2006-08-13 22:09:58 +0000425/// declarator[opt] ':' constant-expression
Chris Lattnere37e2332006-08-15 04:50:22 +0000426/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000427///
428void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
429 assert((Tok.getKind() == tok::kw_struct ||
430 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
431 bool isUnion = Tok.getKind() == tok::kw_union;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000432 SourceLocation StartLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000433
434 // If attributes exist after tag, parse them.
435 if (Tok.getKind() == tok::kw___attribute)
436 ParseAttributes();
437
Chris Lattner1890ac82006-08-13 01:16:23 +0000438 // Must have either 'struct name' or 'struct {...}'.
439 if (Tok.getKind() != tok::identifier &&
440 Tok.getKind() != tok::l_brace) {
441 Diag(Tok, diag::err_expected_ident_lbrace);
442 return;
443 }
444
445 if (Tok.getKind() == tok::identifier)
446 ConsumeToken();
447
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000448 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000449 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner1890ac82006-08-13 01:16:23 +0000450
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000451 if (Tok.getKind() == tok::r_brace)
452 Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
Chris Lattner1890ac82006-08-13 01:16:23 +0000453
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000454 while (Tok.getKind() != tok::r_brace &&
455 Tok.getKind() != tok::eof) {
456 // Each iteration of this loop reads one struct-declaration.
Chris Lattner1890ac82006-08-13 01:16:23 +0000457
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000458 // Parse the common specifier-qualifiers-list piece.
459 DeclSpec DS;
460 SourceLocation SpecQualLoc = Tok.getLocation();
461 ParseSpecifierQualifierList(DS);
462 // TODO: Does specifier-qualifier list correctly check that *something* is
463 // specified?
464
465 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Chris Lattner1890ac82006-08-13 01:16:23 +0000466
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000467 // If there are no declarators, issue a warning.
468 if (Tok.getKind() == tok::semi) {
469 Diag(SpecQualLoc, diag::w_no_declarators);
470 } else {
471 // Read struct-declarators until we find the semicolon.
472 while (1) {
473 /// struct-declarator: declarator
474 /// struct-declarator: declarator[opt] ':' constant-expression
475 if (Tok.getKind() != tok::colon)
476 ParseDeclarator(DeclaratorInfo);
477
478 if (Tok.getKind() == tok::colon) {
479 ConsumeToken();
480 ExprResult Res = ParseConstantExpression();
481 if (Res.isInvalid) {
482 SkipUntil(tok::semi, true, true);
483 } else {
484 // Process it.
485 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000486 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000487
488 // If attributes exist after the declarator, parse them.
489 if (Tok.getKind() == tok::kw___attribute)
490 ParseAttributes();
Chris Lattner1890ac82006-08-13 01:16:23 +0000491
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000492 // TODO: install declarator.
493
494 // If we don't have a comma, it is either the end of the list (a ';')
495 // or an error, bail out.
496 if (Tok.getKind() != tok::comma)
497 break;
498
499 // Consume the comma.
500 ConsumeToken();
501
502 // Parse the next declarator.
503 DeclaratorInfo.clear();
Chris Lattnere37e2332006-08-15 04:50:22 +0000504
505 // Attributes are only allowed on the second declarator.
506 if (Tok.getKind() == tok::kw___attribute)
507 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000508 }
509 }
510
511 if (Tok.getKind() == tok::semi) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000512 ConsumeToken();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000513 } else {
514 Diag(Tok, diag::err_expected_semi_decl_list);
515 // Skip to end of block or statement
516 SkipUntil(tok::r_brace, true, true);
Chris Lattner1890ac82006-08-13 01:16:23 +0000517 }
518 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000519
Chris Lattner04f80192006-08-15 04:55:54 +0000520 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000521
522 // If attributes exist after struct contents, parse them.
523 if (Tok.getKind() == tok::kw___attribute)
524 ParseAttributes();
Chris Lattnerb8bbad72006-08-13 22:21:02 +0000525 }
Chris Lattnerda72c822006-08-13 22:16:42 +0000526
527 const char *PrevSpec = 0;
528 if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct,
Chris Lattnerb20e8942006-11-28 05:30:29 +0000529 StartLoc, PrevSpec))
530 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000531}
532
533
Chris Lattner3b561a32006-08-13 00:12:11 +0000534/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000535/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000536/// 'enum' identifier[opt] '{' enumerator-list '}'
537/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000538/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
539/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000540/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000541/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000542/// enumerator-list:
543/// enumerator
Chris Lattner1890ac82006-08-13 01:16:23 +0000544/// enumerator-list ',' enumerator
Chris Lattner3b561a32006-08-13 00:12:11 +0000545/// enumerator:
546/// enumeration-constant
Chris Lattner1890ac82006-08-13 01:16:23 +0000547/// enumeration-constant '=' constant-expression
Chris Lattner3b561a32006-08-13 00:12:11 +0000548/// enumeration-constant:
549/// identifier
550///
551void Parser::ParseEnumSpecifier(DeclSpec &DS) {
552 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000553 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000554
Chris Lattnere37e2332006-08-15 04:50:22 +0000555 if (Tok.getKind() == tok::kw___attribute)
556 ParseAttributes();
557
Chris Lattner3b561a32006-08-13 00:12:11 +0000558 // Must have either 'enum name' or 'enum {...}'.
559 if (Tok.getKind() != tok::identifier &&
560 Tok.getKind() != tok::l_brace) {
561 Diag(Tok, diag::err_expected_ident_lbrace);
562 return;
563 }
564
565 if (Tok.getKind() == tok::identifier)
566 ConsumeToken();
567
Chris Lattner0fb8b362006-08-14 01:30:12 +0000568 if (Tok.getKind() == tok::l_brace) {
Chris Lattner04132372006-10-16 06:12:55 +0000569 SourceLocation LBraceLoc = ConsumeBrace();
Chris Lattner3b561a32006-08-13 00:12:11 +0000570
Chris Lattner0fb8b362006-08-14 01:30:12 +0000571 if (Tok.getKind() == tok::r_brace)
572 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
573
574 // Parse the enumerator-list.
575 while (Tok.getKind() == tok::identifier) {
Chris Lattner3b561a32006-08-13 00:12:11 +0000576 ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000577
578 if (Tok.getKind() == tok::equal) {
579 ConsumeToken();
580 ExprResult Res = ParseConstantExpression();
581 if (Res.isInvalid) SkipUntil(tok::comma, true, false);
582 }
583
584 if (Tok.getKind() != tok::comma)
585 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000586 SourceLocation CommaLoc = ConsumeToken();
Chris Lattner0fb8b362006-08-14 01:30:12 +0000587
588 if (Tok.getKind() != tok::identifier && !getLang().C99)
589 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
Chris Lattner3b561a32006-08-13 00:12:11 +0000590 }
591
Chris Lattner0fb8b362006-08-14 01:30:12 +0000592 // Eat the }.
Chris Lattner04f80192006-08-15 04:55:54 +0000593 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnere37e2332006-08-15 04:50:22 +0000594
595 // If attributes exist after the identifier list, parse them.
596 if (Tok.getKind() == tok::kw___attribute)
597 ParseAttributes();
Chris Lattner3b561a32006-08-13 00:12:11 +0000598 }
599 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000600
601
602 const char *PrevSpec = 0;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000603 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec))
604 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000605}
606
607
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000608/// isTypeSpecifierQualifier - Return true if the current token could be the
609/// start of a specifier-qualifier-list.
610bool Parser::isTypeSpecifierQualifier() const {
611 switch (Tok.getKind()) {
612 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000613 // GNU attributes support.
614 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000615 // type-specifiers
616 case tok::kw_short:
617 case tok::kw_long:
618 case tok::kw_signed:
619 case tok::kw_unsigned:
620 case tok::kw__Complex:
621 case tok::kw__Imaginary:
622 case tok::kw_void:
623 case tok::kw_char:
624 case tok::kw_int:
625 case tok::kw_float:
626 case tok::kw_double:
627 case tok::kw__Bool:
628 case tok::kw__Decimal32:
629 case tok::kw__Decimal64:
630 case tok::kw__Decimal128:
631
632 // struct-or-union-specifier
633 case tok::kw_struct:
634 case tok::kw_union:
635 // enum-specifier
636 case tok::kw_enum:
637
638 // type-qualifier
639 case tok::kw_const:
640 case tok::kw_volatile:
641 case tok::kw_restrict:
642 return true;
643
644 // typedef-name
645 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000646 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000647
648 // TODO: Attributes.
649 }
650}
651
Chris Lattneracd58a32006-08-06 17:24:14 +0000652/// isDeclarationSpecifier() - Return true if the current token is part of a
653/// declaration specifier.
654bool Parser::isDeclarationSpecifier() const {
655 switch (Tok.getKind()) {
656 default: return false;
657 // storage-class-specifier
658 case tok::kw_typedef:
659 case tok::kw_extern:
660 case tok::kw_static:
661 case tok::kw_auto:
662 case tok::kw_register:
663 case tok::kw___thread:
664
665 // type-specifiers
666 case tok::kw_short:
667 case tok::kw_long:
668 case tok::kw_signed:
669 case tok::kw_unsigned:
670 case tok::kw__Complex:
671 case tok::kw__Imaginary:
672 case tok::kw_void:
673 case tok::kw_char:
674 case tok::kw_int:
675 case tok::kw_float:
676 case tok::kw_double:
677 case tok::kw__Bool:
678 case tok::kw__Decimal32:
679 case tok::kw__Decimal64:
680 case tok::kw__Decimal128:
681
682 // struct-or-union-specifier
683 case tok::kw_struct:
684 case tok::kw_union:
685 // enum-specifier
686 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000687
Chris Lattneracd58a32006-08-06 17:24:14 +0000688 // type-qualifier
689 case tok::kw_const:
690 case tok::kw_volatile:
691 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000692
Chris Lattneracd58a32006-08-06 17:24:14 +0000693 // function-specifier
694 case tok::kw_inline:
695 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000696
Chris Lattneracd58a32006-08-06 17:24:14 +0000697 // typedef-name
698 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000699 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000700 // TODO: Attributes.
701 }
702}
703
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000704
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000705/// ParseTypeQualifierListOpt
706/// type-qualifier-list: [C99 6.7.5]
707/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000708/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000709/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000710/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000711///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000712void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000713 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000714 int isInvalid = false;
715 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000716 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000717
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000718 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000719 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000720 // If this is not a type-qualifier token, we're done reading type
721 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000722 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000723 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000724 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000725 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
726 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000727 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000728 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000729 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
730 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000731 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000732 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000733 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
734 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000735 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000736
737 case tok::kw___attribute:
738 ParseAttributes();
739 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000740 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000741
742 // If the specifier combination wasn't legal, issue a diagnostic.
743 if (isInvalid) {
744 assert(PrevSpec && "Method did not return previous specifier!");
745 if (isInvalid == 1) // Error.
746 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
747 else // extwarn.
748 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
749 }
750 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000751 }
752}
753
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000754
755/// ParseDeclarator - Parse and verify a newly-initialized declarator.
756///
757void Parser::ParseDeclarator(Declarator &D) {
758 /// This implements the 'declarator' production in the C grammar, then checks
759 /// for well-formedness and issues diagnostics.
760 ParseDeclaratorInternal(D);
761
Chris Lattner9fab3b92006-08-12 18:25:42 +0000762 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000763
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000764}
765
766/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000767/// declarator: [C99 6.7.5]
768/// pointer[opt] direct-declarator
769///
770/// pointer: [C99 6.7.5]
771/// '*' type-qualifier-list[opt]
772/// '*' type-qualifier-list[opt] pointer
773///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000774void Parser::ParseDeclaratorInternal(Declarator &D) {
Chris Lattner6c7416c2006-08-07 00:19:33 +0000775 if (Tok.getKind() != tok::star)
776 return ParseDirectDeclarator(D);
777
778 // Otherwise, '*' -> pointer.
Chris Lattneraf635312006-10-16 06:06:51 +0000779 SourceLocation Loc = ConsumeToken(); // Eat the *.
Chris Lattner6c7416c2006-08-07 00:19:33 +0000780 DeclSpec DS;
781 ParseTypeQualifierListOpt(DS);
782
783 // Recursively parse the declarator.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000784 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000785
Chris Lattner6c7416c2006-08-07 00:19:33 +0000786 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +0000787 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
Chris Lattner6c7416c2006-08-07 00:19:33 +0000788}
789
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000790
791/// ParseDirectDeclarator
792/// direct-declarator: [C99 6.7.5]
793/// identifier
794/// '(' declarator ')'
795/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +0000796/// [C90] direct-declarator '[' constant-expression[opt] ']'
797/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
798/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
799/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
800/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000801/// direct-declarator '(' parameter-type-list ')'
802/// direct-declarator '(' identifier-list[opt] ')'
803/// [GNU] direct-declarator '(' parameter-forward-declarations
804/// parameter-type-list[opt] ')'
805///
Chris Lattneracd58a32006-08-06 17:24:14 +0000806void Parser::ParseDirectDeclarator(Declarator &D) {
807 // Parse the first direct-declarator seen.
808 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
809 assert(Tok.getIdentifierInfo() && "Not an identifier?");
810 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
811 ConsumeToken();
812 } else if (Tok.getKind() == tok::l_paren) {
813 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000814 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000815 // Example: 'char (*X)' or 'int (*XX)(void)'
816 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000817 } else if (D.mayOmitIdentifier()) {
818 // This could be something simple like "int" (in which case the declarator
819 // portion is empty), if an abstract-declarator is allowed.
820 D.SetIdentifier(0, Tok.getLocation());
821 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +0000822 // Expected identifier or '('.
823 Diag(Tok, diag::err_expected_ident_lparen);
824 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +0000825 }
826
827 assert(D.isPastIdentifier() &&
828 "Haven't past the location of the identifier yet?");
829
830 while (1) {
831 if (Tok.getKind() == tok::l_paren) {
832 ParseParenDeclarator(D);
833 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000834 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +0000835 } else {
836 break;
837 }
838 }
839}
840
841/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
842/// either be before the identifier (in which case these are just grouping
843/// parens for precedence) or it may be after the identifier, in which case
844/// these are function arguments.
845///
846/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000847/// parameter-type-list: [C99 6.7.5]
848/// parameter-list
849/// parameter-list ',' '...'
850///
851/// parameter-list: [C99 6.7.5]
852/// parameter-declaration
853/// parameter-list ',' parameter-declaration
854///
855/// parameter-declaration: [C99 6.7.5]
856/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +0000857/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000858/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000859/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000860///
861/// identifier-list: [C99 6.7.5]
862/// identifier
863/// identifier-list ',' identifier
864///
Chris Lattneracd58a32006-08-06 17:24:14 +0000865void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +0000866 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000867
Chris Lattneracd58a32006-08-06 17:24:14 +0000868 // If we haven't past the identifier yet (or where the identifier would be
869 // stored, if this is an abstract declarator), then this is probably just
870 // grouping parens.
871 if (!D.isPastIdentifier()) {
872 // Okay, this is probably a grouping paren. However, if this could be an
873 // abstract-declarator, then this could also be the start of function
874 // arguments (consider 'void()').
875 bool isGrouping;
876
877 if (!D.mayOmitIdentifier()) {
878 // If this can't be an abstract-declarator, this *must* be a grouping
879 // paren, because we haven't seen the identifier yet.
880 isGrouping = true;
881 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
882 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000883 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
884 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +0000885 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000886 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000887 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000888 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000889 }
Chris Lattneracd58a32006-08-06 17:24:14 +0000890
891 // If this is a grouping paren, handle:
892 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +0000893 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +0000894 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +0000895 if (Tok.getKind() == tok::kw___attribute)
896 ParseAttributes();
897
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000898 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +0000899 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000900 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +0000901 return;
902 }
903
904 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +0000905 // argument list. Recognize that this declarator will never have an
906 // identifier (and remember where it would have been), then fall through to
907 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +0000908 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000909 }
910
Chris Lattneracd58a32006-08-06 17:24:14 +0000911 // Okay, this is the parameter list of a function definition, or it is an
912 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000913 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +0000914 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +0000915 bool ErrorEmitted = false;
916
Chris Lattneredc9e392006-12-02 06:21:46 +0000917 // Build up an array of information about the parsed arguments.
Chris Lattnercbc426d2006-12-02 06:43:02 +0000918 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneredc9e392006-12-02 06:21:46 +0000919
Chris Lattneracd58a32006-08-06 17:24:14 +0000920 if (Tok.getKind() == tok::r_paren) {
921 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000922 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000923 HasPrototype = false;
924 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +0000925 // K&R identifier lists can't have typedefs as identifiers, per
926 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000927 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +0000928 // Identifier list. Note that '(' identifier-list ')' is only allowed for
929 // normal declarators, not for abstract-declarators.
930 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
931
932 // If there was no identifier specified, either we are in an
933 // abstract-declarator, or we are in a parameter declarator which was found
934 // to be abstract. In abstract-declarators, identifier lists are not valid,
935 // diagnose this.
936 if (!D.getIdentifier())
937 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +0000938
Chris Lattnercbc426d2006-12-02 06:43:02 +0000939 // Remember this identifier in ParamInfo.
940 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
941 Tok.getLocation(), 0));
942
Chris Lattneracd58a32006-08-06 17:24:14 +0000943 ConsumeToken();
944 while (Tok.getKind() == tok::comma) {
945 // Eat the comma.
946 ConsumeToken();
947
Chris Lattnercbc426d2006-12-02 06:43:02 +0000948 if (Tok.getKind() != tok::identifier) {
949 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +0000950 ErrorEmitted = true;
951 break;
952 }
Chris Lattnercbc426d2006-12-02 06:43:02 +0000953
954 // Remember this identifier in ParamInfo.
955 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
956 Tok.getLocation(), 0));
957
958 // Eat the identifier.
959 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +0000960 }
961
Chris Lattneracd58a32006-08-06 17:24:14 +0000962 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000963 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000964 HasPrototype = false;
965 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +0000966 // Finally, a normal, non-empty parameter type list.
967
Chris Lattnercbc426d2006-12-02 06:43:02 +0000968 // Enter function-declaration scope, limiting any declarators for struct
969 // tags to the function prototype scope.
970 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +0000971 EnterScope(0);
972
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000973 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +0000974 while (1) {
975 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000976 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +0000977
978 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +0000979 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +0000980 // Otherwise, parse parameter type list. If it starts with an
981 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +0000982 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000983 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +0000984 }
985
986 // Consume the ellipsis.
987 ConsumeToken();
988 break;
989 }
990
Chris Lattneracd58a32006-08-06 17:24:14 +0000991 // Parse the declaration-specifiers.
992 DeclSpec DS;
993 ParseDeclarationSpecifiers(DS);
994
995 // Parse the declarator. This is "PrototypeContext", because we must
996 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +0000997 Declarator ParmDecl(DS, Declarator::PrototypeContext);
998 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +0000999
Chris Lattnere37e2332006-08-15 04:50:22 +00001000 // Parse GNU attributes, if present.
1001 if (Tok.getKind() == tok::kw___attribute)
1002 ParseAttributes();
1003
Chris Lattner43e956c2006-11-28 04:05:37 +00001004 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattnerf055d432006-11-28 04:28:12 +00001005 switch (DS.getStorageClassSpec()) {
Chris Lattner43e956c2006-11-28 04:05:37 +00001006 case DeclSpec::SCS_unspecified:
1007 case DeclSpec::SCS_register:
1008 break;
1009 case DeclSpec::SCS_auto:
1010 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1011 default:
Chris Lattner4d8f8732006-11-28 05:05:08 +00001012 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001013 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001014 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001015 break;
1016 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001017 if (DS.isThreadSpecified()) {
1018 Diag(DS.getThreadSpecLoc(),
1019 diag::err_invalid_storage_class_in_func_decl);
1020 DS.ClearStorageClassSpecs();
1021 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001022
1023 // Inform the actions module about the parameter declarator, so it gets
1024 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001025 Action::TypeResult ParamTy =
1026 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001027
1028 // Remember this parsed parameter in ParamInfo.
1029 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmDecl.getIdentifier(),
1030 ParmDecl.getIdentifierLoc(),
1031 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001032
1033 // If the next token is a comma, consume it and keep reading arguments.
1034 if (Tok.getKind() != tok::comma) break;
1035
1036 // Consume the comma.
1037 ConsumeToken();
1038 }
1039
1040 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001041
1042 // Leave prototype scope.
1043 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001044 }
1045
Chris Lattnercbc426d2006-12-02 06:43:02 +00001046 DeclaratorChunk::ParamInfo *ParamArray = 0;
Chris Lattneredc9e392006-12-02 06:21:46 +00001047 if (!ParamInfo.empty()) {
Chris Lattnercbc426d2006-12-02 06:43:02 +00001048 ParamArray = new DeclaratorChunk::ParamInfo[ParamInfo.size()];
Chris Lattneredc9e392006-12-02 06:21:46 +00001049 memcpy(ParamArray, &ParamInfo[0], sizeof(ParamInfo[0])*ParamInfo.size());
1050 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001051
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001052 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001053 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1054 ParamInfo.size(), ParamArray,
1055 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001056
Chris Lattner14776b92006-08-06 22:27:40 +00001057
1058 // If we have the closing ')', eat it and we're done.
1059 if (Tok.getKind() == tok::r_paren) {
1060 ConsumeParen();
1061 } else {
1062 // If an error happened earlier parsing something else in the proto, don't
1063 // issue another error.
1064 if (!ErrorEmitted)
1065 Diag(Tok, diag::err_expected_rparen);
1066 SkipUntil(tok::r_paren);
1067 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001068}
Chris Lattneracd58a32006-08-06 17:24:14 +00001069
Chris Lattnere8074e62006-08-06 18:30:15 +00001070
1071/// [C90] direct-declarator '[' constant-expression[opt] ']'
1072/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1073/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1074/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1075/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1076void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001077 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001078
1079 // If valid, this location is the position where we read the 'static' keyword.
1080 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001081 if (Tok.getKind() == tok::kw_static)
1082 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001083
1084 // If there is a type-qualifier-list, read it now.
1085 DeclSpec DS;
1086 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001087
1088 // If we haven't already read 'static', check to see if there is one after the
1089 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001090 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1091 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001092
1093 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001094 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001095 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001096 if (Tok.getKind() == tok::star) {
1097 // Remember the '*' token, in case we have to un-get it.
1098 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001099 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001100
1101 // Check that the ']' token is present to avoid incorrectly parsing
1102 // expressions starting with '*' as [*].
1103 if (Tok.getKind() == tok::r_square) {
1104 if (StaticLoc.isValid())
1105 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1106 StaticLoc = SourceLocation(); // Drop the static.
1107 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001108 } else {
1109 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001110 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001111 // need to reparse it. This handles cases like 'X[*p + 4]'
1112 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001113 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001114 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001115 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001116 NumElements = ParseAssignmentExpression();
1117 }
1118
1119 // If there was an error parsing the assignment-expression, recover.
1120 if (NumElements.isInvalid) {
1121 // If the expression was invalid, skip it.
1122 SkipUntil(tok::r_square);
1123 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001124 }
1125
Chris Lattner04f80192006-08-15 04:55:54 +00001126 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001127
Chris Lattnere8074e62006-08-06 18:30:15 +00001128 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1129 // it was not a constant expression.
1130 if (!getLang().C99) {
1131 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001132 if (isStar || StaticLoc.isValid() ||
1133 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001134 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001135 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001136
1137 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001138 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1139 StaticLoc.isValid(), isStar,
1140 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001141}
1142