blob: 532aff797fbb4bc13f2a6fcd5399d781faa6f23f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.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 Redlef65f062009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnereaaebc72009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Sean Huntbbd37c62009-11-21 08:43:09 +000048/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000049///
50/// [GNU] attributes:
51/// attribute
52/// attributes attribute
53///
54/// [GNU] attribute:
55/// '__attribute__' '(' '(' attribute-list ')' ')'
56///
57/// [GNU] attribute-list:
58/// attrib
59/// attribute_list ',' attrib
60///
61/// [GNU] attrib:
62/// empty
63/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
67///
68/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000073///
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000075/// token lookahead. Comment from gcc: "If they start with an identifier
76/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// start with that identifier; otherwise they are an expression list."
78///
79/// At the moment, I am not doing 2 token lookahead. I am also unaware of
80/// any attributes that don't work (based on my limited testing). Most
81/// attributes are very simple in practice. Until we find a bug, I don't see
82/// a pressing need to implement the 2 token lookahead.
83
Sean Huntbbd37c62009-11-21 08:43:09 +000084AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner04d66662007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000090 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
94 return CurrAttr;
95 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
98 return CurrAttr;
99 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000113 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner04d66662007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // __attribute__(( mode(byte) ))
123 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Chris Lattner04d66662007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner04d66662007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 }
154 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // __attribute__(( nonnull() ))
159 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
176 case tok::kw_typeof:
177 // If it's a builtin type name, eat it and expect a rparen
178 // __attribute__(( vec_type_hint(char) ))
179 ConsumeToken();
Sean Huntbbd37c62009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 }
Chris Lattner04d66662007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 CurrAttr);
211 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000212 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
214 }
215 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
218 }
219 }
220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
224 SkipUntil(tok::r_paren, false);
225 }
226 if (EndLoc)
227 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
229 return CurrAttr;
230}
231
Eli Friedmana23b4852009-06-08 07:21:15 +0000232/// ParseMicrosoftDeclSpec - Parse an __declspec construct
233///
234/// [MS] decl-specifier:
235/// __declspec ( extended-decl-modifier-seq )
236///
237/// [MS] extended-decl-modifier-seq:
238/// extended-decl-modifier[opt]
239/// extended-decl-modifier extended-decl-modifier-seq
240
Eli Friedman290eeb02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000243
Steve Narofff59e17e2008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000245 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
246 "declspec")) {
247 SkipUntil(tok::r_paren, true); // skip until ) or ;
248 return CurrAttr;
249 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000251 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
252 SourceLocation AttrNameLoc = ConsumeToken();
253 if (Tok.is(tok::l_paren)) {
254 ConsumeParen();
255 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
256 // correctly.
257 OwningExprResult ArgExpr(ParseAssignmentExpression());
258 if (!ArgExpr.isInvalid()) {
259 ExprTy* ExprList = ArgExpr.take();
Sean Huntbbd37c62009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 SourceLocation(), &ExprList, 1,
262 CurrAttr, true);
263 }
264 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
265 SkipUntil(tok::r_paren, false);
266 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000273 return CurrAttr;
274}
275
276AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
277 // Treat these like attributes
278 // FIXME: Allow Sema to distinguish between these and real attributes!
279 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000280 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
281 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner97144fc2009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Chris Lattner682bf922009-03-29 16:50:03 +0000312 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000314 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000315 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000320 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000321 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000325 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000326 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000327 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000329 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000330 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000335 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000336 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000338 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000339
Chris Lattner682bf922009-03-29 16:50:03 +0000340 // This routine returns a DeclGroup, if the thing we parsed only contains a
341 // single decl, convert it now.
342 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000343}
344
345/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
346/// declaration-specifiers init-declarator-list[opt] ';'
347///[C90/C++]init-declarator-list ';' [TODO]
348/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000349///
350/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000351/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000352Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000353 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000354 AttributeList *Attr,
355 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000357 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000358 if (Attr)
359 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000360 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
361 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
364 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000366 if (RequireSemi) ConsumeToken();
John McCallaec03712010-05-21 20:45:30 +0000367 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, AS_none,
368 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000369 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000370 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner5c5db552010-04-05 18:18:31 +0000373 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000374}
Mike Stump1eb44332009-09-09 15:08:12 +0000375
John McCalld8ac0572009-11-03 19:26:08 +0000376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000385 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000386
John McCalld8ac0572009-11-03 19:26:08 +0000387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
John McCalld8ac0572009-11-03 19:26:08 +0000396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000420 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000428 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000449 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000470}
471
Douglas Gregor1426e532009-05-12 21:31:51 +0000472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000476///
Reid Spencer5f016e22007-07-11 17:01:13 +0000477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000492///
Douglas Gregore542c862009-06-23 23:11:28 +0000493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregor1426e532009-05-12 21:31:51 +0000504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor1426e532009-05-12 21:31:51 +0000508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000512 D.AddAttributes(AttrList, Loc);
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregor1426e532009-05-12 21:31:51 +0000515 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor1426e532009-05-12 21:31:51 +0000547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
John McCall731ad842009-12-19 09:28:58 +0000554 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
555 EnterScope(0);
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000556 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000557 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000558
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000559 if (Tok.is(tok::code_completion)) {
560 Actions.CodeCompleteInitializer(CurScope, ThisDecl);
561 ConsumeCodeCompletionToken();
562 SkipUntil(tok::comma, true, true);
563 return ThisDecl;
564 }
565
Douglas Gregor1426e532009-05-12 21:31:51 +0000566 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000567
John McCall731ad842009-12-19 09:28:58 +0000568 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000569 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000570 ExitScope();
571 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000572
Douglas Gregor1426e532009-05-12 21:31:51 +0000573 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000574 SkipUntil(tok::comma, true, true);
575 Actions.ActOnInitializerError(ThisDecl);
576 } else
577 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000578 }
579 } else if (Tok.is(tok::l_paren)) {
580 // Parse C++ direct initializer: '(' expression-list ')'
581 SourceLocation LParenLoc = ConsumeParen();
582 ExprVector Exprs(Actions);
583 CommaLocsTy CommaLocs;
584
Douglas Gregorb4debae2009-12-22 17:47:17 +0000585 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
586 EnterScope(0);
587 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
588 }
589
Douglas Gregor1426e532009-05-12 21:31:51 +0000590 if (ParseExpressionList(Exprs, CommaLocs)) {
591 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000592
593 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
594 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
595 ExitScope();
596 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000597 } else {
598 // Match the ')'.
599 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
600
601 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
602 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000603
604 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
605 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
606 ExitScope();
607 }
608
Douglas Gregor1426e532009-05-12 21:31:51 +0000609 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
610 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000611 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000612 }
613 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000614 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000615 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
616 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000617 }
618
619 return ThisDecl;
620}
621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622/// ParseSpecifierQualifierList
623/// specifier-qualifier-list:
624/// type-specifier specifier-qualifier-list[opt]
625/// type-qualifier specifier-qualifier-list[opt]
626/// [GNU] attributes specifier-qualifier-list[opt]
627///
628void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
629 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
630 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 // Validate declspec for type-name.
634 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000635 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
636 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // Issue diagnostic and remove storage class if present.
640 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
641 if (DS.getStorageClassSpecLoc().isValid())
642 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
643 else
644 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
645 DS.ClearStorageClassSpecs();
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 // Issue diagnostic and remove function specfier if present.
649 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000650 if (DS.isInlineSpecified())
651 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
652 if (DS.isVirtualSpecified())
653 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
654 if (DS.isExplicitSpecified())
655 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 DS.ClearFunctionSpecs();
657 }
658}
659
Chris Lattnerc199ab32009-04-12 20:42:31 +0000660/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
661/// specified token is valid after the identifier in a declarator which
662/// immediately follows the declspec. For example, these things are valid:
663///
664/// int x [ 4]; // direct-declarator
665/// int x ( int y); // direct-declarator
666/// int(int x ) // direct-declarator
667/// int x ; // simple-declaration
668/// int x = 17; // init-declarator-list
669/// int x , y; // init-declarator-list
670/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000671/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000672/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000673///
674/// This is not, because 'x' does not immediately follow the declspec (though
675/// ')' happens to be valid anyway).
676/// int (x)
677///
678static bool isValidAfterIdentifierInDeclarator(const Token &T) {
679 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
680 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000681 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000682}
683
Chris Lattnere40c2952009-04-14 21:34:55 +0000684
685/// ParseImplicitInt - This method is called when we have an non-typename
686/// identifier in a declspec (which normally terminates the decl spec) when
687/// the declspec has no type specifier. In this case, the declspec is either
688/// malformed or is "implicit int" (in K&R and C89).
689///
690/// This method handles diagnosing this prettily and returns false if the
691/// declspec is done being processed. If it recovers and thinks there may be
692/// other pieces of declspec after it, it returns true.
693///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000694bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000695 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000696 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000697 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Chris Lattnere40c2952009-04-14 21:34:55 +0000699 SourceLocation Loc = Tok.getLocation();
700 // If we see an identifier that is not a type name, we normally would
701 // parse it as the identifer being declared. However, when a typename
702 // is typo'd or the definition is not included, this will incorrectly
703 // parse the typename as the identifier name and fall over misparsing
704 // later parts of the diagnostic.
705 //
706 // As such, we try to do some look-ahead in cases where this would
707 // otherwise be an "implicit-int" case to see if this is invalid. For
708 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
709 // an identifier with implicit int, we'd get a parse error because the
710 // next token is obviously invalid for a type. Parse these as a case
711 // with an invalid type specifier.
712 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattnere40c2952009-04-14 21:34:55 +0000714 // Since we know that this either implicit int (which is rare) or an
715 // error, we'd do lookahead to try to do better recovery.
716 if (isValidAfterIdentifierInDeclarator(NextToken())) {
717 // If this token is valid for implicit int, e.g. "static x = 4", then
718 // we just avoid eating the identifier, so it will be parsed as the
719 // identifier in the declarator.
720 return false;
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattnere40c2952009-04-14 21:34:55 +0000723 // Otherwise, if we don't consume this token, we are going to emit an
724 // error anyway. Try to recover from various common problems. Check
725 // to see if this was a reference to a tag name without a tag specified.
726 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000727 //
728 // C++ doesn't need this, and isTagName doesn't take SS.
729 if (SS == 0) {
730 const char *TagName = 0;
731 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattnere40c2952009-04-14 21:34:55 +0000733 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
734 default: break;
735 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
736 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
737 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
738 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
739 }
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattnerf4382f52009-04-14 22:17:06 +0000741 if (TagName) {
742 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000743 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000744 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattnerf4382f52009-04-14 22:17:06 +0000746 // Parse this as a tag as if the missing tag were present.
747 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000748 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000749 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000750 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000751 return true;
752 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Douglas Gregora786fdb2009-10-13 23:27:22 +0000755 // This is almost certainly an invalid type name. Let the action emit a
756 // diagnostic and attempt to recover.
757 Action::TypeTy *T = 0;
758 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
759 CurScope, SS, T)) {
760 // The action emitted a diagnostic, so we don't have to.
761 if (T) {
762 // The action has suggested that the type T could be used. Set that as
763 // the type in the declaration specifiers, consume the would-be type
764 // name token, and we're done.
765 const char *PrevSpec;
766 unsigned DiagID;
767 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
768 false);
769 DS.SetRangeEnd(Tok.getLocation());
770 ConsumeToken();
771
772 // There may be other declaration specifiers after this.
773 return true;
774 }
775
776 // Fall through; the action had no suggestion for us.
777 } else {
778 // The action did not emit a diagnostic, so emit one now.
779 SourceRange R;
780 if (SS) R = SS->getRange();
781 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Douglas Gregora786fdb2009-10-13 23:27:22 +0000784 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000785 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000786 unsigned DiagID;
787 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000788 DS.SetRangeEnd(Tok.getLocation());
789 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattnere40c2952009-04-14 21:34:55 +0000791 // TODO: Could inject an invalid typedef decl in an enclosing scope to
792 // avoid rippling error messages on subsequent uses of the same type,
793 // could be useful if #include was forgotten.
794 return false;
795}
796
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000797/// \brief Determine the declaration specifier context from the declarator
798/// context.
799///
800/// \param Context the declarator context, which is one of the
801/// Declarator::TheContext enumerator values.
802Parser::DeclSpecContext
803Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
804 if (Context == Declarator::MemberContext)
805 return DSC_class;
806 if (Context == Declarator::FileContext)
807 return DSC_top_level;
808 return DSC_normal;
809}
810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811/// ParseDeclarationSpecifiers
812/// declaration-specifiers: [C99 6.7]
813/// storage-class-specifier declaration-specifiers[opt]
814/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000815/// [C99] function-specifier declaration-specifiers[opt]
816/// [GNU] attributes declaration-specifiers[opt]
817///
818/// storage-class-specifier: [C99 6.7.1]
819/// 'typedef'
820/// 'extern'
821/// 'static'
822/// 'auto'
823/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000824/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000825/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000826/// function-specifier: [C99 6.7.4]
827/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000828/// [C++] 'virtual'
829/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000830/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000831/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000832
Reid Spencer5f016e22007-07-11 17:01:13 +0000833///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000834void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000835 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000836 AccessSpecifier AS,
837 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000838 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000839 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
840 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
841 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
842 : Action::CCC_Template;
843 else if (DSContext == DSC_class)
844 CCC = Action::CCC_Class;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000845 else if (ObjCImpDecl)
846 CCC = Action::CCC_ObjCImplementation;
847
Douglas Gregor01dfea02010-01-10 23:08:15 +0000848 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregordc845342010-05-25 05:58:43 +0000849 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +0000850 }
851
Chris Lattner81c018d2008-03-13 06:29:04 +0000852 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000854 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000856 unsigned DiagID = 0;
857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000861 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000862 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 // If this is not a declaration specifier token, we're done reading decl
864 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000865 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Chris Lattner5e02c472009-01-05 00:07:25 +0000868 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000869 // C++ scope specifier. Annotate and loop, or bail out on error.
870 if (TryAnnotateCXXScopeToken(true)) {
871 if (!DS.hasTypeSpecifier())
872 DS.SetTypeSpecError();
873 goto DoneWithDeclSpec;
874 }
John McCall2e0a7152010-03-01 18:20:46 +0000875 if (Tok.is(tok::coloncolon)) // ::new or ::delete
876 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000877 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000878
879 case tok::annot_cxxscope: {
880 if (DS.hasTypeSpecifier())
881 goto DoneWithDeclSpec;
882
John McCallaa87d332009-12-12 11:40:51 +0000883 CXXScopeSpec SS;
884 SS.setScopeRep(Tok.getAnnotationValue());
885 SS.setRange(Tok.getAnnotationRange());
886
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000887 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000888 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000889 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000890 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000891 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000892 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000893
894 // C++ [class.qual]p2:
895 // In a lookup in which the constructor is an acceptable lookup
896 // result and the nested-name-specifier nominates a class C:
897 //
898 // - if the name specified after the
899 // nested-name-specifier, when looked up in C, is the
900 // injected-class-name of C (Clause 9), or
901 //
902 // - if the name specified after the nested-name-specifier
903 // is the same as the identifier or the
904 // simple-template-id's template-name in the last
905 // component of the nested-name-specifier,
906 //
907 // the name is instead considered to name the constructor of
908 // class C.
909 //
910 // Thus, if the template-name is actually the constructor
911 // name, then the code is ill-formed; this interpretation is
912 // reinforced by the NAD status of core issue 635.
913 TemplateIdAnnotation *TemplateId
914 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000915 if ((DSContext == DSC_top_level ||
916 (DSContext == DSC_class && DS.isFriendSpecified())) &&
917 TemplateId->Name &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000918 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
919 if (isConstructorDeclarator()) {
920 // The user meant this to be an out-of-line constructor
921 // definition, but template arguments are not allowed
922 // there. Just allow this as a constructor; we'll
923 // complain about it later.
924 goto DoneWithDeclSpec;
925 }
926
927 // The user meant this to name a type, but it actually names
928 // a constructor with some extraneous template
929 // arguments. Complain, then parse it as a type as the user
930 // intended.
931 Diag(TemplateId->TemplateNameLoc,
932 diag::err_out_of_line_template_id_names_constructor)
933 << TemplateId->Name;
934 }
935
John McCallaa87d332009-12-12 11:40:51 +0000936 DS.getTypeSpecScope() = SS;
937 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000938 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000939 "ParseOptionalCXXScopeSpecifier not working");
940 AnnotateTemplateIdTokenAsType(&SS);
941 continue;
942 }
943
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000944 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000945 DS.getTypeSpecScope() = SS;
946 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000947 if (Tok.getAnnotationValue())
948 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
949 PrevSpec, DiagID,
950 Tok.getAnnotationValue());
951 else
952 DS.SetTypeSpecError();
953 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
954 ConsumeToken(); // The typename
955 }
956
Douglas Gregor9135c722009-03-25 15:40:00 +0000957 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000958 goto DoneWithDeclSpec;
959
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000960 // If we're in a context where the identifier could be a class name,
961 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +0000962 if ((DSContext == DSC_top_level ||
963 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000964 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
965 &SS)) {
966 if (isConstructorDeclarator())
967 goto DoneWithDeclSpec;
968
969 // As noted in C++ [class.qual]p2 (cited above), when the name
970 // of the class is qualified in a context where it could name
971 // a constructor, its a constructor name. However, we've
972 // looked at the declarator, and the user probably meant this
973 // to be a type. Complain that it isn't supposed to be treated
974 // as a type, then proceed to parse it as a type.
975 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
976 << Next.getIdentifierInfo();
977 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000978
Douglas Gregorb696ea32009-02-04 17:00:24 +0000979 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
980 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000981
Chris Lattnerf4382f52009-04-14 22:17:06 +0000982 // If the referenced identifier is not a type, then this declspec is
983 // erroneous: We already checked about that it has no type specifier, and
984 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000985 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000986 if (TypeRep == 0) {
987 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000988 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000989 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
John McCallaa87d332009-12-12 11:40:51 +0000992 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000993 ConsumeToken(); // The C++ scope.
994
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000995 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000996 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000997 if (isInvalid)
998 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001000 DS.SetRangeEnd(Tok.getLocation());
1001 ConsumeToken(); // The typename.
1002
1003 continue;
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner80d0c892009-01-21 19:48:37 +00001006 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001007 if (Tok.getAnnotationValue())
1008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001009 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001010 else
1011 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001012
1013 if (isInvalid)
1014 break;
1015
Chris Lattner80d0c892009-01-21 19:48:37 +00001016 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1017 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Chris Lattner80d0c892009-01-21 19:48:37 +00001019 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1020 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1021 // Objective-C interface. If we don't have Objective-C or a '<', this is
1022 // just a normal reference to a typedef name.
1023 if (!Tok.is(tok::less) || !getLang().ObjC1)
1024 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001026 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001027 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001028 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1029 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1030 LAngleLoc, EndProtoLoc);
1031 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1032 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner80d0c892009-01-21 19:48:37 +00001034 DS.SetRangeEnd(EndProtoLoc);
1035 continue;
1036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattner3bd934a2008-07-26 01:18:38 +00001038 // typedef-name
1039 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001040 // In C++, check to see if this is a scope specifier like foo::bar::, if
1041 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001042 if (getLang().CPlusPlus) {
1043 if (TryAnnotateCXXScopeToken(true)) {
1044 if (!DS.hasTypeSpecifier())
1045 DS.SetTypeSpecError();
1046 goto DoneWithDeclSpec;
1047 }
1048 if (!Tok.is(tok::identifier))
1049 continue;
1050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Chris Lattner3bd934a2008-07-26 01:18:38 +00001052 // This identifier can only be a typedef name if we haven't already seen
1053 // a type-specifier. Without this check we misparse:
1054 // typedef int X; struct Y { short X; }; as 'short int'.
1055 if (DS.hasTypeSpecifier())
1056 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001057
John Thompson82287d12010-02-05 00:12:22 +00001058 // Check for need to substitute AltiVec keyword tokens.
1059 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1060 break;
1061
Chris Lattner3bd934a2008-07-26 01:18:38 +00001062 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +00001063 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001064 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001065
Chris Lattnerc199ab32009-04-12 20:42:31 +00001066 // If this is not a typedef name, don't parse it as part of the declspec,
1067 // it must be an implicit int or an error.
1068 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001069 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001070 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001071 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001072
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001073 // If we're in a context where the identifier could be a class name,
1074 // check whether this is a constructor declaration.
1075 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump1eb44332009-09-09 15:08:12 +00001076 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001077 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001078 goto DoneWithDeclSpec;
1079
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001081 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001082 if (isInvalid)
1083 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattner3bd934a2008-07-26 01:18:38 +00001085 DS.SetRangeEnd(Tok.getLocation());
1086 ConsumeToken(); // The identifier
1087
1088 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1089 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1090 // Objective-C interface. If we don't have Objective-C or a '<', this is
1091 // just a normal reference to a typedef name.
1092 if (!Tok.is(tok::less) || !getLang().ObjC1)
1093 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001095 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001096 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001097 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1098 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1099 LAngleLoc, EndProtoLoc);
1100 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1101 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner3bd934a2008-07-26 01:18:38 +00001103 DS.SetRangeEnd(EndProtoLoc);
1104
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001105 // Need to support trailing type qualifiers (e.g. "id<p> const").
1106 // If a type specifier follows, it will be diagnosed elsewhere.
1107 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001108 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001109
1110 // type-name
1111 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001112 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001113 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001114 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001115 // This template-id does not refer to a type name, so we're
1116 // done with the type-specifiers.
1117 goto DoneWithDeclSpec;
1118 }
1119
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001120 // If we're in a context where the template-id could be a
1121 // constructor name or specialization, check whether this is a
1122 // constructor declaration.
1123 if (getLang().CPlusPlus && DSContext == DSC_class &&
1124 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1125 isConstructorDeclarator())
1126 goto DoneWithDeclSpec;
1127
Douglas Gregor39a8de12009-02-25 19:37:18 +00001128 // Turn the template-id annotation token into a type annotation
1129 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001130 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001131 continue;
1132 }
1133
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 // GNU attributes support.
1135 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001136 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001138
1139 // Microsoft declspec support.
1140 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001141 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001142 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Steve Naroff239f0732008-12-25 14:16:32 +00001144 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001145 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001146 // FIXME: Add handling here!
1147 break;
1148
1149 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001150 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001151 case tok::kw___cdecl:
1152 case tok::kw___stdcall:
1153 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001154 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001155 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1156 continue;
1157
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 // storage-class-specifier
1159 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001160 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1161 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 break;
1163 case tok::kw_extern:
1164 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001165 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001166 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1167 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001169 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001170 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001171 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001172 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 case tok::kw_static:
1174 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001175 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001176 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1177 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 break;
1179 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001180 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001181 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1182 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001183 else
John McCallfec54012009-08-03 20:12:06 +00001184 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1185 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 break;
1187 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001188 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1189 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001191 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001192 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1193 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001194 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001196 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 // function-specifier
1200 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001201 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001203 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001204 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001205 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001206 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001207 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001208 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001209
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001210 // friend
1211 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001212 if (DSContext == DSC_class)
1213 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1214 else {
1215 PrevSpec = ""; // not actually used by the diagnostic
1216 DiagID = diag::err_friend_invalid_in_context;
1217 isInvalid = true;
1218 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001219 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Sebastian Redl2ac67232009-11-05 15:47:02 +00001221 // constexpr
1222 case tok::kw_constexpr:
1223 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1224 break;
1225
Chris Lattner80d0c892009-01-21 19:48:37 +00001226 // type-specifier
1227 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001228 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1229 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001230 break;
1231 case tok::kw_long:
1232 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001233 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1234 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001235 else
John McCallfec54012009-08-03 20:12:06 +00001236 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1237 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001238 break;
1239 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001240 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1241 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001242 break;
1243 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1245 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001246 break;
1247 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001248 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1249 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001250 break;
1251 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001252 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1253 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001254 break;
1255 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001256 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1257 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001258 break;
1259 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001260 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1261 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001262 break;
1263 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001264 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1265 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001266 break;
1267 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001268 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1269 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001270 break;
1271 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1273 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001274 break;
1275 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1277 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001278 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001279 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1281 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001282 break;
1283 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1285 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001286 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001287 case tok::kw_bool:
1288 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1290 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001291 break;
1292 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1294 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001295 break;
1296 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1298 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001299 break;
1300 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1302 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001303 break;
John Thompson82287d12010-02-05 00:12:22 +00001304 case tok::kw___vector:
1305 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1306 break;
1307 case tok::kw___pixel:
1308 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1309 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001310
1311 // class-specifier:
1312 case tok::kw_class:
1313 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001314 case tok::kw_union: {
1315 tok::TokenKind Kind = Tok.getKind();
1316 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001317 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001318 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001319 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001320
1321 // enum-specifier:
1322 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001323 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001324 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001325 continue;
1326
1327 // cv-qualifier:
1328 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1330 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001331 break;
1332 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1334 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1338 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001339 break;
1340
Douglas Gregord57959a2009-03-27 23:10:48 +00001341 // C++ typename-specifier:
1342 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001343 if (TryAnnotateTypeOrScopeToken()) {
1344 DS.SetTypeSpecError();
1345 goto DoneWithDeclSpec;
1346 }
1347 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001348 continue;
1349 break;
1350
Chris Lattner80d0c892009-01-21 19:48:37 +00001351 // GNU typeof support.
1352 case tok::kw_typeof:
1353 ParseTypeofSpecifier(DS);
1354 continue;
1355
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001356 case tok::kw_decltype:
1357 ParseDecltypeSpecifier(DS);
1358 continue;
1359
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001360 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001361 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001362 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1363 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001364 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001365 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Chris Lattnerbce61352008-07-26 00:20:22 +00001367 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001368 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001369 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001370 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1371 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1372 LAngleLoc, EndProtoLoc);
1373 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1374 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001375 DS.SetRangeEnd(EndProtoLoc);
1376
Chris Lattner1ab3b962008-11-18 07:48:38 +00001377 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001378 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001379 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001380 // Need to support trailing type qualifiers (e.g. "id<p> const").
1381 // If a type specifier follows, it will be diagnosed elsewhere.
1382 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001383 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 }
John McCallfec54012009-08-03 20:12:06 +00001385 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 if (isInvalid) {
1387 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001388 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001389 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001391 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 ConsumeToken();
1393 }
1394}
Douglas Gregoradcac882008-12-01 23:54:00 +00001395
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001396/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001397/// primarily follow the C++ grammar with additions for C99 and GNU,
1398/// which together subsume the C grammar. Note that the C++
1399/// type-specifier also includes the C type-qualifier (for const,
1400/// volatile, and C99 restrict). Returns true if a type-specifier was
1401/// found (and parsed), false otherwise.
1402///
1403/// type-specifier: [C++ 7.1.5]
1404/// simple-type-specifier
1405/// class-specifier
1406/// enum-specifier
1407/// elaborated-type-specifier [TODO]
1408/// cv-qualifier
1409///
1410/// cv-qualifier: [C++ 7.1.5.1]
1411/// 'const'
1412/// 'volatile'
1413/// [C99] 'restrict'
1414///
1415/// simple-type-specifier: [ C++ 7.1.5.2]
1416/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1417/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1418/// 'char'
1419/// 'wchar_t'
1420/// 'bool'
1421/// 'short'
1422/// 'int'
1423/// 'long'
1424/// 'signed'
1425/// 'unsigned'
1426/// 'float'
1427/// 'double'
1428/// 'void'
1429/// [C99] '_Bool'
1430/// [C99] '_Complex'
1431/// [C99] '_Imaginary' // Removed in TC2?
1432/// [GNU] '_Decimal32'
1433/// [GNU] '_Decimal64'
1434/// [GNU] '_Decimal128'
1435/// [GNU] typeof-specifier
1436/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1437/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001438/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001439/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001440bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001441 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001442 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001443 const ParsedTemplateInfo &TemplateInfo,
1444 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001445 SourceLocation Loc = Tok.getLocation();
1446
1447 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001448 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001449 // If we already have a type specifier, this identifier is not a type.
1450 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1451 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1452 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1453 return false;
John Thompson82287d12010-02-05 00:12:22 +00001454 // Check for need to substitute AltiVec keyword tokens.
1455 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1456 break;
1457 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001458 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001459 // Annotate typenames and C++ scope specifiers. If we get one, just
1460 // recurse to handle whatever we get.
1461 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001462 return true;
1463 if (Tok.is(tok::identifier))
1464 return false;
1465 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1466 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001467 case tok::coloncolon: // ::foo::bar
1468 if (NextToken().is(tok::kw_new) || // ::new
1469 NextToken().is(tok::kw_delete)) // ::delete
1470 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Chris Lattner166a8fc2009-01-04 23:41:41 +00001472 // Annotate typenames and C++ scope specifiers. If we get one, just
1473 // recurse to handle whatever we get.
1474 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001475 return true;
1476 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1477 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Douglas Gregor12e083c2008-11-07 15:42:26 +00001479 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001480 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001481 if (Tok.getAnnotationValue())
1482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001483 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001484 else
1485 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001486 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1487 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Douglas Gregor12e083c2008-11-07 15:42:26 +00001489 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1490 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1491 // Objective-C interface. If we don't have Objective-C or a '<', this is
1492 // just a normal reference to a typedef name.
1493 if (!Tok.is(tok::less) || !getLang().ObjC1)
1494 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001496 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001497 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001498 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1499 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1500 LAngleLoc, EndProtoLoc);
1501 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1502 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Douglas Gregor12e083c2008-11-07 15:42:26 +00001504 DS.SetRangeEnd(EndProtoLoc);
1505 return true;
1506 }
1507
1508 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001509 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001510 break;
1511 case tok::kw_long:
1512 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001513 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1514 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001515 else
John McCallfec54012009-08-03 20:12:06 +00001516 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1517 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001518 break;
1519 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001520 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001521 break;
1522 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001523 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1524 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001525 break;
1526 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001527 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1528 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001529 break;
1530 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001531 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1532 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001533 break;
1534 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001535 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001536 break;
1537 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001539 break;
1540 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001541 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001542 break;
1543 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001544 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001545 break;
1546 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001548 break;
1549 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001551 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001552 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001554 break;
1555 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001557 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001558 case tok::kw_bool:
1559 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001560 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001561 break;
1562 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001563 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1564 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001565 break;
1566 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1568 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001569 break;
1570 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001571 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1572 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001573 break;
John Thompson82287d12010-02-05 00:12:22 +00001574 case tok::kw___vector:
1575 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1576 break;
1577 case tok::kw___pixel:
1578 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1579 break;
1580
Douglas Gregor12e083c2008-11-07 15:42:26 +00001581 // class-specifier:
1582 case tok::kw_class:
1583 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001584 case tok::kw_union: {
1585 tok::TokenKind Kind = Tok.getKind();
1586 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001587 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1588 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001589 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001590 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001591
1592 // enum-specifier:
1593 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001594 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001595 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596 return true;
1597
1598 // cv-qualifier:
1599 case tok::kw_const:
1600 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001601 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw_volatile:
1604 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001605 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw_restrict:
1608 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001609 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001610 break;
1611
1612 // GNU typeof support.
1613 case tok::kw_typeof:
1614 ParseTypeofSpecifier(DS);
1615 return true;
1616
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001617 // C++0x decltype support.
1618 case tok::kw_decltype:
1619 ParseDecltypeSpecifier(DS);
1620 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001622 // C++0x auto support.
1623 case tok::kw_auto:
1624 if (!getLang().CPlusPlus0x)
1625 return false;
1626
John McCallfec54012009-08-03 20:12:06 +00001627 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001628 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001629 case tok::kw___ptr64:
1630 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001631 case tok::kw___cdecl:
1632 case tok::kw___stdcall:
1633 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001634 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001635 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001636 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001637
Douglas Gregor12e083c2008-11-07 15:42:26 +00001638 default:
1639 // Not a type-specifier; do nothing.
1640 return false;
1641 }
1642
1643 // If the specifier combination wasn't legal, issue a diagnostic.
1644 if (isInvalid) {
1645 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001646 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001647 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001648 }
1649 DS.SetRangeEnd(Tok.getLocation());
1650 ConsumeToken(); // whatever we parsed above.
1651 return true;
1652}
Reid Spencer5f016e22007-07-11 17:01:13 +00001653
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001654/// ParseStructDeclaration - Parse a struct declaration without the terminating
1655/// semicolon.
1656///
Reid Spencer5f016e22007-07-11 17:01:13 +00001657/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001658/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001659/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001660/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001661/// struct-declarator-list:
1662/// struct-declarator
1663/// struct-declarator-list ',' struct-declarator
1664/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1665/// struct-declarator:
1666/// declarator
1667/// [GNU] declarator attributes[opt]
1668/// declarator[opt] ':' constant-expression
1669/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1670///
Chris Lattnere1359422008-04-10 06:46:29 +00001671void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001672ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001673 if (Tok.is(tok::kw___extension__)) {
1674 // __extension__ silences extension warnings in the subexpression.
1675 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001676 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001677 return ParseStructDeclaration(DS, Fields);
1678 }
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Steve Naroff28a7ca82007-08-20 22:28:22 +00001680 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001681 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001682 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001684 // If there are no declarators, this is a free-standing declaration
1685 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001686 if (Tok.is(tok::semi)) {
John McCallaec03712010-05-21 20:45:30 +00001687 Actions.ParsedFreeStandingDeclSpec(CurScope, AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001688 return;
1689 }
1690
1691 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001692 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001693 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001694 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001695 FieldDeclarator DeclaratorInfo(DS);
1696
1697 // Attributes are only allowed here on successive declarators.
1698 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1699 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001700 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001701 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1702 }
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Steve Naroff28a7ca82007-08-20 22:28:22 +00001704 /// struct-declarator: declarator
1705 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001706 if (Tok.isNot(tok::colon)) {
1707 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1708 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001709 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Chris Lattner04d66662007-10-09 17:33:22 +00001712 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001713 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001714 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001715 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001716 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001717 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001718 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001719 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001720
Steve Naroff28a7ca82007-08-20 22:28:22 +00001721 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001722 if (Tok.is(tok::kw___attribute)) {
1723 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001724 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001725 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1726 }
1727
John McCallbdd563e2009-11-03 02:38:08 +00001728 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001729 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1730 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001731
Steve Naroff28a7ca82007-08-20 22:28:22 +00001732 // If we don't have a comma, it is either the end of the list (a ';')
1733 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001734 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001735 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001736
Steve Naroff28a7ca82007-08-20 22:28:22 +00001737 // Consume the comma.
1738 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001739
John McCallbdd563e2009-11-03 02:38:08 +00001740 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001741 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001742}
1743
1744/// ParseStructUnionBody
1745/// struct-contents:
1746/// struct-declaration-list
1747/// [EXT] empty
1748/// [GNU] "struct-declaration-list" without terminatoring ';'
1749/// struct-declaration-list:
1750/// struct-declaration
1751/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001752/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001753///
Reid Spencer5f016e22007-07-11 17:01:13 +00001754void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001755 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001756 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1757 PP.getSourceManager(),
1758 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001762 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001763 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1764
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1766 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001767 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001768 Diag(Tok, diag::ext_empty_struct_union_enum)
1769 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001770
Chris Lattnerb28317a2009-03-28 19:18:32 +00001771 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001772
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001774 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001778 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001779 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001780 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 ConsumeToken();
1782 continue;
1783 }
Chris Lattnere1359422008-04-10 06:46:29 +00001784
1785 // Parse all the comma separated declarators.
1786 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001787
John McCallbdd563e2009-11-03 02:38:08 +00001788 if (!Tok.is(tok::at)) {
1789 struct CFieldCallback : FieldCallback {
1790 Parser &P;
1791 DeclPtrTy TagDecl;
1792 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1793
1794 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1795 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1796 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1797
1798 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001799 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001800 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1801 FD.D.getDeclSpec().getSourceRange().getBegin(),
1802 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001803 FieldDecls.push_back(Field);
1804 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001805 }
John McCallbdd563e2009-11-03 02:38:08 +00001806 } Callback(*this, TagDecl, FieldDecls);
1807
1808 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001809 } else { // Handle @defs
1810 ConsumeToken();
1811 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1812 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001813 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001814 continue;
1815 }
1816 ConsumeToken();
1817 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1818 if (!Tok.is(tok::identifier)) {
1819 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001820 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001821 continue;
1822 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001823 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001824 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001825 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001826 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1827 ConsumeToken();
1828 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001829 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001830
Chris Lattner04d66662007-10-09 17:33:22 +00001831 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001833 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001834 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001835 break;
1836 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001837 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1838 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001839 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001840 // If we stopped at a ';', eat it.
1841 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001842 }
1843 }
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Steve Naroff60fccee2007-10-29 21:38:07 +00001845 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Ted Kremenek1e377652010-02-11 02:19:13 +00001847 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001849 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001850 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001851
1852 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001853 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001854 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001855 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001856 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001857 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001858}
1859
1860
1861/// ParseEnumSpecifier
1862/// enum-specifier: [C99 6.7.2.2]
1863/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001864///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001865/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1866/// '}' attributes[opt]
1867/// 'enum' identifier
1868/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001869///
1870/// [C++] elaborated-type-specifier:
1871/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1872///
Chris Lattner4c97d762009-04-12 21:49:30 +00001873void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001874 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001875 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001877 if (Tok.is(tok::code_completion)) {
1878 // Code completion for an enum name.
1879 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001880 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001881 }
1882
Ted Kremenek1e377652010-02-11 02:19:13 +00001883 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001884 // If attributes exist after tag, parse them.
1885 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001886 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001887
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001888 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001889 if (getLang().CPlusPlus) {
1890 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1891 return;
1892
1893 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001894 Diag(Tok, diag::err_expected_ident);
1895 if (Tok.isNot(tok::l_brace)) {
1896 // Has no name and is not a definition.
1897 // Skip the rest of this declarator, up until the comma or semicolon.
1898 SkipUntil(tok::comma, true);
1899 return;
1900 }
1901 }
1902 }
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001904 // Must have either 'enum name' or 'enum {...}'.
1905 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1906 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001908 // Skip the rest of this declarator, up until the comma or semicolon.
1909 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001911 }
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001913 // If an identifier is present, consume and remember it.
1914 IdentifierInfo *Name = 0;
1915 SourceLocation NameLoc;
1916 if (Tok.is(tok::identifier)) {
1917 Name = Tok.getIdentifierInfo();
1918 NameLoc = ConsumeToken();
1919 }
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001921 // There are three options here. If we have 'enum foo;', then this is a
1922 // forward declaration. If we have 'enum foo {...' then this is a
1923 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1924 //
1925 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1926 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1927 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1928 //
John McCall0f434ec2009-07-31 02:45:11 +00001929 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001930 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001931 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001932 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001933 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001934 else
John McCall0f434ec2009-07-31 02:45:11 +00001935 TUK = Action::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00001936
1937 // enums cannot be templates, although they can be referenced from a
1938 // template.
1939 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1940 TUK != Action::TUK_Reference) {
1941 Diag(Tok, diag::err_enum_template);
1942
1943 // Skip the rest of this declarator, up until the comma or semicolon.
1944 SkipUntil(tok::comma, true);
1945 return;
1946 }
1947
Douglas Gregor402abb52009-05-28 23:31:59 +00001948 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001949 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00001950 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1951 const char *PrevSpec = 0;
1952 unsigned DiagID;
John McCall0f434ec2009-07-31 02:45:11 +00001953 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Ted Kremenek1e377652010-02-11 02:19:13 +00001954 StartLoc, SS, Name, NameLoc, Attr.get(),
1955 AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001956 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001957 Owned, IsDependent);
Douglas Gregor48c89f42010-04-24 16:38:41 +00001958 if (IsDependent) {
1959 // This enum has a dependent nested-name-specifier. Handle it as a
1960 // dependent tag.
1961 if (!Name) {
1962 DS.SetTypeSpecError();
1963 Diag(Tok, diag::err_expected_type_name_after_typename);
1964 return;
1965 }
1966
1967 TypeResult Type = Actions.ActOnDependentTag(CurScope, DeclSpec::TST_enum,
1968 TUK, SS, Name, StartLoc,
1969 NameLoc);
1970 if (Type.isInvalid()) {
1971 DS.SetTypeSpecError();
1972 return;
1973 }
1974
1975 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
1976 Type.get(), false))
1977 Diag(StartLoc, DiagID) << PrevSpec;
1978
1979 return;
1980 }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor48c89f42010-04-24 16:38:41 +00001982 if (!TagDecl.get()) {
1983 // The action failed to produce an enumeration tag. If this is a
1984 // definition, consume the entire definition.
1985 if (Tok.is(tok::l_brace)) {
1986 ConsumeBrace();
1987 SkipUntil(tok::r_brace);
1988 }
1989
1990 DS.SetTypeSpecError();
1991 return;
1992 }
1993
Chris Lattner04d66662007-10-09 17:33:22 +00001994 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001995 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001997 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1998 // name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001999 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00002000 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00002001 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002002}
2003
2004/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2005/// enumerator-list:
2006/// enumerator
2007/// enumerator-list ',' enumerator
2008/// enumerator:
2009/// enumeration-constant
2010/// enumeration-constant '=' constant-expression
2011/// enumeration-constant:
2012/// identifier
2013///
Chris Lattnerb28317a2009-03-28 19:18:32 +00002014void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002015 // Enter the scope of the enum body and start the definition.
2016 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00002017 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002018
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Chris Lattner7946dd32007-08-27 17:24:30 +00002021 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002022 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002023 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Chris Lattnerb28317a2009-03-28 19:18:32 +00002025 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002026
Chris Lattnerb28317a2009-03-28 19:18:32 +00002027 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002030 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2032 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002035 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00002036 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002038 AssignedVal = ParseConstantExpression();
2039 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002040 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 }
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Reid Spencer5f016e22007-07-11 17:01:13 +00002043 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002044 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
2045 LastEnumConstDecl,
2046 IdentLoc, Ident,
2047 EqualLoc,
2048 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 EnumConstantDecls.push_back(EnumConstDecl);
2050 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002051
Chris Lattner04d66662007-10-09 17:33:22 +00002052 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002053 break;
2054 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002055
2056 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002057 !(getLang().C99 || getLang().CPlusPlus0x))
2058 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2059 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002060 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 }
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002064 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002065
Ted Kremenek1e377652010-02-11 02:19:13 +00002066 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002067 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002068 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002069 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002070
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002071 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2072 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +00002073 CurScope, Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Douglas Gregor72de6672009-01-08 20:45:30 +00002075 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00002076 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002077}
2078
2079/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002080/// start of a type-qualifier-list.
2081bool Parser::isTypeQualifier() const {
2082 switch (Tok.getKind()) {
2083 default: return false;
2084 // type-qualifier
2085 case tok::kw_const:
2086 case tok::kw_volatile:
2087 case tok::kw_restrict:
2088 return true;
2089 }
2090}
2091
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002092/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2093/// is definitely a type-specifier. Return false if it isn't part of a type
2094/// specifier or if we're not sure.
2095bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2096 switch (Tok.getKind()) {
2097 default: return false;
2098 // type-specifiers
2099 case tok::kw_short:
2100 case tok::kw_long:
2101 case tok::kw_signed:
2102 case tok::kw_unsigned:
2103 case tok::kw__Complex:
2104 case tok::kw__Imaginary:
2105 case tok::kw_void:
2106 case tok::kw_char:
2107 case tok::kw_wchar_t:
2108 case tok::kw_char16_t:
2109 case tok::kw_char32_t:
2110 case tok::kw_int:
2111 case tok::kw_float:
2112 case tok::kw_double:
2113 case tok::kw_bool:
2114 case tok::kw__Bool:
2115 case tok::kw__Decimal32:
2116 case tok::kw__Decimal64:
2117 case tok::kw__Decimal128:
2118 case tok::kw___vector:
2119
2120 // struct-or-union-specifier (C99) or class-specifier (C++)
2121 case tok::kw_class:
2122 case tok::kw_struct:
2123 case tok::kw_union:
2124 // enum-specifier
2125 case tok::kw_enum:
2126
2127 // typedef-name
2128 case tok::annot_typename:
2129 return true;
2130 }
2131}
2132
Steve Naroff5f8aa692008-02-11 23:15:56 +00002133/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002134/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002135bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002136 switch (Tok.getKind()) {
2137 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Chris Lattner166a8fc2009-01-04 23:41:41 +00002139 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002140 if (TryAltiVecVectorToken())
2141 return true;
2142 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002143 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002144 // Annotate typenames and C++ scope specifiers. If we get one, just
2145 // recurse to handle whatever we get.
2146 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002147 return true;
2148 if (Tok.is(tok::identifier))
2149 return false;
2150 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002151
Chris Lattner166a8fc2009-01-04 23:41:41 +00002152 case tok::coloncolon: // ::foo::bar
2153 if (NextToken().is(tok::kw_new) || // ::new
2154 NextToken().is(tok::kw_delete)) // ::delete
2155 return false;
2156
Chris Lattner166a8fc2009-01-04 23:41:41 +00002157 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002158 return true;
2159 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Reid Spencer5f016e22007-07-11 17:01:13 +00002161 // GNU attributes support.
2162 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002163 // GNU typeof support.
2164 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Reid Spencer5f016e22007-07-11 17:01:13 +00002166 // type-specifiers
2167 case tok::kw_short:
2168 case tok::kw_long:
2169 case tok::kw_signed:
2170 case tok::kw_unsigned:
2171 case tok::kw__Complex:
2172 case tok::kw__Imaginary:
2173 case tok::kw_void:
2174 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002175 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002176 case tok::kw_char16_t:
2177 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 case tok::kw_int:
2179 case tok::kw_float:
2180 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002181 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002182 case tok::kw__Bool:
2183 case tok::kw__Decimal32:
2184 case tok::kw__Decimal64:
2185 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002186 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002187
Chris Lattner99dc9142008-04-13 18:59:07 +00002188 // struct-or-union-specifier (C99) or class-specifier (C++)
2189 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002190 case tok::kw_struct:
2191 case tok::kw_union:
2192 // enum-specifier
2193 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Reid Spencer5f016e22007-07-11 17:01:13 +00002195 // type-qualifier
2196 case tok::kw_const:
2197 case tok::kw_volatile:
2198 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002199
2200 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002201 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002202 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002203
Chris Lattner7c186be2008-10-20 00:25:30 +00002204 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2205 case tok::less:
2206 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002207
Steve Naroff239f0732008-12-25 14:16:32 +00002208 case tok::kw___cdecl:
2209 case tok::kw___stdcall:
2210 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002211 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002212 case tok::kw___w64:
2213 case tok::kw___ptr64:
2214 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002215 }
2216}
2217
2218/// isDeclarationSpecifier() - Return true if the current token is part of a
2219/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002220bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002221 switch (Tok.getKind()) {
2222 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Chris Lattner166a8fc2009-01-04 23:41:41 +00002224 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002225 // Unfortunate hack to support "Class.factoryMethod" notation.
2226 if (getLang().ObjC1 && NextToken().is(tok::period))
2227 return false;
John Thompson82287d12010-02-05 00:12:22 +00002228 if (TryAltiVecVectorToken())
2229 return true;
2230 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002231 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002232 // Annotate typenames and C++ scope specifiers. If we get one, just
2233 // recurse to handle whatever we get.
2234 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002235 return true;
2236 if (Tok.is(tok::identifier))
2237 return false;
2238 return isDeclarationSpecifier();
2239
Chris Lattner166a8fc2009-01-04 23:41:41 +00002240 case tok::coloncolon: // ::foo::bar
2241 if (NextToken().is(tok::kw_new) || // ::new
2242 NextToken().is(tok::kw_delete)) // ::delete
2243 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Chris Lattner166a8fc2009-01-04 23:41:41 +00002245 // Annotate typenames and C++ scope specifiers. If we get one, just
2246 // recurse to handle whatever we get.
2247 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002248 return true;
2249 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Reid Spencer5f016e22007-07-11 17:01:13 +00002251 // storage-class-specifier
2252 case tok::kw_typedef:
2253 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002254 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002255 case tok::kw_static:
2256 case tok::kw_auto:
2257 case tok::kw_register:
2258 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Reid Spencer5f016e22007-07-11 17:01:13 +00002260 // type-specifiers
2261 case tok::kw_short:
2262 case tok::kw_long:
2263 case tok::kw_signed:
2264 case tok::kw_unsigned:
2265 case tok::kw__Complex:
2266 case tok::kw__Imaginary:
2267 case tok::kw_void:
2268 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002269 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002270 case tok::kw_char16_t:
2271 case tok::kw_char32_t:
2272
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 case tok::kw_int:
2274 case tok::kw_float:
2275 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002276 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002277 case tok::kw__Bool:
2278 case tok::kw__Decimal32:
2279 case tok::kw__Decimal64:
2280 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002281 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Chris Lattner99dc9142008-04-13 18:59:07 +00002283 // struct-or-union-specifier (C99) or class-specifier (C++)
2284 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002285 case tok::kw_struct:
2286 case tok::kw_union:
2287 // enum-specifier
2288 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Reid Spencer5f016e22007-07-11 17:01:13 +00002290 // type-qualifier
2291 case tok::kw_const:
2292 case tok::kw_volatile:
2293 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002294
Reid Spencer5f016e22007-07-11 17:01:13 +00002295 // function-specifier
2296 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002297 case tok::kw_virtual:
2298 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002299
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002300 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002301 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002302
Chris Lattner1ef08762007-08-09 17:01:07 +00002303 // GNU typeof support.
2304 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002305
Chris Lattner1ef08762007-08-09 17:01:07 +00002306 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002307 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002308 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Chris Lattnerf3948c42008-07-26 03:38:44 +00002310 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2311 case tok::less:
2312 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Steve Naroff47f52092009-01-06 19:34:12 +00002314 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002315 case tok::kw___cdecl:
2316 case tok::kw___stdcall:
2317 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002318 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002319 case tok::kw___w64:
2320 case tok::kw___ptr64:
2321 case tok::kw___forceinline:
2322 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 }
2324}
2325
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002326bool Parser::isConstructorDeclarator() {
2327 TentativeParsingAction TPA(*this);
2328
2329 // Parse the C++ scope specifier.
2330 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002331 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2332 TPA.Revert();
2333 return false;
2334 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002335
2336 // Parse the constructor name.
2337 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2338 // We already know that we have a constructor name; just consume
2339 // the token.
2340 ConsumeToken();
2341 } else {
2342 TPA.Revert();
2343 return false;
2344 }
2345
2346 // Current class name must be followed by a left parentheses.
2347 if (Tok.isNot(tok::l_paren)) {
2348 TPA.Revert();
2349 return false;
2350 }
2351 ConsumeParen();
2352
2353 // A right parentheses or ellipsis signals that we have a constructor.
2354 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2355 TPA.Revert();
2356 return true;
2357 }
2358
2359 // If we need to, enter the specified scope.
2360 DeclaratorScopeObj DeclScopeObj(*this, SS);
2361 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2362 DeclScopeObj.EnterDeclaratorScope();
2363
2364 // Check whether the next token(s) are part of a declaration
2365 // specifier, in which case we have the start of a parameter and,
2366 // therefore, we know that this is a constructor.
2367 bool IsConstructor = isDeclarationSpecifier();
2368 TPA.Revert();
2369 return IsConstructor;
2370}
Reid Spencer5f016e22007-07-11 17:01:13 +00002371
2372/// ParseTypeQualifierListOpt
2373/// type-qualifier-list: [C99 6.7.5]
2374/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002375/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002376/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002377/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002378/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2379/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002380///
Sean Huntbbd37c62009-11-21 08:43:09 +00002381void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2382 bool CXX0XAttributesAllowed) {
2383 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2384 SourceLocation Loc = Tok.getLocation();
2385 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2386 if (CXX0XAttributesAllowed)
2387 DS.AddAttributes(Attr.AttrList);
2388 else
2389 Diag(Loc, diag::err_attributes_not_allowed);
2390 }
2391
Reid Spencer5f016e22007-07-11 17:01:13 +00002392 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002393 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002394 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002395 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002396 SourceLocation Loc = Tok.getLocation();
2397
2398 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002399 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002400 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2401 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 break;
2403 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002404 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2405 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002406 break;
2407 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002408 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2409 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002410 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002411 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002412 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002413 case tok::kw___cdecl:
2414 case tok::kw___stdcall:
2415 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002416 case tok::kw___thiscall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002417 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002418 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2419 continue;
2420 }
2421 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002422 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002423 if (GNUAttributesAllowed) {
2424 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002425 continue; // do *not* consume the next token!
2426 }
2427 // otherwise, FALL THROUGH!
2428 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002429 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002430 // If this is not a type-qualifier token, we're done reading type
2431 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002432 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002433 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002434 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002435
Reid Spencer5f016e22007-07-11 17:01:13 +00002436 // If the specifier combination wasn't legal, issue a diagnostic.
2437 if (isInvalid) {
2438 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002439 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002440 }
2441 ConsumeToken();
2442 }
2443}
2444
2445
2446/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2447///
2448void Parser::ParseDeclarator(Declarator &D) {
2449 /// This implements the 'declarator' production in the C grammar, then checks
2450 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002451 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002452}
2453
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002454/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2455/// is parsed by the function passed to it. Pass null, and the direct-declarator
2456/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002457/// ptr-operator production.
2458///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002459/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2460/// [C] pointer[opt] direct-declarator
2461/// [C++] direct-declarator
2462/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002463///
2464/// pointer: [C99 6.7.5]
2465/// '*' type-qualifier-list[opt]
2466/// '*' type-qualifier-list[opt] pointer
2467///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002468/// ptr-operator:
2469/// '*' cv-qualifier-seq[opt]
2470/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002471/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002472/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002473/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002474/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002475void Parser::ParseDeclaratorInternal(Declarator &D,
2476 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002477 if (Diags.hasAllExtensionsSilenced())
2478 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002479 // C++ member pointers start with a '::' or a nested-name.
2480 // Member pointers get special handling, since there's no place for the
2481 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002482 if (getLang().CPlusPlus &&
2483 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2484 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002485 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002486 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2487
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002488 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002489 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002490 // The scope spec really belongs to the direct-declarator.
2491 D.getCXXScopeSpec() = SS;
2492 if (DirectDeclParser)
2493 (this->*DirectDeclParser)(D);
2494 return;
2495 }
2496
2497 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002498 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002499 DeclSpec DS;
2500 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002501 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002502
2503 // Recurse to parse whatever is left.
2504 ParseDeclaratorInternal(D, DirectDeclParser);
2505
2506 // Sema will have to catch (syntactically invalid) pointers into global
2507 // scope. It has to catch pointers into namespace scope anyway.
2508 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002509 Loc, DS.TakeAttributes()),
2510 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002511 return;
2512 }
2513 }
2514
2515 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002516 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002517 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002518 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002519 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002520 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002521 if (DirectDeclParser)
2522 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002523 return;
2524 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002525
Sebastian Redl05532f22009-03-15 22:02:01 +00002526 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2527 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002528 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002529 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002530
Chris Lattner9af55002009-03-27 04:18:06 +00002531 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002532 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002533 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002534
Reid Spencer5f016e22007-07-11 17:01:13 +00002535 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002536 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002537
Reid Spencer5f016e22007-07-11 17:01:13 +00002538 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002539 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002540 if (Kind == tok::star)
2541 // Remember that we parsed a pointer type, and remember the type-quals.
2542 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002543 DS.TakeAttributes()),
2544 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002545 else
2546 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002547 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002548 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002549 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002550 } else {
2551 // Is a reference
2552 DeclSpec DS;
2553
Sebastian Redl743de1f2009-03-23 00:00:23 +00002554 // Complain about rvalue references in C++03, but then go on and build
2555 // the declarator.
2556 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2557 Diag(Loc, diag::err_rvalue_reference);
2558
Reid Spencer5f016e22007-07-11 17:01:13 +00002559 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2560 // cv-qualifiers are introduced through the use of a typedef or of a
2561 // template type argument, in which case the cv-qualifiers are ignored.
2562 //
2563 // [GNU] Retricted references are allowed.
2564 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002565 // [C++0x] Attributes on references are not allowed.
2566 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002567 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002568
2569 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2570 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2571 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002572 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002573 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2574 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002575 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 }
2577
2578 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002579 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002580
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002581 if (D.getNumTypeObjects() > 0) {
2582 // C++ [dcl.ref]p4: There shall be no references to references.
2583 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2584 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002585 if (const IdentifierInfo *II = D.getIdentifier())
2586 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2587 << II;
2588 else
2589 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2590 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002591
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002592 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002593 // can go ahead and build the (technically ill-formed)
2594 // declarator: reference collapsing will take care of it.
2595 }
2596 }
2597
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002599 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002600 DS.TakeAttributes(),
2601 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002602 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002603 }
2604}
2605
2606/// ParseDirectDeclarator
2607/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002608/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002609/// '(' declarator ')'
2610/// [GNU] '(' attributes declarator ')'
2611/// [C90] direct-declarator '[' constant-expression[opt] ']'
2612/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2613/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2614/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2615/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2616/// direct-declarator '(' parameter-type-list ')'
2617/// direct-declarator '(' identifier-list[opt] ')'
2618/// [GNU] direct-declarator '(' parameter-forward-declarations
2619/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002620/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2621/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002622/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002623///
2624/// declarator-id: [C++ 8]
2625/// id-expression
2626/// '::'[opt] nested-name-specifier[opt] type-name
2627///
2628/// id-expression: [C++ 5.1]
2629/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002630/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002631///
2632/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002633/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002634/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002635/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002636/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002637/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002638///
Reid Spencer5f016e22007-07-11 17:01:13 +00002639void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002640 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002641
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002642 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2643 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002644 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002645 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2646 true);
John McCall9ba61662010-02-26 08:45:28 +00002647 }
2648
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002649 if (D.getCXXScopeSpec().isValid()) {
John McCalle7e278b2009-12-11 20:04:54 +00002650 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2651 // Change the declaration context for name lookup, until this function
2652 // is exited (and the declarator has been parsed).
2653 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002654 }
2655
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002656 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2657 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2658 // We found something that indicates the start of an unqualified-id.
2659 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002660 bool AllowConstructorName;
2661 if (D.getDeclSpec().hasTypeSpecifier())
2662 AllowConstructorName = false;
2663 else if (D.getCXXScopeSpec().isSet())
2664 AllowConstructorName =
2665 (D.getContext() == Declarator::FileContext ||
2666 (D.getContext() == Declarator::MemberContext &&
2667 D.getDeclSpec().isFriendSpecified()));
2668 else
2669 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2670
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002671 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2672 /*EnteringContext=*/true,
2673 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002674 AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002675 /*ObjectType=*/0,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002676 D.getName()) ||
2677 // Once we're past the identifier, if the scope was bad, mark the
2678 // whole declarator bad.
2679 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002680 D.SetIdentifier(0, Tok.getLocation());
2681 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002682 } else {
2683 // Parsed the unqualified-id; update range information and move along.
2684 if (D.getSourceRange().getBegin().isInvalid())
2685 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2686 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002687 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002688 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002689 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002690 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002691 assert(!getLang().CPlusPlus &&
2692 "There's a C++-specific check for tok::identifier above");
2693 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2694 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2695 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002696 goto PastIdentifier;
2697 }
2698
2699 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002700 // direct-declarator: '(' declarator ')'
2701 // direct-declarator: '(' attributes declarator ')'
2702 // Example: 'char (*X)' or 'int (*XX)(void)'
2703 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002704
2705 // If the declarator was parenthesized, we entered the declarator
2706 // scope when parsing the parenthesized declarator, then exited
2707 // the scope already. Re-enter the scope, if we need to.
2708 if (D.getCXXScopeSpec().isSet()) {
2709 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2710 // Change the declaration context for name lookup, until this function
2711 // is exited (and the declarator has been parsed).
2712 DeclScopeObj.EnterDeclaratorScope();
2713 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002714 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002715 // This could be something simple like "int" (in which case the declarator
2716 // portion is empty), if an abstract-declarator is allowed.
2717 D.SetIdentifier(0, Tok.getLocation());
2718 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002719 if (D.getContext() == Declarator::MemberContext)
2720 Diag(Tok, diag::err_expected_member_name_or_semi)
2721 << D.getDeclSpec().getSourceRange();
2722 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002723 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002724 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002725 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002726 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002727 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002728 }
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002730 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002731 assert(D.isPastIdentifier() &&
2732 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002733
Sean Huntbbd37c62009-11-21 08:43:09 +00002734 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002735 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002736 && isCXX0XAttributeSpecifier(true)) {
2737 SourceLocation AttrEndLoc;
2738 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2739 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2740 }
2741
Reid Spencer5f016e22007-07-11 17:01:13 +00002742 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002743 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002744 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2745 // In such a case, check if we actually have a function declarator; if it
2746 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002747 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2748 // When not in file scope, warn for ambiguous function declarators, just
2749 // in case the author intended it as a variable definition.
2750 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2751 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2752 break;
2753 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002754 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002755 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002756 ParseBracketDeclarator(D);
2757 } else {
2758 break;
2759 }
2760 }
2761}
2762
Chris Lattneref4715c2008-04-06 05:45:57 +00002763/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2764/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002765/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002766/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2767///
2768/// direct-declarator:
2769/// '(' declarator ')'
2770/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002771/// direct-declarator '(' parameter-type-list ')'
2772/// direct-declarator '(' identifier-list[opt] ')'
2773/// [GNU] direct-declarator '(' parameter-forward-declarations
2774/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002775///
2776void Parser::ParseParenDeclarator(Declarator &D) {
2777 SourceLocation StartLoc = ConsumeParen();
2778 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002779
Chris Lattner7399ee02008-10-20 02:05:46 +00002780 // Eat any attributes before we look at whether this is a grouping or function
2781 // declarator paren. If this is a grouping paren, the attribute applies to
2782 // the type being built up, for example:
2783 // int (__attribute__(()) *x)(long y)
2784 // If this ends up not being a grouping paren, the attribute applies to the
2785 // first argument, for example:
2786 // int (__attribute__(()) int x)
2787 // In either case, we need to eat any attributes to be able to determine what
2788 // sort of paren this is.
2789 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002790 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002791 bool RequiresArg = false;
2792 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002793 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002794
Chris Lattner7399ee02008-10-20 02:05:46 +00002795 // We require that the argument list (if this is a non-grouping paren) be
2796 // present even if the attribute list was empty.
2797 RequiresArg = true;
2798 }
Steve Naroff239f0732008-12-25 14:16:32 +00002799 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002800 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002801 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2802 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002803 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002804 }
Mike Stump1eb44332009-09-09 15:08:12 +00002805
Chris Lattneref4715c2008-04-06 05:45:57 +00002806 // If we haven't past the identifier yet (or where the identifier would be
2807 // stored, if this is an abstract declarator), then this is probably just
2808 // grouping parens. However, if this could be an abstract-declarator, then
2809 // this could also be the start of function arguments (consider 'void()').
2810 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Chris Lattneref4715c2008-04-06 05:45:57 +00002812 if (!D.mayOmitIdentifier()) {
2813 // If this can't be an abstract-declarator, this *must* be a grouping
2814 // paren, because we haven't seen the identifier yet.
2815 isGrouping = true;
2816 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002817 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002818 isDeclarationSpecifier()) { // 'int(int)' is a function.
2819 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2820 // considered to be a type, not a K&R identifier-list.
2821 isGrouping = false;
2822 } else {
2823 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2824 isGrouping = true;
2825 }
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Chris Lattneref4715c2008-04-06 05:45:57 +00002827 // If this is a grouping paren, handle:
2828 // direct-declarator: '(' declarator ')'
2829 // direct-declarator: '(' attributes declarator ')'
2830 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002831 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002832 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002833 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002834 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002835
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002836 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002837 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002838 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002839
2840 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002841 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002842 return;
2843 }
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Chris Lattneref4715c2008-04-06 05:45:57 +00002845 // Okay, if this wasn't a grouping paren, it must be the start of a function
2846 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002847 // identifier (and remember where it would have been), then call into
2848 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002849 D.SetIdentifier(0, Tok.getLocation());
2850
Ted Kremenek1e377652010-02-11 02:19:13 +00002851 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002852}
2853
2854/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2855/// declarator D up to a paren, which indicates that we are parsing function
2856/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002857///
Chris Lattner7399ee02008-10-20 02:05:46 +00002858/// If AttrList is non-null, then the caller parsed those arguments immediately
2859/// after the open paren - they should be considered to be the first argument of
2860/// a parameter. If RequiresArg is true, then the first argument of the
2861/// function is required to be present and required to not be an identifier
2862/// list.
2863///
Reid Spencer5f016e22007-07-11 17:01:13 +00002864/// This method also handles this portion of the grammar:
2865/// parameter-type-list: [C99 6.7.5]
2866/// parameter-list
2867/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002868/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002869///
2870/// parameter-list: [C99 6.7.5]
2871/// parameter-declaration
2872/// parameter-list ',' parameter-declaration
2873///
2874/// parameter-declaration: [C99 6.7.5]
2875/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002876/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002877/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002878/// declaration-specifiers abstract-declarator[opt]
2879/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002880/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002881/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2882///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002883/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002884/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002885///
Chris Lattner7399ee02008-10-20 02:05:46 +00002886void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2887 AttributeList *AttrList,
2888 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002889 // lparen is already consumed!
2890 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Chris Lattner7399ee02008-10-20 02:05:46 +00002892 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002893 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002894 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002895 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002896 delete AttrList;
2897 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002898
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002899 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2900 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002901
2902 // cv-qualifier-seq[opt].
2903 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002904 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002905 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002906 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002907 llvm::SmallVector<TypeTy*, 2> Exceptions;
2908 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002909 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002910 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002911 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002912 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002913
2914 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002915 if (Tok.is(tok::kw_throw)) {
2916 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002917 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002918 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002919 hasAnyExceptionSpec);
2920 assert(Exceptions.size() == ExceptionRanges.size() &&
2921 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002922 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002923 }
2924
Chris Lattnerf97409f2008-04-06 06:57:35 +00002925 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002926 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002927 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002928 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002929 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002930 /*arglist*/ 0, 0,
2931 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002932 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002933 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002934 Exceptions.data(),
2935 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002936 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002937 LParenLoc, RParenLoc, D),
2938 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002939 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002940 }
2941
Chris Lattner7399ee02008-10-20 02:05:46 +00002942 // Alternatively, this parameter list may be an identifier list form for a
2943 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00002944 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2945 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00002946 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002947 // K&R identifier lists can't have typedefs as identifiers, per
2948 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002949 if (RequiresArg) {
2950 Diag(Tok, diag::err_argument_required_after_attribute);
2951 delete AttrList;
2952 }
Chris Lattner83a94472010-05-14 17:23:36 +00002953
Steve Naroff2d081c42009-01-28 19:16:40 +00002954 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00002955 // normal declarators, not for abstract-declarators. Get the first
2956 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002957 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00002958 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002959
2960 // Identifier lists follow a really simple grammar: the identifiers can
2961 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2962 // identifier lists are really rare in the brave new modern world, and it
2963 // is very common for someone to typo a type in a non-k&r style list. If
2964 // we are presented with something like: "void foo(intptr x, float y)",
2965 // we don't want to start parsing the function declarator as though it is
2966 // a K&R style declarator just because intptr is an invalid type.
2967 //
2968 // To handle this, we check to see if the token after the first identifier
2969 // is a "," or ")". Only if so, do we parse it as an identifier list.
2970 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2971 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
2972 FirstTok.getIdentifierInfo(),
2973 FirstTok.getLocation(), D);
2974
2975 // If we get here, the code is invalid. Push the first identifier back
2976 // into the token stream and parse the first argument as an (invalid)
2977 // normal argument declarator.
2978 PP.EnterToken(Tok);
2979 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00002980 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002981 }
Mike Stump1eb44332009-09-09 15:08:12 +00002982
Chris Lattnerf97409f2008-04-06 06:57:35 +00002983 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002984
Chris Lattnerf97409f2008-04-06 06:57:35 +00002985 // Build up an array of information about the parsed arguments.
2986 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002987
2988 // Enter function-declaration scope, limiting any declarators to the
2989 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002990 ParseScope PrototypeScope(this,
2991 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Chris Lattnerf97409f2008-04-06 06:57:35 +00002993 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002994 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002995 while (1) {
2996 if (Tok.is(tok::ellipsis)) {
2997 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002998 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002999 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003000 }
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Chris Lattnerf97409f2008-04-06 06:57:35 +00003002 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003003
Chris Lattnerf97409f2008-04-06 06:57:35 +00003004 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003005 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003006 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003007
3008 // If the caller parsed attributes for the first argument, add them now.
3009 if (AttrList) {
3010 DS.AddAttributes(AttrList);
3011 AttrList = 0; // Only apply the attributes to the first parameter.
3012 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003013 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Chris Lattnerf97409f2008-04-06 06:57:35 +00003015 // Parse the declarator. This is "PrototypeContext", because we must
3016 // accept either 'declarator' or 'abstract-declarator' here.
3017 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3018 ParseDeclarator(ParmDecl);
3019
3020 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003021 if (Tok.is(tok::kw___attribute)) {
3022 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003023 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003024 ParmDecl.AddAttributes(AttrList, Loc);
3025 }
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Chris Lattnerf97409f2008-04-06 06:57:35 +00003027 // Remember this parsed parameter in ParamInfo.
3028 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Douglas Gregor72b505b2008-12-16 21:30:33 +00003030 // DefArgToks is used when the parsing of default arguments needs
3031 // to be delayed.
3032 CachedTokens *DefArgToks = 0;
3033
Chris Lattnerf97409f2008-04-06 06:57:35 +00003034 // If no parameter was specified, verify that *something* was specified,
3035 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003036 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3037 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003038 // Completely missing, emit error.
3039 Diag(DSStart, diag::err_missing_param);
3040 } else {
3041 // Otherwise, we have something. Add it and let semantic analysis try
3042 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Chris Lattnerf97409f2008-04-06 06:57:35 +00003044 // Inform the actions module about the parameter declarator, so it gets
3045 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00003046 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003047
3048 // Parse the default argument, if any. We parse the default
3049 // arguments in all dialects; the semantic analysis in
3050 // ActOnParamDefaultArgument will reject the default argument in
3051 // C.
3052 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003053 SourceLocation EqualLoc = Tok.getLocation();
3054
Chris Lattner04421082008-04-08 04:40:51 +00003055 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003056 if (D.getContext() == Declarator::MemberContext) {
3057 // If we're inside a class definition, cache the tokens
3058 // corresponding to the default argument. We'll actually parse
3059 // them when we see the end of the class definition.
3060 // FIXME: Templates will require something similar.
3061 // FIXME: Can we use a smart pointer for Toks?
3062 DefArgToks = new CachedTokens;
3063
Mike Stump1eb44332009-09-09 15:08:12 +00003064 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003065 /*StopAtSemi=*/true,
3066 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003067 delete DefArgToks;
3068 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003069 Actions.ActOnParamDefaultArgumentError(Param);
3070 } else
Mike Stump1eb44332009-09-09 15:08:12 +00003071 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003072 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00003073 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003074 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003075 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003076
Douglas Gregor72b505b2008-12-16 21:30:33 +00003077 OwningExprResult DefArgResult(ParseAssignmentExpression());
3078 if (DefArgResult.isInvalid()) {
3079 Actions.ActOnParamDefaultArgumentError(Param);
3080 SkipUntil(tok::comma, tok::r_paren, true, true);
3081 } else {
3082 // Inform the actions module about the default argument
3083 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00003084 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00003085 }
Chris Lattner04421082008-04-08 04:40:51 +00003086 }
3087 }
Mike Stump1eb44332009-09-09 15:08:12 +00003088
3089 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3090 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003091 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003092 }
3093
3094 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003095 if (Tok.isNot(tok::comma)) {
3096 if (Tok.is(tok::ellipsis)) {
3097 IsVariadic = true;
3098 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3099
3100 if (!getLang().CPlusPlus) {
3101 // We have ellipsis without a preceding ',', which is ill-formed
3102 // in C. Complain and provide the fix.
3103 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003104 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003105 }
3106 }
3107
3108 break;
3109 }
Mike Stump1eb44332009-09-09 15:08:12 +00003110
Chris Lattnerf97409f2008-04-06 06:57:35 +00003111 // Consume the comma.
3112 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003113 }
Mike Stump1eb44332009-09-09 15:08:12 +00003114
Chris Lattnerf97409f2008-04-06 06:57:35 +00003115 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003116 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003118 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003119 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3120 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003121
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003122 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003123 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003124 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003125 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00003126 llvm::SmallVector<TypeTy*, 2> Exceptions;
3127 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003128
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003129 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003130 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003131 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003132 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003133 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003134
3135 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003136 if (Tok.is(tok::kw_throw)) {
3137 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003138 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003139 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003140 hasAnyExceptionSpec);
3141 assert(Exceptions.size() == ExceptionRanges.size() &&
3142 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003143 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003144 }
3145
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003147 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003148 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003149 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003150 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003151 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003152 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003153 Exceptions.data(),
3154 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003155 Exceptions.size(),
3156 LParenLoc, RParenLoc, D),
3157 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003158}
3159
Chris Lattner66d28652008-04-06 06:34:08 +00003160/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3161/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003162/// first identifier has already been consumed, and the current token is the
3163/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003164///
3165/// identifier-list: [C99 6.7.5]
3166/// identifier
3167/// identifier-list ',' identifier
3168///
3169void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003170 IdentifierInfo *FirstIdent,
3171 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003172 Declarator &D) {
3173 // Build up an array of information about the parsed arguments.
3174 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3175 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003176
Chris Lattner66d28652008-04-06 06:34:08 +00003177 // If there was no identifier specified for the declarator, either we are in
3178 // an abstract-declarator, or we are in a parameter declarator which was found
3179 // to be abstract. In abstract-declarators, identifier lists are not valid:
3180 // diagnose this.
3181 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003182 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003183
Chris Lattner83a94472010-05-14 17:23:36 +00003184 // The first identifier was already read, and is known to be the first
3185 // identifier in the list. Remember this identifier in ParamInfo.
3186 ParamsSoFar.insert(FirstIdent);
3187 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003188 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00003189
Chris Lattner66d28652008-04-06 06:34:08 +00003190 while (Tok.is(tok::comma)) {
3191 // Eat the comma.
3192 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003193
Chris Lattner50c64772008-04-06 06:39:19 +00003194 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003195 if (Tok.isNot(tok::identifier)) {
3196 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003197 SkipUntil(tok::r_paren);
3198 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003199 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003200
Chris Lattner66d28652008-04-06 06:34:08 +00003201 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003202
3203 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00003204 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003205 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003206
Chris Lattner66d28652008-04-06 06:34:08 +00003207 // Verify that the argument identifier has not already been mentioned.
3208 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003209 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003210 } else {
3211 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003212 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003213 Tok.getLocation(),
3214 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00003215 }
Mike Stump1eb44332009-09-09 15:08:12 +00003216
Chris Lattner66d28652008-04-06 06:34:08 +00003217 // Eat the identifier.
3218 ConsumeToken();
3219 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003220
3221 // If we have the closing ')', eat it and we're done.
3222 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3223
Chris Lattner50c64772008-04-06 06:39:19 +00003224 // Remember that we parsed a function type, and remember the attributes. This
3225 // function type is always a K&R style function type, which is not varargs and
3226 // has no prototype.
3227 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003228 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003229 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003230 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003231 /*exception*/false,
3232 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003233 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003234 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003235}
Chris Lattneref4715c2008-04-06 05:45:57 +00003236
Reid Spencer5f016e22007-07-11 17:01:13 +00003237/// [C90] direct-declarator '[' constant-expression[opt] ']'
3238/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3239/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3240/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3241/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3242void Parser::ParseBracketDeclarator(Declarator &D) {
3243 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003244
Chris Lattner378c7e42008-12-18 07:27:21 +00003245 // C array syntax has many features, but by-far the most common is [] and [4].
3246 // This code does a fast path to handle some of the most obvious cases.
3247 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003248 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003249 //FIXME: Use these
3250 CXX0XAttributeList Attr;
3251 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3252 Attr = ParseCXX0XAttributes();
3253 }
3254
Chris Lattner378c7e42008-12-18 07:27:21 +00003255 // Remember that we parsed the empty array type.
3256 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003257 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3258 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003259 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003260 return;
3261 } else if (Tok.getKind() == tok::numeric_constant &&
3262 GetLookAheadToken(1).is(tok::r_square)) {
3263 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00003264 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003265 ConsumeToken();
3266
Sebastian Redlab197ba2009-02-09 18:23:29 +00003267 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003268 //FIXME: Use these
3269 CXX0XAttributeList Attr;
3270 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3271 Attr = ParseCXX0XAttributes();
3272 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003273
3274 // If there was an error parsing the assignment-expression, recover.
3275 if (ExprRes.isInvalid())
3276 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003277
Chris Lattner378c7e42008-12-18 07:27:21 +00003278 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003279 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3280 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003281 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003282 return;
3283 }
Mike Stump1eb44332009-09-09 15:08:12 +00003284
Reid Spencer5f016e22007-07-11 17:01:13 +00003285 // If valid, this location is the position where we read the 'static' keyword.
3286 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003287 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003289
Reid Spencer5f016e22007-07-11 17:01:13 +00003290 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003291 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003293 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003294
Reid Spencer5f016e22007-07-11 17:01:13 +00003295 // If we haven't already read 'static', check to see if there is one after the
3296 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003297 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003299
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3301 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00003302 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003304 // Handle the case where we have '[*]' as the array size. However, a leading
3305 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3306 // the the token after the star is a ']'. Since stars in arrays are
3307 // infrequent, use of lookahead is not costly here.
3308 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003309 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003310
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003311 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003312 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003313 StaticLoc = SourceLocation(); // Drop the static.
3314 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003315 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003316 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003317 // Note, in C89, this production uses the constant-expr production instead
3318 // of assignment-expr. The only difference is that assignment-expr allows
3319 // things like '=' and '*='. Sema rejects these in C89 mode because they
3320 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003321
Douglas Gregore0762c92009-06-19 23:52:42 +00003322 // Parse the constant-expression or assignment-expression now (depending
3323 // on dialect).
3324 if (getLang().CPlusPlus)
3325 NumElements = ParseConstantExpression();
3326 else
3327 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003328 }
Mike Stump1eb44332009-09-09 15:08:12 +00003329
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003331 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003332 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003333 // If the expression was invalid, skip it.
3334 SkipUntil(tok::r_square);
3335 return;
3336 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003337
3338 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3339
Sean Huntbbd37c62009-11-21 08:43:09 +00003340 //FIXME: Use these
3341 CXX0XAttributeList Attr;
3342 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3343 Attr = ParseCXX0XAttributes();
3344 }
3345
Chris Lattner378c7e42008-12-18 07:27:21 +00003346 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003347 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3348 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003349 NumElements.release(),
3350 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003351 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003352}
3353
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003354/// [GNU] typeof-specifier:
3355/// typeof ( expressions )
3356/// typeof ( type-name )
3357/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003358///
3359void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003360 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003361 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003362 SourceLocation StartLoc = ConsumeToken();
3363
John McCallcfb708c2010-01-13 20:03:27 +00003364 const bool hasParens = Tok.is(tok::l_paren);
3365
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003366 bool isCastExpr;
3367 TypeTy *CastTy;
3368 SourceRange CastRange;
3369 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3370 isCastExpr,
3371 CastTy,
3372 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003373 if (hasParens)
3374 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003375
3376 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003377 // FIXME: Not accurate, the range gets one token more than it should.
3378 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003379 else
3380 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003381
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003382 if (isCastExpr) {
3383 if (!CastTy) {
3384 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003385 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003386 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003387
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003388 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003389 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003390 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3391 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003392 DiagID, CastTy))
3393 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003394 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003395 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003396
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003397 // If we get here, the operand to the typeof was an expresion.
3398 if (Operand.isInvalid()) {
3399 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003400 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003401 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003402
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003403 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003404 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003405 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3406 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003407 DiagID, Operand.release()))
3408 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003409}
Chris Lattner1b492422010-02-28 18:33:55 +00003410
3411
3412/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3413/// from TryAltiVecVectorToken.
3414bool Parser::TryAltiVecVectorTokenOutOfLine() {
3415 Token Next = NextToken();
3416 switch (Next.getKind()) {
3417 default: return false;
3418 case tok::kw_short:
3419 case tok::kw_long:
3420 case tok::kw_signed:
3421 case tok::kw_unsigned:
3422 case tok::kw_void:
3423 case tok::kw_char:
3424 case tok::kw_int:
3425 case tok::kw_float:
3426 case tok::kw_double:
3427 case tok::kw_bool:
3428 case tok::kw___pixel:
3429 Tok.setKind(tok::kw___vector);
3430 return true;
3431 case tok::identifier:
3432 if (Next.getIdentifierInfo() == Ident_pixel) {
3433 Tok.setKind(tok::kw___vector);
3434 return true;
3435 }
3436 return false;
3437 }
3438}
3439
3440bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3441 const char *&PrevSpec, unsigned &DiagID,
3442 bool &isInvalid) {
3443 if (Tok.getIdentifierInfo() == Ident_vector) {
3444 Token Next = NextToken();
3445 switch (Next.getKind()) {
3446 case tok::kw_short:
3447 case tok::kw_long:
3448 case tok::kw_signed:
3449 case tok::kw_unsigned:
3450 case tok::kw_void:
3451 case tok::kw_char:
3452 case tok::kw_int:
3453 case tok::kw_float:
3454 case tok::kw_double:
3455 case tok::kw_bool:
3456 case tok::kw___pixel:
3457 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3458 return true;
3459 case tok::identifier:
3460 if (Next.getIdentifierInfo() == Ident_pixel) {
3461 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3462 return true;
3463 }
3464 break;
3465 default:
3466 break;
3467 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003468 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003469 DS.isTypeAltiVecVector()) {
3470 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3471 return true;
3472 }
3473 return false;
3474}
3475