blob: 2ba1edc415efce0c9d7fd60086502641b76b45e4 [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 Redl76ad2e82009-02-05 15:02:23 +0000315 Actions.AddInitializerToDecl(LastDeclInGroup, move(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 Gregorddc29e12009-02-06 22:42:48 +00001216 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217
Chris Lattner04d66662007-10-09 17:33:22 +00001218 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 ParseEnumBody(StartLoc, TagDecl);
1220
1221 // TODO: semantic analysis on the declspec for enums.
1222 const char *PrevSpec = 0;
1223 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001224 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001225}
1226
1227/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1228/// enumerator-list:
1229/// enumerator
1230/// enumerator-list ',' enumerator
1231/// enumerator:
1232/// enumeration-constant
1233/// enumeration-constant '=' constant-expression
1234/// enumeration-constant:
1235/// identifier
1236///
1237void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001238 // Enter the scope of the enum body and start the definition.
1239 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001240 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 SourceLocation LBraceLoc = ConsumeBrace();
1243
Chris Lattner7946dd32007-08-27 17:24:30 +00001244 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001245 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001246 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001247
1248 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1249
1250 DeclTy *LastEnumConstDecl = 0;
1251
1252 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001253 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1255 SourceLocation IdentLoc = ConsumeToken();
1256
1257 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001258 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001259 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001261 AssignedVal = ParseConstantExpression();
1262 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 }
1265
1266 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001267 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 LastEnumConstDecl,
1269 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001270 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001271 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 EnumConstantDecls.push_back(EnumConstDecl);
1273 LastEnumConstDecl = EnumConstDecl;
1274
Chris Lattner04d66662007-10-09 17:33:22 +00001275 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 break;
1277 SourceLocation CommaLoc = ConsumeToken();
1278
Chris Lattner04d66662007-10-09 17:33:22 +00001279 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1281 }
1282
1283 // Eat the }.
1284 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1285
Steve Naroff08d92e42007-09-15 18:49:24 +00001286 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 EnumConstantDecls.size());
1288
1289 DeclTy *AttrList = 0;
1290 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001291 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001293
1294 EnumScope.Exit();
1295 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296}
1297
1298/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001299/// start of a type-qualifier-list.
1300bool Parser::isTypeQualifier() const {
1301 switch (Tok.getKind()) {
1302 default: return false;
1303 // type-qualifier
1304 case tok::kw_const:
1305 case tok::kw_volatile:
1306 case tok::kw_restrict:
1307 return true;
1308 }
1309}
1310
1311/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001312/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001313bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 switch (Tok.getKind()) {
1315 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001316
1317 case tok::identifier: // foo::bar
1318 // Annotate typenames and C++ scope specifiers. If we get one, just
1319 // recurse to handle whatever we get.
1320 if (TryAnnotateTypeOrScopeToken())
1321 return isTypeSpecifierQualifier();
1322 // Otherwise, not a type specifier.
1323 return false;
1324 case tok::coloncolon: // ::foo::bar
1325 if (NextToken().is(tok::kw_new) || // ::new
1326 NextToken().is(tok::kw_delete)) // ::delete
1327 return false;
1328
1329 // Annotate typenames and C++ scope specifiers. If we get one, just
1330 // recurse to handle whatever we get.
1331 if (TryAnnotateTypeOrScopeToken())
1332 return isTypeSpecifierQualifier();
1333 // Otherwise, not a type specifier.
1334 return false;
1335
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 // GNU attributes support.
1337 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001338 // GNU typeof support.
1339 case tok::kw_typeof:
1340
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // type-specifiers
1342 case tok::kw_short:
1343 case tok::kw_long:
1344 case tok::kw_signed:
1345 case tok::kw_unsigned:
1346 case tok::kw__Complex:
1347 case tok::kw__Imaginary:
1348 case tok::kw_void:
1349 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001350 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 case tok::kw_int:
1352 case tok::kw_float:
1353 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001354 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 case tok::kw__Bool:
1356 case tok::kw__Decimal32:
1357 case tok::kw__Decimal64:
1358 case tok::kw__Decimal128:
1359
Chris Lattner99dc9142008-04-13 18:59:07 +00001360 // struct-or-union-specifier (C99) or class-specifier (C++)
1361 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 case tok::kw_struct:
1363 case tok::kw_union:
1364 // enum-specifier
1365 case tok::kw_enum:
1366
1367 // type-qualifier
1368 case tok::kw_const:
1369 case tok::kw_volatile:
1370 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001371
1372 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001373 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001375
1376 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1377 case tok::less:
1378 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001379
1380 case tok::kw___cdecl:
1381 case tok::kw___stdcall:
1382 case tok::kw___fastcall:
1383 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 }
1385}
1386
1387/// isDeclarationSpecifier() - Return true if the current token is part of a
1388/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001389bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 switch (Tok.getKind()) {
1391 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001392
1393 case tok::identifier: // foo::bar
1394 // Annotate typenames and C++ scope specifiers. If we get one, just
1395 // recurse to handle whatever we get.
1396 if (TryAnnotateTypeOrScopeToken())
1397 return isDeclarationSpecifier();
1398 // Otherwise, not a declaration specifier.
1399 return false;
1400 case tok::coloncolon: // ::foo::bar
1401 if (NextToken().is(tok::kw_new) || // ::new
1402 NextToken().is(tok::kw_delete)) // ::delete
1403 return false;
1404
1405 // Annotate typenames and C++ scope specifiers. If we get one, just
1406 // recurse to handle whatever we get.
1407 if (TryAnnotateTypeOrScopeToken())
1408 return isDeclarationSpecifier();
1409 // Otherwise, not a declaration specifier.
1410 return false;
1411
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 // storage-class-specifier
1413 case tok::kw_typedef:
1414 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001415 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 case tok::kw_static:
1417 case tok::kw_auto:
1418 case tok::kw_register:
1419 case tok::kw___thread:
1420
1421 // type-specifiers
1422 case tok::kw_short:
1423 case tok::kw_long:
1424 case tok::kw_signed:
1425 case tok::kw_unsigned:
1426 case tok::kw__Complex:
1427 case tok::kw__Imaginary:
1428 case tok::kw_void:
1429 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001430 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 case tok::kw_int:
1432 case tok::kw_float:
1433 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001434 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 case tok::kw__Bool:
1436 case tok::kw__Decimal32:
1437 case tok::kw__Decimal64:
1438 case tok::kw__Decimal128:
1439
Chris Lattner99dc9142008-04-13 18:59:07 +00001440 // struct-or-union-specifier (C99) or class-specifier (C++)
1441 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 case tok::kw_struct:
1443 case tok::kw_union:
1444 // enum-specifier
1445 case tok::kw_enum:
1446
1447 // type-qualifier
1448 case tok::kw_const:
1449 case tok::kw_volatile:
1450 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001451
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 // function-specifier
1453 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001454 case tok::kw_virtual:
1455 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001456
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001457 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001458 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001459
Chris Lattner1ef08762007-08-09 17:01:07 +00001460 // GNU typeof support.
1461 case tok::kw_typeof:
1462
1463 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001464 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001465 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001466
1467 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1468 case tok::less:
1469 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001470
Steve Naroff47f52092009-01-06 19:34:12 +00001471 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001472 case tok::kw___cdecl:
1473 case tok::kw___stdcall:
1474 case tok::kw___fastcall:
1475 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 }
1477}
1478
1479
1480/// ParseTypeQualifierListOpt
1481/// type-qualifier-list: [C99 6.7.5]
1482/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001483/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001484/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001485/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001486///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001487void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 while (1) {
1489 int isInvalid = false;
1490 const char *PrevSpec = 0;
1491 SourceLocation Loc = Tok.getLocation();
1492
1493 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 case tok::kw_const:
1495 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1496 getLang())*2;
1497 break;
1498 case tok::kw_volatile:
1499 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1500 getLang())*2;
1501 break;
1502 case tok::kw_restrict:
1503 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1504 getLang())*2;
1505 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001506 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001507 case tok::kw___cdecl:
1508 case tok::kw___stdcall:
1509 case tok::kw___fastcall:
1510 if (!PP.getLangOptions().Microsoft)
1511 goto DoneWithTypeQuals;
1512 // Just ignore it.
1513 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001515 if (AttributesAllowed) {
1516 DS.AddAttributes(ParseAttributes());
1517 continue; // do *not* consume the next token!
1518 }
1519 // otherwise, FALL THROUGH!
1520 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001521 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001522 // If this is not a type-qualifier token, we're done reading type
1523 // qualifiers. First verify that DeclSpec's are consistent.
1524 DS.Finish(Diags, PP.getSourceManager(), getLang());
1525 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001527
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 // If the specifier combination wasn't legal, issue a diagnostic.
1529 if (isInvalid) {
1530 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001531 // Pick between error or extwarn.
1532 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1533 : diag::ext_duplicate_declspec;
1534 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 }
1536 ConsumeToken();
1537 }
1538}
1539
1540
1541/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1542///
1543void Parser::ParseDeclarator(Declarator &D) {
1544 /// This implements the 'declarator' production in the C grammar, then checks
1545 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001546 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001547}
1548
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001549/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1550/// is parsed by the function passed to it. Pass null, and the direct-declarator
1551/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001552/// ptr-operator production.
1553///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001554/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1555/// [C] pointer[opt] direct-declarator
1556/// [C++] direct-declarator
1557/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001558///
1559/// pointer: [C99 6.7.5]
1560/// '*' type-qualifier-list[opt]
1561/// '*' type-qualifier-list[opt] pointer
1562///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001563/// ptr-operator:
1564/// '*' cv-qualifier-seq[opt]
1565/// '&'
1566/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001567/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001568void Parser::ParseDeclaratorInternal(Declarator &D,
1569 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001570
Sebastian Redlf30208a2009-01-24 21:16:55 +00001571 // C++ member pointers start with a '::' or a nested-name.
1572 // Member pointers get special handling, since there's no place for the
1573 // scope spec in the generic path below.
1574 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1575 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1576 CXXScopeSpec SS;
1577 if (ParseOptionalCXXScopeSpecifier(SS)) {
1578 if(Tok.isNot(tok::star)) {
1579 // The scope spec really belongs to the direct-declarator.
1580 D.getCXXScopeSpec() = SS;
1581 if (DirectDeclParser)
1582 (this->*DirectDeclParser)(D);
1583 return;
1584 }
1585
1586 SourceLocation Loc = ConsumeToken();
1587 DeclSpec DS;
1588 ParseTypeQualifierListOpt(DS);
1589
1590 // Recurse to parse whatever is left.
1591 ParseDeclaratorInternal(D, DirectDeclParser);
1592
1593 // Sema will have to catch (syntactically invalid) pointers into global
1594 // scope. It has to catch pointers into namespace scope anyway.
1595 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
1596 Loc,DS.TakeAttributes()));
1597 return;
1598 }
1599 }
1600
1601 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001602 // Not a pointer, C++ reference, or block.
1603 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001604 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001605 if (DirectDeclParser)
1606 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001607 return;
1608 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001609
Steve Naroff4ef1c992008-08-28 10:07:06 +00001610 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1612
Steve Naroff4ef1c992008-08-28 10:07:06 +00001613 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001614 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001616
Reid Spencer5f016e22007-07-11 17:01:13 +00001617 ParseTypeQualifierListOpt(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001618
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001620 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001621 if (Kind == tok::star)
1622 // Remember that we parsed a pointer type, and remember the type-quals.
1623 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1624 DS.TakeAttributes()));
1625 else
1626 // Remember that we parsed a Block type, and remember the type-quals.
1627 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1628 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001629 } else {
1630 // Is a reference
1631 DeclSpec DS;
1632
1633 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1634 // cv-qualifiers are introduced through the use of a typedef or of a
1635 // template type argument, in which case the cv-qualifiers are ignored.
1636 //
1637 // [GNU] Retricted references are allowed.
1638 // [GNU] Attributes on references are allowed.
1639 ParseTypeQualifierListOpt(DS);
1640
1641 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1642 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1643 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001644 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1646 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001647 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 }
1649
1650 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001651 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001652
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001653 if (D.getNumTypeObjects() > 0) {
1654 // C++ [dcl.ref]p4: There shall be no references to references.
1655 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1656 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001657 if (const IdentifierInfo *II = D.getIdentifier())
1658 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1659 << II;
1660 else
1661 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1662 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001663
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001664 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001665 // can go ahead and build the (technically ill-formed)
1666 // declarator: reference collapsing will take care of it.
1667 }
1668 }
1669
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001671 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1672 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 }
1674}
1675
1676/// ParseDirectDeclarator
1677/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001678/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001679/// '(' declarator ')'
1680/// [GNU] '(' attributes declarator ')'
1681/// [C90] direct-declarator '[' constant-expression[opt] ']'
1682/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1683/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1684/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1685/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1686/// direct-declarator '(' parameter-type-list ')'
1687/// direct-declarator '(' identifier-list[opt] ')'
1688/// [GNU] direct-declarator '(' parameter-forward-declarations
1689/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001690/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1691/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001692/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001693///
1694/// declarator-id: [C++ 8]
1695/// id-expression
1696/// '::'[opt] nested-name-specifier[opt] type-name
1697///
1698/// id-expression: [C++ 5.1]
1699/// unqualified-id
1700/// qualified-id [TODO]
1701///
1702/// unqualified-id: [C++ 5.1]
1703/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001704/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001705/// conversion-function-id [TODO]
1706/// '~' class-name
1707/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001708///
Reid Spencer5f016e22007-07-11 17:01:13 +00001709void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001710 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001711
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001712 if (getLang().CPlusPlus) {
1713 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001714 // ParseDeclaratorInternal might already have parsed the scope.
1715 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1716 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001717 if (afterCXXScope) {
1718 // Change the declaration context for name lookup, until this function
1719 // is exited (and the declarator has been parsed).
1720 DeclScopeObj.EnterDeclaratorScope();
1721 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001722
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001723 if (Tok.is(tok::identifier)) {
1724 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001725
1726 // If this identifier is followed by a '<', we may have a template-id.
1727 DeclTy *Template;
Douglas Gregoraaba5e32009-02-04 19:02:06 +00001728 if (getLang().CPlusPlus && NextToken().is(tok::less) &&
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001729 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1730 CurScope))) {
1731 IdentifierInfo *II = Tok.getIdentifierInfo();
1732 AnnotateTemplateIdToken(Template, 0);
1733 // FIXME: Set the declarator to a template-id. How? I don't
1734 // know... for now, just use the identifier.
1735 D.SetIdentifier(II, Tok.getLocation());
1736 }
1737 // If this identifier is the name of the current class, it's a
1738 // constructor name.
Douglas Gregor70316a02008-12-26 15:00:45 +00001739 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Steve Naroffb43a50f2009-01-28 19:39:02 +00001740 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001741 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001742 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001743 // This is a normal identifier.
1744 else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001745 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1746 ConsumeToken();
1747 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001748 } else if (Tok.is(tok::kw_operator)) {
1749 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001750
Douglas Gregor70316a02008-12-26 15:00:45 +00001751 // First try the name of an overloaded operator
1752 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1753 D.setOverloadedOperator(Op, OperatorLoc);
1754 } else {
1755 // This must be a conversion function (C++ [class.conv.fct]).
1756 if (TypeTy *ConvType = ParseConversionFunctionId())
1757 D.setConversionFunction(ConvType, OperatorLoc);
1758 else
1759 D.SetIdentifier(0, Tok.getLocation());
1760 }
1761 goto PastIdentifier;
1762 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001763 // This should be a C++ destructor.
1764 SourceLocation TildeLoc = ConsumeToken();
1765 if (Tok.is(tok::identifier)) {
1766 if (TypeTy *Type = ParseClassName())
1767 D.setDestructor(Type, TildeLoc);
1768 else
1769 D.SetIdentifier(0, TildeLoc);
1770 } else {
1771 Diag(Tok, diag::err_expected_class_name);
1772 D.SetIdentifier(0, TildeLoc);
1773 }
1774 goto PastIdentifier;
1775 }
1776
1777 // If we reached this point, token is not identifier and not '~'.
1778
1779 if (afterCXXScope) {
1780 Diag(Tok, diag::err_expected_unqualified_id);
1781 D.SetIdentifier(0, Tok.getLocation());
1782 D.setInvalidType(true);
1783 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001784 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001785 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001786 }
1787
1788 // If we reached this point, we are either in C/ObjC or the token didn't
1789 // satisfy any of the C++-specific checks.
1790
1791 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1792 assert(!getLang().CPlusPlus &&
1793 "There's a C++-specific check for tok::identifier above");
1794 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1795 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1796 ConsumeToken();
1797 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 // direct-declarator: '(' declarator ')'
1799 // direct-declarator: '(' attributes declarator ')'
1800 // Example: 'char (*X)' or 'int (*XX)(void)'
1801 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001802 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 // This could be something simple like "int" (in which case the declarator
1804 // portion is empty), if an abstract-declarator is allowed.
1805 D.SetIdentifier(0, Tok.getLocation());
1806 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001807 if (getLang().CPlusPlus)
1808 Diag(Tok, diag::err_expected_unqualified_id);
1809 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001810 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001812 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 }
1814
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001815 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 assert(D.isPastIdentifier() &&
1817 "Haven't past the location of the identifier yet?");
1818
1819 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001820 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001821 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1822 // In such a case, check if we actually have a function declarator; if it
1823 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001824 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1825 // When not in file scope, warn for ambiguous function declarators, just
1826 // in case the author intended it as a variable definition.
1827 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1828 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1829 break;
1830 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001831 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001832 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 ParseBracketDeclarator(D);
1834 } else {
1835 break;
1836 }
1837 }
1838}
1839
Chris Lattneref4715c2008-04-06 05:45:57 +00001840/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1841/// only called before the identifier, so these are most likely just grouping
1842/// parens for precedence. If we find that these are actually function
1843/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1844///
1845/// direct-declarator:
1846/// '(' declarator ')'
1847/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001848/// direct-declarator '(' parameter-type-list ')'
1849/// direct-declarator '(' identifier-list[opt] ')'
1850/// [GNU] direct-declarator '(' parameter-forward-declarations
1851/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001852///
1853void Parser::ParseParenDeclarator(Declarator &D) {
1854 SourceLocation StartLoc = ConsumeParen();
1855 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1856
Chris Lattner7399ee02008-10-20 02:05:46 +00001857 // Eat any attributes before we look at whether this is a grouping or function
1858 // declarator paren. If this is a grouping paren, the attribute applies to
1859 // the type being built up, for example:
1860 // int (__attribute__(()) *x)(long y)
1861 // If this ends up not being a grouping paren, the attribute applies to the
1862 // first argument, for example:
1863 // int (__attribute__(()) int x)
1864 // In either case, we need to eat any attributes to be able to determine what
1865 // sort of paren this is.
1866 //
1867 AttributeList *AttrList = 0;
1868 bool RequiresArg = false;
1869 if (Tok.is(tok::kw___attribute)) {
1870 AttrList = ParseAttributes();
1871
1872 // We require that the argument list (if this is a non-grouping paren) be
1873 // present even if the attribute list was empty.
1874 RequiresArg = true;
1875 }
Steve Naroff239f0732008-12-25 14:16:32 +00001876 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00001877 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1878 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00001879 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001880
Chris Lattneref4715c2008-04-06 05:45:57 +00001881 // If we haven't past the identifier yet (or where the identifier would be
1882 // stored, if this is an abstract declarator), then this is probably just
1883 // grouping parens. However, if this could be an abstract-declarator, then
1884 // this could also be the start of function arguments (consider 'void()').
1885 bool isGrouping;
1886
1887 if (!D.mayOmitIdentifier()) {
1888 // If this can't be an abstract-declarator, this *must* be a grouping
1889 // paren, because we haven't seen the identifier yet.
1890 isGrouping = true;
1891 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001892 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001893 isDeclarationSpecifier()) { // 'int(int)' is a function.
1894 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1895 // considered to be a type, not a K&R identifier-list.
1896 isGrouping = false;
1897 } else {
1898 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1899 isGrouping = true;
1900 }
1901
1902 // If this is a grouping paren, handle:
1903 // direct-declarator: '(' declarator ')'
1904 // direct-declarator: '(' attributes declarator ')'
1905 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001906 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001907 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001908 if (AttrList)
1909 D.AddAttributes(AttrList);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001910
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001911 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001912 // Match the ')'.
1913 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001914
1915 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00001916 return;
1917 }
1918
1919 // Okay, if this wasn't a grouping paren, it must be the start of a function
1920 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001921 // identifier (and remember where it would have been), then call into
1922 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001923 D.SetIdentifier(0, Tok.getLocation());
1924
Chris Lattner7399ee02008-10-20 02:05:46 +00001925 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001926}
1927
1928/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1929/// declarator D up to a paren, which indicates that we are parsing function
1930/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001931///
Chris Lattner7399ee02008-10-20 02:05:46 +00001932/// If AttrList is non-null, then the caller parsed those arguments immediately
1933/// after the open paren - they should be considered to be the first argument of
1934/// a parameter. If RequiresArg is true, then the first argument of the
1935/// function is required to be present and required to not be an identifier
1936/// list.
1937///
Reid Spencer5f016e22007-07-11 17:01:13 +00001938/// This method also handles this portion of the grammar:
1939/// parameter-type-list: [C99 6.7.5]
1940/// parameter-list
1941/// parameter-list ',' '...'
1942///
1943/// parameter-list: [C99 6.7.5]
1944/// parameter-declaration
1945/// parameter-list ',' parameter-declaration
1946///
1947/// parameter-declaration: [C99 6.7.5]
1948/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001949/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001950/// [GNU] declaration-specifiers declarator attributes
1951/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001952/// [C++] declaration-specifiers abstract-declarator[opt]
1953/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001954/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1955///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001956/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1957/// and "exception-specification[opt]"(TODO).
1958///
Chris Lattner7399ee02008-10-20 02:05:46 +00001959void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1960 AttributeList *AttrList,
1961 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001962 // lparen is already consumed!
1963 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001964
Chris Lattner7399ee02008-10-20 02:05:46 +00001965 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00001966 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001967 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001968 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001969 delete AttrList;
1970 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001971
1972 ConsumeParen(); // Eat the closing ')'.
1973
1974 // cv-qualifier-seq[opt].
1975 DeclSpec DS;
1976 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001977 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001978
1979 // Parse exception-specification[opt].
1980 if (Tok.is(tok::kw_throw))
1981 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001982 }
1983
Chris Lattnerf97409f2008-04-06 06:57:35 +00001984 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001986 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00001987 /*variadic*/ false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001988 /*arglist*/ 0, 0,
1989 DS.getTypeQualifiers(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001990 LParenLoc, D));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001991 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00001992 }
1993
1994 // Alternatively, this parameter list may be an identifier list form for a
1995 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00001996 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00001997 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001998 // K&R identifier lists can't have typedefs as identifiers, per
1999 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002000 if (RequiresArg) {
2001 Diag(Tok, diag::err_argument_required_after_attribute);
2002 delete AttrList;
2003 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002004 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2005 // normal declarators, not for abstract-declarators.
2006 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002007 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002008 }
2009
2010 // Finally, a normal, non-empty parameter type list.
2011
2012 // Build up an array of information about the parsed arguments.
2013 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002014
2015 // Enter function-declaration scope, limiting any declarators to the
2016 // function prototype scope, including parameter declarators.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002017 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002018
2019 bool IsVariadic = false;
2020 while (1) {
2021 if (Tok.is(tok::ellipsis)) {
2022 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002023
Chris Lattnerf97409f2008-04-06 06:57:35 +00002024 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002025 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002026 // Otherwise, parse parameter type list. If it starts with an
2027 // ellipsis, diagnose the malformed function.
2028 Diag(Tok, diag::err_ellipsis_first_arg);
2029 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002030 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00002031
Chris Lattnerf97409f2008-04-06 06:57:35 +00002032 ConsumeToken(); // Consume the ellipsis.
2033 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 }
2035
Chris Lattnerf97409f2008-04-06 06:57:35 +00002036 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002037
Chris Lattnerf97409f2008-04-06 06:57:35 +00002038 // Parse the declaration-specifiers.
2039 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002040
2041 // If the caller parsed attributes for the first argument, add them now.
2042 if (AttrList) {
2043 DS.AddAttributes(AttrList);
2044 AttrList = 0; // Only apply the attributes to the first parameter.
2045 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002046 ParseDeclarationSpecifiers(DS);
2047
2048 // Parse the declarator. This is "PrototypeContext", because we must
2049 // accept either 'declarator' or 'abstract-declarator' here.
2050 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2051 ParseDeclarator(ParmDecl);
2052
2053 // Parse GNU attributes, if present.
2054 if (Tok.is(tok::kw___attribute))
2055 ParmDecl.AddAttributes(ParseAttributes());
2056
Chris Lattnerf97409f2008-04-06 06:57:35 +00002057 // Remember this parsed parameter in ParamInfo.
2058 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2059
Douglas Gregor72b505b2008-12-16 21:30:33 +00002060 // DefArgToks is used when the parsing of default arguments needs
2061 // to be delayed.
2062 CachedTokens *DefArgToks = 0;
2063
Chris Lattnerf97409f2008-04-06 06:57:35 +00002064 // If no parameter was specified, verify that *something* was specified,
2065 // otherwise we have a missing type and identifier.
2066 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
2067 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
2068 // Completely missing, emit error.
2069 Diag(DSStart, diag::err_missing_param);
2070 } else {
2071 // Otherwise, we have something. Add it and let semantic analysis try
2072 // to grok it and add the result to the ParamInfo we are building.
2073
2074 // Inform the actions module about the parameter declarator, so it gets
2075 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00002076 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2077
2078 // Parse the default argument, if any. We parse the default
2079 // arguments in all dialects; the semantic analysis in
2080 // ActOnParamDefaultArgument will reject the default argument in
2081 // C.
2082 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002083 SourceLocation EqualLoc = Tok.getLocation();
2084
Chris Lattner04421082008-04-08 04:40:51 +00002085 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002086 if (D.getContext() == Declarator::MemberContext) {
2087 // If we're inside a class definition, cache the tokens
2088 // corresponding to the default argument. We'll actually parse
2089 // them when we see the end of the class definition.
2090 // FIXME: Templates will require something similar.
2091 // FIXME: Can we use a smart pointer for Toks?
2092 DefArgToks = new CachedTokens;
2093
2094 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2095 tok::semi, false)) {
2096 delete DefArgToks;
2097 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002098 Actions.ActOnParamDefaultArgumentError(Param);
2099 } else
2100 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002101 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002102 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002103 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002104
2105 OwningExprResult DefArgResult(ParseAssignmentExpression());
2106 if (DefArgResult.isInvalid()) {
2107 Actions.ActOnParamDefaultArgumentError(Param);
2108 SkipUntil(tok::comma, tok::r_paren, true, true);
2109 } else {
2110 // Inform the actions module about the default argument
2111 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2112 DefArgResult.release());
2113 }
Chris Lattner04421082008-04-08 04:40:51 +00002114 }
2115 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002116
2117 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002118 ParmDecl.getIdentifierLoc(), Param,
2119 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002120 }
2121
2122 // If the next token is a comma, consume it and keep reading arguments.
2123 if (Tok.isNot(tok::comma)) break;
2124
2125 // Consume the comma.
2126 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002127 }
2128
Chris Lattnerf97409f2008-04-06 06:57:35 +00002129 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002130 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002131
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002132 // If we have the closing ')', eat it.
2133 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2134
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002135 DeclSpec DS;
2136 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002137 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002138 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002139
2140 // Parse exception-specification[opt].
2141 if (Tok.is(tok::kw_throw))
2142 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002143 }
2144
Reid Spencer5f016e22007-07-11 17:01:13 +00002145 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002146 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2147 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002148 DS.getTypeQualifiers(),
Chris Lattner5af2f352009-01-20 19:11:22 +00002149 LParenLoc, D));
Reid Spencer5f016e22007-07-11 17:01:13 +00002150}
2151
Chris Lattner66d28652008-04-06 06:34:08 +00002152/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2153/// we found a K&R-style identifier list instead of a type argument list. The
2154/// current token is known to be the first identifier in the list.
2155///
2156/// identifier-list: [C99 6.7.5]
2157/// identifier
2158/// identifier-list ',' identifier
2159///
2160void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2161 Declarator &D) {
2162 // Build up an array of information about the parsed arguments.
2163 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2164 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2165
2166 // If there was no identifier specified for the declarator, either we are in
2167 // an abstract-declarator, or we are in a parameter declarator which was found
2168 // to be abstract. In abstract-declarators, identifier lists are not valid:
2169 // diagnose this.
2170 if (!D.getIdentifier())
2171 Diag(Tok, diag::ext_ident_list_in_param);
2172
2173 // Tok is known to be the first identifier in the list. Remember this
2174 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002175 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002176 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2177 Tok.getLocation(), 0));
2178
Chris Lattner50c64772008-04-06 06:39:19 +00002179 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002180
2181 while (Tok.is(tok::comma)) {
2182 // Eat the comma.
2183 ConsumeToken();
2184
Chris Lattner50c64772008-04-06 06:39:19 +00002185 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002186 if (Tok.isNot(tok::identifier)) {
2187 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002188 SkipUntil(tok::r_paren);
2189 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002190 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002191
Chris Lattner66d28652008-04-06 06:34:08 +00002192 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002193
2194 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002195 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002196 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002197
2198 // Verify that the argument identifier has not already been mentioned.
2199 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002200 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002201 } else {
2202 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002203 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2204 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002205 }
Chris Lattner66d28652008-04-06 06:34:08 +00002206
2207 // Eat the identifier.
2208 ConsumeToken();
2209 }
2210
Chris Lattner50c64772008-04-06 06:39:19 +00002211 // Remember that we parsed a function type, and remember the attributes. This
2212 // function type is always a K&R style function type, which is not varargs and
2213 // has no prototype.
2214 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2215 &ParamInfo[0], ParamInfo.size(),
Chris Lattner5af2f352009-01-20 19:11:22 +00002216 /*TypeQuals*/0, LParenLoc, D));
Chris Lattner66d28652008-04-06 06:34:08 +00002217
2218 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00002219 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002220}
Chris Lattneref4715c2008-04-06 05:45:57 +00002221
Reid Spencer5f016e22007-07-11 17:01:13 +00002222/// [C90] direct-declarator '[' constant-expression[opt] ']'
2223/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2224/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2225/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2226/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2227void Parser::ParseBracketDeclarator(Declarator &D) {
2228 SourceLocation StartLoc = ConsumeBracket();
2229
Chris Lattner378c7e42008-12-18 07:27:21 +00002230 // C array syntax has many features, but by-far the most common is [] and [4].
2231 // This code does a fast path to handle some of the most obvious cases.
2232 if (Tok.getKind() == tok::r_square) {
2233 MatchRHSPunctuation(tok::r_square, StartLoc);
2234 // Remember that we parsed the empty array type.
2235 OwningExprResult NumElements(Actions);
2236 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2237 return;
2238 } else if (Tok.getKind() == tok::numeric_constant &&
2239 GetLookAheadToken(1).is(tok::r_square)) {
2240 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002241 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002242 ConsumeToken();
2243
2244 MatchRHSPunctuation(tok::r_square, StartLoc);
2245
2246 // If there was an error parsing the assignment-expression, recover.
2247 if (ExprRes.isInvalid())
2248 ExprRes.release(); // Deallocate expr, just use [].
2249
2250 // Remember that we parsed a array type, and remember its features.
2251 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2252 ExprRes.release(), StartLoc));
2253 return;
2254 }
2255
Reid Spencer5f016e22007-07-11 17:01:13 +00002256 // If valid, this location is the position where we read the 'static' keyword.
2257 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002258 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002259 StaticLoc = ConsumeToken();
2260
2261 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002262 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002263 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002264 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002265
2266 // If we haven't already read 'static', check to see if there is one after the
2267 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002268 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002269 StaticLoc = ConsumeToken();
2270
2271 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2272 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002273 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002274
2275 // Handle the case where we have '[*]' as the array size. However, a leading
2276 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2277 // the the token after the star is a ']'. Since stars in arrays are
2278 // infrequent, use of lookahead is not costly here.
2279 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002280 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002281
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002282 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002283 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002284 StaticLoc = SourceLocation(); // Drop the static.
2285 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002286 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002287 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002288 // Note, in C89, this production uses the constant-expr production instead
2289 // of assignment-expr. The only difference is that assignment-expr allows
2290 // things like '=' and '*='. Sema rejects these in C89 mode because they
2291 // are not i-c-e's, so we don't need to distinguish between the two here.
2292
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 // Parse the assignment-expression now.
2294 NumElements = ParseAssignmentExpression();
2295 }
2296
2297 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002298 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002299 // If the expression was invalid, skip it.
2300 SkipUntil(tok::r_square);
2301 return;
2302 }
2303
2304 MatchRHSPunctuation(tok::r_square, StartLoc);
2305
Chris Lattner378c7e42008-12-18 07:27:21 +00002306 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002307 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2308 StaticLoc.isValid(), isStar,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002309 NumElements.release(), StartLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00002310}
2311
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002312/// [GNU] typeof-specifier:
2313/// typeof ( expressions )
2314/// typeof ( type-name )
2315/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002316///
2317void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002318 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002319 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002320 SourceLocation StartLoc = ConsumeToken();
2321
Chris Lattner04d66662007-10-09 17:33:22 +00002322 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002323 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002324 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002325 return;
2326 }
2327
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002328 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002329 if (Result.isInvalid())
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002330 return;
2331
2332 const char *PrevSpec = 0;
2333 // Check for duplicate type specifiers.
2334 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002335 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002336 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002337
2338 // FIXME: Not accurate, the range gets one token more than it should.
2339 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002340 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002341 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002342
Steve Naroffd1861fd2007-07-31 12:34:36 +00002343 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2344
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002345 if (isTypeIdInParens()) {
Steve Naroffd1861fd2007-07-31 12:34:36 +00002346 TypeTy *Ty = ParseTypeName();
2347
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002348 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2349
Chris Lattner04d66662007-10-09 17:33:22 +00002350 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002351 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002352 return;
2353 }
2354 RParenLoc = ConsumeParen();
2355 const char *PrevSpec = 0;
2356 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2357 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002358 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002359 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002360 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002361
2362 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002363 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002364 return;
2365 }
2366 RParenLoc = ConsumeParen();
2367 const char *PrevSpec = 0;
2368 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2369 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002370 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002371 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002372 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002373 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002374}
2375
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002376