blob: c063654781ab9263a4d719637de2f4378ef44497 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlcee63fb2008-12-02 14:43:59 +000031Parser::TypeTy *Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Sebastian Redlcee63fb2008-12-02 14:43:59 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Reid Spencer5f016e22007-07-11 17:01:13 +000041}
42
43/// ParseAttributes - Parse a non-empty attributes list.
44///
45/// [GNU] attributes:
46/// attribute
47/// attributes attribute
48///
49/// [GNU] attribute:
50/// '__attribute__' '(' '(' attribute-list ')' ')'
51///
52/// [GNU] attribute-list:
53/// attrib
54/// attribute_list ',' attrib
55///
56/// [GNU] attrib:
57/// empty
58/// attrib-name
59/// attrib-name '(' identifier ')'
60/// attrib-name '(' identifier ',' nonempty-expr-list ')'
61/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
62///
63/// [GNU] attrib-name:
64/// identifier
65/// typespec
66/// typequal
67/// storageclass
68///
69/// FIXME: The GCC grammar/code for this construct implies we need two
70/// token lookahead. Comment from gcc: "If they start with an identifier
71/// which is followed by a comma or close parenthesis, then the arguments
72/// start with that identifier; otherwise they are an expression list."
73///
74/// At the moment, I am not doing 2 token lookahead. I am also unaware of
75/// any attributes that don't work (based on my limited testing). Most
76/// attributes are very simple in practice. Until we find a bug, I don't see
77/// a pressing need to implement the 2 token lookahead.
78
79AttributeList *Parser::ParseAttributes() {
Chris Lattner04d66662007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner04d66662007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000085 ConsumeToken();
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
87 "attribute")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000098
Chris Lattner04d66662007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
101 ConsumeToken();
102 continue;
103 }
104 // we have an identifier or declaration specifier (const, int, etc.)
105 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
106 SourceLocation AttrNameLoc = ConsumeToken();
107
108 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 // __attribute__(( mode(byte) ))
118 ConsumeParen(); // ignore the right paren loc for now
119 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
120 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 }
Chris Lattner04d66662007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 // __attribute__(( nonnull() ))
151 ConsumeParen(); // ignore the right paren loc for now
152 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
153 0, SourceLocation(), 0, 0, CurrAttr);
154 } else {
155 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Chris Lattner04d66662007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 CurrAttr);
179 }
180 }
181 }
182 } else {
183 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
184 0, SourceLocation(), 0, 0, CurrAttr);
185 }
186 }
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
190 SkipUntil(tok::r_paren, false);
191 }
192 return CurrAttr;
193}
194
Steve Narofff59e17e2008-12-24 20:59:21 +0000195/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
196/// routine is called to skip/ignore tokens that comprise the MS declspec.
197void Parser::FuzzyParseMicrosoftDeclSpec() {
198 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
199 ConsumeToken();
200 if (Tok.is(tok::l_paren)) {
201 unsigned short savedParenCount = ParenCount;
202 do {
203 ConsumeAnyToken();
204 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
205 }
206 return;
207}
208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209/// ParseDeclaration - Parse a full 'declaration', which consists of
210/// declaration-specifiers, some number of declarators, and a semicolon.
211/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000212///
213/// declaration: [C99 6.7]
214/// block-declaration ->
215/// simple-declaration
216/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000217/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000218/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000219/// [C++] using-directive
220/// [C++] using-declaration [TODO]
Chris Lattner8f08cb72007-08-25 06:57:03 +0000221/// others... [FIXME]
222///
Reid Spencer5f016e22007-07-11 17:01:13 +0000223Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000224 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000225 case tok::kw_export:
226 case tok::kw_template:
227 return ParseTemplateDeclaration(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000228 case tok::kw_namespace:
229 return ParseNamespace(Context);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000230 case tok::kw_using:
231 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000232 default:
233 return ParseSimpleDeclaration(Context);
234 }
235}
236
237/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
238/// declaration-specifiers init-declarator-list[opt] ';'
239///[C90/C++]init-declarator-list ';' [TODO]
240/// [OMP] threadprivate-directive [TODO]
241Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 // Parse the common declaration-specifiers piece.
243 DeclSpec DS;
244 ParseDeclarationSpecifiers(DS);
245
246 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
247 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000248 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 ConsumeToken();
250 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
251 }
252
253 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
254 ParseDeclarator(DeclaratorInfo);
255
256 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
257}
258
Chris Lattner8f08cb72007-08-25 06:57:03 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
261/// parsing 'declaration-specifiers declarator'. This method is split out this
262/// way to handle the ambiguity between top-level function-definitions and
263/// declarations.
264///
Reid Spencer5f016e22007-07-11 17:01:13 +0000265/// init-declarator-list: [C99 6.7]
266/// init-declarator
267/// init-declarator-list ',' init-declarator
268/// init-declarator: [C99 6.7]
269/// declarator
270/// declarator '=' initializer
271/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
272/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000273/// [C++] declarator initializer[opt]
274///
275/// [C++] initializer:
276/// [C++] '=' initializer-clause
277/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000278///
279Parser::DeclTy *Parser::
280ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
281
282 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
283 // that they can be chained properly if the actions want this.
284 Parser::DeclTy *LastDeclInGroup = 0;
285
286 // At this point, we know that it is not a function definition. Parse the
287 // rest of the init-declarator-list.
288 while (1) {
289 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000290 if (Tok.is(tok::kw_asm)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000291 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000292 if (AsmLabel.isInvalid()) {
Daniel Dunbara80f8742008-08-05 01:35:17 +0000293 SkipUntil(tok::semi);
294 return 0;
295 }
Daniel Dunbar914701e2008-08-05 16:28:08 +0000296
Sebastian Redleffa8d12008-12-10 00:02:53 +0000297 D.setAsmLabel(AsmLabel.release());
Daniel Dunbara80f8742008-08-05 01:35:17 +0000298 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000299
300 // If attributes are present, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000301 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 D.AddAttributes(ParseAttributes());
Steve Naroffbb204692007-09-12 14:07:44 +0000303
304 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000305 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000308 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000310 OwningExprResult Init(ParseInitializer());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000311 if (Init.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 SkipUntil(tok::semi);
313 return 0;
314 }
Sebastian Redl798d1192008-12-13 16:23:55 +0000315 Actions.AddInitializerToDecl(LastDeclInGroup, move_convert(Init));
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000316 } else if (Tok.is(tok::l_paren)) {
317 // Parse C++ direct initializer: '(' expression-list ')'
318 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000319 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000320 CommaLocsTy CommaLocs;
321
322 bool InvalidExpr = false;
323 if (ParseExpressionList(Exprs, CommaLocs)) {
324 SkipUntil(tok::r_paren);
325 InvalidExpr = true;
326 }
327 // Match the ')'.
328 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
329
330 if (!InvalidExpr) {
331 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
332 "Unexpected number of commas!");
333 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000334 Exprs.take(), Exprs.size(),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000335 &CommaLocs[0], RParenLoc);
336 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000337 } else {
338 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 }
340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // If we don't have a comma, it is either the end of the list (a ';') or an
342 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000343 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 break;
345
346 // Consume the comma.
347 ConsumeToken();
348
349 // Parse the next declarator.
350 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000351
352 // Accept attributes in an init-declarator. In the first declarator in a
353 // declaration, these would be part of the declspec. In subsequent
354 // declarators, they become part of the declarator itself, so that they
355 // don't apply to declarators after *this* one. Examples:
356 // short __attribute__((common)) var; -> declspec
357 // short var __attribute__((common)); -> declarator
358 // short x, __attribute__((common)) var; -> declarator
359 if (Tok.is(tok::kw___attribute))
360 D.AddAttributes(ParseAttributes());
361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 ParseDeclarator(D);
363 }
364
Chris Lattner04d66662007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 ConsumeToken();
367 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
368 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000369 // If this is an ObjC2 for-each loop, this is a successful declarator
370 // parse. The syntax for these looks like:
371 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000372 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000373 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
374 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 Diag(Tok, diag::err_parse_error);
376 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000377 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000378 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 ConsumeToken();
380 return 0;
381}
382
383/// ParseSpecifierQualifierList
384/// specifier-qualifier-list:
385/// type-specifier specifier-qualifier-list[opt]
386/// type-qualifier specifier-qualifier-list[opt]
387/// [GNU] attributes specifier-qualifier-list[opt]
388///
389void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
390 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
391 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 ParseDeclarationSpecifiers(DS);
393
394 // Validate declspec for type-name.
395 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000396 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 Diag(Tok, diag::err_typename_requires_specqual);
398
399 // Issue diagnostic and remove storage class if present.
400 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
401 if (DS.getStorageClassSpecLoc().isValid())
402 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
403 else
404 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
405 DS.ClearStorageClassSpecs();
406 }
407
408 // Issue diagnostic and remove function specfier if present.
409 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000410 if (DS.isInlineSpecified())
411 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
412 if (DS.isVirtualSpecified())
413 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
414 if (DS.isExplicitSpecified())
415 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 DS.ClearFunctionSpecs();
417 }
418}
419
420/// ParseDeclarationSpecifiers
421/// declaration-specifiers: [C99 6.7]
422/// storage-class-specifier declaration-specifiers[opt]
423/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000424/// [C99] function-specifier declaration-specifiers[opt]
425/// [GNU] attributes declaration-specifiers[opt]
426///
427/// storage-class-specifier: [C99 6.7.1]
428/// 'typedef'
429/// 'extern'
430/// 'static'
431/// 'auto'
432/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000433/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000434/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000435/// function-specifier: [C99 6.7.4]
436/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000437/// [C++] 'virtual'
438/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000439///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000440void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner5e02c472009-01-05 00:07:25 +0000441 TemplateParameterLists *TemplateParams){
Chris Lattner81c018d2008-03-13 06:29:04 +0000442 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 while (1) {
444 int isInvalid = false;
445 const char *PrevSpec = 0;
446 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000447
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000449 default:
Douglas Gregoradcac882008-12-01 23:54:00 +0000450 // Try to parse a type-specifier; if we found one, continue. If it's not
451 // a type, this falls through.
Chris Lattner5e02c472009-01-05 00:07:25 +0000452 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams))
Douglas Gregor12e083c2008-11-07 15:42:26 +0000453 continue;
454
Chris Lattnerbce61352008-07-26 00:20:22 +0000455 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 // If this is not a declaration specifier token, we're done reading decl
457 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000458 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000460
461 case tok::coloncolon: // ::foo::bar
462 // Annotate C++ scope specifiers. If we get one, loop.
463 if (TryAnnotateCXXScopeToken())
464 continue;
465 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000466
467 case tok::annot_cxxscope: {
468 if (DS.hasTypeSpecifier())
469 goto DoneWithDeclSpec;
470
471 // We are looking for a qualified typename.
472 if (NextToken().isNot(tok::identifier))
473 goto DoneWithDeclSpec;
474
475 CXXScopeSpec SS;
476 SS.setScopeRep(Tok.getAnnotationValue());
477 SS.setRange(Tok.getAnnotationRange());
478
479 // If the next token is the name of the class type that the C++ scope
480 // denotes, followed by a '(', then this is a constructor declaration.
481 // We're done with the decl-specifiers.
482 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
483 CurScope, &SS) &&
484 GetLookAheadToken(2).is(tok::l_paren))
485 goto DoneWithDeclSpec;
486
487 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
488 CurScope, &SS);
489 if (TypeRep == 0)
490 goto DoneWithDeclSpec;
491
492 ConsumeToken(); // The C++ scope.
493
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
495 TypeRep);
496 if (isInvalid)
497 break;
498
499 DS.SetRangeEnd(Tok.getLocation());
500 ConsumeToken(); // The typename.
501
502 continue;
503 }
504
Chris Lattner3bd934a2008-07-26 01:18:38 +0000505 // typedef-name
506 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000507 // In C++, check to see if this is a scope specifier like foo::bar::, if
508 // so handle it as such. This is important for ctor parsing.
509 if (getLang().CPlusPlus &&
510 TryAnnotateCXXScopeToken())
511 continue;
512
Chris Lattner3bd934a2008-07-26 01:18:38 +0000513 // This identifier can only be a typedef name if we haven't already seen
514 // a type-specifier. Without this check we misparse:
515 // typedef int X; struct Y { short X; }; as 'short int'.
516 if (DS.hasTypeSpecifier())
517 goto DoneWithDeclSpec;
518
519 // It has to be available as a typedef too!
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000520 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000521 if (TypeRep == 0)
522 goto DoneWithDeclSpec;
523
Douglas Gregorb48fe382008-10-31 09:07:45 +0000524 // C++: If the identifier is actually the name of the class type
525 // being defined and the next token is a '(', then this is a
526 // constructor declaration. We're done with the decl-specifiers
527 // and will treat this token as an identifier.
528 if (getLang().CPlusPlus &&
529 CurScope->isCXXClassScope() &&
530 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
531 NextToken().getKind() == tok::l_paren)
532 goto DoneWithDeclSpec;
533
Chris Lattner3bd934a2008-07-26 01:18:38 +0000534 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
535 TypeRep);
536 if (isInvalid)
537 break;
538
539 DS.SetRangeEnd(Tok.getLocation());
540 ConsumeToken(); // The identifier
541
542 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
543 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
544 // Objective-C interface. If we don't have Objective-C or a '<', this is
545 // just a normal reference to a typedef name.
546 if (!Tok.is(tok::less) || !getLang().ObjC1)
547 continue;
548
549 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000550 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000551 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000552 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000553
554 DS.SetRangeEnd(EndProtoLoc);
555
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000556 // Need to support trailing type qualifiers (e.g. "id<p> const").
557 // If a type specifier follows, it will be diagnosed elsewhere.
558 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000559 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 // GNU attributes support.
561 case tok::kw___attribute:
562 DS.AddAttributes(ParseAttributes());
563 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000564
565 // Microsoft declspec support.
566 case tok::kw___declspec:
567 if (!PP.getLangOptions().Microsoft)
568 goto DoneWithDeclSpec;
569 FuzzyParseMicrosoftDeclSpec();
570 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000571
Steve Naroff239f0732008-12-25 14:16:32 +0000572 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000573 case tok::kw___forceinline:
574 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000575 case tok::kw___cdecl:
576 case tok::kw___stdcall:
577 case tok::kw___fastcall:
578 if (!PP.getLangOptions().Microsoft)
579 goto DoneWithDeclSpec;
580 // Just ignore it.
581 break;
582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 // storage-class-specifier
584 case tok::kw_typedef:
585 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
586 break;
587 case tok::kw_extern:
588 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000589 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
591 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000592 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000593 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
594 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000595 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 case tok::kw_static:
597 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000598 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
600 break;
601 case tok::kw_auto:
602 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
603 break;
604 case tok::kw_register:
605 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
606 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000607 case tok::kw_mutable:
608 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
609 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 case tok::kw___thread:
611 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
612 break;
613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 // function-specifier
617 case tok::kw_inline:
618 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
619 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000620
621 case tok::kw_virtual:
622 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
623 break;
624
625 case tok::kw_explicit:
626 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
627 break;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000628
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000629 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000630 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000631 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
632 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000633 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000634 goto DoneWithDeclSpec;
635
636 {
637 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000638 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000639 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000640 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000641 DS.SetRangeEnd(EndProtoLoc);
642
Chris Lattner1ab3b962008-11-18 07:48:38 +0000643 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
644 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000645 // Need to support trailing type qualifiers (e.g. "id<p> const").
646 // If a type specifier follows, it will be diagnosed elsewhere.
647 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000648 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 }
650 // If the specifier combination wasn't legal, issue a diagnostic.
651 if (isInvalid) {
652 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000653 // Pick between error or extwarn.
654 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
655 : diag::ext_duplicate_declspec;
656 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000658 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 ConsumeToken();
660 }
661}
Douglas Gregoradcac882008-12-01 23:54:00 +0000662
Douglas Gregor12e083c2008-11-07 15:42:26 +0000663/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
664/// primarily follow the C++ grammar with additions for C99 and GNU,
665/// which together subsume the C grammar. Note that the C++
666/// type-specifier also includes the C type-qualifier (for const,
667/// volatile, and C99 restrict). Returns true if a type-specifier was
668/// found (and parsed), false otherwise.
669///
670/// type-specifier: [C++ 7.1.5]
671/// simple-type-specifier
672/// class-specifier
673/// enum-specifier
674/// elaborated-type-specifier [TODO]
675/// cv-qualifier
676///
677/// cv-qualifier: [C++ 7.1.5.1]
678/// 'const'
679/// 'volatile'
680/// [C99] 'restrict'
681///
682/// simple-type-specifier: [ C++ 7.1.5.2]
683/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
684/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
685/// 'char'
686/// 'wchar_t'
687/// 'bool'
688/// 'short'
689/// 'int'
690/// 'long'
691/// 'signed'
692/// 'unsigned'
693/// 'float'
694/// 'double'
695/// 'void'
696/// [C99] '_Bool'
697/// [C99] '_Complex'
698/// [C99] '_Imaginary' // Removed in TC2?
699/// [GNU] '_Decimal32'
700/// [GNU] '_Decimal64'
701/// [GNU] '_Decimal128'
702/// [GNU] typeof-specifier
703/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
704/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
705bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000706 const char *&PrevSpec,
707 TemplateParameterLists *TemplateParams) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000708 SourceLocation Loc = Tok.getLocation();
709
710 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +0000711 case tok::identifier: // foo::bar
712 // Annotate typenames and C++ scope specifiers. If we get one, just
713 // recurse to handle whatever we get.
714 if (TryAnnotateTypeOrScopeToken())
715 return MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams);
716 // Otherwise, not a type specifier.
717 return false;
718 case tok::coloncolon: // ::foo::bar
719 if (NextToken().is(tok::kw_new) || // ::new
720 NextToken().is(tok::kw_delete)) // ::delete
721 return false;
722
723 // Annotate typenames and C++ scope specifiers. If we get one, just
724 // recurse to handle whatever we get.
725 if (TryAnnotateTypeOrScopeToken())
726 return MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams);
727 // Otherwise, not a type specifier.
728 return false;
729
Douglas Gregor12e083c2008-11-07 15:42:26 +0000730 // simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000731 case tok::annot_qualtypename: {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000733 Tok.getAnnotationValue());
734 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
735 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000736
737 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
738 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
739 // Objective-C interface. If we don't have Objective-C or a '<', this is
740 // just a normal reference to a typedef name.
741 if (!Tok.is(tok::less) || !getLang().ObjC1)
742 return true;
743
744 SourceLocation EndProtoLoc;
745 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
746 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
747 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
748
749 DS.SetRangeEnd(EndProtoLoc);
750 return true;
751 }
752
753 case tok::kw_short:
754 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
755 break;
756 case tok::kw_long:
757 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
758 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
759 else
760 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
761 break;
762 case tok::kw_signed:
763 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
764 break;
765 case tok::kw_unsigned:
766 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
767 break;
768 case tok::kw__Complex:
769 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
770 break;
771 case tok::kw__Imaginary:
772 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
773 break;
774 case tok::kw_void:
775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
776 break;
777 case tok::kw_char:
778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
779 break;
780 case tok::kw_int:
781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
782 break;
783 case tok::kw_float:
784 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
785 break;
786 case tok::kw_double:
787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
788 break;
789 case tok::kw_wchar_t:
790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
791 break;
792 case tok::kw_bool:
793 case tok::kw__Bool:
794 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
795 break;
796 case tok::kw__Decimal32:
797 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
798 break;
799 case tok::kw__Decimal64:
800 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
801 break;
802 case tok::kw__Decimal128:
803 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
804 break;
805
806 // class-specifier:
807 case tok::kw_class:
808 case tok::kw_struct:
809 case tok::kw_union:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000810 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +0000811 return true;
812
813 // enum-specifier:
814 case tok::kw_enum:
815 ParseEnumSpecifier(DS);
816 return true;
817
818 // cv-qualifier:
819 case tok::kw_const:
820 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
821 getLang())*2;
822 break;
823 case tok::kw_volatile:
824 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
825 getLang())*2;
826 break;
827 case tok::kw_restrict:
828 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
829 getLang())*2;
830 break;
831
832 // GNU typeof support.
833 case tok::kw_typeof:
834 ParseTypeofSpecifier(DS);
835 return true;
836
Steve Naroff239f0732008-12-25 14:16:32 +0000837 case tok::kw___cdecl:
838 case tok::kw___stdcall:
839 case tok::kw___fastcall:
840 return PP.getLangOptions().Microsoft;
841
Douglas Gregor12e083c2008-11-07 15:42:26 +0000842 default:
843 // Not a type-specifier; do nothing.
844 return false;
845 }
846
847 // If the specifier combination wasn't legal, issue a diagnostic.
848 if (isInvalid) {
849 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000850 // Pick between error or extwarn.
851 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
852 : diag::ext_duplicate_declspec;
853 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000854 }
855 DS.SetRangeEnd(Tok.getLocation());
856 ConsumeToken(); // whatever we parsed above.
857 return true;
858}
Reid Spencer5f016e22007-07-11 17:01:13 +0000859
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000860/// ParseStructDeclaration - Parse a struct declaration without the terminating
861/// semicolon.
862///
Reid Spencer5f016e22007-07-11 17:01:13 +0000863/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000864/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000865/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000866/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000867/// struct-declarator-list:
868/// struct-declarator
869/// struct-declarator-list ',' struct-declarator
870/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
871/// struct-declarator:
872/// declarator
873/// [GNU] declarator attributes[opt]
874/// declarator[opt] ':' constant-expression
875/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
876///
Chris Lattnere1359422008-04-10 06:46:29 +0000877void Parser::
878ParseStructDeclaration(DeclSpec &DS,
879 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000880 if (Tok.is(tok::kw___extension__)) {
881 // __extension__ silences extension warnings in the subexpression.
882 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +0000883 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000884 return ParseStructDeclaration(DS, Fields);
885 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000886
887 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000888 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000889 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000890
891 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000892 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000893 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000894 return;
895 }
896
897 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000898 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000899 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +0000900 FieldDeclarator &DeclaratorInfo = Fields.back();
901
Steve Naroff28a7ca82007-08-20 22:28:22 +0000902 /// struct-declarator: declarator
903 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000904 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +0000905 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000906
Chris Lattner04d66662007-10-09 17:33:22 +0000907 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000908 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000909 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000910 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +0000911 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000912 else
Sebastian Redleffa8d12008-12-10 00:02:53 +0000913 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000914 }
915
916 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000917 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000918 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000919
920 // If we don't have a comma, it is either the end of the list (a ';')
921 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000922 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000923 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000924
925 // Consume the comma.
926 ConsumeToken();
927
928 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000929 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000930
931 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000932 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000933 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000934 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000935}
936
937/// ParseStructUnionBody
938/// struct-contents:
939/// struct-declaration-list
940/// [EXT] empty
941/// [GNU] "struct-declaration-list" without terminatoring ';'
942/// struct-declaration-list:
943/// struct-declaration
944/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000945/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +0000946///
Reid Spencer5f016e22007-07-11 17:01:13 +0000947void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
948 unsigned TagType, DeclTy *TagDecl) {
949 SourceLocation LBraceLoc = ConsumeBrace();
950
951 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
952 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000953 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +0000954 Diag(Tok, diag::ext_empty_struct_union_enum)
955 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000956
957 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000958 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
959
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000961 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // Each iteration of this loop reads one struct-declaration.
963
964 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000965 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 Diag(Tok, diag::ext_extra_struct_semi);
967 ConsumeToken();
968 continue;
969 }
Chris Lattnere1359422008-04-10 06:46:29 +0000970
971 // Parse all the comma separated declarators.
972 DeclSpec DS;
973 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000974 if (!Tok.is(tok::at)) {
975 ParseStructDeclaration(DS, FieldDeclarators);
976
977 // Convert them all to fields.
978 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
979 FieldDeclarator &FD = FieldDeclarators[i];
980 // Install the declarator into the current TagDecl.
Douglas Gregor44b43212008-12-11 16:49:14 +0000981 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000982 DS.getSourceRange().getBegin(),
983 FD.D, FD.BitfieldSize);
984 FieldDecls.push_back(Field);
985 }
986 } else { // Handle @defs
987 ConsumeToken();
988 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
989 Diag(Tok, diag::err_unexpected_at);
990 SkipUntil(tok::semi, true, true);
991 continue;
992 }
993 ConsumeToken();
994 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
995 if (!Tok.is(tok::identifier)) {
996 Diag(Tok, diag::err_expected_ident);
997 SkipUntil(tok::semi, true, true);
998 continue;
999 }
1000 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001001 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1002 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001003 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1004 ConsumeToken();
1005 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1006 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001007
Chris Lattner04d66662007-10-09 17:33:22 +00001008 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001010 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001011 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 break;
1013 } else {
1014 Diag(Tok, diag::err_expected_semi_decl_list);
1015 // Skip to end of block or statement
1016 SkipUntil(tok::r_brace, true, true);
1017 }
1018 }
1019
Steve Naroff60fccee2007-10-29 21:38:07 +00001020 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001021
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 AttributeList *AttrList = 0;
1023 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001024 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001025 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001026
1027 Actions.ActOnFields(CurScope,
1028 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1029 LBraceLoc, RBraceLoc,
1030 AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +00001031}
1032
1033
1034/// ParseEnumSpecifier
1035/// enum-specifier: [C99 6.7.2.2]
1036/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001037///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001038/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1039/// '}' attributes[opt]
1040/// 'enum' identifier
1041/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001042///
1043/// [C++] elaborated-type-specifier:
1044/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1045///
Reid Spencer5f016e22007-07-11 17:01:13 +00001046void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001047 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 SourceLocation StartLoc = ConsumeToken();
1049
1050 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001051
1052 AttributeList *Attr = 0;
1053 // If attributes exist after tag, parse them.
1054 if (Tok.is(tok::kw___attribute))
1055 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001056
1057 CXXScopeSpec SS;
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +00001058 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001059 if (Tok.isNot(tok::identifier)) {
1060 Diag(Tok, diag::err_expected_ident);
1061 if (Tok.isNot(tok::l_brace)) {
1062 // Has no name and is not a definition.
1063 // Skip the rest of this declarator, up until the comma or semicolon.
1064 SkipUntil(tok::comma, true);
1065 return;
1066 }
1067 }
1068 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001069
1070 // Must have either 'enum name' or 'enum {...}'.
1071 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1072 Diag(Tok, diag::err_expected_ident_lbrace);
1073
1074 // Skip the rest of this declarator, up until the comma or semicolon.
1075 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001077 }
1078
1079 // If an identifier is present, consume and remember it.
1080 IdentifierInfo *Name = 0;
1081 SourceLocation NameLoc;
1082 if (Tok.is(tok::identifier)) {
1083 Name = Tok.getIdentifierInfo();
1084 NameLoc = ConsumeToken();
1085 }
1086
1087 // There are three options here. If we have 'enum foo;', then this is a
1088 // forward declaration. If we have 'enum foo {...' then this is a
1089 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1090 //
1091 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1092 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1093 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1094 //
1095 Action::TagKind TK;
1096 if (Tok.is(tok::l_brace))
1097 TK = Action::TK_Definition;
1098 else if (Tok.is(tok::semi))
1099 TK = Action::TK_Declaration;
1100 else
1101 TK = Action::TK_Reference;
1102 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001103 SS, Name, NameLoc, Attr,
1104 Action::MultiTemplateParamsArg(Actions));
Reid Spencer5f016e22007-07-11 17:01:13 +00001105
Chris Lattner04d66662007-10-09 17:33:22 +00001106 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 ParseEnumBody(StartLoc, TagDecl);
1108
1109 // TODO: semantic analysis on the declspec for enums.
1110 const char *PrevSpec = 0;
1111 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001112 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001113}
1114
1115/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1116/// enumerator-list:
1117/// enumerator
1118/// enumerator-list ',' enumerator
1119/// enumerator:
1120/// enumeration-constant
1121/// enumeration-constant '=' constant-expression
1122/// enumeration-constant:
1123/// identifier
1124///
1125void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1126 SourceLocation LBraceLoc = ConsumeBrace();
1127
Chris Lattner7946dd32007-08-27 17:24:30 +00001128 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001129 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001130 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001131
1132 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1133
1134 DeclTy *LastEnumConstDecl = 0;
1135
1136 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001137 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1139 SourceLocation IdentLoc = ConsumeToken();
1140
1141 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001142 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001143 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001145 AssignedVal = ParseConstantExpression();
1146 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 }
1149
1150 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001151 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 LastEnumConstDecl,
1153 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001154 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001155 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 EnumConstantDecls.push_back(EnumConstDecl);
1157 LastEnumConstDecl = EnumConstDecl;
1158
Chris Lattner04d66662007-10-09 17:33:22 +00001159 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 break;
1161 SourceLocation CommaLoc = ConsumeToken();
1162
Chris Lattner04d66662007-10-09 17:33:22 +00001163 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1165 }
1166
1167 // Eat the }.
1168 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1169
Steve Naroff08d92e42007-09-15 18:49:24 +00001170 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 EnumConstantDecls.size());
1172
1173 DeclTy *AttrList = 0;
1174 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001175 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 AttrList = ParseAttributes(); // FIXME: where do they do?
1177}
1178
1179/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001180/// start of a type-qualifier-list.
1181bool Parser::isTypeQualifier() const {
1182 switch (Tok.getKind()) {
1183 default: return false;
1184 // type-qualifier
1185 case tok::kw_const:
1186 case tok::kw_volatile:
1187 case tok::kw_restrict:
1188 return true;
1189 }
1190}
1191
1192/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001193/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001194bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 switch (Tok.getKind()) {
1196 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001197
1198 case tok::identifier: // foo::bar
1199 // Annotate typenames and C++ scope specifiers. If we get one, just
1200 // recurse to handle whatever we get.
1201 if (TryAnnotateTypeOrScopeToken())
1202 return isTypeSpecifierQualifier();
1203 // Otherwise, not a type specifier.
1204 return false;
1205 case tok::coloncolon: // ::foo::bar
1206 if (NextToken().is(tok::kw_new) || // ::new
1207 NextToken().is(tok::kw_delete)) // ::delete
1208 return false;
1209
1210 // Annotate typenames and C++ scope specifiers. If we get one, just
1211 // recurse to handle whatever we get.
1212 if (TryAnnotateTypeOrScopeToken())
1213 return isTypeSpecifierQualifier();
1214 // Otherwise, not a type specifier.
1215 return false;
1216
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 // GNU attributes support.
1218 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001219 // GNU typeof support.
1220 case tok::kw_typeof:
1221
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 // type-specifiers
1223 case tok::kw_short:
1224 case tok::kw_long:
1225 case tok::kw_signed:
1226 case tok::kw_unsigned:
1227 case tok::kw__Complex:
1228 case tok::kw__Imaginary:
1229 case tok::kw_void:
1230 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001231 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 case tok::kw_int:
1233 case tok::kw_float:
1234 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001235 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 case tok::kw__Bool:
1237 case tok::kw__Decimal32:
1238 case tok::kw__Decimal64:
1239 case tok::kw__Decimal128:
1240
Chris Lattner99dc9142008-04-13 18:59:07 +00001241 // struct-or-union-specifier (C99) or class-specifier (C++)
1242 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 case tok::kw_struct:
1244 case tok::kw_union:
1245 // enum-specifier
1246 case tok::kw_enum:
1247
1248 // type-qualifier
1249 case tok::kw_const:
1250 case tok::kw_volatile:
1251 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001252
1253 // typedef-name
1254 case tok::annot_qualtypename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001256
1257 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1258 case tok::less:
1259 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001260
1261 case tok::kw___cdecl:
1262 case tok::kw___stdcall:
1263 case tok::kw___fastcall:
1264 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 }
1266}
1267
1268/// isDeclarationSpecifier() - Return true if the current token is part of a
1269/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001270bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 switch (Tok.getKind()) {
1272 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001273
1274 case tok::identifier: // foo::bar
1275 // Annotate typenames and C++ scope specifiers. If we get one, just
1276 // recurse to handle whatever we get.
1277 if (TryAnnotateTypeOrScopeToken())
1278 return isDeclarationSpecifier();
1279 // Otherwise, not a declaration specifier.
1280 return false;
1281 case tok::coloncolon: // ::foo::bar
1282 if (NextToken().is(tok::kw_new) || // ::new
1283 NextToken().is(tok::kw_delete)) // ::delete
1284 return false;
1285
1286 // Annotate typenames and C++ scope specifiers. If we get one, just
1287 // recurse to handle whatever we get.
1288 if (TryAnnotateTypeOrScopeToken())
1289 return isDeclarationSpecifier();
1290 // Otherwise, not a declaration specifier.
1291 return false;
1292
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 // storage-class-specifier
1294 case tok::kw_typedef:
1295 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001296 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001297 case tok::kw_static:
1298 case tok::kw_auto:
1299 case tok::kw_register:
1300 case tok::kw___thread:
1301
1302 // type-specifiers
1303 case tok::kw_short:
1304 case tok::kw_long:
1305 case tok::kw_signed:
1306 case tok::kw_unsigned:
1307 case tok::kw__Complex:
1308 case tok::kw__Imaginary:
1309 case tok::kw_void:
1310 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001311 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 case tok::kw_int:
1313 case tok::kw_float:
1314 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001315 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 case tok::kw__Bool:
1317 case tok::kw__Decimal32:
1318 case tok::kw__Decimal64:
1319 case tok::kw__Decimal128:
1320
Chris Lattner99dc9142008-04-13 18:59:07 +00001321 // struct-or-union-specifier (C99) or class-specifier (C++)
1322 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 case tok::kw_struct:
1324 case tok::kw_union:
1325 // enum-specifier
1326 case tok::kw_enum:
1327
1328 // type-qualifier
1329 case tok::kw_const:
1330 case tok::kw_volatile:
1331 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001332
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 // function-specifier
1334 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001335 case tok::kw_virtual:
1336 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001337
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001338 // typedef-name
1339 case tok::annot_qualtypename:
1340
Chris Lattner1ef08762007-08-09 17:01:07 +00001341 // GNU typeof support.
1342 case tok::kw_typeof:
1343
1344 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001345 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001346 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001347
1348 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1349 case tok::less:
1350 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001351
1352 case tok::kw___cdecl:
1353 case tok::kw___stdcall:
1354 case tok::kw___fastcall:
1355 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001356 }
1357}
1358
1359
1360/// ParseTypeQualifierListOpt
1361/// type-qualifier-list: [C99 6.7.5]
1362/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001363/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001364/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001365/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001366///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001367void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001368 while (1) {
1369 int isInvalid = false;
1370 const char *PrevSpec = 0;
1371 SourceLocation Loc = Tok.getLocation();
1372
1373 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 case tok::kw_const:
1375 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1376 getLang())*2;
1377 break;
1378 case tok::kw_volatile:
1379 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1380 getLang())*2;
1381 break;
1382 case tok::kw_restrict:
1383 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1384 getLang())*2;
1385 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001386 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001387 case tok::kw___cdecl:
1388 case tok::kw___stdcall:
1389 case tok::kw___fastcall:
1390 if (!PP.getLangOptions().Microsoft)
1391 goto DoneWithTypeQuals;
1392 // Just ignore it.
1393 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001395 if (AttributesAllowed) {
1396 DS.AddAttributes(ParseAttributes());
1397 continue; // do *not* consume the next token!
1398 }
1399 // otherwise, FALL THROUGH!
1400 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001401 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001402 // If this is not a type-qualifier token, we're done reading type
1403 // qualifiers. First verify that DeclSpec's are consistent.
1404 DS.Finish(Diags, PP.getSourceManager(), getLang());
1405 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001407
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 // If the specifier combination wasn't legal, issue a diagnostic.
1409 if (isInvalid) {
1410 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001411 // Pick between error or extwarn.
1412 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1413 : diag::ext_duplicate_declspec;
1414 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 }
1416 ConsumeToken();
1417 }
1418}
1419
1420
1421/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1422///
1423void Parser::ParseDeclarator(Declarator &D) {
1424 /// This implements the 'declarator' production in the C grammar, then checks
1425 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001426 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001427}
1428
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001429/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1430/// is parsed by the function passed to it. Pass null, and the direct-declarator
1431/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001432/// ptr-operator production.
1433///
Reid Spencer5f016e22007-07-11 17:01:13 +00001434/// declarator: [C99 6.7.5]
1435/// pointer[opt] direct-declarator
1436/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1437/// [GNU] '&' restrict[opt] attributes[opt] declarator
1438///
1439/// pointer: [C99 6.7.5]
1440/// '*' type-qualifier-list[opt]
1441/// '*' type-qualifier-list[opt] pointer
1442///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001443/// ptr-operator:
1444/// '*' cv-qualifier-seq[opt]
1445/// '&'
1446/// [GNU] '&' restrict[opt] attributes[opt]
1447/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001448void Parser::ParseDeclaratorInternal(Declarator &D,
1449 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 tok::TokenKind Kind = Tok.getKind();
1451
Steve Naroff5618bd42008-08-27 16:04:49 +00001452 // Not a pointer, C++ reference, or block.
1453 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001454 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001455 if (DirectDeclParser)
1456 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001457 return;
1458 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001459
Steve Naroff4ef1c992008-08-28 10:07:06 +00001460 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001461 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1462
Steve Naroff4ef1c992008-08-28 10:07:06 +00001463 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001464 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001465 DeclSpec DS;
1466
1467 ParseTypeQualifierListOpt(DS);
1468
1469 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001470 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001471 if (Kind == tok::star)
1472 // Remember that we parsed a pointer type, and remember the type-quals.
1473 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1474 DS.TakeAttributes()));
1475 else
1476 // Remember that we parsed a Block type, and remember the type-quals.
1477 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1478 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 } else {
1480 // Is a reference
1481 DeclSpec DS;
1482
1483 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1484 // cv-qualifiers are introduced through the use of a typedef or of a
1485 // template type argument, in which case the cv-qualifiers are ignored.
1486 //
1487 // [GNU] Retricted references are allowed.
1488 // [GNU] Attributes on references are allowed.
1489 ParseTypeQualifierListOpt(DS);
1490
1491 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1492 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1493 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001494 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1496 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001497 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 }
1499
1500 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001501 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001502
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001503 if (D.getNumTypeObjects() > 0) {
1504 // C++ [dcl.ref]p4: There shall be no references to references.
1505 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1506 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001507 if (const IdentifierInfo *II = D.getIdentifier())
1508 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1509 << II;
1510 else
1511 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1512 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001513
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001514 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001515 // can go ahead and build the (technically ill-formed)
1516 // declarator: reference collapsing will take care of it.
1517 }
1518 }
1519
Reid Spencer5f016e22007-07-11 17:01:13 +00001520 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001521 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1522 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 }
1524}
1525
1526/// ParseDirectDeclarator
1527/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001528/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001529/// '(' declarator ')'
1530/// [GNU] '(' attributes declarator ')'
1531/// [C90] direct-declarator '[' constant-expression[opt] ']'
1532/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1533/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1534/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1535/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1536/// direct-declarator '(' parameter-type-list ')'
1537/// direct-declarator '(' identifier-list[opt] ')'
1538/// [GNU] direct-declarator '(' parameter-forward-declarations
1539/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001540/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1541/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001542/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001543///
1544/// declarator-id: [C++ 8]
1545/// id-expression
1546/// '::'[opt] nested-name-specifier[opt] type-name
1547///
1548/// id-expression: [C++ 5.1]
1549/// unqualified-id
1550/// qualified-id [TODO]
1551///
1552/// unqualified-id: [C++ 5.1]
1553/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001554/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001555/// conversion-function-id [TODO]
1556/// '~' class-name
1557/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001558///
Reid Spencer5f016e22007-07-11 17:01:13 +00001559void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001560 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001561
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001562 if (getLang().CPlusPlus) {
1563 if (D.mayHaveIdentifier()) {
1564 bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec());
1565 if (afterCXXScope) {
1566 // Change the declaration context for name lookup, until this function
1567 // is exited (and the declarator has been parsed).
1568 DeclScopeObj.EnterDeclaratorScope();
1569 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001570
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001571 if (Tok.is(tok::identifier)) {
1572 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001573
1574 // If this identifier is followed by a '<', we may have a template-id.
1575 DeclTy *Template;
Douglas Gregor70316a02008-12-26 15:00:45 +00001576 if (NextToken().is(tok::less) &&
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001577 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1578 CurScope))) {
1579 IdentifierInfo *II = Tok.getIdentifierInfo();
1580 AnnotateTemplateIdToken(Template, 0);
1581 // FIXME: Set the declarator to a template-id. How? I don't
1582 // know... for now, just use the identifier.
1583 D.SetIdentifier(II, Tok.getLocation());
1584 }
1585 // If this identifier is the name of the current class, it's a
1586 // constructor name.
Douglas Gregor70316a02008-12-26 15:00:45 +00001587 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001588 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1589 CurScope),
1590 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001591 // This is a normal identifier.
1592 else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001593 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1594 ConsumeToken();
1595 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001596 } else if (Tok.is(tok::kw_operator)) {
1597 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001598
Douglas Gregor70316a02008-12-26 15:00:45 +00001599 // First try the name of an overloaded operator
1600 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1601 D.setOverloadedOperator(Op, OperatorLoc);
1602 } else {
1603 // This must be a conversion function (C++ [class.conv.fct]).
1604 if (TypeTy *ConvType = ParseConversionFunctionId())
1605 D.setConversionFunction(ConvType, OperatorLoc);
1606 else
1607 D.SetIdentifier(0, Tok.getLocation());
1608 }
1609 goto PastIdentifier;
1610 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001611 // This should be a C++ destructor.
1612 SourceLocation TildeLoc = ConsumeToken();
1613 if (Tok.is(tok::identifier)) {
1614 if (TypeTy *Type = ParseClassName())
1615 D.setDestructor(Type, TildeLoc);
1616 else
1617 D.SetIdentifier(0, TildeLoc);
1618 } else {
1619 Diag(Tok, diag::err_expected_class_name);
1620 D.SetIdentifier(0, TildeLoc);
1621 }
1622 goto PastIdentifier;
1623 }
1624
1625 // If we reached this point, token is not identifier and not '~'.
1626
1627 if (afterCXXScope) {
1628 Diag(Tok, diag::err_expected_unqualified_id);
1629 D.SetIdentifier(0, Tok.getLocation());
1630 D.setInvalidType(true);
1631 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001632 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001633 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001634 }
1635
1636 // If we reached this point, we are either in C/ObjC or the token didn't
1637 // satisfy any of the C++-specific checks.
1638
1639 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1640 assert(!getLang().CPlusPlus &&
1641 "There's a C++-specific check for tok::identifier above");
1642 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1643 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1644 ConsumeToken();
1645 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 // direct-declarator: '(' declarator ')'
1647 // direct-declarator: '(' attributes declarator ')'
1648 // Example: 'char (*X)' or 'int (*XX)(void)'
1649 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001650 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 // This could be something simple like "int" (in which case the declarator
1652 // portion is empty), if an abstract-declarator is allowed.
1653 D.SetIdentifier(0, Tok.getLocation());
1654 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001655 if (getLang().CPlusPlus)
1656 Diag(Tok, diag::err_expected_unqualified_id);
1657 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001658 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001660 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 }
1662
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001663 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001664 assert(D.isPastIdentifier() &&
1665 "Haven't past the location of the identifier yet?");
1666
1667 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001668 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001669 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1670 // In such a case, check if we actually have a function declarator; if it
1671 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001672 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1673 // When not in file scope, warn for ambiguous function declarators, just
1674 // in case the author intended it as a variable definition.
1675 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1676 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1677 break;
1678 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001679 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001680 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 ParseBracketDeclarator(D);
1682 } else {
1683 break;
1684 }
1685 }
1686}
1687
Chris Lattneref4715c2008-04-06 05:45:57 +00001688/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1689/// only called before the identifier, so these are most likely just grouping
1690/// parens for precedence. If we find that these are actually function
1691/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1692///
1693/// direct-declarator:
1694/// '(' declarator ')'
1695/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001696/// direct-declarator '(' parameter-type-list ')'
1697/// direct-declarator '(' identifier-list[opt] ')'
1698/// [GNU] direct-declarator '(' parameter-forward-declarations
1699/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001700///
1701void Parser::ParseParenDeclarator(Declarator &D) {
1702 SourceLocation StartLoc = ConsumeParen();
1703 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1704
Chris Lattner7399ee02008-10-20 02:05:46 +00001705 // Eat any attributes before we look at whether this is a grouping or function
1706 // declarator paren. If this is a grouping paren, the attribute applies to
1707 // the type being built up, for example:
1708 // int (__attribute__(()) *x)(long y)
1709 // If this ends up not being a grouping paren, the attribute applies to the
1710 // first argument, for example:
1711 // int (__attribute__(()) int x)
1712 // In either case, we need to eat any attributes to be able to determine what
1713 // sort of paren this is.
1714 //
1715 AttributeList *AttrList = 0;
1716 bool RequiresArg = false;
1717 if (Tok.is(tok::kw___attribute)) {
1718 AttrList = ParseAttributes();
1719
1720 // We require that the argument list (if this is a non-grouping paren) be
1721 // present even if the attribute list was empty.
1722 RequiresArg = true;
1723 }
Steve Naroff239f0732008-12-25 14:16:32 +00001724 // Eat any Microsoft extensions.
1725 if ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1726 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
1727 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001728
Chris Lattneref4715c2008-04-06 05:45:57 +00001729 // If we haven't past the identifier yet (or where the identifier would be
1730 // stored, if this is an abstract declarator), then this is probably just
1731 // grouping parens. However, if this could be an abstract-declarator, then
1732 // this could also be the start of function arguments (consider 'void()').
1733 bool isGrouping;
1734
1735 if (!D.mayOmitIdentifier()) {
1736 // If this can't be an abstract-declarator, this *must* be a grouping
1737 // paren, because we haven't seen the identifier yet.
1738 isGrouping = true;
1739 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001740 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001741 isDeclarationSpecifier()) { // 'int(int)' is a function.
1742 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1743 // considered to be a type, not a K&R identifier-list.
1744 isGrouping = false;
1745 } else {
1746 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1747 isGrouping = true;
1748 }
1749
1750 // If this is a grouping paren, handle:
1751 // direct-declarator: '(' declarator ')'
1752 // direct-declarator: '(' attributes declarator ')'
1753 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001754 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001755 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001756 if (AttrList)
1757 D.AddAttributes(AttrList);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001758
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001759 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001760 // Match the ')'.
1761 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001762
1763 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00001764 return;
1765 }
1766
1767 // Okay, if this wasn't a grouping paren, it must be the start of a function
1768 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001769 // identifier (and remember where it would have been), then call into
1770 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001771 D.SetIdentifier(0, Tok.getLocation());
1772
Chris Lattner7399ee02008-10-20 02:05:46 +00001773 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001774}
1775
1776/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1777/// declarator D up to a paren, which indicates that we are parsing function
1778/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001779///
Chris Lattner7399ee02008-10-20 02:05:46 +00001780/// If AttrList is non-null, then the caller parsed those arguments immediately
1781/// after the open paren - they should be considered to be the first argument of
1782/// a parameter. If RequiresArg is true, then the first argument of the
1783/// function is required to be present and required to not be an identifier
1784/// list.
1785///
Reid Spencer5f016e22007-07-11 17:01:13 +00001786/// This method also handles this portion of the grammar:
1787/// parameter-type-list: [C99 6.7.5]
1788/// parameter-list
1789/// parameter-list ',' '...'
1790///
1791/// parameter-list: [C99 6.7.5]
1792/// parameter-declaration
1793/// parameter-list ',' parameter-declaration
1794///
1795/// parameter-declaration: [C99 6.7.5]
1796/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001797/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001798/// [GNU] declaration-specifiers declarator attributes
1799/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001800/// [C++] declaration-specifiers abstract-declarator[opt]
1801/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001802/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1803///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001804/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1805/// and "exception-specification[opt]"(TODO).
1806///
Chris Lattner7399ee02008-10-20 02:05:46 +00001807void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1808 AttributeList *AttrList,
1809 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001810 // lparen is already consumed!
1811 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001812
Chris Lattner7399ee02008-10-20 02:05:46 +00001813 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00001814 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001815 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001816 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001817 delete AttrList;
1818 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001819
1820 ConsumeParen(); // Eat the closing ')'.
1821
1822 // cv-qualifier-seq[opt].
1823 DeclSpec DS;
1824 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001825 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001826
1827 // Parse exception-specification[opt].
1828 if (Tok.is(tok::kw_throw))
1829 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001830 }
1831
Chris Lattnerf97409f2008-04-06 06:57:35 +00001832 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001834 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00001835 /*variadic*/ false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001836 /*arglist*/ 0, 0,
1837 DS.getTypeQualifiers(),
1838 LParenLoc));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001839 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00001840 }
1841
1842 // Alternatively, this parameter list may be an identifier list form for a
1843 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001844 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner7399ee02008-10-20 02:05:46 +00001845 // K&R identifier lists can't have typedefs as identifiers, per
1846 // C99 6.7.5.3p11.
1847 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1848 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001849 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001850 delete AttrList;
1851 }
1852
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1854 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00001855 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001856 }
1857
1858 // Finally, a normal, non-empty parameter type list.
1859
1860 // Build up an array of information about the parsed arguments.
1861 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00001862
1863 // Enter function-declaration scope, limiting any declarators to the
1864 // function prototype scope, including parameter declarators.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001865 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001866
1867 bool IsVariadic = false;
1868 while (1) {
1869 if (Tok.is(tok::ellipsis)) {
1870 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001871
Chris Lattnerf97409f2008-04-06 06:57:35 +00001872 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001873 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00001874 // Otherwise, parse parameter type list. If it starts with an
1875 // ellipsis, diagnose the malformed function.
1876 Diag(Tok, diag::err_ellipsis_first_arg);
1877 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001879
Chris Lattnerf97409f2008-04-06 06:57:35 +00001880 ConsumeToken(); // Consume the ellipsis.
1881 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 }
1883
Chris Lattnerf97409f2008-04-06 06:57:35 +00001884 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001885
Chris Lattnerf97409f2008-04-06 06:57:35 +00001886 // Parse the declaration-specifiers.
1887 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00001888
1889 // If the caller parsed attributes for the first argument, add them now.
1890 if (AttrList) {
1891 DS.AddAttributes(AttrList);
1892 AttrList = 0; // Only apply the attributes to the first parameter.
1893 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001894 ParseDeclarationSpecifiers(DS);
1895
1896 // Parse the declarator. This is "PrototypeContext", because we must
1897 // accept either 'declarator' or 'abstract-declarator' here.
1898 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1899 ParseDeclarator(ParmDecl);
1900
1901 // Parse GNU attributes, if present.
1902 if (Tok.is(tok::kw___attribute))
1903 ParmDecl.AddAttributes(ParseAttributes());
1904
Chris Lattnerf97409f2008-04-06 06:57:35 +00001905 // Remember this parsed parameter in ParamInfo.
1906 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1907
Douglas Gregor72b505b2008-12-16 21:30:33 +00001908 // DefArgToks is used when the parsing of default arguments needs
1909 // to be delayed.
1910 CachedTokens *DefArgToks = 0;
1911
Chris Lattnerf97409f2008-04-06 06:57:35 +00001912 // If no parameter was specified, verify that *something* was specified,
1913 // otherwise we have a missing type and identifier.
1914 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1915 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1916 // Completely missing, emit error.
1917 Diag(DSStart, diag::err_missing_param);
1918 } else {
1919 // Otherwise, we have something. Add it and let semantic analysis try
1920 // to grok it and add the result to the ParamInfo we are building.
1921
1922 // Inform the actions module about the parameter declarator, so it gets
1923 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00001924 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1925
1926 // Parse the default argument, if any. We parse the default
1927 // arguments in all dialects; the semantic analysis in
1928 // ActOnParamDefaultArgument will reject the default argument in
1929 // C.
1930 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001931 SourceLocation EqualLoc = Tok.getLocation();
1932
Chris Lattner04421082008-04-08 04:40:51 +00001933 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00001934 if (D.getContext() == Declarator::MemberContext) {
1935 // If we're inside a class definition, cache the tokens
1936 // corresponding to the default argument. We'll actually parse
1937 // them when we see the end of the class definition.
1938 // FIXME: Templates will require something similar.
1939 // FIXME: Can we use a smart pointer for Toks?
1940 DefArgToks = new CachedTokens;
1941
1942 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1943 tok::semi, false)) {
1944 delete DefArgToks;
1945 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00001946 Actions.ActOnParamDefaultArgumentError(Param);
1947 } else
1948 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00001949 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00001950 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00001951 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00001952
1953 OwningExprResult DefArgResult(ParseAssignmentExpression());
1954 if (DefArgResult.isInvalid()) {
1955 Actions.ActOnParamDefaultArgumentError(Param);
1956 SkipUntil(tok::comma, tok::r_paren, true, true);
1957 } else {
1958 // Inform the actions module about the default argument
1959 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1960 DefArgResult.release());
1961 }
Chris Lattner04421082008-04-08 04:40:51 +00001962 }
1963 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001964
1965 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00001966 ParmDecl.getIdentifierLoc(), Param,
1967 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001968 }
1969
1970 // If the next token is a comma, consume it and keep reading arguments.
1971 if (Tok.isNot(tok::comma)) break;
1972
1973 // Consume the comma.
1974 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 }
1976
Chris Lattnerf97409f2008-04-06 06:57:35 +00001977 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001978 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00001979
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001980 // If we have the closing ')', eat it.
1981 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1982
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001983 DeclSpec DS;
1984 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001985 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001986 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001987
1988 // Parse exception-specification[opt].
1989 if (Tok.is(tok::kw_throw))
1990 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001991 }
1992
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001994 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1995 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001996 DS.getTypeQualifiers(),
Chris Lattnerf97409f2008-04-06 06:57:35 +00001997 LParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001998}
1999
Chris Lattner66d28652008-04-06 06:34:08 +00002000/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2001/// we found a K&R-style identifier list instead of a type argument list. The
2002/// current token is known to be the first identifier in the list.
2003///
2004/// identifier-list: [C99 6.7.5]
2005/// identifier
2006/// identifier-list ',' identifier
2007///
2008void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2009 Declarator &D) {
2010 // Build up an array of information about the parsed arguments.
2011 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2012 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2013
2014 // If there was no identifier specified for the declarator, either we are in
2015 // an abstract-declarator, or we are in a parameter declarator which was found
2016 // to be abstract. In abstract-declarators, identifier lists are not valid:
2017 // diagnose this.
2018 if (!D.getIdentifier())
2019 Diag(Tok, diag::ext_ident_list_in_param);
2020
2021 // Tok is known to be the first identifier in the list. Remember this
2022 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002023 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002024 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2025 Tok.getLocation(), 0));
2026
Chris Lattner50c64772008-04-06 06:39:19 +00002027 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002028
2029 while (Tok.is(tok::comma)) {
2030 // Eat the comma.
2031 ConsumeToken();
2032
Chris Lattner50c64772008-04-06 06:39:19 +00002033 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002034 if (Tok.isNot(tok::identifier)) {
2035 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002036 SkipUntil(tok::r_paren);
2037 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002038 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002039
Chris Lattner66d28652008-04-06 06:34:08 +00002040 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002041
2042 // Reject 'typedef int y; int test(x, y)', but continue parsing.
2043 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002044 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002045
2046 // Verify that the argument identifier has not already been mentioned.
2047 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002048 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002049 } else {
2050 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002051 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2052 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002053 }
Chris Lattner66d28652008-04-06 06:34:08 +00002054
2055 // Eat the identifier.
2056 ConsumeToken();
2057 }
2058
Chris Lattner50c64772008-04-06 06:39:19 +00002059 // Remember that we parsed a function type, and remember the attributes. This
2060 // function type is always a K&R style function type, which is not varargs and
2061 // has no prototype.
2062 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2063 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002064 /*TypeQuals*/0, LParenLoc));
Chris Lattner66d28652008-04-06 06:34:08 +00002065
2066 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00002067 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002068}
Chris Lattneref4715c2008-04-06 05:45:57 +00002069
Reid Spencer5f016e22007-07-11 17:01:13 +00002070/// [C90] direct-declarator '[' constant-expression[opt] ']'
2071/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2072/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2073/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2074/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2075void Parser::ParseBracketDeclarator(Declarator &D) {
2076 SourceLocation StartLoc = ConsumeBracket();
2077
Chris Lattner378c7e42008-12-18 07:27:21 +00002078 // C array syntax has many features, but by-far the most common is [] and [4].
2079 // This code does a fast path to handle some of the most obvious cases.
2080 if (Tok.getKind() == tok::r_square) {
2081 MatchRHSPunctuation(tok::r_square, StartLoc);
2082 // Remember that we parsed the empty array type.
2083 OwningExprResult NumElements(Actions);
2084 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2085 return;
2086 } else if (Tok.getKind() == tok::numeric_constant &&
2087 GetLookAheadToken(1).is(tok::r_square)) {
2088 // [4] is very common. Parse the numeric constant expression.
2089 OwningExprResult ExprRes(Actions, Actions.ActOnNumericConstant(Tok));
2090 ConsumeToken();
2091
2092 MatchRHSPunctuation(tok::r_square, StartLoc);
2093
2094 // If there was an error parsing the assignment-expression, recover.
2095 if (ExprRes.isInvalid())
2096 ExprRes.release(); // Deallocate expr, just use [].
2097
2098 // Remember that we parsed a array type, and remember its features.
2099 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2100 ExprRes.release(), StartLoc));
2101 return;
2102 }
2103
Reid Spencer5f016e22007-07-11 17:01:13 +00002104 // If valid, this location is the position where we read the 'static' keyword.
2105 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002106 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002107 StaticLoc = ConsumeToken();
2108
2109 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002110 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002111 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002112 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002113
2114 // If we haven't already read 'static', check to see if there is one after the
2115 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002116 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 StaticLoc = ConsumeToken();
2118
2119 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2120 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002121 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002122
2123 // Handle the case where we have '[*]' as the array size. However, a leading
2124 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2125 // the the token after the star is a ']'. Since stars in arrays are
2126 // infrequent, use of lookahead is not costly here.
2127 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002128 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002129
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002130 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002131 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002132 StaticLoc = SourceLocation(); // Drop the static.
2133 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002134 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002135 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002136 // Note, in C89, this production uses the constant-expr production instead
2137 // of assignment-expr. The only difference is that assignment-expr allows
2138 // things like '=' and '*='. Sema rejects these in C89 mode because they
2139 // are not i-c-e's, so we don't need to distinguish between the two here.
2140
Reid Spencer5f016e22007-07-11 17:01:13 +00002141 // Parse the assignment-expression now.
2142 NumElements = ParseAssignmentExpression();
2143 }
2144
2145 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002146 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 // If the expression was invalid, skip it.
2148 SkipUntil(tok::r_square);
2149 return;
2150 }
2151
2152 MatchRHSPunctuation(tok::r_square, StartLoc);
2153
Chris Lattner378c7e42008-12-18 07:27:21 +00002154 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2156 StaticLoc.isValid(), isStar,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002157 NumElements.release(), StartLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00002158}
2159
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002160/// [GNU] typeof-specifier:
2161/// typeof ( expressions )
2162/// typeof ( type-name )
2163/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002164///
2165void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002166 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002167 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002168 SourceLocation StartLoc = ConsumeToken();
2169
Chris Lattner04d66662007-10-09 17:33:22 +00002170 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002171 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002172 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002173 return;
2174 }
2175
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002176 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002177 if (Result.isInvalid())
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002178 return;
2179
2180 const char *PrevSpec = 0;
2181 // Check for duplicate type specifiers.
2182 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002183 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002184 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002185
2186 // FIXME: Not accurate, the range gets one token more than it should.
2187 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002188 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002189 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002190
Steve Naroffd1861fd2007-07-31 12:34:36 +00002191 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2192
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002193 if (isTypeIdInParens()) {
Steve Naroffd1861fd2007-07-31 12:34:36 +00002194 TypeTy *Ty = ParseTypeName();
2195
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002196 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2197
Chris Lattner04d66662007-10-09 17:33:22 +00002198 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002199 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002200 return;
2201 }
2202 RParenLoc = ConsumeParen();
2203 const char *PrevSpec = 0;
2204 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2205 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002206 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002207 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002208 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002209
2210 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002211 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002212 return;
2213 }
2214 RParenLoc = ConsumeParen();
2215 const char *PrevSpec = 0;
2216 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2217 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002218 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002219 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002220 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002221 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002222}
2223
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002224