blob: 114f4cb5703ac8d4f1fd538a88579ebbd664c3ba [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"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.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
Douglas Gregor5ac8aff2009-01-26 22:44:13 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
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 Redlf512e822009-01-18 18:03:53 +0000315 Actions.AddInitializerToDecl(LastDeclInGroup, move_arg(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();
Fariborz Jahanian41f2b322009-01-17 00:00:40 +0000367 // for(is key; in keys) is error.
368 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
369 Diag(Tok, diag::err_parse_error);
370 return 0;
371 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
373 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000374 // If this is an ObjC2 for-each loop, this is a successful declarator
375 // parse. The syntax for these looks like:
376 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000377 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000378 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
379 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 Diag(Tok, diag::err_parse_error);
381 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000382 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000383 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 ConsumeToken();
385 return 0;
386}
387
388/// ParseSpecifierQualifierList
389/// specifier-qualifier-list:
390/// type-specifier specifier-qualifier-list[opt]
391/// type-qualifier specifier-qualifier-list[opt]
392/// [GNU] attributes specifier-qualifier-list[opt]
393///
394void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
395 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
396 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 ParseDeclarationSpecifiers(DS);
398
399 // Validate declspec for type-name.
400 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000401 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 Diag(Tok, diag::err_typename_requires_specqual);
403
404 // Issue diagnostic and remove storage class if present.
405 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
406 if (DS.getStorageClassSpecLoc().isValid())
407 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
408 else
409 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
410 DS.ClearStorageClassSpecs();
411 }
412
413 // Issue diagnostic and remove function specfier if present.
414 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000415 if (DS.isInlineSpecified())
416 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
417 if (DS.isVirtualSpecified())
418 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
419 if (DS.isExplicitSpecified())
420 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 DS.ClearFunctionSpecs();
422 }
423}
424
425/// ParseDeclarationSpecifiers
426/// declaration-specifiers: [C99 6.7]
427/// storage-class-specifier declaration-specifiers[opt]
428/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000429/// [C99] function-specifier declaration-specifiers[opt]
430/// [GNU] attributes declaration-specifiers[opt]
431///
432/// storage-class-specifier: [C99 6.7.1]
433/// 'typedef'
434/// 'extern'
435/// 'static'
436/// 'auto'
437/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000438/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000439/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000440/// function-specifier: [C99 6.7.4]
441/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000442/// [C++] 'virtual'
443/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000444///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000445void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner5e02c472009-01-05 00:07:25 +0000446 TemplateParameterLists *TemplateParams){
Chris Lattner81c018d2008-03-13 06:29:04 +0000447 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 while (1) {
449 int isInvalid = false;
450 const char *PrevSpec = 0;
451 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000452
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000454 default:
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
Douglas Gregorb696ea32009-02-04 17:00:24 +0000487 Token Next = NextToken();
488 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
489 Next.getLocation(), CurScope, &SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000490 if (TypeRep == 0)
491 goto DoneWithDeclSpec;
492
493 ConsumeToken(); // The C++ scope.
494
495 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
496 TypeRep);
497 if (isInvalid)
498 break;
499
500 DS.SetRangeEnd(Tok.getLocation());
501 ConsumeToken(); // The typename.
502
503 continue;
504 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000505
506 case tok::annot_typename: {
507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
508 Tok.getAnnotationValue());
509 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
510 ConsumeToken(); // The typename
511
512 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
513 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
514 // Objective-C interface. If we don't have Objective-C or a '<', this is
515 // just a normal reference to a typedef name.
516 if (!Tok.is(tok::less) || !getLang().ObjC1)
517 continue;
518
519 SourceLocation EndProtoLoc;
520 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
521 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
522 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
523
524 DS.SetRangeEnd(EndProtoLoc);
525 continue;
526 }
527
Chris Lattner3bd934a2008-07-26 01:18:38 +0000528 // typedef-name
529 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000530 // In C++, check to see if this is a scope specifier like foo::bar::, if
531 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000532 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
533 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000534
Chris Lattner3bd934a2008-07-26 01:18:38 +0000535 // This identifier can only be a typedef name if we haven't already seen
536 // a type-specifier. Without this check we misparse:
537 // typedef int X; struct Y { short X; }; as 'short int'.
538 if (DS.hasTypeSpecifier())
539 goto DoneWithDeclSpec;
540
541 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000542 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
543 Tok.getLocation(), CurScope);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000544 if (TypeRep == 0)
545 goto DoneWithDeclSpec;
546
Douglas Gregorb48fe382008-10-31 09:07:45 +0000547 // C++: If the identifier is actually the name of the class type
548 // being defined and the next token is a '(', then this is a
549 // constructor declaration. We're done with the decl-specifiers
550 // and will treat this token as an identifier.
551 if (getLang().CPlusPlus &&
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000552 CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000553 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
554 NextToken().getKind() == tok::l_paren)
555 goto DoneWithDeclSpec;
556
Chris Lattner3bd934a2008-07-26 01:18:38 +0000557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
558 TypeRep);
559 if (isInvalid)
560 break;
561
562 DS.SetRangeEnd(Tok.getLocation());
563 ConsumeToken(); // The identifier
564
565 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
566 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
567 // Objective-C interface. If we don't have Objective-C or a '<', this is
568 // just a normal reference to a typedef name.
569 if (!Tok.is(tok::less) || !getLang().ObjC1)
570 continue;
571
572 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000573 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000574 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000575 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000576
577 DS.SetRangeEnd(EndProtoLoc);
578
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000579 // Need to support trailing type qualifiers (e.g. "id<p> const").
580 // If a type specifier follows, it will be diagnosed elsewhere.
581 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000582 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 // GNU attributes support.
584 case tok::kw___attribute:
585 DS.AddAttributes(ParseAttributes());
586 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000587
588 // Microsoft declspec support.
589 case tok::kw___declspec:
590 if (!PP.getLangOptions().Microsoft)
591 goto DoneWithDeclSpec;
592 FuzzyParseMicrosoftDeclSpec();
593 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000594
Steve Naroff239f0732008-12-25 14:16:32 +0000595 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000596 case tok::kw___forceinline:
597 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000598 case tok::kw___cdecl:
599 case tok::kw___stdcall:
600 case tok::kw___fastcall:
601 if (!PP.getLangOptions().Microsoft)
602 goto DoneWithDeclSpec;
603 // Just ignore it.
604 break;
605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 // storage-class-specifier
607 case tok::kw_typedef:
608 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
609 break;
610 case tok::kw_extern:
611 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000612 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
614 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000615 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000616 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
617 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000618 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 case tok::kw_static:
620 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000621 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
623 break;
624 case tok::kw_auto:
625 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
626 break;
627 case tok::kw_register:
628 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
629 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000630 case tok::kw_mutable:
631 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
632 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 case tok::kw___thread:
634 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
635 break;
636
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // function-specifier
640 case tok::kw_inline:
641 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
642 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000643 case tok::kw_virtual:
644 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
645 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000646 case tok::kw_explicit:
647 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
648 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000649
650 // type-specifier
651 case tok::kw_short:
652 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
653 break;
654 case tok::kw_long:
655 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
656 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
657 else
658 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
659 break;
660 case tok::kw_signed:
661 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
662 break;
663 case tok::kw_unsigned:
664 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
665 break;
666 case tok::kw__Complex:
667 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
668 break;
669 case tok::kw__Imaginary:
670 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
671 break;
672 case tok::kw_void:
673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
674 break;
675 case tok::kw_char:
676 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
677 break;
678 case tok::kw_int:
679 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
680 break;
681 case tok::kw_float:
682 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
683 break;
684 case tok::kw_double:
685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
686 break;
687 case tok::kw_wchar_t:
688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
689 break;
690 case tok::kw_bool:
691 case tok::kw__Bool:
692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
693 break;
694 case tok::kw__Decimal32:
695 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
696 break;
697 case tok::kw__Decimal64:
698 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
699 break;
700 case tok::kw__Decimal128:
701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
702 break;
703
704 // class-specifier:
705 case tok::kw_class:
706 case tok::kw_struct:
707 case tok::kw_union:
708 ParseClassSpecifier(DS, TemplateParams);
709 continue;
710
711 // enum-specifier:
712 case tok::kw_enum:
713 ParseEnumSpecifier(DS);
714 continue;
715
716 // cv-qualifier:
717 case tok::kw_const:
718 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
719 break;
720 case tok::kw_volatile:
721 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
722 getLang())*2;
723 break;
724 case tok::kw_restrict:
725 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
726 getLang())*2;
727 break;
728
729 // GNU typeof support.
730 case tok::kw_typeof:
731 ParseTypeofSpecifier(DS);
732 continue;
733
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000734 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000735 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000736 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
737 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000738 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000739 goto DoneWithDeclSpec;
740
741 {
742 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000743 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000744 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000745 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000746 DS.SetRangeEnd(EndProtoLoc);
747
Chris Lattner1ab3b962008-11-18 07:48:38 +0000748 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
749 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000750 // Need to support trailing type qualifiers (e.g. "id<p> const").
751 // If a type specifier follows, it will be diagnosed elsewhere.
752 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000753 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 }
755 // If the specifier combination wasn't legal, issue a diagnostic.
756 if (isInvalid) {
757 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000758 // Pick between error or extwarn.
759 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
760 : diag::ext_duplicate_declspec;
761 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000763 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 ConsumeToken();
765 }
766}
Douglas Gregoradcac882008-12-01 23:54:00 +0000767
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000768/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +0000769/// primarily follow the C++ grammar with additions for C99 and GNU,
770/// which together subsume the C grammar. Note that the C++
771/// type-specifier also includes the C type-qualifier (for const,
772/// volatile, and C99 restrict). Returns true if a type-specifier was
773/// found (and parsed), false otherwise.
774///
775/// type-specifier: [C++ 7.1.5]
776/// simple-type-specifier
777/// class-specifier
778/// enum-specifier
779/// elaborated-type-specifier [TODO]
780/// cv-qualifier
781///
782/// cv-qualifier: [C++ 7.1.5.1]
783/// 'const'
784/// 'volatile'
785/// [C99] 'restrict'
786///
787/// simple-type-specifier: [ C++ 7.1.5.2]
788/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
789/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
790/// 'char'
791/// 'wchar_t'
792/// 'bool'
793/// 'short'
794/// 'int'
795/// 'long'
796/// 'signed'
797/// 'unsigned'
798/// 'float'
799/// 'double'
800/// 'void'
801/// [C99] '_Bool'
802/// [C99] '_Complex'
803/// [C99] '_Imaginary' // Removed in TC2?
804/// [GNU] '_Decimal32'
805/// [GNU] '_Decimal64'
806/// [GNU] '_Decimal128'
807/// [GNU] typeof-specifier
808/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
809/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000810bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
811 const char *&PrevSpec,
812 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +0000813 SourceLocation Loc = Tok.getLocation();
814
815 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +0000816 case tok::identifier: // foo::bar
817 // Annotate typenames and C++ scope specifiers. If we get one, just
818 // recurse to handle whatever we get.
819 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000820 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000821 // Otherwise, not a type specifier.
822 return false;
823 case tok::coloncolon: // ::foo::bar
824 if (NextToken().is(tok::kw_new) || // ::new
825 NextToken().is(tok::kw_delete)) // ::delete
826 return false;
827
828 // Annotate typenames and C++ scope specifiers. If we get one, just
829 // recurse to handle whatever we get.
830 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000831 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000832 // Otherwise, not a type specifier.
833 return false;
834
Douglas Gregor12e083c2008-11-07 15:42:26 +0000835 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000836 case tok::annot_typename: {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000837 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000838 Tok.getAnnotationValue());
839 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
840 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000841
842 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
843 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
844 // Objective-C interface. If we don't have Objective-C or a '<', this is
845 // just a normal reference to a typedef name.
846 if (!Tok.is(tok::less) || !getLang().ObjC1)
847 return true;
848
849 SourceLocation EndProtoLoc;
850 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
851 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
852 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
853
854 DS.SetRangeEnd(EndProtoLoc);
855 return true;
856 }
857
858 case tok::kw_short:
859 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
860 break;
861 case tok::kw_long:
862 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
863 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
864 else
865 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
866 break;
867 case tok::kw_signed:
868 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
869 break;
870 case tok::kw_unsigned:
871 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
872 break;
873 case tok::kw__Complex:
874 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
875 break;
876 case tok::kw__Imaginary:
877 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
878 break;
879 case tok::kw_void:
880 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
881 break;
882 case tok::kw_char:
883 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
884 break;
885 case tok::kw_int:
886 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
887 break;
888 case tok::kw_float:
889 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
890 break;
891 case tok::kw_double:
892 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
893 break;
894 case tok::kw_wchar_t:
895 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
896 break;
897 case tok::kw_bool:
898 case tok::kw__Bool:
899 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
900 break;
901 case tok::kw__Decimal32:
902 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
903 break;
904 case tok::kw__Decimal64:
905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
906 break;
907 case tok::kw__Decimal128:
908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
909 break;
910
911 // class-specifier:
912 case tok::kw_class:
913 case tok::kw_struct:
914 case tok::kw_union:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000915 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +0000916 return true;
917
918 // enum-specifier:
919 case tok::kw_enum:
920 ParseEnumSpecifier(DS);
921 return true;
922
923 // cv-qualifier:
924 case tok::kw_const:
925 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
926 getLang())*2;
927 break;
928 case tok::kw_volatile:
929 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
930 getLang())*2;
931 break;
932 case tok::kw_restrict:
933 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
934 getLang())*2;
935 break;
936
937 // GNU typeof support.
938 case tok::kw_typeof:
939 ParseTypeofSpecifier(DS);
940 return true;
941
Steve Naroff239f0732008-12-25 14:16:32 +0000942 case tok::kw___cdecl:
943 case tok::kw___stdcall:
944 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +0000945 if (!PP.getLangOptions().Microsoft) return false;
946 ConsumeToken();
947 return true;
Steve Naroff239f0732008-12-25 14:16:32 +0000948
Douglas Gregor12e083c2008-11-07 15:42:26 +0000949 default:
950 // Not a type-specifier; do nothing.
951 return false;
952 }
953
954 // If the specifier combination wasn't legal, issue a diagnostic.
955 if (isInvalid) {
956 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000957 // Pick between error or extwarn.
958 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
959 : diag::ext_duplicate_declspec;
960 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000961 }
962 DS.SetRangeEnd(Tok.getLocation());
963 ConsumeToken(); // whatever we parsed above.
964 return true;
965}
Reid Spencer5f016e22007-07-11 17:01:13 +0000966
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000967/// ParseStructDeclaration - Parse a struct declaration without the terminating
968/// semicolon.
969///
Reid Spencer5f016e22007-07-11 17:01:13 +0000970/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000971/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000972/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000973/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000974/// struct-declarator-list:
975/// struct-declarator
976/// struct-declarator-list ',' struct-declarator
977/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
978/// struct-declarator:
979/// declarator
980/// [GNU] declarator attributes[opt]
981/// declarator[opt] ':' constant-expression
982/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
983///
Chris Lattnere1359422008-04-10 06:46:29 +0000984void Parser::
985ParseStructDeclaration(DeclSpec &DS,
986 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000987 if (Tok.is(tok::kw___extension__)) {
988 // __extension__ silences extension warnings in the subexpression.
989 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +0000990 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000991 return ParseStructDeclaration(DS, Fields);
992 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000993
994 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000995 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000996 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000997
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000998 // If there are no declarators, this is a free-standing declaration
999 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001000 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001001 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001002 return;
1003 }
1004
1005 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001006 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001007 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001008 FieldDeclarator &DeclaratorInfo = Fields.back();
1009
Steve Naroff28a7ca82007-08-20 22:28:22 +00001010 /// struct-declarator: declarator
1011 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001012 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001013 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001014
Chris Lattner04d66662007-10-09 17:33:22 +00001015 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001016 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001017 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001018 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001019 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001020 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001021 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001022 }
1023
1024 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001025 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +00001026 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +00001027
1028 // If we don't have a comma, it is either the end of the list (a ';')
1029 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001030 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001031 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001032
1033 // Consume the comma.
1034 ConsumeToken();
1035
1036 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001037 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001038
1039 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +00001040 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +00001041 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +00001042 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001043}
1044
1045/// ParseStructUnionBody
1046/// struct-contents:
1047/// struct-declaration-list
1048/// [EXT] empty
1049/// [GNU] "struct-declaration-list" without terminatoring ';'
1050/// struct-declaration-list:
1051/// struct-declaration
1052/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001053/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001054///
Reid Spencer5f016e22007-07-11 17:01:13 +00001055void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1056 unsigned TagType, DeclTy *TagDecl) {
1057 SourceLocation LBraceLoc = ConsumeBrace();
1058
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001059 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001060 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1061
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1063 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001064 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001065 Diag(Tok, diag::ext_empty_struct_union_enum)
1066 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001067
1068 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001069 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1070
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001072 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 // Each iteration of this loop reads one struct-declaration.
1074
1075 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001076 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 Diag(Tok, diag::ext_extra_struct_semi);
1078 ConsumeToken();
1079 continue;
1080 }
Chris Lattnere1359422008-04-10 06:46:29 +00001081
1082 // Parse all the comma separated declarators.
1083 DeclSpec DS;
1084 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001085 if (!Tok.is(tok::at)) {
1086 ParseStructDeclaration(DS, FieldDeclarators);
1087
1088 // Convert them all to fields.
1089 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1090 FieldDeclarator &FD = FieldDeclarators[i];
1091 // Install the declarator into the current TagDecl.
Douglas Gregor44b43212008-12-11 16:49:14 +00001092 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001093 DS.getSourceRange().getBegin(),
1094 FD.D, FD.BitfieldSize);
1095 FieldDecls.push_back(Field);
1096 }
1097 } else { // Handle @defs
1098 ConsumeToken();
1099 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1100 Diag(Tok, diag::err_unexpected_at);
1101 SkipUntil(tok::semi, true, true);
1102 continue;
1103 }
1104 ConsumeToken();
1105 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1106 if (!Tok.is(tok::identifier)) {
1107 Diag(Tok, diag::err_expected_ident);
1108 SkipUntil(tok::semi, true, true);
1109 continue;
1110 }
1111 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001112 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1113 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001114 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1115 ConsumeToken();
1116 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1117 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001118
Chris Lattner04d66662007-10-09 17:33:22 +00001119 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001121 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001122 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 break;
1124 } else {
1125 Diag(Tok, diag::err_expected_semi_decl_list);
1126 // Skip to end of block or statement
1127 SkipUntil(tok::r_brace, true, true);
1128 }
1129 }
1130
Steve Naroff60fccee2007-10-29 21:38:07 +00001131 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 AttributeList *AttrList = 0;
1134 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001135 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001136 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001137
1138 Actions.ActOnFields(CurScope,
1139 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1140 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001141 AttrList);
1142 StructScope.Exit();
1143 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001144}
1145
1146
1147/// ParseEnumSpecifier
1148/// enum-specifier: [C99 6.7.2.2]
1149/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001150///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001151/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1152/// '}' attributes[opt]
1153/// 'enum' identifier
1154/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001155///
1156/// [C++] elaborated-type-specifier:
1157/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1158///
Reid Spencer5f016e22007-07-11 17:01:13 +00001159void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001160 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 SourceLocation StartLoc = ConsumeToken();
1162
1163 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001164
1165 AttributeList *Attr = 0;
1166 // If attributes exist after tag, parse them.
1167 if (Tok.is(tok::kw___attribute))
1168 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001169
1170 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001171 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001172 if (Tok.isNot(tok::identifier)) {
1173 Diag(Tok, diag::err_expected_ident);
1174 if (Tok.isNot(tok::l_brace)) {
1175 // Has no name and is not a definition.
1176 // Skip the rest of this declarator, up until the comma or semicolon.
1177 SkipUntil(tok::comma, true);
1178 return;
1179 }
1180 }
1181 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001182
1183 // Must have either 'enum name' or 'enum {...}'.
1184 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1185 Diag(Tok, diag::err_expected_ident_lbrace);
1186
1187 // Skip the rest of this declarator, up until the comma or semicolon.
1188 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001190 }
1191
1192 // If an identifier is present, consume and remember it.
1193 IdentifierInfo *Name = 0;
1194 SourceLocation NameLoc;
1195 if (Tok.is(tok::identifier)) {
1196 Name = Tok.getIdentifierInfo();
1197 NameLoc = ConsumeToken();
1198 }
1199
1200 // There are three options here. If we have 'enum foo;', then this is a
1201 // forward declaration. If we have 'enum foo {...' then this is a
1202 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1203 //
1204 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1205 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1206 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1207 //
1208 Action::TagKind TK;
1209 if (Tok.is(tok::l_brace))
1210 TK = Action::TK_Definition;
1211 else if (Tok.is(tok::semi))
1212 TK = Action::TK_Declaration;
1213 else
1214 TK = Action::TK_Reference;
1215 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001216 SS, Name, NameLoc, Attr,
1217 Action::MultiTemplateParamsArg(Actions));
Reid Spencer5f016e22007-07-11 17:01:13 +00001218
Chris Lattner04d66662007-10-09 17:33:22 +00001219 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 ParseEnumBody(StartLoc, TagDecl);
1221
1222 // TODO: semantic analysis on the declspec for enums.
1223 const char *PrevSpec = 0;
1224 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001225 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001226}
1227
1228/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1229/// enumerator-list:
1230/// enumerator
1231/// enumerator-list ',' enumerator
1232/// enumerator:
1233/// enumeration-constant
1234/// enumeration-constant '=' constant-expression
1235/// enumeration-constant:
1236/// identifier
1237///
1238void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001239 // Enter the scope of the enum body and start the definition.
1240 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001241 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001242
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 SourceLocation LBraceLoc = ConsumeBrace();
1244
Chris Lattner7946dd32007-08-27 17:24:30 +00001245 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001246 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001247 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001248
1249 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1250
1251 DeclTy *LastEnumConstDecl = 0;
1252
1253 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001254 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1256 SourceLocation IdentLoc = ConsumeToken();
1257
1258 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001259 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001260 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001262 AssignedVal = ParseConstantExpression();
1263 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 }
1266
1267 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001268 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 LastEnumConstDecl,
1270 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001271 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001272 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 EnumConstantDecls.push_back(EnumConstDecl);
1274 LastEnumConstDecl = EnumConstDecl;
1275
Chris Lattner04d66662007-10-09 17:33:22 +00001276 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 break;
1278 SourceLocation CommaLoc = ConsumeToken();
1279
Chris Lattner04d66662007-10-09 17:33:22 +00001280 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1282 }
1283
1284 // Eat the }.
1285 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1286
Steve Naroff08d92e42007-09-15 18:49:24 +00001287 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 EnumConstantDecls.size());
1289
1290 DeclTy *AttrList = 0;
1291 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001292 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001294
1295 EnumScope.Exit();
1296 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001297}
1298
1299/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001300/// start of a type-qualifier-list.
1301bool Parser::isTypeQualifier() const {
1302 switch (Tok.getKind()) {
1303 default: return false;
1304 // type-qualifier
1305 case tok::kw_const:
1306 case tok::kw_volatile:
1307 case tok::kw_restrict:
1308 return true;
1309 }
1310}
1311
1312/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001313/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001314bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 switch (Tok.getKind()) {
1316 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001317
1318 case tok::identifier: // foo::bar
1319 // Annotate typenames and C++ scope specifiers. If we get one, just
1320 // recurse to handle whatever we get.
1321 if (TryAnnotateTypeOrScopeToken())
1322 return isTypeSpecifierQualifier();
1323 // Otherwise, not a type specifier.
1324 return false;
1325 case tok::coloncolon: // ::foo::bar
1326 if (NextToken().is(tok::kw_new) || // ::new
1327 NextToken().is(tok::kw_delete)) // ::delete
1328 return false;
1329
1330 // Annotate typenames and C++ scope specifiers. If we get one, just
1331 // recurse to handle whatever we get.
1332 if (TryAnnotateTypeOrScopeToken())
1333 return isTypeSpecifierQualifier();
1334 // Otherwise, not a type specifier.
1335 return false;
1336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 // GNU attributes support.
1338 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001339 // GNU typeof support.
1340 case tok::kw_typeof:
1341
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 // type-specifiers
1343 case tok::kw_short:
1344 case tok::kw_long:
1345 case tok::kw_signed:
1346 case tok::kw_unsigned:
1347 case tok::kw__Complex:
1348 case tok::kw__Imaginary:
1349 case tok::kw_void:
1350 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001351 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001352 case tok::kw_int:
1353 case tok::kw_float:
1354 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001355 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001356 case tok::kw__Bool:
1357 case tok::kw__Decimal32:
1358 case tok::kw__Decimal64:
1359 case tok::kw__Decimal128:
1360
Chris Lattner99dc9142008-04-13 18:59:07 +00001361 // struct-or-union-specifier (C99) or class-specifier (C++)
1362 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 case tok::kw_struct:
1364 case tok::kw_union:
1365 // enum-specifier
1366 case tok::kw_enum:
1367
1368 // type-qualifier
1369 case tok::kw_const:
1370 case tok::kw_volatile:
1371 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001372
1373 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001374 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001376
1377 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1378 case tok::less:
1379 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001380
1381 case tok::kw___cdecl:
1382 case tok::kw___stdcall:
1383 case tok::kw___fastcall:
1384 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 }
1386}
1387
1388/// isDeclarationSpecifier() - Return true if the current token is part of a
1389/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001390bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 switch (Tok.getKind()) {
1392 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001393
1394 case tok::identifier: // foo::bar
1395 // Annotate typenames and C++ scope specifiers. If we get one, just
1396 // recurse to handle whatever we get.
1397 if (TryAnnotateTypeOrScopeToken())
1398 return isDeclarationSpecifier();
1399 // Otherwise, not a declaration specifier.
1400 return false;
1401 case tok::coloncolon: // ::foo::bar
1402 if (NextToken().is(tok::kw_new) || // ::new
1403 NextToken().is(tok::kw_delete)) // ::delete
1404 return false;
1405
1406 // Annotate typenames and C++ scope specifiers. If we get one, just
1407 // recurse to handle whatever we get.
1408 if (TryAnnotateTypeOrScopeToken())
1409 return isDeclarationSpecifier();
1410 // Otherwise, not a declaration specifier.
1411 return false;
1412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // storage-class-specifier
1414 case tok::kw_typedef:
1415 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001416 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 case tok::kw_static:
1418 case tok::kw_auto:
1419 case tok::kw_register:
1420 case tok::kw___thread:
1421
1422 // type-specifiers
1423 case tok::kw_short:
1424 case tok::kw_long:
1425 case tok::kw_signed:
1426 case tok::kw_unsigned:
1427 case tok::kw__Complex:
1428 case tok::kw__Imaginary:
1429 case tok::kw_void:
1430 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001431 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 case tok::kw_int:
1433 case tok::kw_float:
1434 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001435 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 case tok::kw__Bool:
1437 case tok::kw__Decimal32:
1438 case tok::kw__Decimal64:
1439 case tok::kw__Decimal128:
1440
Chris Lattner99dc9142008-04-13 18:59:07 +00001441 // struct-or-union-specifier (C99) or class-specifier (C++)
1442 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 case tok::kw_struct:
1444 case tok::kw_union:
1445 // enum-specifier
1446 case tok::kw_enum:
1447
1448 // type-qualifier
1449 case tok::kw_const:
1450 case tok::kw_volatile:
1451 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001452
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 // function-specifier
1454 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001455 case tok::kw_virtual:
1456 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001457
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001458 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001459 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001460
Chris Lattner1ef08762007-08-09 17:01:07 +00001461 // GNU typeof support.
1462 case tok::kw_typeof:
1463
1464 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001465 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001467
1468 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1469 case tok::less:
1470 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001471
Steve Naroff47f52092009-01-06 19:34:12 +00001472 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001473 case tok::kw___cdecl:
1474 case tok::kw___stdcall:
1475 case tok::kw___fastcall:
1476 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 }
1478}
1479
1480
1481/// ParseTypeQualifierListOpt
1482/// type-qualifier-list: [C99 6.7.5]
1483/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001484/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001485/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001486/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001487///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001488void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001489 while (1) {
1490 int isInvalid = false;
1491 const char *PrevSpec = 0;
1492 SourceLocation Loc = Tok.getLocation();
1493
1494 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 case tok::kw_const:
1496 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1497 getLang())*2;
1498 break;
1499 case tok::kw_volatile:
1500 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1501 getLang())*2;
1502 break;
1503 case tok::kw_restrict:
1504 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1505 getLang())*2;
1506 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001507 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001508 case tok::kw___cdecl:
1509 case tok::kw___stdcall:
1510 case tok::kw___fastcall:
1511 if (!PP.getLangOptions().Microsoft)
1512 goto DoneWithTypeQuals;
1513 // Just ignore it.
1514 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001516 if (AttributesAllowed) {
1517 DS.AddAttributes(ParseAttributes());
1518 continue; // do *not* consume the next token!
1519 }
1520 // otherwise, FALL THROUGH!
1521 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001522 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001523 // If this is not a type-qualifier token, we're done reading type
1524 // qualifiers. First verify that DeclSpec's are consistent.
1525 DS.Finish(Diags, PP.getSourceManager(), getLang());
1526 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001527 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001528
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 // If the specifier combination wasn't legal, issue a diagnostic.
1530 if (isInvalid) {
1531 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001532 // Pick between error or extwarn.
1533 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1534 : diag::ext_duplicate_declspec;
1535 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 }
1537 ConsumeToken();
1538 }
1539}
1540
1541
1542/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1543///
1544void Parser::ParseDeclarator(Declarator &D) {
1545 /// This implements the 'declarator' production in the C grammar, then checks
1546 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001547 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001548}
1549
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001550/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1551/// is parsed by the function passed to it. Pass null, and the direct-declarator
1552/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001553/// ptr-operator production.
1554///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001555/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1556/// [C] pointer[opt] direct-declarator
1557/// [C++] direct-declarator
1558/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001559///
1560/// pointer: [C99 6.7.5]
1561/// '*' type-qualifier-list[opt]
1562/// '*' type-qualifier-list[opt] pointer
1563///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001564/// ptr-operator:
1565/// '*' cv-qualifier-seq[opt]
1566/// '&'
1567/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001568/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001569void Parser::ParseDeclaratorInternal(Declarator &D,
1570 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001571
Sebastian Redlf30208a2009-01-24 21:16:55 +00001572 // C++ member pointers start with a '::' or a nested-name.
1573 // Member pointers get special handling, since there's no place for the
1574 // scope spec in the generic path below.
1575 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1576 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1577 CXXScopeSpec SS;
1578 if (ParseOptionalCXXScopeSpecifier(SS)) {
1579 if(Tok.isNot(tok::star)) {
1580 // The scope spec really belongs to the direct-declarator.
1581 D.getCXXScopeSpec() = SS;
1582 if (DirectDeclParser)
1583 (this->*DirectDeclParser)(D);
1584 return;
1585 }
1586
1587 SourceLocation Loc = ConsumeToken();
1588 DeclSpec DS;
1589 ParseTypeQualifierListOpt(DS);
1590
1591 // Recurse to parse whatever is left.
1592 ParseDeclaratorInternal(D, DirectDeclParser);
1593
1594 // Sema will have to catch (syntactically invalid) pointers into global
1595 // scope. It has to catch pointers into namespace scope anyway.
1596 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
1597 Loc,DS.TakeAttributes()));
1598 return;
1599 }
1600 }
1601
1602 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001603 // Not a pointer, C++ reference, or block.
1604 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001605 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001606 if (DirectDeclParser)
1607 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001608 return;
1609 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001610
Steve Naroff4ef1c992008-08-28 10:07:06 +00001611 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1613
Steve Naroff4ef1c992008-08-28 10:07:06 +00001614 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001615 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001617
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 ParseTypeQualifierListOpt(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001619
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001621 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001622 if (Kind == tok::star)
1623 // Remember that we parsed a pointer type, and remember the type-quals.
1624 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1625 DS.TakeAttributes()));
1626 else
1627 // Remember that we parsed a Block type, and remember the type-quals.
1628 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1629 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 } else {
1631 // Is a reference
1632 DeclSpec DS;
1633
1634 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1635 // cv-qualifiers are introduced through the use of a typedef or of a
1636 // template type argument, in which case the cv-qualifiers are ignored.
1637 //
1638 // [GNU] Retricted references are allowed.
1639 // [GNU] Attributes on references are allowed.
1640 ParseTypeQualifierListOpt(DS);
1641
1642 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1643 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1644 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001645 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1647 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001648 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 }
1650
1651 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001652 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001653
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001654 if (D.getNumTypeObjects() > 0) {
1655 // C++ [dcl.ref]p4: There shall be no references to references.
1656 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1657 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001658 if (const IdentifierInfo *II = D.getIdentifier())
1659 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1660 << II;
1661 else
1662 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1663 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001664
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001665 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001666 // can go ahead and build the (technically ill-formed)
1667 // declarator: reference collapsing will take care of it.
1668 }
1669 }
1670
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001672 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1673 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 }
1675}
1676
1677/// ParseDirectDeclarator
1678/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001679/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001680/// '(' declarator ')'
1681/// [GNU] '(' attributes declarator ')'
1682/// [C90] direct-declarator '[' constant-expression[opt] ']'
1683/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1684/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1685/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1686/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1687/// direct-declarator '(' parameter-type-list ')'
1688/// direct-declarator '(' identifier-list[opt] ')'
1689/// [GNU] direct-declarator '(' parameter-forward-declarations
1690/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001691/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1692/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001693/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001694///
1695/// declarator-id: [C++ 8]
1696/// id-expression
1697/// '::'[opt] nested-name-specifier[opt] type-name
1698///
1699/// id-expression: [C++ 5.1]
1700/// unqualified-id
1701/// qualified-id [TODO]
1702///
1703/// unqualified-id: [C++ 5.1]
1704/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001705/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001706/// conversion-function-id [TODO]
1707/// '~' class-name
1708/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001709///
Reid Spencer5f016e22007-07-11 17:01:13 +00001710void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001711 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001712
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001713 if (getLang().CPlusPlus) {
1714 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001715 // ParseDeclaratorInternal might already have parsed the scope.
1716 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1717 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001718 if (afterCXXScope) {
1719 // Change the declaration context for name lookup, until this function
1720 // is exited (and the declarator has been parsed).
1721 DeclScopeObj.EnterDeclaratorScope();
1722 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001723
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001724 if (Tok.is(tok::identifier)) {
1725 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001726
1727 // If this identifier is followed by a '<', we may have a template-id.
1728 DeclTy *Template;
Douglas Gregor70316a02008-12-26 15:00:45 +00001729 if (NextToken().is(tok::less) &&
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001730 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1731 CurScope))) {
1732 IdentifierInfo *II = Tok.getIdentifierInfo();
1733 AnnotateTemplateIdToken(Template, 0);
1734 // FIXME: Set the declarator to a template-id. How? I don't
1735 // know... for now, just use the identifier.
1736 D.SetIdentifier(II, Tok.getLocation());
1737 }
1738 // If this identifier is the name of the current class, it's a
1739 // constructor name.
Douglas Gregor70316a02008-12-26 15:00:45 +00001740 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Steve Naroffb43a50f2009-01-28 19:39:02 +00001741 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001742 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001743 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001744 // This is a normal identifier.
1745 else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001746 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1747 ConsumeToken();
1748 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001749 } else if (Tok.is(tok::kw_operator)) {
1750 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001751
Douglas Gregor70316a02008-12-26 15:00:45 +00001752 // First try the name of an overloaded operator
1753 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1754 D.setOverloadedOperator(Op, OperatorLoc);
1755 } else {
1756 // This must be a conversion function (C++ [class.conv.fct]).
1757 if (TypeTy *ConvType = ParseConversionFunctionId())
1758 D.setConversionFunction(ConvType, OperatorLoc);
1759 else
1760 D.SetIdentifier(0, Tok.getLocation());
1761 }
1762 goto PastIdentifier;
1763 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001764 // This should be a C++ destructor.
1765 SourceLocation TildeLoc = ConsumeToken();
1766 if (Tok.is(tok::identifier)) {
1767 if (TypeTy *Type = ParseClassName())
1768 D.setDestructor(Type, TildeLoc);
1769 else
1770 D.SetIdentifier(0, TildeLoc);
1771 } else {
1772 Diag(Tok, diag::err_expected_class_name);
1773 D.SetIdentifier(0, TildeLoc);
1774 }
1775 goto PastIdentifier;
1776 }
1777
1778 // If we reached this point, token is not identifier and not '~'.
1779
1780 if (afterCXXScope) {
1781 Diag(Tok, diag::err_expected_unqualified_id);
1782 D.SetIdentifier(0, Tok.getLocation());
1783 D.setInvalidType(true);
1784 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001785 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001786 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001787 }
1788
1789 // If we reached this point, we are either in C/ObjC or the token didn't
1790 // satisfy any of the C++-specific checks.
1791
1792 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1793 assert(!getLang().CPlusPlus &&
1794 "There's a C++-specific check for tok::identifier above");
1795 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1796 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1797 ConsumeToken();
1798 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 // direct-declarator: '(' declarator ')'
1800 // direct-declarator: '(' attributes declarator ')'
1801 // Example: 'char (*X)' or 'int (*XX)(void)'
1802 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001803 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001804 // This could be something simple like "int" (in which case the declarator
1805 // portion is empty), if an abstract-declarator is allowed.
1806 D.SetIdentifier(0, Tok.getLocation());
1807 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001808 if (getLang().CPlusPlus)
1809 Diag(Tok, diag::err_expected_unqualified_id);
1810 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001811 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001812 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001813 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 }
1815
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001816 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001817 assert(D.isPastIdentifier() &&
1818 "Haven't past the location of the identifier yet?");
1819
1820 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001821 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001822 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1823 // In such a case, check if we actually have a function declarator; if it
1824 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001825 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1826 // When not in file scope, warn for ambiguous function declarators, just
1827 // in case the author intended it as a variable definition.
1828 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1829 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1830 break;
1831 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001832 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001833 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 ParseBracketDeclarator(D);
1835 } else {
1836 break;
1837 }
1838 }
1839}
1840
Chris Lattneref4715c2008-04-06 05:45:57 +00001841/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1842/// only called before the identifier, so these are most likely just grouping
1843/// parens for precedence. If we find that these are actually function
1844/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1845///
1846/// direct-declarator:
1847/// '(' declarator ')'
1848/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001849/// direct-declarator '(' parameter-type-list ')'
1850/// direct-declarator '(' identifier-list[opt] ')'
1851/// [GNU] direct-declarator '(' parameter-forward-declarations
1852/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001853///
1854void Parser::ParseParenDeclarator(Declarator &D) {
1855 SourceLocation StartLoc = ConsumeParen();
1856 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1857
Chris Lattner7399ee02008-10-20 02:05:46 +00001858 // Eat any attributes before we look at whether this is a grouping or function
1859 // declarator paren. If this is a grouping paren, the attribute applies to
1860 // the type being built up, for example:
1861 // int (__attribute__(()) *x)(long y)
1862 // If this ends up not being a grouping paren, the attribute applies to the
1863 // first argument, for example:
1864 // int (__attribute__(()) int x)
1865 // In either case, we need to eat any attributes to be able to determine what
1866 // sort of paren this is.
1867 //
1868 AttributeList *AttrList = 0;
1869 bool RequiresArg = false;
1870 if (Tok.is(tok::kw___attribute)) {
1871 AttrList = ParseAttributes();
1872
1873 // We require that the argument list (if this is a non-grouping paren) be
1874 // present even if the attribute list was empty.
1875 RequiresArg = true;
1876 }
Steve Naroff239f0732008-12-25 14:16:32 +00001877 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00001878 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1879 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00001880 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001881
Chris Lattneref4715c2008-04-06 05:45:57 +00001882 // If we haven't past the identifier yet (or where the identifier would be
1883 // stored, if this is an abstract declarator), then this is probably just
1884 // grouping parens. However, if this could be an abstract-declarator, then
1885 // this could also be the start of function arguments (consider 'void()').
1886 bool isGrouping;
1887
1888 if (!D.mayOmitIdentifier()) {
1889 // If this can't be an abstract-declarator, this *must* be a grouping
1890 // paren, because we haven't seen the identifier yet.
1891 isGrouping = true;
1892 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001893 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001894 isDeclarationSpecifier()) { // 'int(int)' is a function.
1895 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1896 // considered to be a type, not a K&R identifier-list.
1897 isGrouping = false;
1898 } else {
1899 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1900 isGrouping = true;
1901 }
1902
1903 // If this is a grouping paren, handle:
1904 // direct-declarator: '(' declarator ')'
1905 // direct-declarator: '(' attributes declarator ')'
1906 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001907 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001908 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001909 if (AttrList)
1910 D.AddAttributes(AttrList);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001911
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001912 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001913 // Match the ')'.
1914 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001915
1916 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00001917 return;
1918 }
1919
1920 // Okay, if this wasn't a grouping paren, it must be the start of a function
1921 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001922 // identifier (and remember where it would have been), then call into
1923 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001924 D.SetIdentifier(0, Tok.getLocation());
1925
Chris Lattner7399ee02008-10-20 02:05:46 +00001926 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001927}
1928
1929/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1930/// declarator D up to a paren, which indicates that we are parsing function
1931/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001932///
Chris Lattner7399ee02008-10-20 02:05:46 +00001933/// If AttrList is non-null, then the caller parsed those arguments immediately
1934/// after the open paren - they should be considered to be the first argument of
1935/// a parameter. If RequiresArg is true, then the first argument of the
1936/// function is required to be present and required to not be an identifier
1937/// list.
1938///
Reid Spencer5f016e22007-07-11 17:01:13 +00001939/// This method also handles this portion of the grammar:
1940/// parameter-type-list: [C99 6.7.5]
1941/// parameter-list
1942/// parameter-list ',' '...'
1943///
1944/// parameter-list: [C99 6.7.5]
1945/// parameter-declaration
1946/// parameter-list ',' parameter-declaration
1947///
1948/// parameter-declaration: [C99 6.7.5]
1949/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001950/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001951/// [GNU] declaration-specifiers declarator attributes
1952/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001953/// [C++] declaration-specifiers abstract-declarator[opt]
1954/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001955/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1956///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001957/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1958/// and "exception-specification[opt]"(TODO).
1959///
Chris Lattner7399ee02008-10-20 02:05:46 +00001960void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1961 AttributeList *AttrList,
1962 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001963 // lparen is already consumed!
1964 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001965
Chris Lattner7399ee02008-10-20 02:05:46 +00001966 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00001967 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001968 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001969 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001970 delete AttrList;
1971 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001972
1973 ConsumeParen(); // Eat the closing ')'.
1974
1975 // cv-qualifier-seq[opt].
1976 DeclSpec DS;
1977 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001978 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001979
1980 // Parse exception-specification[opt].
1981 if (Tok.is(tok::kw_throw))
1982 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001983 }
1984
Chris Lattnerf97409f2008-04-06 06:57:35 +00001985 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001986 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001987 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00001988 /*variadic*/ false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001989 /*arglist*/ 0, 0,
1990 DS.getTypeQualifiers(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001991 LParenLoc, D));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001992 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00001993 }
1994
1995 // Alternatively, this parameter list may be an identifier list form for a
1996 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00001997 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00001998 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001999 // K&R identifier lists can't have typedefs as identifiers, per
2000 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002001 if (RequiresArg) {
2002 Diag(Tok, diag::err_argument_required_after_attribute);
2003 delete AttrList;
2004 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002005 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2006 // normal declarators, not for abstract-declarators.
2007 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002008 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002009 }
2010
2011 // Finally, a normal, non-empty parameter type list.
2012
2013 // Build up an array of information about the parsed arguments.
2014 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002015
2016 // Enter function-declaration scope, limiting any declarators to the
2017 // function prototype scope, including parameter declarators.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002018 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002019
2020 bool IsVariadic = false;
2021 while (1) {
2022 if (Tok.is(tok::ellipsis)) {
2023 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002024
Chris Lattnerf97409f2008-04-06 06:57:35 +00002025 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002026 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002027 // Otherwise, parse parameter type list. If it starts with an
2028 // ellipsis, diagnose the malformed function.
2029 Diag(Tok, diag::err_ellipsis_first_arg);
2030 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00002032
Chris Lattnerf97409f2008-04-06 06:57:35 +00002033 ConsumeToken(); // Consume the ellipsis.
2034 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002035 }
2036
Chris Lattnerf97409f2008-04-06 06:57:35 +00002037 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002038
Chris Lattnerf97409f2008-04-06 06:57:35 +00002039 // Parse the declaration-specifiers.
2040 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002041
2042 // If the caller parsed attributes for the first argument, add them now.
2043 if (AttrList) {
2044 DS.AddAttributes(AttrList);
2045 AttrList = 0; // Only apply the attributes to the first parameter.
2046 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002047 ParseDeclarationSpecifiers(DS);
2048
2049 // Parse the declarator. This is "PrototypeContext", because we must
2050 // accept either 'declarator' or 'abstract-declarator' here.
2051 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2052 ParseDeclarator(ParmDecl);
2053
2054 // Parse GNU attributes, if present.
2055 if (Tok.is(tok::kw___attribute))
2056 ParmDecl.AddAttributes(ParseAttributes());
2057
Chris Lattnerf97409f2008-04-06 06:57:35 +00002058 // Remember this parsed parameter in ParamInfo.
2059 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2060
Douglas Gregor72b505b2008-12-16 21:30:33 +00002061 // DefArgToks is used when the parsing of default arguments needs
2062 // to be delayed.
2063 CachedTokens *DefArgToks = 0;
2064
Chris Lattnerf97409f2008-04-06 06:57:35 +00002065 // If no parameter was specified, verify that *something* was specified,
2066 // otherwise we have a missing type and identifier.
2067 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
2068 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
2069 // Completely missing, emit error.
2070 Diag(DSStart, diag::err_missing_param);
2071 } else {
2072 // Otherwise, we have something. Add it and let semantic analysis try
2073 // to grok it and add the result to the ParamInfo we are building.
2074
2075 // Inform the actions module about the parameter declarator, so it gets
2076 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00002077 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2078
2079 // Parse the default argument, if any. We parse the default
2080 // arguments in all dialects; the semantic analysis in
2081 // ActOnParamDefaultArgument will reject the default argument in
2082 // C.
2083 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002084 SourceLocation EqualLoc = Tok.getLocation();
2085
Chris Lattner04421082008-04-08 04:40:51 +00002086 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002087 if (D.getContext() == Declarator::MemberContext) {
2088 // If we're inside a class definition, cache the tokens
2089 // corresponding to the default argument. We'll actually parse
2090 // them when we see the end of the class definition.
2091 // FIXME: Templates will require something similar.
2092 // FIXME: Can we use a smart pointer for Toks?
2093 DefArgToks = new CachedTokens;
2094
2095 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2096 tok::semi, false)) {
2097 delete DefArgToks;
2098 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002099 Actions.ActOnParamDefaultArgumentError(Param);
2100 } else
2101 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002102 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002103 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002104 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002105
2106 OwningExprResult DefArgResult(ParseAssignmentExpression());
2107 if (DefArgResult.isInvalid()) {
2108 Actions.ActOnParamDefaultArgumentError(Param);
2109 SkipUntil(tok::comma, tok::r_paren, true, true);
2110 } else {
2111 // Inform the actions module about the default argument
2112 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2113 DefArgResult.release());
2114 }
Chris Lattner04421082008-04-08 04:40:51 +00002115 }
2116 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002117
2118 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002119 ParmDecl.getIdentifierLoc(), Param,
2120 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002121 }
2122
2123 // If the next token is a comma, consume it and keep reading arguments.
2124 if (Tok.isNot(tok::comma)) break;
2125
2126 // Consume the comma.
2127 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002128 }
2129
Chris Lattnerf97409f2008-04-06 06:57:35 +00002130 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002131 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002132
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002133 // If we have the closing ')', eat it.
2134 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2135
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002136 DeclSpec DS;
2137 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002138 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002139 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002140
2141 // Parse exception-specification[opt].
2142 if (Tok.is(tok::kw_throw))
2143 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002144 }
2145
Reid Spencer5f016e22007-07-11 17:01:13 +00002146 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002147 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2148 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002149 DS.getTypeQualifiers(),
Chris Lattner5af2f352009-01-20 19:11:22 +00002150 LParenLoc, D));
Reid Spencer5f016e22007-07-11 17:01:13 +00002151}
2152
Chris Lattner66d28652008-04-06 06:34:08 +00002153/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2154/// we found a K&R-style identifier list instead of a type argument list. The
2155/// current token is known to be the first identifier in the list.
2156///
2157/// identifier-list: [C99 6.7.5]
2158/// identifier
2159/// identifier-list ',' identifier
2160///
2161void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2162 Declarator &D) {
2163 // Build up an array of information about the parsed arguments.
2164 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2165 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2166
2167 // If there was no identifier specified for the declarator, either we are in
2168 // an abstract-declarator, or we are in a parameter declarator which was found
2169 // to be abstract. In abstract-declarators, identifier lists are not valid:
2170 // diagnose this.
2171 if (!D.getIdentifier())
2172 Diag(Tok, diag::ext_ident_list_in_param);
2173
2174 // Tok is known to be the first identifier in the list. Remember this
2175 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002176 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002177 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2178 Tok.getLocation(), 0));
2179
Chris Lattner50c64772008-04-06 06:39:19 +00002180 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002181
2182 while (Tok.is(tok::comma)) {
2183 // Eat the comma.
2184 ConsumeToken();
2185
Chris Lattner50c64772008-04-06 06:39:19 +00002186 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002187 if (Tok.isNot(tok::identifier)) {
2188 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002189 SkipUntil(tok::r_paren);
2190 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002191 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002192
Chris Lattner66d28652008-04-06 06:34:08 +00002193 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002194
2195 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002196 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002197 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002198
2199 // Verify that the argument identifier has not already been mentioned.
2200 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002201 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002202 } else {
2203 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002204 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2205 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002206 }
Chris Lattner66d28652008-04-06 06:34:08 +00002207
2208 // Eat the identifier.
2209 ConsumeToken();
2210 }
2211
Chris Lattner50c64772008-04-06 06:39:19 +00002212 // Remember that we parsed a function type, and remember the attributes. This
2213 // function type is always a K&R style function type, which is not varargs and
2214 // has no prototype.
2215 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2216 &ParamInfo[0], ParamInfo.size(),
Chris Lattner5af2f352009-01-20 19:11:22 +00002217 /*TypeQuals*/0, LParenLoc, D));
Chris Lattner66d28652008-04-06 06:34:08 +00002218
2219 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00002220 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002221}
Chris Lattneref4715c2008-04-06 05:45:57 +00002222
Reid Spencer5f016e22007-07-11 17:01:13 +00002223/// [C90] direct-declarator '[' constant-expression[opt] ']'
2224/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2225/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2226/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2227/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2228void Parser::ParseBracketDeclarator(Declarator &D) {
2229 SourceLocation StartLoc = ConsumeBracket();
2230
Chris Lattner378c7e42008-12-18 07:27:21 +00002231 // C array syntax has many features, but by-far the most common is [] and [4].
2232 // This code does a fast path to handle some of the most obvious cases.
2233 if (Tok.getKind() == tok::r_square) {
2234 MatchRHSPunctuation(tok::r_square, StartLoc);
2235 // Remember that we parsed the empty array type.
2236 OwningExprResult NumElements(Actions);
2237 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2238 return;
2239 } else if (Tok.getKind() == tok::numeric_constant &&
2240 GetLookAheadToken(1).is(tok::r_square)) {
2241 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002242 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002243 ConsumeToken();
2244
2245 MatchRHSPunctuation(tok::r_square, StartLoc);
2246
2247 // If there was an error parsing the assignment-expression, recover.
2248 if (ExprRes.isInvalid())
2249 ExprRes.release(); // Deallocate expr, just use [].
2250
2251 // Remember that we parsed a array type, and remember its features.
2252 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2253 ExprRes.release(), StartLoc));
2254 return;
2255 }
2256
Reid Spencer5f016e22007-07-11 17:01:13 +00002257 // If valid, this location is the position where we read the 'static' keyword.
2258 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002259 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002260 StaticLoc = ConsumeToken();
2261
2262 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002263 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002264 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002265 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002266
2267 // If we haven't already read 'static', check to see if there is one after the
2268 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002269 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002270 StaticLoc = ConsumeToken();
2271
2272 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2273 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002274 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002275
2276 // Handle the case where we have '[*]' as the array size. However, a leading
2277 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2278 // the the token after the star is a ']'. Since stars in arrays are
2279 // infrequent, use of lookahead is not costly here.
2280 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002281 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002282
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002283 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002284 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002285 StaticLoc = SourceLocation(); // Drop the static.
2286 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002287 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002288 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002289 // Note, in C89, this production uses the constant-expr production instead
2290 // of assignment-expr. The only difference is that assignment-expr allows
2291 // things like '=' and '*='. Sema rejects these in C89 mode because they
2292 // are not i-c-e's, so we don't need to distinguish between the two here.
2293
Reid Spencer5f016e22007-07-11 17:01:13 +00002294 // Parse the assignment-expression now.
2295 NumElements = ParseAssignmentExpression();
2296 }
2297
2298 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002299 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002300 // If the expression was invalid, skip it.
2301 SkipUntil(tok::r_square);
2302 return;
2303 }
2304
2305 MatchRHSPunctuation(tok::r_square, StartLoc);
2306
Chris Lattner378c7e42008-12-18 07:27:21 +00002307 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002308 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2309 StaticLoc.isValid(), isStar,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002310 NumElements.release(), StartLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00002311}
2312
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002313/// [GNU] typeof-specifier:
2314/// typeof ( expressions )
2315/// typeof ( type-name )
2316/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002317///
2318void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002319 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002320 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002321 SourceLocation StartLoc = ConsumeToken();
2322
Chris Lattner04d66662007-10-09 17:33:22 +00002323 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002324 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002325 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002326 return;
2327 }
2328
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002329 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002330 if (Result.isInvalid())
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002331 return;
2332
2333 const char *PrevSpec = 0;
2334 // Check for duplicate type specifiers.
2335 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002336 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002337 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002338
2339 // FIXME: Not accurate, the range gets one token more than it should.
2340 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002341 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002342 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002343
Steve Naroffd1861fd2007-07-31 12:34:36 +00002344 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2345
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002346 if (isTypeIdInParens()) {
Steve Naroffd1861fd2007-07-31 12:34:36 +00002347 TypeTy *Ty = ParseTypeName();
2348
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002349 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2350
Chris Lattner04d66662007-10-09 17:33:22 +00002351 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002352 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002353 return;
2354 }
2355 RParenLoc = ConsumeParen();
2356 const char *PrevSpec = 0;
2357 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2358 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002359 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002360 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002361 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002362
2363 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002364 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002365 return;
2366 }
2367 RParenLoc = ConsumeParen();
2368 const char *PrevSpec = 0;
2369 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2370 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002371 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002372 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002373 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002374 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002375}
2376
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002377