blob: 0ac9e08982293d5d41bbf156673bbf126987061e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlcee63fb2008-12-02 14:43:59 +000031Parser::TypeTy *Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Sebastian Redlcee63fb2008-12-02 14:43:59 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Reid Spencer5f016e22007-07-11 17:01:13 +000041}
42
43/// ParseAttributes - Parse a non-empty attributes list.
44///
45/// [GNU] attributes:
46/// attribute
47/// attributes attribute
48///
49/// [GNU] attribute:
50/// '__attribute__' '(' '(' attribute-list ')' ')'
51///
52/// [GNU] attribute-list:
53/// attrib
54/// attribute_list ',' attrib
55///
56/// [GNU] attrib:
57/// empty
58/// attrib-name
59/// attrib-name '(' identifier ')'
60/// attrib-name '(' identifier ',' nonempty-expr-list ')'
61/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
62///
63/// [GNU] attrib-name:
64/// identifier
65/// typespec
66/// typequal
67/// storageclass
68///
69/// FIXME: The GCC grammar/code for this construct implies we need two
70/// token lookahead. Comment from gcc: "If they start with an identifier
71/// which is followed by a comma or close parenthesis, then the arguments
72/// start with that identifier; otherwise they are an expression list."
73///
74/// At the moment, I am not doing 2 token lookahead. I am also unaware of
75/// any attributes that don't work (based on my limited testing). Most
76/// attributes are very simple in practice. Until we find a bug, I don't see
77/// a pressing need to implement the 2 token lookahead.
78
79AttributeList *Parser::ParseAttributes() {
Chris Lattner04d66662007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner04d66662007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000085 ConsumeToken();
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
87 "attribute")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000098
Chris Lattner04d66662007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
101 ConsumeToken();
102 continue;
103 }
104 // we have an identifier or declaration specifier (const, int, etc.)
105 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
106 SourceLocation AttrNameLoc = ConsumeToken();
107
108 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 // __attribute__(( mode(byte) ))
118 ConsumeParen(); // ignore the right paren loc for now
119 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
120 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 }
Chris Lattner04d66662007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 // __attribute__(( nonnull() ))
151 ConsumeParen(); // ignore the right paren loc for now
152 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
153 0, SourceLocation(), 0, 0, CurrAttr);
154 } else {
155 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Chris Lattner04d66662007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 CurrAttr);
179 }
180 }
181 }
182 } else {
183 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
184 0, SourceLocation(), 0, 0, CurrAttr);
185 }
186 }
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
190 SkipUntil(tok::r_paren, false);
191 }
192 return CurrAttr;
193}
194
Steve Narofff59e17e2008-12-24 20:59:21 +0000195/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
196/// routine is called to skip/ignore tokens that comprise the MS declspec.
197void Parser::FuzzyParseMicrosoftDeclSpec() {
198 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
199 ConsumeToken();
200 if (Tok.is(tok::l_paren)) {
201 unsigned short savedParenCount = ParenCount;
202 do {
203 ConsumeAnyToken();
204 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
205 }
206 return;
207}
208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209/// ParseDeclaration - Parse a full 'declaration', which consists of
210/// declaration-specifiers, some number of declarators, and a semicolon.
211/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000212///
213/// declaration: [C99 6.7]
214/// block-declaration ->
215/// simple-declaration
216/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000217/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000218/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000219/// [C++] using-directive
220/// [C++] using-declaration [TODO]
Chris Lattner8f08cb72007-08-25 06:57:03 +0000221/// others... [FIXME]
222///
Reid Spencer5f016e22007-07-11 17:01:13 +0000223Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000224 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000225 case tok::kw_export:
226 case tok::kw_template:
227 return ParseTemplateDeclaration(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000228 case tok::kw_namespace:
229 return ParseNamespace(Context);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000230 case tok::kw_using:
231 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000232 default:
233 return ParseSimpleDeclaration(Context);
234 }
235}
236
237/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
238/// declaration-specifiers init-declarator-list[opt] ';'
239///[C90/C++]init-declarator-list ';' [TODO]
240/// [OMP] threadprivate-directive [TODO]
241Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 // Parse the common declaration-specifiers piece.
243 DeclSpec DS;
244 ParseDeclarationSpecifiers(DS);
245
246 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
247 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000248 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 ConsumeToken();
250 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
251 }
252
253 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
254 ParseDeclarator(DeclaratorInfo);
255
256 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
257}
258
Chris Lattner8f08cb72007-08-25 06:57:03 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
261/// parsing 'declaration-specifiers declarator'. This method is split out this
262/// way to handle the ambiguity between top-level function-definitions and
263/// declarations.
264///
Reid Spencer5f016e22007-07-11 17:01:13 +0000265/// init-declarator-list: [C99 6.7]
266/// init-declarator
267/// init-declarator-list ',' init-declarator
268/// init-declarator: [C99 6.7]
269/// declarator
270/// declarator '=' initializer
271/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
272/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000273/// [C++] declarator initializer[opt]
274///
275/// [C++] initializer:
276/// [C++] '=' initializer-clause
277/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000278///
279Parser::DeclTy *Parser::
280ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
281
282 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
283 // that they can be chained properly if the actions want this.
284 Parser::DeclTy *LastDeclInGroup = 0;
285
286 // At this point, we know that it is not a function definition. Parse the
287 // rest of the init-declarator-list.
288 while (1) {
289 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000290 if (Tok.is(tok::kw_asm)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000291 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000292 if (AsmLabel.isInvalid()) {
Daniel Dunbara80f8742008-08-05 01:35:17 +0000293 SkipUntil(tok::semi);
294 return 0;
295 }
Daniel Dunbar914701e2008-08-05 16:28:08 +0000296
Sebastian Redleffa8d12008-12-10 00:02:53 +0000297 D.setAsmLabel(AsmLabel.release());
Daniel Dunbara80f8742008-08-05 01:35:17 +0000298 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000299
300 // If attributes are present, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000301 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 D.AddAttributes(ParseAttributes());
Steve Naroffbb204692007-09-12 14:07:44 +0000303
304 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000305 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000308 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000310 OwningExprResult Init(ParseInitializer());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000311 if (Init.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 SkipUntil(tok::semi);
313 return 0;
314 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000315 Actions.AddInitializerToDecl(LastDeclInGroup, move_arg(Init));
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000316 } else if (Tok.is(tok::l_paren)) {
317 // Parse C++ direct initializer: '(' expression-list ')'
318 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000319 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000320 CommaLocsTy CommaLocs;
321
322 bool InvalidExpr = false;
323 if (ParseExpressionList(Exprs, CommaLocs)) {
324 SkipUntil(tok::r_paren);
325 InvalidExpr = true;
326 }
327 // Match the ')'.
328 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
329
330 if (!InvalidExpr) {
331 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
332 "Unexpected number of commas!");
333 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000334 Exprs.take(), Exprs.size(),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000335 &CommaLocs[0], RParenLoc);
336 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000337 } else {
338 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 }
340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // If we don't have a comma, it is either the end of the list (a ';') or an
342 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000343 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 break;
345
346 // Consume the comma.
347 ConsumeToken();
348
349 // Parse the next declarator.
350 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000351
352 // Accept attributes in an init-declarator. In the first declarator in a
353 // declaration, these would be part of the declspec. In subsequent
354 // declarators, they become part of the declarator itself, so that they
355 // don't apply to declarators after *this* one. Examples:
356 // short __attribute__((common)) var; -> declspec
357 // short var __attribute__((common)); -> declarator
358 // short x, __attribute__((common)) var; -> declarator
359 if (Tok.is(tok::kw___attribute))
360 D.AddAttributes(ParseAttributes());
361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 ParseDeclarator(D);
363 }
364
Chris Lattner04d66662007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 ConsumeToken();
Fariborz Jahanian41f2b322009-01-17 00:00:40 +0000367 // for(is key; in keys) is error.
368 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
369 Diag(Tok, diag::err_parse_error);
370 return 0;
371 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
373 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000374 // If this is an ObjC2 for-each loop, this is a successful declarator
375 // parse. The syntax for these looks like:
376 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000377 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000378 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
379 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 Diag(Tok, diag::err_parse_error);
381 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000382 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000383 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 ConsumeToken();
385 return 0;
386}
387
388/// ParseSpecifierQualifierList
389/// specifier-qualifier-list:
390/// type-specifier specifier-qualifier-list[opt]
391/// type-qualifier specifier-qualifier-list[opt]
392/// [GNU] attributes specifier-qualifier-list[opt]
393///
394void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
395 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
396 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 ParseDeclarationSpecifiers(DS);
398
399 // Validate declspec for type-name.
400 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000401 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 Diag(Tok, diag::err_typename_requires_specqual);
403
404 // Issue diagnostic and remove storage class if present.
405 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
406 if (DS.getStorageClassSpecLoc().isValid())
407 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
408 else
409 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
410 DS.ClearStorageClassSpecs();
411 }
412
413 // Issue diagnostic and remove function specfier if present.
414 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000415 if (DS.isInlineSpecified())
416 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
417 if (DS.isVirtualSpecified())
418 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
419 if (DS.isExplicitSpecified())
420 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 DS.ClearFunctionSpecs();
422 }
423}
424
425/// ParseDeclarationSpecifiers
426/// declaration-specifiers: [C99 6.7]
427/// storage-class-specifier declaration-specifiers[opt]
428/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000429/// [C99] function-specifier declaration-specifiers[opt]
430/// [GNU] attributes declaration-specifiers[opt]
431///
432/// storage-class-specifier: [C99 6.7.1]
433/// 'typedef'
434/// 'extern'
435/// 'static'
436/// 'auto'
437/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000438/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000439/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000440/// function-specifier: [C99 6.7.4]
441/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000442/// [C++] 'virtual'
443/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000444///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000445void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner5e02c472009-01-05 00:07:25 +0000446 TemplateParameterLists *TemplateParams){
Chris Lattner81c018d2008-03-13 06:29:04 +0000447 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 while (1) {
449 int isInvalid = false;
450 const char *PrevSpec = 0;
451 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000452
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000454 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000455 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 // If this is not a declaration specifier token, we're done reading decl
457 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000458 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000460
461 case tok::coloncolon: // ::foo::bar
462 // Annotate C++ scope specifiers. If we get one, loop.
463 if (TryAnnotateCXXScopeToken())
464 continue;
465 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000466
467 case tok::annot_cxxscope: {
468 if (DS.hasTypeSpecifier())
469 goto DoneWithDeclSpec;
470
471 // We are looking for a qualified typename.
472 if (NextToken().isNot(tok::identifier))
473 goto DoneWithDeclSpec;
474
475 CXXScopeSpec SS;
476 SS.setScopeRep(Tok.getAnnotationValue());
477 SS.setRange(Tok.getAnnotationRange());
478
479 // If the next token is the name of the class type that the C++ scope
480 // denotes, followed by a '(', then this is a constructor declaration.
481 // We're done with the decl-specifiers.
482 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
483 CurScope, &SS) &&
484 GetLookAheadToken(2).is(tok::l_paren))
485 goto DoneWithDeclSpec;
486
487 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
488 CurScope, &SS);
489 if (TypeRep == 0)
490 goto DoneWithDeclSpec;
491
492 ConsumeToken(); // The C++ scope.
493
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
495 TypeRep);
496 if (isInvalid)
497 break;
498
499 DS.SetRangeEnd(Tok.getLocation());
500 ConsumeToken(); // The typename.
501
502 continue;
503 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000504
505 case tok::annot_typename: {
506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
507 Tok.getAnnotationValue());
508 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
509 ConsumeToken(); // The typename
510
511 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
512 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
513 // Objective-C interface. If we don't have Objective-C or a '<', this is
514 // just a normal reference to a typedef name.
515 if (!Tok.is(tok::less) || !getLang().ObjC1)
516 continue;
517
518 SourceLocation EndProtoLoc;
519 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
520 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
521 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
522
523 DS.SetRangeEnd(EndProtoLoc);
524 continue;
525 }
526
Chris Lattner3bd934a2008-07-26 01:18:38 +0000527 // typedef-name
528 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000529 // In C++, check to see if this is a scope specifier like foo::bar::, if
530 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000531 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
532 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000533
Chris Lattner3bd934a2008-07-26 01:18:38 +0000534 // This identifier can only be a typedef name if we haven't already seen
535 // a type-specifier. Without this check we misparse:
536 // typedef int X; struct Y { short X; }; as 'short int'.
537 if (DS.hasTypeSpecifier())
538 goto DoneWithDeclSpec;
539
540 // It has to be available as a typedef too!
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000541 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000542 if (TypeRep == 0)
543 goto DoneWithDeclSpec;
544
Douglas Gregorb48fe382008-10-31 09:07:45 +0000545 // C++: If the identifier is actually the name of the class type
546 // being defined and the next token is a '(', then this is a
547 // constructor declaration. We're done with the decl-specifiers
548 // and will treat this token as an identifier.
549 if (getLang().CPlusPlus &&
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000550 CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000551 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
552 NextToken().getKind() == tok::l_paren)
553 goto DoneWithDeclSpec;
554
Chris Lattner3bd934a2008-07-26 01:18:38 +0000555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
556 TypeRep);
557 if (isInvalid)
558 break;
559
560 DS.SetRangeEnd(Tok.getLocation());
561 ConsumeToken(); // The identifier
562
563 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
564 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
565 // Objective-C interface. If we don't have Objective-C or a '<', this is
566 // just a normal reference to a typedef name.
567 if (!Tok.is(tok::less) || !getLang().ObjC1)
568 continue;
569
570 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000571 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000572 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000573 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000574
575 DS.SetRangeEnd(EndProtoLoc);
576
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000577 // Need to support trailing type qualifiers (e.g. "id<p> const").
578 // If a type specifier follows, it will be diagnosed elsewhere.
579 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000580 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 // GNU attributes support.
582 case tok::kw___attribute:
583 DS.AddAttributes(ParseAttributes());
584 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000585
586 // Microsoft declspec support.
587 case tok::kw___declspec:
588 if (!PP.getLangOptions().Microsoft)
589 goto DoneWithDeclSpec;
590 FuzzyParseMicrosoftDeclSpec();
591 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000592
Steve Naroff239f0732008-12-25 14:16:32 +0000593 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000594 case tok::kw___forceinline:
595 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000596 case tok::kw___cdecl:
597 case tok::kw___stdcall:
598 case tok::kw___fastcall:
599 if (!PP.getLangOptions().Microsoft)
600 goto DoneWithDeclSpec;
601 // Just ignore it.
602 break;
603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 // storage-class-specifier
605 case tok::kw_typedef:
606 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
607 break;
608 case tok::kw_extern:
609 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000610 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
612 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000613 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000614 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
615 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000616 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 case tok::kw_static:
618 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000619 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
621 break;
622 case tok::kw_auto:
623 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
624 break;
625 case tok::kw_register:
626 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
627 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000628 case tok::kw_mutable:
629 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
630 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 case tok::kw___thread:
632 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
633 break;
634
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000636
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 // function-specifier
638 case tok::kw_inline:
639 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
640 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000641 case tok::kw_virtual:
642 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
643 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000644 case tok::kw_explicit:
645 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
646 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000647
648 // type-specifier
649 case tok::kw_short:
650 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
651 break;
652 case tok::kw_long:
653 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
654 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
655 else
656 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
657 break;
658 case tok::kw_signed:
659 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
660 break;
661 case tok::kw_unsigned:
662 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
663 break;
664 case tok::kw__Complex:
665 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
666 break;
667 case tok::kw__Imaginary:
668 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
669 break;
670 case tok::kw_void:
671 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
672 break;
673 case tok::kw_char:
674 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
675 break;
676 case tok::kw_int:
677 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
678 break;
679 case tok::kw_float:
680 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
681 break;
682 case tok::kw_double:
683 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
684 break;
685 case tok::kw_wchar_t:
686 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
687 break;
688 case tok::kw_bool:
689 case tok::kw__Bool:
690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
691 break;
692 case tok::kw__Decimal32:
693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
694 break;
695 case tok::kw__Decimal64:
696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
697 break;
698 case tok::kw__Decimal128:
699 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
700 break;
701
702 // class-specifier:
703 case tok::kw_class:
704 case tok::kw_struct:
705 case tok::kw_union:
706 ParseClassSpecifier(DS, TemplateParams);
707 continue;
708
709 // enum-specifier:
710 case tok::kw_enum:
711 ParseEnumSpecifier(DS);
712 continue;
713
714 // cv-qualifier:
715 case tok::kw_const:
716 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
717 break;
718 case tok::kw_volatile:
719 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
720 getLang())*2;
721 break;
722 case tok::kw_restrict:
723 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
724 getLang())*2;
725 break;
726
727 // GNU typeof support.
728 case tok::kw_typeof:
729 ParseTypeofSpecifier(DS);
730 continue;
731
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000732 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000733 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000734 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
735 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000736 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000737 goto DoneWithDeclSpec;
738
739 {
740 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000741 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000742 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000743 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000744 DS.SetRangeEnd(EndProtoLoc);
745
Chris Lattner1ab3b962008-11-18 07:48:38 +0000746 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
747 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000748 // Need to support trailing type qualifiers (e.g. "id<p> const").
749 // If a type specifier follows, it will be diagnosed elsewhere.
750 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000751 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 }
753 // If the specifier combination wasn't legal, issue a diagnostic.
754 if (isInvalid) {
755 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000756 // Pick between error or extwarn.
757 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
758 : diag::ext_duplicate_declspec;
759 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000761 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 ConsumeToken();
763 }
764}
Douglas Gregoradcac882008-12-01 23:54:00 +0000765
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000766/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +0000767/// primarily follow the C++ grammar with additions for C99 and GNU,
768/// which together subsume the C grammar. Note that the C++
769/// type-specifier also includes the C type-qualifier (for const,
770/// volatile, and C99 restrict). Returns true if a type-specifier was
771/// found (and parsed), false otherwise.
772///
773/// type-specifier: [C++ 7.1.5]
774/// simple-type-specifier
775/// class-specifier
776/// enum-specifier
777/// elaborated-type-specifier [TODO]
778/// cv-qualifier
779///
780/// cv-qualifier: [C++ 7.1.5.1]
781/// 'const'
782/// 'volatile'
783/// [C99] 'restrict'
784///
785/// simple-type-specifier: [ C++ 7.1.5.2]
786/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
787/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
788/// 'char'
789/// 'wchar_t'
790/// 'bool'
791/// 'short'
792/// 'int'
793/// 'long'
794/// 'signed'
795/// 'unsigned'
796/// 'float'
797/// 'double'
798/// 'void'
799/// [C99] '_Bool'
800/// [C99] '_Complex'
801/// [C99] '_Imaginary' // Removed in TC2?
802/// [GNU] '_Decimal32'
803/// [GNU] '_Decimal64'
804/// [GNU] '_Decimal128'
805/// [GNU] typeof-specifier
806/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
807/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000808bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
809 const char *&PrevSpec,
810 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +0000811 SourceLocation Loc = Tok.getLocation();
812
813 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +0000814 case tok::identifier: // foo::bar
815 // Annotate typenames and C++ scope specifiers. If we get one, just
816 // recurse to handle whatever we get.
817 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000818 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000819 // Otherwise, not a type specifier.
820 return false;
821 case tok::coloncolon: // ::foo::bar
822 if (NextToken().is(tok::kw_new) || // ::new
823 NextToken().is(tok::kw_delete)) // ::delete
824 return false;
825
826 // Annotate typenames and C++ scope specifiers. If we get one, just
827 // recurse to handle whatever we get.
828 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000829 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000830 // Otherwise, not a type specifier.
831 return false;
832
Douglas Gregor12e083c2008-11-07 15:42:26 +0000833 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000834 case tok::annot_typename: {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000835 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000836 Tok.getAnnotationValue());
837 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
838 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000839
840 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
841 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
842 // Objective-C interface. If we don't have Objective-C or a '<', this is
843 // just a normal reference to a typedef name.
844 if (!Tok.is(tok::less) || !getLang().ObjC1)
845 return true;
846
847 SourceLocation EndProtoLoc;
848 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
849 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
850 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
851
852 DS.SetRangeEnd(EndProtoLoc);
853 return true;
854 }
855
856 case tok::kw_short:
857 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
858 break;
859 case tok::kw_long:
860 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
861 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
862 else
863 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
864 break;
865 case tok::kw_signed:
866 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
867 break;
868 case tok::kw_unsigned:
869 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
870 break;
871 case tok::kw__Complex:
872 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
873 break;
874 case tok::kw__Imaginary:
875 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
876 break;
877 case tok::kw_void:
878 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
879 break;
880 case tok::kw_char:
881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
882 break;
883 case tok::kw_int:
884 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
885 break;
886 case tok::kw_float:
887 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
888 break;
889 case tok::kw_double:
890 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
891 break;
892 case tok::kw_wchar_t:
893 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
894 break;
895 case tok::kw_bool:
896 case tok::kw__Bool:
897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
898 break;
899 case tok::kw__Decimal32:
900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
901 break;
902 case tok::kw__Decimal64:
903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
904 break;
905 case tok::kw__Decimal128:
906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
907 break;
908
909 // class-specifier:
910 case tok::kw_class:
911 case tok::kw_struct:
912 case tok::kw_union:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000913 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +0000914 return true;
915
916 // enum-specifier:
917 case tok::kw_enum:
918 ParseEnumSpecifier(DS);
919 return true;
920
921 // cv-qualifier:
922 case tok::kw_const:
923 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
924 getLang())*2;
925 break;
926 case tok::kw_volatile:
927 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
928 getLang())*2;
929 break;
930 case tok::kw_restrict:
931 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
932 getLang())*2;
933 break;
934
935 // GNU typeof support.
936 case tok::kw_typeof:
937 ParseTypeofSpecifier(DS);
938 return true;
939
Steve Naroff239f0732008-12-25 14:16:32 +0000940 case tok::kw___cdecl:
941 case tok::kw___stdcall:
942 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +0000943 if (!PP.getLangOptions().Microsoft) return false;
944 ConsumeToken();
945 return true;
Steve Naroff239f0732008-12-25 14:16:32 +0000946
Douglas Gregor12e083c2008-11-07 15:42:26 +0000947 default:
948 // Not a type-specifier; do nothing.
949 return false;
950 }
951
952 // If the specifier combination wasn't legal, issue a diagnostic.
953 if (isInvalid) {
954 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000955 // Pick between error or extwarn.
956 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
957 : diag::ext_duplicate_declspec;
958 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000959 }
960 DS.SetRangeEnd(Tok.getLocation());
961 ConsumeToken(); // whatever we parsed above.
962 return true;
963}
Reid Spencer5f016e22007-07-11 17:01:13 +0000964
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000965/// ParseStructDeclaration - Parse a struct declaration without the terminating
966/// semicolon.
967///
Reid Spencer5f016e22007-07-11 17:01:13 +0000968/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000969/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000970/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000971/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000972/// struct-declarator-list:
973/// struct-declarator
974/// struct-declarator-list ',' struct-declarator
975/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
976/// struct-declarator:
977/// declarator
978/// [GNU] declarator attributes[opt]
979/// declarator[opt] ':' constant-expression
980/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
981///
Chris Lattnere1359422008-04-10 06:46:29 +0000982void Parser::
983ParseStructDeclaration(DeclSpec &DS,
984 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000985 if (Tok.is(tok::kw___extension__)) {
986 // __extension__ silences extension warnings in the subexpression.
987 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +0000988 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000989 return ParseStructDeclaration(DS, Fields);
990 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000991
992 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000993 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000994 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000995
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000996 // If there are no declarators, this is a free-standing declaration
997 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +0000998 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000999 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001000 return;
1001 }
1002
1003 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001004 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001005 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001006 FieldDeclarator &DeclaratorInfo = Fields.back();
1007
Steve Naroff28a7ca82007-08-20 22:28:22 +00001008 /// struct-declarator: declarator
1009 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001010 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001011 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001012
Chris Lattner04d66662007-10-09 17:33:22 +00001013 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001014 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001015 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001016 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001017 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001018 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001019 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001020 }
1021
1022 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001023 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +00001024 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +00001025
1026 // If we don't have a comma, it is either the end of the list (a ';')
1027 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001028 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001029 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001030
1031 // Consume the comma.
1032 ConsumeToken();
1033
1034 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001035 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001036
1037 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +00001038 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +00001039 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +00001040 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001041}
1042
1043/// ParseStructUnionBody
1044/// struct-contents:
1045/// struct-declaration-list
1046/// [EXT] empty
1047/// [GNU] "struct-declaration-list" without terminatoring ';'
1048/// struct-declaration-list:
1049/// struct-declaration
1050/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001051/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001052///
Reid Spencer5f016e22007-07-11 17:01:13 +00001053void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1054 unsigned TagType, DeclTy *TagDecl) {
1055 SourceLocation LBraceLoc = ConsumeBrace();
1056
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001057 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001058 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1059
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1061 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001062 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001063 Diag(Tok, diag::ext_empty_struct_union_enum)
1064 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001065
1066 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001067 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1068
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001070 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 // Each iteration of this loop reads one struct-declaration.
1072
1073 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001074 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 Diag(Tok, diag::ext_extra_struct_semi);
1076 ConsumeToken();
1077 continue;
1078 }
Chris Lattnere1359422008-04-10 06:46:29 +00001079
1080 // Parse all the comma separated declarators.
1081 DeclSpec DS;
1082 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001083 if (!Tok.is(tok::at)) {
1084 ParseStructDeclaration(DS, FieldDeclarators);
1085
1086 // Convert them all to fields.
1087 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1088 FieldDeclarator &FD = FieldDeclarators[i];
1089 // Install the declarator into the current TagDecl.
Douglas Gregor44b43212008-12-11 16:49:14 +00001090 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001091 DS.getSourceRange().getBegin(),
1092 FD.D, FD.BitfieldSize);
1093 FieldDecls.push_back(Field);
1094 }
1095 } else { // Handle @defs
1096 ConsumeToken();
1097 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1098 Diag(Tok, diag::err_unexpected_at);
1099 SkipUntil(tok::semi, true, true);
1100 continue;
1101 }
1102 ConsumeToken();
1103 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1104 if (!Tok.is(tok::identifier)) {
1105 Diag(Tok, diag::err_expected_ident);
1106 SkipUntil(tok::semi, true, true);
1107 continue;
1108 }
1109 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001110 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1111 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001112 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1113 ConsumeToken();
1114 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1115 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001116
Chris Lattner04d66662007-10-09 17:33:22 +00001117 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001119 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001120 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 break;
1122 } else {
1123 Diag(Tok, diag::err_expected_semi_decl_list);
1124 // Skip to end of block or statement
1125 SkipUntil(tok::r_brace, true, true);
1126 }
1127 }
1128
Steve Naroff60fccee2007-10-29 21:38:07 +00001129 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 AttributeList *AttrList = 0;
1132 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001133 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001134 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001135
1136 Actions.ActOnFields(CurScope,
1137 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1138 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001139 AttrList);
1140 StructScope.Exit();
1141 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142}
1143
1144
1145/// ParseEnumSpecifier
1146/// enum-specifier: [C99 6.7.2.2]
1147/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001148///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001149/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1150/// '}' attributes[opt]
1151/// 'enum' identifier
1152/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001153///
1154/// [C++] elaborated-type-specifier:
1155/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1156///
Reid Spencer5f016e22007-07-11 17:01:13 +00001157void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001158 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 SourceLocation StartLoc = ConsumeToken();
1160
1161 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001162
1163 AttributeList *Attr = 0;
1164 // If attributes exist after tag, parse them.
1165 if (Tok.is(tok::kw___attribute))
1166 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001167
1168 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001169 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001170 if (Tok.isNot(tok::identifier)) {
1171 Diag(Tok, diag::err_expected_ident);
1172 if (Tok.isNot(tok::l_brace)) {
1173 // Has no name and is not a definition.
1174 // Skip the rest of this declarator, up until the comma or semicolon.
1175 SkipUntil(tok::comma, true);
1176 return;
1177 }
1178 }
1179 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001180
1181 // Must have either 'enum name' or 'enum {...}'.
1182 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1183 Diag(Tok, diag::err_expected_ident_lbrace);
1184
1185 // Skip the rest of this declarator, up until the comma or semicolon.
1186 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001188 }
1189
1190 // If an identifier is present, consume and remember it.
1191 IdentifierInfo *Name = 0;
1192 SourceLocation NameLoc;
1193 if (Tok.is(tok::identifier)) {
1194 Name = Tok.getIdentifierInfo();
1195 NameLoc = ConsumeToken();
1196 }
1197
1198 // There are three options here. If we have 'enum foo;', then this is a
1199 // forward declaration. If we have 'enum foo {...' then this is a
1200 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1201 //
1202 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1203 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1204 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1205 //
1206 Action::TagKind TK;
1207 if (Tok.is(tok::l_brace))
1208 TK = Action::TK_Definition;
1209 else if (Tok.is(tok::semi))
1210 TK = Action::TK_Declaration;
1211 else
1212 TK = Action::TK_Reference;
1213 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001214 SS, Name, NameLoc, Attr,
1215 Action::MultiTemplateParamsArg(Actions));
Reid Spencer5f016e22007-07-11 17:01:13 +00001216
Chris Lattner04d66662007-10-09 17:33:22 +00001217 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 ParseEnumBody(StartLoc, TagDecl);
1219
1220 // TODO: semantic analysis on the declspec for enums.
1221 const char *PrevSpec = 0;
1222 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001223 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001224}
1225
1226/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1227/// enumerator-list:
1228/// enumerator
1229/// enumerator-list ',' enumerator
1230/// enumerator:
1231/// enumeration-constant
1232/// enumeration-constant '=' constant-expression
1233/// enumeration-constant:
1234/// identifier
1235///
1236void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001237 // Enter the scope of the enum body and start the definition.
1238 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001239 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 SourceLocation LBraceLoc = ConsumeBrace();
1242
Chris Lattner7946dd32007-08-27 17:24:30 +00001243 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001244 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001245 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001246
1247 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1248
1249 DeclTy *LastEnumConstDecl = 0;
1250
1251 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001252 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1254 SourceLocation IdentLoc = ConsumeToken();
1255
1256 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001257 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001258 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001260 AssignedVal = ParseConstantExpression();
1261 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 }
1264
1265 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001266 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 LastEnumConstDecl,
1268 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001269 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001270 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 EnumConstantDecls.push_back(EnumConstDecl);
1272 LastEnumConstDecl = EnumConstDecl;
1273
Chris Lattner04d66662007-10-09 17:33:22 +00001274 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 break;
1276 SourceLocation CommaLoc = ConsumeToken();
1277
Chris Lattner04d66662007-10-09 17:33:22 +00001278 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1280 }
1281
1282 // Eat the }.
1283 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1284
Steve Naroff08d92e42007-09-15 18:49:24 +00001285 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 EnumConstantDecls.size());
1287
1288 DeclTy *AttrList = 0;
1289 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001290 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001292
1293 EnumScope.Exit();
1294 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001295}
1296
1297/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001298/// start of a type-qualifier-list.
1299bool Parser::isTypeQualifier() const {
1300 switch (Tok.getKind()) {
1301 default: return false;
1302 // type-qualifier
1303 case tok::kw_const:
1304 case tok::kw_volatile:
1305 case tok::kw_restrict:
1306 return true;
1307 }
1308}
1309
1310/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001311/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001312bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 switch (Tok.getKind()) {
1314 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001315
1316 case tok::identifier: // foo::bar
1317 // Annotate typenames and C++ scope specifiers. If we get one, just
1318 // recurse to handle whatever we get.
1319 if (TryAnnotateTypeOrScopeToken())
1320 return isTypeSpecifierQualifier();
1321 // Otherwise, not a type specifier.
1322 return false;
1323 case tok::coloncolon: // ::foo::bar
1324 if (NextToken().is(tok::kw_new) || // ::new
1325 NextToken().is(tok::kw_delete)) // ::delete
1326 return false;
1327
1328 // Annotate typenames and C++ scope specifiers. If we get one, just
1329 // recurse to handle whatever we get.
1330 if (TryAnnotateTypeOrScopeToken())
1331 return isTypeSpecifierQualifier();
1332 // Otherwise, not a type specifier.
1333 return false;
1334
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // GNU attributes support.
1336 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001337 // GNU typeof support.
1338 case tok::kw_typeof:
1339
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 // type-specifiers
1341 case tok::kw_short:
1342 case tok::kw_long:
1343 case tok::kw_signed:
1344 case tok::kw_unsigned:
1345 case tok::kw__Complex:
1346 case tok::kw__Imaginary:
1347 case tok::kw_void:
1348 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001349 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 case tok::kw_int:
1351 case tok::kw_float:
1352 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001353 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001354 case tok::kw__Bool:
1355 case tok::kw__Decimal32:
1356 case tok::kw__Decimal64:
1357 case tok::kw__Decimal128:
1358
Chris Lattner99dc9142008-04-13 18:59:07 +00001359 // struct-or-union-specifier (C99) or class-specifier (C++)
1360 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 case tok::kw_struct:
1362 case tok::kw_union:
1363 // enum-specifier
1364 case tok::kw_enum:
1365
1366 // type-qualifier
1367 case tok::kw_const:
1368 case tok::kw_volatile:
1369 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001370
1371 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001372 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001374
1375 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1376 case tok::less:
1377 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001378
1379 case tok::kw___cdecl:
1380 case tok::kw___stdcall:
1381 case tok::kw___fastcall:
1382 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 }
1384}
1385
1386/// isDeclarationSpecifier() - Return true if the current token is part of a
1387/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001388bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 switch (Tok.getKind()) {
1390 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001391
1392 case tok::identifier: // foo::bar
1393 // Annotate typenames and C++ scope specifiers. If we get one, just
1394 // recurse to handle whatever we get.
1395 if (TryAnnotateTypeOrScopeToken())
1396 return isDeclarationSpecifier();
1397 // Otherwise, not a declaration specifier.
1398 return false;
1399 case tok::coloncolon: // ::foo::bar
1400 if (NextToken().is(tok::kw_new) || // ::new
1401 NextToken().is(tok::kw_delete)) // ::delete
1402 return false;
1403
1404 // Annotate typenames and C++ scope specifiers. If we get one, just
1405 // recurse to handle whatever we get.
1406 if (TryAnnotateTypeOrScopeToken())
1407 return isDeclarationSpecifier();
1408 // Otherwise, not a declaration specifier.
1409 return false;
1410
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 // storage-class-specifier
1412 case tok::kw_typedef:
1413 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001414 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 case tok::kw_static:
1416 case tok::kw_auto:
1417 case tok::kw_register:
1418 case tok::kw___thread:
1419
1420 // type-specifiers
1421 case tok::kw_short:
1422 case tok::kw_long:
1423 case tok::kw_signed:
1424 case tok::kw_unsigned:
1425 case tok::kw__Complex:
1426 case tok::kw__Imaginary:
1427 case tok::kw_void:
1428 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001429 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 case tok::kw_int:
1431 case tok::kw_float:
1432 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001433 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001434 case tok::kw__Bool:
1435 case tok::kw__Decimal32:
1436 case tok::kw__Decimal64:
1437 case tok::kw__Decimal128:
1438
Chris Lattner99dc9142008-04-13 18:59:07 +00001439 // struct-or-union-specifier (C99) or class-specifier (C++)
1440 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 case tok::kw_struct:
1442 case tok::kw_union:
1443 // enum-specifier
1444 case tok::kw_enum:
1445
1446 // type-qualifier
1447 case tok::kw_const:
1448 case tok::kw_volatile:
1449 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001450
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 // function-specifier
1452 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001453 case tok::kw_virtual:
1454 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001455
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001456 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001457 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001458
Chris Lattner1ef08762007-08-09 17:01:07 +00001459 // GNU typeof support.
1460 case tok::kw_typeof:
1461
1462 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001463 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001465
1466 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1467 case tok::less:
1468 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001469
Steve Naroff47f52092009-01-06 19:34:12 +00001470 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001471 case tok::kw___cdecl:
1472 case tok::kw___stdcall:
1473 case tok::kw___fastcall:
1474 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 }
1476}
1477
1478
1479/// ParseTypeQualifierListOpt
1480/// type-qualifier-list: [C99 6.7.5]
1481/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001482/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001483/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001484/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001485///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001486void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 while (1) {
1488 int isInvalid = false;
1489 const char *PrevSpec = 0;
1490 SourceLocation Loc = Tok.getLocation();
1491
1492 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 case tok::kw_const:
1494 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1495 getLang())*2;
1496 break;
1497 case tok::kw_volatile:
1498 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1499 getLang())*2;
1500 break;
1501 case tok::kw_restrict:
1502 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1503 getLang())*2;
1504 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001505 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001506 case tok::kw___cdecl:
1507 case tok::kw___stdcall:
1508 case tok::kw___fastcall:
1509 if (!PP.getLangOptions().Microsoft)
1510 goto DoneWithTypeQuals;
1511 // Just ignore it.
1512 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001514 if (AttributesAllowed) {
1515 DS.AddAttributes(ParseAttributes());
1516 continue; // do *not* consume the next token!
1517 }
1518 // otherwise, FALL THROUGH!
1519 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001520 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001521 // If this is not a type-qualifier token, we're done reading type
1522 // qualifiers. First verify that DeclSpec's are consistent.
1523 DS.Finish(Diags, PP.getSourceManager(), getLang());
1524 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001526
Reid Spencer5f016e22007-07-11 17:01:13 +00001527 // If the specifier combination wasn't legal, issue a diagnostic.
1528 if (isInvalid) {
1529 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001530 // Pick between error or extwarn.
1531 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1532 : diag::ext_duplicate_declspec;
1533 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 }
1535 ConsumeToken();
1536 }
1537}
1538
1539
1540/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1541///
1542void Parser::ParseDeclarator(Declarator &D) {
1543 /// This implements the 'declarator' production in the C grammar, then checks
1544 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001545 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001546}
1547
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001548/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1549/// is parsed by the function passed to it. Pass null, and the direct-declarator
1550/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001551/// ptr-operator production.
1552///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001553/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1554/// [C] pointer[opt] direct-declarator
1555/// [C++] direct-declarator
1556/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001557///
1558/// pointer: [C99 6.7.5]
1559/// '*' type-qualifier-list[opt]
1560/// '*' type-qualifier-list[opt] pointer
1561///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001562/// ptr-operator:
1563/// '*' cv-qualifier-seq[opt]
1564/// '&'
1565/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001566/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001567void Parser::ParseDeclaratorInternal(Declarator &D,
1568 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001569
Sebastian Redlf30208a2009-01-24 21:16:55 +00001570 // C++ member pointers start with a '::' or a nested-name.
1571 // Member pointers get special handling, since there's no place for the
1572 // scope spec in the generic path below.
1573 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1574 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1575 CXXScopeSpec SS;
1576 if (ParseOptionalCXXScopeSpecifier(SS)) {
1577 if(Tok.isNot(tok::star)) {
1578 // The scope spec really belongs to the direct-declarator.
1579 D.getCXXScopeSpec() = SS;
1580 if (DirectDeclParser)
1581 (this->*DirectDeclParser)(D);
1582 return;
1583 }
1584
1585 SourceLocation Loc = ConsumeToken();
1586 DeclSpec DS;
1587 ParseTypeQualifierListOpt(DS);
1588
1589 // Recurse to parse whatever is left.
1590 ParseDeclaratorInternal(D, DirectDeclParser);
1591
1592 // Sema will have to catch (syntactically invalid) pointers into global
1593 // scope. It has to catch pointers into namespace scope anyway.
1594 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
1595 Loc,DS.TakeAttributes()));
1596 return;
1597 }
1598 }
1599
1600 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001601 // Not a pointer, C++ reference, or block.
1602 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001603 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001604 if (DirectDeclParser)
1605 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001606 return;
1607 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001608
Steve Naroff4ef1c992008-08-28 10:07:06 +00001609 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001610 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1611
Steve Naroff4ef1c992008-08-28 10:07:06 +00001612 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001613 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 ParseTypeQualifierListOpt(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001617
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001619 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001620 if (Kind == tok::star)
1621 // Remember that we parsed a pointer type, and remember the type-quals.
1622 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1623 DS.TakeAttributes()));
1624 else
1625 // Remember that we parsed a Block type, and remember the type-quals.
1626 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1627 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 } else {
1629 // Is a reference
1630 DeclSpec DS;
1631
1632 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1633 // cv-qualifiers are introduced through the use of a typedef or of a
1634 // template type argument, in which case the cv-qualifiers are ignored.
1635 //
1636 // [GNU] Retricted references are allowed.
1637 // [GNU] Attributes on references are allowed.
1638 ParseTypeQualifierListOpt(DS);
1639
1640 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1641 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1642 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001643 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1645 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001646 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 }
1648
1649 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001650 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001651
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001652 if (D.getNumTypeObjects() > 0) {
1653 // C++ [dcl.ref]p4: There shall be no references to references.
1654 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1655 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001656 if (const IdentifierInfo *II = D.getIdentifier())
1657 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1658 << II;
1659 else
1660 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1661 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001662
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001663 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001664 // can go ahead and build the (technically ill-formed)
1665 // declarator: reference collapsing will take care of it.
1666 }
1667 }
1668
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001670 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1671 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 }
1673}
1674
1675/// ParseDirectDeclarator
1676/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001677/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001678/// '(' declarator ')'
1679/// [GNU] '(' attributes declarator ')'
1680/// [C90] direct-declarator '[' constant-expression[opt] ']'
1681/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1682/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1683/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1684/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1685/// direct-declarator '(' parameter-type-list ')'
1686/// direct-declarator '(' identifier-list[opt] ')'
1687/// [GNU] direct-declarator '(' parameter-forward-declarations
1688/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001689/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1690/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001691/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001692///
1693/// declarator-id: [C++ 8]
1694/// id-expression
1695/// '::'[opt] nested-name-specifier[opt] type-name
1696///
1697/// id-expression: [C++ 5.1]
1698/// unqualified-id
1699/// qualified-id [TODO]
1700///
1701/// unqualified-id: [C++ 5.1]
1702/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001703/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001704/// conversion-function-id [TODO]
1705/// '~' class-name
1706/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001707///
Reid Spencer5f016e22007-07-11 17:01:13 +00001708void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001709 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001710
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001711 if (getLang().CPlusPlus) {
1712 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001713 // ParseDeclaratorInternal might already have parsed the scope.
1714 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1715 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001716 if (afterCXXScope) {
1717 // Change the declaration context for name lookup, until this function
1718 // is exited (and the declarator has been parsed).
1719 DeclScopeObj.EnterDeclaratorScope();
1720 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001721
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001722 if (Tok.is(tok::identifier)) {
1723 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001724
1725 // If this identifier is followed by a '<', we may have a template-id.
1726 DeclTy *Template;
Douglas Gregor70316a02008-12-26 15:00:45 +00001727 if (NextToken().is(tok::less) &&
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001728 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1729 CurScope))) {
1730 IdentifierInfo *II = Tok.getIdentifierInfo();
1731 AnnotateTemplateIdToken(Template, 0);
1732 // FIXME: Set the declarator to a template-id. How? I don't
1733 // know... for now, just use the identifier.
1734 D.SetIdentifier(II, Tok.getLocation());
1735 }
1736 // If this identifier is the name of the current class, it's a
1737 // constructor name.
Douglas Gregor70316a02008-12-26 15:00:45 +00001738 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001739 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1740 CurScope),
1741 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001742 // This is a normal identifier.
1743 else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001744 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1745 ConsumeToken();
1746 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001747 } else if (Tok.is(tok::kw_operator)) {
1748 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001749
Douglas Gregor70316a02008-12-26 15:00:45 +00001750 // First try the name of an overloaded operator
1751 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1752 D.setOverloadedOperator(Op, OperatorLoc);
1753 } else {
1754 // This must be a conversion function (C++ [class.conv.fct]).
1755 if (TypeTy *ConvType = ParseConversionFunctionId())
1756 D.setConversionFunction(ConvType, OperatorLoc);
1757 else
1758 D.SetIdentifier(0, Tok.getLocation());
1759 }
1760 goto PastIdentifier;
1761 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001762 // This should be a C++ destructor.
1763 SourceLocation TildeLoc = ConsumeToken();
1764 if (Tok.is(tok::identifier)) {
1765 if (TypeTy *Type = ParseClassName())
1766 D.setDestructor(Type, TildeLoc);
1767 else
1768 D.SetIdentifier(0, TildeLoc);
1769 } else {
1770 Diag(Tok, diag::err_expected_class_name);
1771 D.SetIdentifier(0, TildeLoc);
1772 }
1773 goto PastIdentifier;
1774 }
1775
1776 // If we reached this point, token is not identifier and not '~'.
1777
1778 if (afterCXXScope) {
1779 Diag(Tok, diag::err_expected_unqualified_id);
1780 D.SetIdentifier(0, Tok.getLocation());
1781 D.setInvalidType(true);
1782 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001783 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001784 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001785 }
1786
1787 // If we reached this point, we are either in C/ObjC or the token didn't
1788 // satisfy any of the C++-specific checks.
1789
1790 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1791 assert(!getLang().CPlusPlus &&
1792 "There's a C++-specific check for tok::identifier above");
1793 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1794 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1795 ConsumeToken();
1796 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 // direct-declarator: '(' declarator ')'
1798 // direct-declarator: '(' attributes declarator ')'
1799 // Example: 'char (*X)' or 'int (*XX)(void)'
1800 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001801 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 // This could be something simple like "int" (in which case the declarator
1803 // portion is empty), if an abstract-declarator is allowed.
1804 D.SetIdentifier(0, Tok.getLocation());
1805 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001806 if (getLang().CPlusPlus)
1807 Diag(Tok, diag::err_expected_unqualified_id);
1808 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001809 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001811 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001812 }
1813
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001814 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 assert(D.isPastIdentifier() &&
1816 "Haven't past the location of the identifier yet?");
1817
1818 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001819 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001820 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1821 // In such a case, check if we actually have a function declarator; if it
1822 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001823 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1824 // When not in file scope, warn for ambiguous function declarators, just
1825 // in case the author intended it as a variable definition.
1826 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1827 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1828 break;
1829 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001830 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001831 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 ParseBracketDeclarator(D);
1833 } else {
1834 break;
1835 }
1836 }
1837}
1838
Chris Lattneref4715c2008-04-06 05:45:57 +00001839/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1840/// only called before the identifier, so these are most likely just grouping
1841/// parens for precedence. If we find that these are actually function
1842/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1843///
1844/// direct-declarator:
1845/// '(' declarator ')'
1846/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001847/// direct-declarator '(' parameter-type-list ')'
1848/// direct-declarator '(' identifier-list[opt] ')'
1849/// [GNU] direct-declarator '(' parameter-forward-declarations
1850/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001851///
1852void Parser::ParseParenDeclarator(Declarator &D) {
1853 SourceLocation StartLoc = ConsumeParen();
1854 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1855
Chris Lattner7399ee02008-10-20 02:05:46 +00001856 // Eat any attributes before we look at whether this is a grouping or function
1857 // declarator paren. If this is a grouping paren, the attribute applies to
1858 // the type being built up, for example:
1859 // int (__attribute__(()) *x)(long y)
1860 // If this ends up not being a grouping paren, the attribute applies to the
1861 // first argument, for example:
1862 // int (__attribute__(()) int x)
1863 // In either case, we need to eat any attributes to be able to determine what
1864 // sort of paren this is.
1865 //
1866 AttributeList *AttrList = 0;
1867 bool RequiresArg = false;
1868 if (Tok.is(tok::kw___attribute)) {
1869 AttrList = ParseAttributes();
1870
1871 // We require that the argument list (if this is a non-grouping paren) be
1872 // present even if the attribute list was empty.
1873 RequiresArg = true;
1874 }
Steve Naroff239f0732008-12-25 14:16:32 +00001875 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00001876 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1877 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00001878 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00001879
Chris Lattneref4715c2008-04-06 05:45:57 +00001880 // If we haven't past the identifier yet (or where the identifier would be
1881 // stored, if this is an abstract declarator), then this is probably just
1882 // grouping parens. However, if this could be an abstract-declarator, then
1883 // this could also be the start of function arguments (consider 'void()').
1884 bool isGrouping;
1885
1886 if (!D.mayOmitIdentifier()) {
1887 // If this can't be an abstract-declarator, this *must* be a grouping
1888 // paren, because we haven't seen the identifier yet.
1889 isGrouping = true;
1890 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001891 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001892 isDeclarationSpecifier()) { // 'int(int)' is a function.
1893 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1894 // considered to be a type, not a K&R identifier-list.
1895 isGrouping = false;
1896 } else {
1897 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1898 isGrouping = true;
1899 }
1900
1901 // If this is a grouping paren, handle:
1902 // direct-declarator: '(' declarator ')'
1903 // direct-declarator: '(' attributes declarator ')'
1904 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001905 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001906 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001907 if (AttrList)
1908 D.AddAttributes(AttrList);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001909
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001910 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001911 // Match the ')'.
1912 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001913
1914 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00001915 return;
1916 }
1917
1918 // Okay, if this wasn't a grouping paren, it must be the start of a function
1919 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001920 // identifier (and remember where it would have been), then call into
1921 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001922 D.SetIdentifier(0, Tok.getLocation());
1923
Chris Lattner7399ee02008-10-20 02:05:46 +00001924 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001925}
1926
1927/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1928/// declarator D up to a paren, which indicates that we are parsing function
1929/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001930///
Chris Lattner7399ee02008-10-20 02:05:46 +00001931/// If AttrList is non-null, then the caller parsed those arguments immediately
1932/// after the open paren - they should be considered to be the first argument of
1933/// a parameter. If RequiresArg is true, then the first argument of the
1934/// function is required to be present and required to not be an identifier
1935/// list.
1936///
Reid Spencer5f016e22007-07-11 17:01:13 +00001937/// This method also handles this portion of the grammar:
1938/// parameter-type-list: [C99 6.7.5]
1939/// parameter-list
1940/// parameter-list ',' '...'
1941///
1942/// parameter-list: [C99 6.7.5]
1943/// parameter-declaration
1944/// parameter-list ',' parameter-declaration
1945///
1946/// parameter-declaration: [C99 6.7.5]
1947/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001948/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001949/// [GNU] declaration-specifiers declarator attributes
1950/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001951/// [C++] declaration-specifiers abstract-declarator[opt]
1952/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001953/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1954///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001955/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1956/// and "exception-specification[opt]"(TODO).
1957///
Chris Lattner7399ee02008-10-20 02:05:46 +00001958void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1959 AttributeList *AttrList,
1960 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001961 // lparen is already consumed!
1962 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001963
Chris Lattner7399ee02008-10-20 02:05:46 +00001964 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00001965 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001966 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001967 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001968 delete AttrList;
1969 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001970
1971 ConsumeParen(); // Eat the closing ')'.
1972
1973 // cv-qualifier-seq[opt].
1974 DeclSpec DS;
1975 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001976 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001977
1978 // Parse exception-specification[opt].
1979 if (Tok.is(tok::kw_throw))
1980 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001981 }
1982
Chris Lattnerf97409f2008-04-06 06:57:35 +00001983 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001985 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00001986 /*variadic*/ false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001987 /*arglist*/ 0, 0,
1988 DS.getTypeQualifiers(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001989 LParenLoc, D));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001990 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00001991 }
1992
1993 // Alternatively, this parameter list may be an identifier list form for a
1994 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001995 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner7399ee02008-10-20 02:05:46 +00001996 // K&R identifier lists can't have typedefs as identifiers, per
1997 // C99 6.7.5.3p11.
1998 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1999 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002000 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002001 delete AttrList;
2002 }
2003
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2005 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00002006 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002007 }
2008
2009 // Finally, a normal, non-empty parameter type list.
2010
2011 // Build up an array of information about the parsed arguments.
2012 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002013
2014 // Enter function-declaration scope, limiting any declarators to the
2015 // function prototype scope, including parameter declarators.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002016 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002017
2018 bool IsVariadic = false;
2019 while (1) {
2020 if (Tok.is(tok::ellipsis)) {
2021 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002022
Chris Lattnerf97409f2008-04-06 06:57:35 +00002023 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002024 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002025 // Otherwise, parse parameter type list. If it starts with an
2026 // ellipsis, diagnose the malformed function.
2027 Diag(Tok, diag::err_ellipsis_first_arg);
2028 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00002030
Chris Lattnerf97409f2008-04-06 06:57:35 +00002031 ConsumeToken(); // Consume the ellipsis.
2032 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002033 }
2034
Chris Lattnerf97409f2008-04-06 06:57:35 +00002035 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002036
Chris Lattnerf97409f2008-04-06 06:57:35 +00002037 // Parse the declaration-specifiers.
2038 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002039
2040 // If the caller parsed attributes for the first argument, add them now.
2041 if (AttrList) {
2042 DS.AddAttributes(AttrList);
2043 AttrList = 0; // Only apply the attributes to the first parameter.
2044 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002045 ParseDeclarationSpecifiers(DS);
2046
2047 // Parse the declarator. This is "PrototypeContext", because we must
2048 // accept either 'declarator' or 'abstract-declarator' here.
2049 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2050 ParseDeclarator(ParmDecl);
2051
2052 // Parse GNU attributes, if present.
2053 if (Tok.is(tok::kw___attribute))
2054 ParmDecl.AddAttributes(ParseAttributes());
2055
Chris Lattnerf97409f2008-04-06 06:57:35 +00002056 // Remember this parsed parameter in ParamInfo.
2057 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2058
Douglas Gregor72b505b2008-12-16 21:30:33 +00002059 // DefArgToks is used when the parsing of default arguments needs
2060 // to be delayed.
2061 CachedTokens *DefArgToks = 0;
2062
Chris Lattnerf97409f2008-04-06 06:57:35 +00002063 // If no parameter was specified, verify that *something* was specified,
2064 // otherwise we have a missing type and identifier.
2065 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
2066 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
2067 // Completely missing, emit error.
2068 Diag(DSStart, diag::err_missing_param);
2069 } else {
2070 // Otherwise, we have something. Add it and let semantic analysis try
2071 // to grok it and add the result to the ParamInfo we are building.
2072
2073 // Inform the actions module about the parameter declarator, so it gets
2074 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00002075 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2076
2077 // Parse the default argument, if any. We parse the default
2078 // arguments in all dialects; the semantic analysis in
2079 // ActOnParamDefaultArgument will reject the default argument in
2080 // C.
2081 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002082 SourceLocation EqualLoc = Tok.getLocation();
2083
Chris Lattner04421082008-04-08 04:40:51 +00002084 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002085 if (D.getContext() == Declarator::MemberContext) {
2086 // If we're inside a class definition, cache the tokens
2087 // corresponding to the default argument. We'll actually parse
2088 // them when we see the end of the class definition.
2089 // FIXME: Templates will require something similar.
2090 // FIXME: Can we use a smart pointer for Toks?
2091 DefArgToks = new CachedTokens;
2092
2093 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2094 tok::semi, false)) {
2095 delete DefArgToks;
2096 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002097 Actions.ActOnParamDefaultArgumentError(Param);
2098 } else
2099 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002100 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002101 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002102 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002103
2104 OwningExprResult DefArgResult(ParseAssignmentExpression());
2105 if (DefArgResult.isInvalid()) {
2106 Actions.ActOnParamDefaultArgumentError(Param);
2107 SkipUntil(tok::comma, tok::r_paren, true, true);
2108 } else {
2109 // Inform the actions module about the default argument
2110 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2111 DefArgResult.release());
2112 }
Chris Lattner04421082008-04-08 04:40:51 +00002113 }
2114 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002115
2116 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002117 ParmDecl.getIdentifierLoc(), Param,
2118 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002119 }
2120
2121 // If the next token is a comma, consume it and keep reading arguments.
2122 if (Tok.isNot(tok::comma)) break;
2123
2124 // Consume the comma.
2125 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002126 }
2127
Chris Lattnerf97409f2008-04-06 06:57:35 +00002128 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002129 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002130
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002131 // If we have the closing ')', eat it.
2132 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2133
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002134 DeclSpec DS;
2135 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002136 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002137 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002138
2139 // Parse exception-specification[opt].
2140 if (Tok.is(tok::kw_throw))
2141 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002142 }
2143
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002145 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2146 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002147 DS.getTypeQualifiers(),
Chris Lattner5af2f352009-01-20 19:11:22 +00002148 LParenLoc, D));
Reid Spencer5f016e22007-07-11 17:01:13 +00002149}
2150
Chris Lattner66d28652008-04-06 06:34:08 +00002151/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2152/// we found a K&R-style identifier list instead of a type argument list. The
2153/// current token is known to be the first identifier in the list.
2154///
2155/// identifier-list: [C99 6.7.5]
2156/// identifier
2157/// identifier-list ',' identifier
2158///
2159void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2160 Declarator &D) {
2161 // Build up an array of information about the parsed arguments.
2162 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2163 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2164
2165 // If there was no identifier specified for the declarator, either we are in
2166 // an abstract-declarator, or we are in a parameter declarator which was found
2167 // to be abstract. In abstract-declarators, identifier lists are not valid:
2168 // diagnose this.
2169 if (!D.getIdentifier())
2170 Diag(Tok, diag::ext_ident_list_in_param);
2171
2172 // Tok is known to be the first identifier in the list. Remember this
2173 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002174 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002175 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2176 Tok.getLocation(), 0));
2177
Chris Lattner50c64772008-04-06 06:39:19 +00002178 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002179
2180 while (Tok.is(tok::comma)) {
2181 // Eat the comma.
2182 ConsumeToken();
2183
Chris Lattner50c64772008-04-06 06:39:19 +00002184 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002185 if (Tok.isNot(tok::identifier)) {
2186 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002187 SkipUntil(tok::r_paren);
2188 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002189 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002190
Chris Lattner66d28652008-04-06 06:34:08 +00002191 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002192
2193 // Reject 'typedef int y; int test(x, y)', but continue parsing.
2194 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002195 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002196
2197 // Verify that the argument identifier has not already been mentioned.
2198 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002199 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002200 } else {
2201 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002202 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2203 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00002204 }
Chris Lattner66d28652008-04-06 06:34:08 +00002205
2206 // Eat the identifier.
2207 ConsumeToken();
2208 }
2209
Chris Lattner50c64772008-04-06 06:39:19 +00002210 // Remember that we parsed a function type, and remember the attributes. This
2211 // function type is always a K&R style function type, which is not varargs and
2212 // has no prototype.
2213 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2214 &ParamInfo[0], ParamInfo.size(),
Chris Lattner5af2f352009-01-20 19:11:22 +00002215 /*TypeQuals*/0, LParenLoc, D));
Chris Lattner66d28652008-04-06 06:34:08 +00002216
2217 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00002218 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002219}
Chris Lattneref4715c2008-04-06 05:45:57 +00002220
Reid Spencer5f016e22007-07-11 17:01:13 +00002221/// [C90] direct-declarator '[' constant-expression[opt] ']'
2222/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2223/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2224/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2225/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2226void Parser::ParseBracketDeclarator(Declarator &D) {
2227 SourceLocation StartLoc = ConsumeBracket();
2228
Chris Lattner378c7e42008-12-18 07:27:21 +00002229 // C array syntax has many features, but by-far the most common is [] and [4].
2230 // This code does a fast path to handle some of the most obvious cases.
2231 if (Tok.getKind() == tok::r_square) {
2232 MatchRHSPunctuation(tok::r_square, StartLoc);
2233 // Remember that we parsed the empty array type.
2234 OwningExprResult NumElements(Actions);
2235 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2236 return;
2237 } else if (Tok.getKind() == tok::numeric_constant &&
2238 GetLookAheadToken(1).is(tok::r_square)) {
2239 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002240 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002241 ConsumeToken();
2242
2243 MatchRHSPunctuation(tok::r_square, StartLoc);
2244
2245 // If there was an error parsing the assignment-expression, recover.
2246 if (ExprRes.isInvalid())
2247 ExprRes.release(); // Deallocate expr, just use [].
2248
2249 // Remember that we parsed a array type, and remember its features.
2250 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2251 ExprRes.release(), StartLoc));
2252 return;
2253 }
2254
Reid Spencer5f016e22007-07-11 17:01:13 +00002255 // If valid, this location is the position where we read the 'static' keyword.
2256 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002257 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002258 StaticLoc = ConsumeToken();
2259
2260 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002261 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002262 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002263 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002264
2265 // If we haven't already read 'static', check to see if there is one after the
2266 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002267 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002268 StaticLoc = ConsumeToken();
2269
2270 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2271 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002272 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002273
2274 // Handle the case where we have '[*]' as the array size. However, a leading
2275 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2276 // the the token after the star is a ']'. Since stars in arrays are
2277 // infrequent, use of lookahead is not costly here.
2278 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002279 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002280
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002281 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002282 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002283 StaticLoc = SourceLocation(); // Drop the static.
2284 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002285 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002286 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002287 // Note, in C89, this production uses the constant-expr production instead
2288 // of assignment-expr. The only difference is that assignment-expr allows
2289 // things like '=' and '*='. Sema rejects these in C89 mode because they
2290 // are not i-c-e's, so we don't need to distinguish between the two here.
2291
Reid Spencer5f016e22007-07-11 17:01:13 +00002292 // Parse the assignment-expression now.
2293 NumElements = ParseAssignmentExpression();
2294 }
2295
2296 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002297 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002298 // If the expression was invalid, skip it.
2299 SkipUntil(tok::r_square);
2300 return;
2301 }
2302
2303 MatchRHSPunctuation(tok::r_square, StartLoc);
2304
Chris Lattner378c7e42008-12-18 07:27:21 +00002305 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002306 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2307 StaticLoc.isValid(), isStar,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002308 NumElements.release(), StartLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00002309}
2310
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002311/// [GNU] typeof-specifier:
2312/// typeof ( expressions )
2313/// typeof ( type-name )
2314/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002315///
2316void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002317 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002318 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002319 SourceLocation StartLoc = ConsumeToken();
2320
Chris Lattner04d66662007-10-09 17:33:22 +00002321 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002322 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002323 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002324 return;
2325 }
2326
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002327 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002328 if (Result.isInvalid())
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002329 return;
2330
2331 const char *PrevSpec = 0;
2332 // Check for duplicate type specifiers.
2333 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002334 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002335 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002336
2337 // FIXME: Not accurate, the range gets one token more than it should.
2338 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002339 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002340 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002341
Steve Naroffd1861fd2007-07-31 12:34:36 +00002342 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2343
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002344 if (isTypeIdInParens()) {
Steve Naroffd1861fd2007-07-31 12:34:36 +00002345 TypeTy *Ty = ParseTypeName();
2346
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002347 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2348
Chris Lattner04d66662007-10-09 17:33:22 +00002349 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002350 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002351 return;
2352 }
2353 RParenLoc = ConsumeParen();
2354 const char *PrevSpec = 0;
2355 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2356 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002357 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002358 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002359 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002360
2361 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002362 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002363 return;
2364 }
2365 RParenLoc = ConsumeParen();
2366 const char *PrevSpec = 0;
2367 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2368 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002369 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002370 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002371 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002372 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002373}
2374
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002375