blob: d1dde24241773fdf93f5c787a531a1f01eede10a [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 Lattnerad9ac942007-01-23 01:14:52 +000016#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000017using 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
Steve Naroff0f2fe172007-06-01 17:11:19 +000053/// attrib-name
54/// attrib-name '(' identifier ')'
55/// attrib-name '(' identifier ',' nonempty-expr-list ')'
56/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000057///
Steve Naroff0f2fe172007-06-01 17:11:19 +000058/// [GNU] attrib-name:
59/// identifier
60/// typespec
61/// typequal
62/// storageclass
63///
64/// FIXME: The GCC grammar/code for this construct implies we need two
65/// token lookahead. Comment from gcc: "If they start with an identifier
66/// which is followed by a comma or close parenthesis, then the arguments
67/// start with that identifier; otherwise they are an expression list."
68///
69/// At the moment, I am not doing 2 token lookahead. I am also unaware of
70/// any attributes that don't work (based on my limited testing). Most
71/// attributes are very simple in practice. Until we find a bug, I don't see
72/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000073
Steve Naroffb8371e12007-06-09 03:39:29 +000074AttributeList *Parser::ParseAttributes() {
Steve Naroff0f2fe172007-06-01 17:11:19 +000075 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
76
Steve Naroffb8371e12007-06-09 03:39:29 +000077 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000078
79 while (Tok.getKind() == tok::kw___attribute) {
80 ConsumeToken();
81 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
82 "attribute")) {
83 SkipUntil(tok::r_paren, true); // skip until ) or ;
84 return CurrAttr;
85 }
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
87 SkipUntil(tok::r_paren, true); // skip until ) or ;
88 return CurrAttr;
89 }
90 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
91 while (Tok.getKind() == tok::identifier || isDeclarationSpecifier() ||
92 Tok.getKind() == tok::comma) {
93
94 if (Tok.getKind() == tok::comma) {
95 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
96 ConsumeToken();
97 continue;
98 }
99 // we have an identifier or declaration specifier (const, int, etc.)
100 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
101 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000102
103 // check if we have a "paramterized" attribute
104 if (Tok.getKind() == tok::l_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000105 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106
107 if (Tok.getKind() == tok::identifier) {
108 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
109 SourceLocation ParmLoc = ConsumeToken();
110
111 if (Tok.getKind() == tok::r_paren) {
112 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000113 ConsumeParen(); // ignore the right paren loc for now
114 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
115 ParmName, ParmLoc, 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 } else if (Tok.getKind() == tok::comma) {
117 ConsumeToken();
118 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000119 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 bool ArgExprsOk = true;
121
122 // now parse the non-empty comma separated list of expressions
123 while (1) {
124 ExprResult ArgExpr = ParseAssignmentExpression();
125 if (ArgExpr.isInvalid) {
126 ArgExprsOk = false;
127 SkipUntil(tok::r_paren);
128 break;
129 } else {
130 ArgExprs.push_back(ArgExpr.Val);
131 }
132 if (Tok.getKind() != tok::comma)
133 break;
134 ConsumeToken(); // Eat the comma, move to the next argument
135 }
136 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000137 ConsumeParen(); // ignore the right paren loc for now
138 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
139 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000140 }
141 }
142 } else { // not an identifier
143 // parse a possibly empty comma separated list of expressions
144 if (Tok.getKind() == tok::r_paren) {
145 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
148 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000149 } else {
150 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000151 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 bool ArgExprsOk = true;
153
154 // now parse the list of expressions
155 while (1) {
156 ExprResult ArgExpr = ParseAssignmentExpression();
157 if (ArgExpr.isInvalid) {
158 ArgExprsOk = false;
159 SkipUntil(tok::r_paren);
160 break;
161 } else {
162 ArgExprs.push_back(ArgExpr.Val);
163 }
164 if (Tok.getKind() != tok::comma)
165 break;
166 ConsumeToken(); // Eat the comma, move to the next argument
167 }
168 // Match the ')'.
169 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000170 ConsumeParen(); // ignore the right paren loc for now
171 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
172 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
173 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174 }
175 }
176 }
177 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000180 }
181 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000182 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
183 SkipUntil(tok::r_paren, false);
184 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
185 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000186 }
187 return CurrAttr;
188}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000189
Chris Lattner53361ac2006-08-10 05:19:57 +0000190/// ParseDeclaration - Parse a full 'declaration', which consists of
191/// declaration-specifiers, some number of declarators, and a semicolon.
192/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000193///
194/// declaration: [C99 6.7]
195/// block-declaration ->
196/// simple-declaration
197/// others [FIXME]
198/// [C++] namespace-definition
199/// others... [FIXME]
200///
Chris Lattner302b4be2006-11-19 02:31:38 +0000201Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnera5235172007-08-25 06:57:03 +0000202 switch (Tok.getKind()) {
203 case tok::kw_namespace:
204 return ParseNamespace(Context);
205 default:
206 return ParseSimpleDeclaration(Context);
207 }
208}
209
210/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
211/// declaration-specifiers init-declarator-list[opt] ';'
212///[C90/C++]init-declarator-list ';' [TODO]
213/// [OMP] threadprivate-directive [TODO]
214Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000215 // Parse the common declaration-specifiers piece.
216 DeclSpec DS;
217 ParseDeclarationSpecifiers(DS);
218
Chris Lattner0e894622006-08-13 19:58:17 +0000219 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
220 // declaration-specifiers init-declarator-list[opt] ';'
221 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +0000222 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000223 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000224 }
225
Chris Lattner53361ac2006-08-10 05:19:57 +0000226 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
227 ParseDeclarator(DeclaratorInfo);
228
Chris Lattner302b4be2006-11-19 02:31:38 +0000229 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000230}
231
Chris Lattnera5235172007-08-25 06:57:03 +0000232
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000233/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
234/// parsing 'declaration-specifiers declarator'. This method is split out this
235/// way to handle the ambiguity between top-level function-definitions and
236/// declarations.
237///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000238/// init-declarator-list: [C99 6.7]
239/// init-declarator
240/// init-declarator-list ',' init-declarator
241/// init-declarator: [C99 6.7]
242/// declarator
243/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000244/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
245/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000246///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000247Parser::DeclTy *Parser::
248ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
249
250 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
251 // that they can be chained properly if the actions want this.
252 Parser::DeclTy *LastDeclInGroup = 0;
253
Chris Lattner53361ac2006-08-10 05:19:57 +0000254 // At this point, we know that it is not a function definition. Parse the
255 // rest of the init-declarator-list.
256 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000257 // If a simple-asm-expr is present, parse it.
258 if (Tok.getKind() == tok::kw_asm)
259 ParseSimpleAsm();
260
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000261 // If attributes are present, parse them.
262 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000263 D.AddAttributes(ParseAttributes());
Chris Lattner6d7e6342006-08-15 03:41:14 +0000264
Chris Lattner53361ac2006-08-10 05:19:57 +0000265 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000266 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000267 if (Tok.getKind() == tok::equal) {
268 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000269 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000270 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000271 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000272 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000273 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000274 }
275
Chris Lattner697e5d62006-11-09 06:32:27 +0000276 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000277 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000278 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
279 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000280
281 // If we don't have a comma, it is either the end of the list (a ';') or an
282 // error, bail out.
283 if (Tok.getKind() != tok::comma)
284 break;
285
286 // Consume the comma.
287 ConsumeToken();
288
289 // Parse the next declarator.
290 D.clear();
291 ParseDeclarator(D);
292 }
293
294 if (Tok.getKind() == tok::semi) {
295 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000296 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000297 }
Chris Lattner776fac82007-06-09 00:53:06 +0000298
299 Diag(Tok, diag::err_parse_error);
300 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000301 SkipUntil(tok::r_brace, true, true);
Chris Lattner776fac82007-06-09 00:53:06 +0000302 if (Tok.getKind() == tok::semi)
303 ConsumeToken();
304 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000305}
306
Chris Lattner1890ac82006-08-13 01:16:23 +0000307/// ParseSpecifierQualifierList
308/// specifier-qualifier-list:
309/// type-specifier specifier-qualifier-list[opt]
310/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000311/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000312///
313void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
314 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
315 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000316 ParseDeclarationSpecifiers(DS);
317
318 // Validate declspec for type-name.
319 unsigned Specs = DS.getParsedSpecifiers();
320 if (Specs == DeclSpec::PQ_None)
321 Diag(Tok, diag::err_typename_requires_specqual);
322
Chris Lattner1b22eed2006-11-28 05:12:07 +0000323 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000324 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000325 if (DS.getStorageClassSpecLoc().isValid())
326 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
327 else
328 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000329 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000330 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000331
332 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000333 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000334 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000335 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000336 }
337}
Chris Lattner53361ac2006-08-10 05:19:57 +0000338
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000339/// ParseDeclarationSpecifiers
340/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000341/// storage-class-specifier declaration-specifiers[opt]
342/// type-specifier declaration-specifiers[opt]
343/// type-qualifier declaration-specifiers[opt]
344/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000345/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000346///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000347/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000348/// 'typedef'
349/// 'extern'
350/// 'static'
351/// 'auto'
352/// 'register'
353/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000354/// type-specifier: [C99 6.7.2]
355/// 'void'
356/// 'char'
357/// 'short'
358/// 'int'
359/// 'long'
360/// 'float'
361/// 'double'
362/// 'signed'
363/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000364/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000365/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000366/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000367/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000368/// [C99] '_Bool'
369/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000370/// [C99] '_Imaginary' // Removed in TC2?
371/// [GNU] '_Decimal32'
372/// [GNU] '_Decimal64'
373/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000374/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000375/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000376/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000377/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000378/// 'const'
379/// 'volatile'
380/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000381/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000382/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000383///
384void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner02c04392007-07-25 00:24:17 +0000385 DS.Range.setBegin(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000386 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000387 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000388 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000389 SourceLocation Loc = Tok.getLocation();
390
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000391 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000392 // typedef-name
393 case tok::identifier:
394 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000395 // a type-specifier. Without this check we misparse:
396 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000397 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000398 // It has to be available as a typedef too!
399 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
400 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000401 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000402 TypeRep);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000403 if (isInvalid)
404 break;
405 else { // FIXME: restrict this to "id" and ObjC classnames.
406 DS.Range.setEnd(Tok.getLocation());
407 ConsumeToken(); // The identifier
408 if (Tok.getKind() == tok::less)
409 ParseObjCProtocolReferences();
410 continue;
411 }
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000412 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000413 }
414 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000415 default:
416 // If this is not a declaration specifier token, we're done reading decl
417 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000418 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000419 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000420
421 // GNU attributes support.
422 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000423 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000424 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000425
426 // storage-class-specifier
427 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000428 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000429 break;
430 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000431 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000432 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000433 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000434 break;
435 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000436 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000437 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000438 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000439 break;
440 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000441 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000442 break;
443 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000444 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000445 break;
446 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000447 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000448 break;
449
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000450 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000451 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000452 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000453 break;
454 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000455 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000456 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000457 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000458 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000459 break;
460 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000461 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000462 break;
463 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000464 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000465 break;
466 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000467 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000468 break;
469 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000470 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000471 break;
472 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000473 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000474 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000475 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000476 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000477 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000478 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000480 break;
481 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000483 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000484 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000486 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000487 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000488 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000489 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000490 break;
491 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000492 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000493 break;
494 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000495 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000496 break;
497 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000499 break;
500
Chris Lattner1890ac82006-08-13 01:16:23 +0000501 case tok::kw_struct:
502 case tok::kw_union:
503 ParseStructUnionSpecifier(DS);
504 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000505 case tok::kw_enum:
506 ParseEnumSpecifier(DS);
507 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000508
Steve Naroffad373bd2007-07-31 12:34:36 +0000509 // GNU typeof support.
510 case tok::kw_typeof:
511 ParseTypeofSpecifier(DS);
512 continue;
513
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000514 // type-qualifier
515 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000516 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
517 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000518 break;
519 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000520 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
521 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000522 break;
523 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000524 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
525 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000526 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000527
528 // function-specifier
529 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000530 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000531 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000532 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000533 // If the specifier combination wasn't legal, issue a diagnostic.
534 if (isInvalid) {
535 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000536 if (isInvalid == 1) // Error.
537 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
538 else // extwarn.
539 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000540 }
Chris Lattner02c04392007-07-25 00:24:17 +0000541 DS.Range.setEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000542 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000543 }
544}
545
Chris Lattnerffbc2712007-01-25 06:05:38 +0000546/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
547/// the first token has already been read and has been turned into an instance
548/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
549/// otherwise it returns false and fills in Decl.
550bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000551 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000552 // If attributes exist after tag, parse them.
553 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000554 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000555
Chris Lattner1890ac82006-08-13 01:16:23 +0000556 // Must have either 'struct name' or 'struct {...}'.
557 if (Tok.getKind() != tok::identifier &&
558 Tok.getKind() != tok::l_brace) {
559 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000560
561 // Skip the rest of this declarator, up until the comma or semicolon.
562 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000563 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000564 }
565
Chris Lattner8c6519a2007-01-22 07:41:36 +0000566 // If an identifier is present, consume and remember it.
567 IdentifierInfo *Name = 0;
568 SourceLocation NameLoc;
569 if (Tok.getKind() == tok::identifier) {
570 Name = Tok.getIdentifierInfo();
571 NameLoc = ConsumeToken();
572 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000573
Chris Lattner8c6519a2007-01-22 07:41:36 +0000574 // There are three options here. If we have 'struct foo;', then this is a
575 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000576 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000577 //
578 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
579 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
580 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
581 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000582 Action::TagKind TK;
583 if (Tok.getKind() == tok::l_brace)
584 TK = Action::TK_Definition;
585 else if (Tok.getKind() == tok::semi)
586 TK = Action::TK_Declaration;
587 else
588 TK = Action::TK_Reference;
Steve Naroffb8371e12007-06-09 03:39:29 +0000589 Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000590 return false;
591}
592
593
594/// ParseStructUnionSpecifier
595/// struct-or-union-specifier: [C99 6.7.2.1]
596/// struct-or-union identifier[opt] '{' struct-contents '}'
597/// struct-or-union identifier
598/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
599/// '}' attributes[opt]
600/// [GNU] struct-or-union attributes[opt] identifier
601/// struct-or-union:
602/// 'struct'
603/// 'union'
604///
605void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
606 assert((Tok.getKind() == tok::kw_struct ||
607 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
608 DeclSpec::TST TagType =
609 Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
610 SourceLocation StartLoc = ConsumeToken();
611
612 // Parse the tag portion of this.
613 DeclTy *TagDecl;
614 if (ParseTag(TagDecl, TagType, StartLoc))
615 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000616
Chris Lattner90a26b02007-01-23 04:38:16 +0000617 // If there is a body, parse it and inform the actions module.
618 if (Tok.getKind() == tok::l_brace)
Chris Lattner1300fb92007-01-23 23:42:53 +0000619 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000620
621 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000622 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000623 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000624}
625
Steve Naroff97170802007-08-20 22:28:22 +0000626/// ParseStructDeclaration
Chris Lattner90a26b02007-01-23 04:38:16 +0000627/// struct-declaration:
628/// specifier-qualifier-list struct-declarator-list ';'
Chris Lattner736ed5d2007-06-09 05:59:07 +0000629/// [GNU] __extension__ struct-declaration
630/// [GNU] specifier-qualifier-list ';'
Chris Lattner90a26b02007-01-23 04:38:16 +0000631/// struct-declarator-list:
632/// struct-declarator
633/// struct-declarator-list ',' struct-declarator
634/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
635/// struct-declarator:
636/// declarator
637/// [GNU] declarator attributes[opt]
638/// declarator[opt] ':' constant-expression
639/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
640///
Steve Naroff97170802007-08-20 22:28:22 +0000641void Parser::ParseStructDeclaration(DeclTy *TagDecl,
642 llvm::SmallVector<DeclTy*, 32> &FieldDecls) {
643 // FIXME: When __extension__ is specified, disable extension diagnostics.
644 if (Tok.getKind() == tok::kw___extension__)
645 ConsumeToken();
646
647 // Parse the common specifier-qualifiers-list piece.
648 DeclSpec DS;
649 SourceLocation SpecQualLoc = Tok.getLocation();
650 ParseSpecifierQualifierList(DS);
651 // TODO: Does specifier-qualifier list correctly check that *something* is
652 // specified?
653
654 // If there are no declarators, issue a warning.
655 if (Tok.getKind() == tok::semi) {
656 Diag(SpecQualLoc, diag::w_no_declarators);
657 ConsumeToken();
658 return;
659 }
660
661 // Read struct-declarators until we find the semicolon.
662 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
663
664 while (1) {
665 /// struct-declarator: declarator
666 /// struct-declarator: declarator[opt] ':' constant-expression
667 if (Tok.getKind() != tok::colon)
668 ParseDeclarator(DeclaratorInfo);
669
670 ExprTy *BitfieldSize = 0;
671 if (Tok.getKind() == tok::colon) {
672 ConsumeToken();
673 ExprResult Res = ParseConstantExpression();
674 if (Res.isInvalid) {
675 SkipUntil(tok::semi, true, true);
676 } else {
677 BitfieldSize = Res.Val;
678 }
679 }
680
681 // If attributes exist after the declarator, parse them.
682 if (Tok.getKind() == tok::kw___attribute)
683 DeclaratorInfo.AddAttributes(ParseAttributes());
684
685 // Install the declarator into the current TagDecl.
686 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
687 DeclaratorInfo, BitfieldSize);
688 FieldDecls.push_back(Field);
689
690 // If we don't have a comma, it is either the end of the list (a ';')
691 // or an error, bail out.
692 if (Tok.getKind() != tok::comma)
693 break;
694
695 // Consume the comma.
696 ConsumeToken();
697
698 // Parse the next declarator.
699 DeclaratorInfo.clear();
700
701 // Attributes are only allowed on the second declarator.
702 if (Tok.getKind() == tok::kw___attribute)
703 DeclaratorInfo.AddAttributes(ParseAttributes());
704 }
705 return;
706}
707
708/// ParseStructUnionBody
709/// struct-contents:
710/// struct-declaration-list
711/// [EXT] empty
712/// [GNU] "struct-declaration-list" without terminatoring ';'
713/// struct-declaration-list:
714/// struct-declaration
715/// struct-declaration-list struct-declaration
716/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
717///
Chris Lattner1300fb92007-01-23 23:42:53 +0000718void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
719 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000720 SourceLocation LBraceLoc = ConsumeBrace();
721
Chris Lattner7b9ace62007-01-23 20:11:08 +0000722 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
723 // C++.
Chris Lattner90a26b02007-01-23 04:38:16 +0000724 if (Tok.getKind() == tok::r_brace)
725 Diag(Tok, diag::ext_empty_struct_union_enum,
726 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000727
Chris Lattner23b7eb62007-06-15 23:05:46 +0000728 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner1300fb92007-01-23 23:42:53 +0000729
Chris Lattner7b9ace62007-01-23 20:11:08 +0000730 // While we still have something to read, read the declarations in the struct.
Chris Lattner90a26b02007-01-23 04:38:16 +0000731 while (Tok.getKind() != tok::r_brace &&
732 Tok.getKind() != tok::eof) {
733 // Each iteration of this loop reads one struct-declaration.
734
Chris Lattner736ed5d2007-06-09 05:59:07 +0000735 // Check for extraneous top-level semicolon.
Chris Lattner36e46a22007-06-09 05:49:55 +0000736 if (Tok.getKind() == tok::semi) {
737 Diag(Tok, diag::ext_extra_struct_semi);
738 ConsumeToken();
739 continue;
740 }
Steve Naroff97170802007-08-20 22:28:22 +0000741 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner736ed5d2007-06-09 05:59:07 +0000742
Chris Lattner90a26b02007-01-23 04:38:16 +0000743 if (Tok.getKind() == tok::semi) {
744 ConsumeToken();
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000745 } else if (Tok.getKind() == tok::r_brace) {
746 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
747 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000748 } else {
749 Diag(Tok, diag::err_expected_semi_decl_list);
750 // Skip to end of block or statement
751 SkipUntil(tok::r_brace, true, true);
752 }
753 }
754
755 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
756
Chris Lattnerc1915e22007-01-25 07:29:02 +0000757 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
758
Steve Naroffb8371e12007-06-09 03:39:29 +0000759 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000760 // If attributes exist after struct contents, parse them.
761 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000762 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000763}
764
765
Chris Lattner3b561a32006-08-13 00:12:11 +0000766/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000767/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000768/// 'enum' identifier[opt] '{' enumerator-list '}'
769/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000770/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
771/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000772/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000773/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000774void Parser::ParseEnumSpecifier(DeclSpec &DS) {
775 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000776 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000777
Chris Lattnerffbc2712007-01-25 06:05:38 +0000778 // Parse the tag portion of this.
779 DeclTy *TagDecl;
780 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000781 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000782
Chris Lattnerc1915e22007-01-25 07:29:02 +0000783 if (Tok.getKind() == tok::l_brace)
784 ParseEnumBody(StartLoc, TagDecl);
785
Chris Lattner3b561a32006-08-13 00:12:11 +0000786 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000787 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000788 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000789 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000790}
791
Chris Lattnerc1915e22007-01-25 07:29:02 +0000792/// ParseEnumBody - Parse a {} enclosed enumerator-list.
793/// enumerator-list:
794/// enumerator
795/// enumerator-list ',' enumerator
796/// enumerator:
797/// enumeration-constant
798/// enumeration-constant '=' constant-expression
799/// enumeration-constant:
800/// identifier
801///
802void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
803 SourceLocation LBraceLoc = ConsumeBrace();
804
805 if (Tok.getKind() == tok::r_brace)
806 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
807
Chris Lattner23b7eb62007-06-15 23:05:46 +0000808 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000809
Chris Lattner4ef40012007-06-11 01:28:17 +0000810 DeclTy *LastEnumConstDecl = 0;
811
Chris Lattnerc1915e22007-01-25 07:29:02 +0000812 // Parse the enumerator-list.
813 while (Tok.getKind() == tok::identifier) {
814 IdentifierInfo *Ident = Tok.getIdentifierInfo();
815 SourceLocation IdentLoc = ConsumeToken();
816
817 SourceLocation EqualLoc;
818 ExprTy *AssignedVal = 0;
819 if (Tok.getKind() == tok::equal) {
820 EqualLoc = ConsumeToken();
821 ExprResult Res = ParseConstantExpression();
822 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000823 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000824 else
825 AssignedVal = Res.Val;
826 }
827
828 // Install the enumerator constant into EnumDecl.
Chris Lattner4ef40012007-06-11 01:28:17 +0000829 DeclTy *EnumConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
830 LastEnumConstDecl,
831 IdentLoc, Ident,
832 EqualLoc, AssignedVal);
833 EnumConstantDecls.push_back(EnumConstDecl);
834 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000835
836 if (Tok.getKind() != tok::comma)
837 break;
838 SourceLocation CommaLoc = ConsumeToken();
839
840 if (Tok.getKind() != tok::identifier && !getLang().C99)
841 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
842 }
843
844 // Eat the }.
845 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
846
847 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
848 EnumConstantDecls.size());
849
Steve Naroff0f2fe172007-06-01 17:11:19 +0000850 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000851 // If attributes exist after the identifier list, parse them.
852 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000853 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000854}
Chris Lattner3b561a32006-08-13 00:12:11 +0000855
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000856/// isTypeSpecifierQualifier - Return true if the current token could be the
857/// start of a specifier-qualifier-list.
858bool Parser::isTypeSpecifierQualifier() const {
859 switch (Tok.getKind()) {
860 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000861 // GNU attributes support.
862 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000863 // GNU typeof support.
864 case tok::kw_typeof:
865
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000866 // type-specifiers
867 case tok::kw_short:
868 case tok::kw_long:
869 case tok::kw_signed:
870 case tok::kw_unsigned:
871 case tok::kw__Complex:
872 case tok::kw__Imaginary:
873 case tok::kw_void:
874 case tok::kw_char:
875 case tok::kw_int:
876 case tok::kw_float:
877 case tok::kw_double:
878 case tok::kw__Bool:
879 case tok::kw__Decimal32:
880 case tok::kw__Decimal64:
881 case tok::kw__Decimal128:
882
883 // struct-or-union-specifier
884 case tok::kw_struct:
885 case tok::kw_union:
886 // enum-specifier
887 case tok::kw_enum:
888
889 // type-qualifier
890 case tok::kw_const:
891 case tok::kw_volatile:
892 case tok::kw_restrict:
893 return true;
894
895 // typedef-name
896 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000897 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000898 }
899}
900
Chris Lattneracd58a32006-08-06 17:24:14 +0000901/// isDeclarationSpecifier() - Return true if the current token is part of a
902/// declaration specifier.
903bool Parser::isDeclarationSpecifier() const {
904 switch (Tok.getKind()) {
905 default: return false;
906 // storage-class-specifier
907 case tok::kw_typedef:
908 case tok::kw_extern:
909 case tok::kw_static:
910 case tok::kw_auto:
911 case tok::kw_register:
912 case tok::kw___thread:
913
914 // type-specifiers
915 case tok::kw_short:
916 case tok::kw_long:
917 case tok::kw_signed:
918 case tok::kw_unsigned:
919 case tok::kw__Complex:
920 case tok::kw__Imaginary:
921 case tok::kw_void:
922 case tok::kw_char:
923 case tok::kw_int:
924 case tok::kw_float:
925 case tok::kw_double:
926 case tok::kw__Bool:
927 case tok::kw__Decimal32:
928 case tok::kw__Decimal64:
929 case tok::kw__Decimal128:
930
931 // struct-or-union-specifier
932 case tok::kw_struct:
933 case tok::kw_union:
934 // enum-specifier
935 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000936
Chris Lattneracd58a32006-08-06 17:24:14 +0000937 // type-qualifier
938 case tok::kw_const:
939 case tok::kw_volatile:
940 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +0000941
Chris Lattneracd58a32006-08-06 17:24:14 +0000942 // function-specifier
943 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +0000944
Chris Lattner599e47e2007-08-09 17:01:07 +0000945 // GNU typeof support.
946 case tok::kw_typeof:
947
948 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +0000949 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +0000950 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000951
Chris Lattneracd58a32006-08-06 17:24:14 +0000952 // typedef-name
953 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000954 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000955 }
956}
957
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000958
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000959/// ParseTypeQualifierListOpt
960/// type-qualifier-list: [C99 6.7.5]
961/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000962/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000963/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000964/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000965///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000966void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000967 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000968 int isInvalid = false;
969 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000970 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000971
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000972 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000973 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000974 // If this is not a type-qualifier token, we're done reading type
975 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000976 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000977 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000978 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000979 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
980 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000981 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000982 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000983 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
984 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000985 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000986 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000987 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
988 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000989 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000990 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000991 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +0000992 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000993 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000994
995 // If the specifier combination wasn't legal, issue a diagnostic.
996 if (isInvalid) {
997 assert(PrevSpec && "Method did not return previous specifier!");
998 if (isInvalid == 1) // Error.
999 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1000 else // extwarn.
1001 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1002 }
1003 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001004 }
1005}
1006
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001007
1008/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1009///
1010void Parser::ParseDeclarator(Declarator &D) {
1011 /// This implements the 'declarator' production in the C grammar, then checks
1012 /// for well-formedness and issues diagnostics.
1013 ParseDeclaratorInternal(D);
1014
Chris Lattner9fab3b92006-08-12 18:25:42 +00001015 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +00001016
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001017}
1018
1019/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001020/// declarator: [C99 6.7.5]
1021/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001022/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1023/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001024///
1025/// pointer: [C99 6.7.5]
1026/// '*' type-qualifier-list[opt]
1027/// '*' type-qualifier-list[opt] pointer
1028///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001029void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001030 tok::TokenKind Kind = Tok.getKind();
1031
1032 // Not a pointer or C++ reference.
1033 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001034 return ParseDirectDeclarator(D);
1035
Bill Wendling3708c182007-05-27 10:15:43 +00001036 // Otherwise, '*' -> pointer or '&' -> reference.
1037 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1038
1039 if (Kind == tok::star) {
1040 // Is a pointer
1041 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001042
Bill Wendling3708c182007-05-27 10:15:43 +00001043 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001044
Bill Wendling3708c182007-05-27 10:15:43 +00001045 // Recursively parse the declarator.
1046 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001047
Bill Wendling3708c182007-05-27 10:15:43 +00001048 // Remember that we parsed a pointer type, and remember the type-quals.
1049 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1050 } else {
1051 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001052 DeclSpec DS;
1053
1054 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1055 // cv-qualifiers are introduced through the use of a typedef or of a
1056 // template type argument, in which case the cv-qualifiers are ignored.
1057 //
1058 // [GNU] Retricted references are allowed.
1059 // [GNU] Attributes on references are allowed.
1060 ParseTypeQualifierListOpt(DS);
1061
1062 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1063 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1064 Diag(DS.getConstSpecLoc(),
1065 diag::err_invalid_reference_qualifier_application,
1066 "const");
1067 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1068 Diag(DS.getVolatileSpecLoc(),
1069 diag::err_invalid_reference_qualifier_application,
1070 "volatile");
1071 }
Bill Wendling3708c182007-05-27 10:15:43 +00001072
1073 // Recursively parse the declarator.
1074 ParseDeclaratorInternal(D);
1075
1076 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001077 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001078 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001079}
1080
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001081/// ParseDirectDeclarator
1082/// direct-declarator: [C99 6.7.5]
1083/// identifier
1084/// '(' declarator ')'
1085/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001086/// [C90] direct-declarator '[' constant-expression[opt] ']'
1087/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1088/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1089/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1090/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001091/// direct-declarator '(' parameter-type-list ')'
1092/// direct-declarator '(' identifier-list[opt] ')'
1093/// [GNU] direct-declarator '(' parameter-forward-declarations
1094/// parameter-type-list[opt] ')'
1095///
Chris Lattneracd58a32006-08-06 17:24:14 +00001096void Parser::ParseDirectDeclarator(Declarator &D) {
1097 // Parse the first direct-declarator seen.
1098 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1099 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1100 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1101 ConsumeToken();
1102 } else if (Tok.getKind() == tok::l_paren) {
1103 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001104 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001105 // Example: 'char (*X)' or 'int (*XX)(void)'
1106 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001107 } else if (D.mayOmitIdentifier()) {
1108 // This could be something simple like "int" (in which case the declarator
1109 // portion is empty), if an abstract-declarator is allowed.
1110 D.SetIdentifier(0, Tok.getLocation());
1111 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001112 // Expected identifier or '('.
1113 Diag(Tok, diag::err_expected_ident_lparen);
1114 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001115 }
1116
1117 assert(D.isPastIdentifier() &&
1118 "Haven't past the location of the identifier yet?");
1119
1120 while (1) {
1121 if (Tok.getKind() == tok::l_paren) {
1122 ParseParenDeclarator(D);
1123 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001124 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001125 } else {
1126 break;
1127 }
1128 }
1129}
1130
1131/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1132/// either be before the identifier (in which case these are just grouping
1133/// parens for precedence) or it may be after the identifier, in which case
1134/// these are function arguments.
1135///
1136/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001137/// parameter-type-list: [C99 6.7.5]
1138/// parameter-list
1139/// parameter-list ',' '...'
1140///
1141/// parameter-list: [C99 6.7.5]
1142/// parameter-declaration
1143/// parameter-list ',' parameter-declaration
1144///
1145/// parameter-declaration: [C99 6.7.5]
1146/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001147/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001148/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001149/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001150///
1151/// identifier-list: [C99 6.7.5]
1152/// identifier
1153/// identifier-list ',' identifier
1154///
Chris Lattneracd58a32006-08-06 17:24:14 +00001155void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001156 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001157
Chris Lattneracd58a32006-08-06 17:24:14 +00001158 // If we haven't past the identifier yet (or where the identifier would be
1159 // stored, if this is an abstract declarator), then this is probably just
1160 // grouping parens.
1161 if (!D.isPastIdentifier()) {
1162 // Okay, this is probably a grouping paren. However, if this could be an
1163 // abstract-declarator, then this could also be the start of function
1164 // arguments (consider 'void()').
1165 bool isGrouping;
1166
1167 if (!D.mayOmitIdentifier()) {
1168 // If this can't be an abstract-declarator, this *must* be a grouping
1169 // paren, because we haven't seen the identifier yet.
1170 isGrouping = true;
1171 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1172 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001173 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1174 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001175 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001176 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001177 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001178 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001179 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001180
1181 // If this is a grouping paren, handle:
1182 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001183 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001184 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +00001185 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001186 D.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001187
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001188 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001189 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001190 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001191 return;
1192 }
1193
1194 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001195 // argument list. Recognize that this declarator will never have an
1196 // identifier (and remember where it would have been), then fall through to
1197 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001198 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001199 }
1200
Chris Lattneracd58a32006-08-06 17:24:14 +00001201 // Okay, this is the parameter list of a function definition, or it is an
1202 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001203 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001204 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001205 bool ErrorEmitted = false;
1206
Chris Lattneredc9e392006-12-02 06:21:46 +00001207 // Build up an array of information about the parsed arguments.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001208 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1209 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001210
Chris Lattneracd58a32006-08-06 17:24:14 +00001211 if (Tok.getKind() == tok::r_paren) {
1212 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001213 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001214 HasPrototype = false;
1215 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001216 // K&R identifier lists can't have typedefs as identifiers, per
1217 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001218 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001219 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1220 // normal declarators, not for abstract-declarators.
1221 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1222
1223 // If there was no identifier specified, either we are in an
1224 // abstract-declarator, or we are in a parameter declarator which was found
1225 // to be abstract. In abstract-declarators, identifier lists are not valid,
1226 // diagnose this.
1227 if (!D.getIdentifier())
1228 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001229
Chris Lattnercbc426d2006-12-02 06:43:02 +00001230 // Remember this identifier in ParamInfo.
1231 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1232 Tok.getLocation(), 0));
1233
Chris Lattneracd58a32006-08-06 17:24:14 +00001234 ConsumeToken();
1235 while (Tok.getKind() == tok::comma) {
1236 // Eat the comma.
1237 ConsumeToken();
1238
Chris Lattnercbc426d2006-12-02 06:43:02 +00001239 if (Tok.getKind() != tok::identifier) {
1240 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001241 ErrorEmitted = true;
1242 break;
1243 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001244
Chris Lattner969ca152006-12-03 06:29:03 +00001245 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1246
1247 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001248 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001249 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1250 ParmII = 0;
1251 }
Chris Lattner969ca152006-12-03 06:29:03 +00001252
Chris Lattnercbc426d2006-12-02 06:43:02 +00001253 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001254 if (ParmII)
1255 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1256 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001257
1258 // Eat the identifier.
1259 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001260 }
1261
Chris Lattneracd58a32006-08-06 17:24:14 +00001262 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001263 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001264 HasPrototype = false;
1265 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001266 // Finally, a normal, non-empty parameter type list.
1267
Chris Lattnercbc426d2006-12-02 06:43:02 +00001268 // Enter function-declaration scope, limiting any declarators for struct
1269 // tags to the function prototype scope.
1270 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001271 EnterScope(0);
1272
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001273 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001274 while (1) {
1275 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001276 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001277
1278 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001279 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001280 // Otherwise, parse parameter type list. If it starts with an
1281 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001282 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001283 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001284 }
1285
1286 // Consume the ellipsis.
1287 ConsumeToken();
1288 break;
1289 }
1290
Chris Lattneracd58a32006-08-06 17:24:14 +00001291 // Parse the declaration-specifiers.
1292 DeclSpec DS;
1293 ParseDeclarationSpecifiers(DS);
1294
1295 // Parse the declarator. This is "PrototypeContext", because we must
1296 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001297 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1298 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001299
Chris Lattnere37e2332006-08-15 04:50:22 +00001300 // Parse GNU attributes, if present.
1301 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001302 ParmDecl.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001303
Chris Lattner43e956c2006-11-28 04:05:37 +00001304 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001305 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1306 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1307 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001308 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001309 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001310 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001311 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001312 if (DS.isThreadSpecified()) {
1313 Diag(DS.getThreadSpecLoc(),
1314 diag::err_invalid_storage_class_in_func_decl);
1315 DS.ClearStorageClassSpecs();
1316 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001317
1318 // Inform the actions module about the parameter declarator, so it gets
1319 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001320 Action::TypeResult ParamTy =
1321 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001322
1323 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001324 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1325
1326 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001327 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001328 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1329 ParmII->getName());
1330 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001331 }
1332
1333 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001334 ParmDecl.getIdentifierLoc(),
1335 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001336
1337 // If the next token is a comma, consume it and keep reading arguments.
1338 if (Tok.getKind() != tok::comma) break;
1339
1340 // Consume the comma.
1341 ConsumeToken();
1342 }
1343
1344 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001345
1346 // Leave prototype scope.
1347 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001348 }
1349
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001350 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001351 if (!ErrorEmitted)
1352 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1353 &ParamInfo[0], ParamInfo.size(),
1354 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001355
Chris Lattner14776b92006-08-06 22:27:40 +00001356 // If we have the closing ')', eat it and we're done.
1357 if (Tok.getKind() == tok::r_paren) {
1358 ConsumeParen();
1359 } else {
1360 // If an error happened earlier parsing something else in the proto, don't
1361 // issue another error.
1362 if (!ErrorEmitted)
1363 Diag(Tok, diag::err_expected_rparen);
1364 SkipUntil(tok::r_paren);
1365 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001366}
Chris Lattneracd58a32006-08-06 17:24:14 +00001367
Chris Lattnere8074e62006-08-06 18:30:15 +00001368
1369/// [C90] direct-declarator '[' constant-expression[opt] ']'
1370/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1371/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1372/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1373/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1374void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001375 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001376
1377 // If valid, this location is the position where we read the 'static' keyword.
1378 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001379 if (Tok.getKind() == tok::kw_static)
1380 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001381
1382 // If there is a type-qualifier-list, read it now.
1383 DeclSpec DS;
1384 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001385
1386 // If we haven't already read 'static', check to see if there is one after the
1387 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001388 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1389 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001390
1391 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001392 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001393 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001394 if (Tok.getKind() == tok::star) {
1395 // Remember the '*' token, in case we have to un-get it.
Chris Lattner146762e2007-07-20 16:59:19 +00001396 Token StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001397 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001398
1399 // Check that the ']' token is present to avoid incorrectly parsing
1400 // expressions starting with '*' as [*].
1401 if (Tok.getKind() == tok::r_square) {
1402 if (StaticLoc.isValid())
1403 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1404 StaticLoc = SourceLocation(); // Drop the static.
1405 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001406 } else {
1407 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001408 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001409 // need to reparse it. This handles cases like 'X[*p + 4]'
1410 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001411 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001412 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001413 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001414 NumElements = ParseAssignmentExpression();
1415 }
1416
1417 // If there was an error parsing the assignment-expression, recover.
1418 if (NumElements.isInvalid) {
1419 // If the expression was invalid, skip it.
1420 SkipUntil(tok::r_square);
1421 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001422 }
1423
Chris Lattner04f80192006-08-15 04:55:54 +00001424 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001425
Chris Lattnere8074e62006-08-06 18:30:15 +00001426 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1427 // it was not a constant expression.
1428 if (!getLang().C99) {
1429 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001430 if (isStar || StaticLoc.isValid() ||
1431 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001432 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001433 }
Bill Wendling93efb222007-06-02 23:28:54 +00001434
Chris Lattner6c7416c2006-08-07 00:19:33 +00001435 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001436 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1437 StaticLoc.isValid(), isStar,
1438 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001439}
1440
Steve Naroffad373bd2007-07-31 12:34:36 +00001441/// [GNU] typeof-specifier:
1442/// typeof ( expressions )
1443/// typeof ( type-name )
1444///
1445void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
1446 assert(Tok.getKind() == tok::kw_typeof && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001447 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001448 SourceLocation StartLoc = ConsumeToken();
1449
1450 if (Tok.getKind() != tok::l_paren) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001451 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1452 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001453 }
1454 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1455
1456 if (isTypeSpecifierQualifier()) {
1457 TypeTy *Ty = ParseTypeName();
1458
Steve Naroff872da802007-07-31 23:56:32 +00001459 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1460
Steve Naroff4bd2f712007-08-02 02:53:48 +00001461 if (Tok.getKind() != tok::r_paren) {
Steve Naroff872da802007-07-31 23:56:32 +00001462 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001463 return;
1464 }
1465 RParenLoc = ConsumeParen();
1466 const char *PrevSpec = 0;
1467 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1468 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1469 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001470 } else { // we have an expression.
1471 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001472
Steve Naroff4bd2f712007-08-02 02:53:48 +00001473 if (Result.isInvalid || Tok.getKind() != tok::r_paren) {
Steve Naroff872da802007-07-31 23:56:32 +00001474 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001475 return;
1476 }
1477 RParenLoc = ConsumeParen();
1478 const char *PrevSpec = 0;
1479 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1480 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1481 Result.Val))
1482 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001483 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001484}
1485