blob: 976e60f0c93c1ab1d3e7c8a61b24578e6582bdfc [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000021#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.7: Declarations.
26//===----------------------------------------------------------------------===//
27
Chris Lattnerf5fbd792006-08-10 23:56:11 +000028/// ParseTypeName
29/// type-name: [C99 6.7.6]
30/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000031///
32/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000033TypeResult Parser::ParseTypeName(SourceRange *Range,
34 Declarator::TheContext Context) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000035 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000036 DeclSpec DS(AttrFactory);
Chris Lattner1890ac82006-08-13 01:16:23 +000037 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000038
Chris Lattnerf5fbd792006-08-10 23:56:11 +000039 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000040 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000041 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000042 if (Range)
43 *Range = DeclaratorInfo.getSourceRange();
44
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000045 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000046 return true;
47
Douglas Gregor0be31a22010-07-02 17:43:08 +000048 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000049}
50
Alexis Hunt96d5c762009-11-21 08:43:09 +000051/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000052///
53/// [GNU] attributes:
54/// attribute
55/// attributes attribute
56///
57/// [GNU] attribute:
58/// '__attribute__' '(' '(' attribute-list ')' ')'
59///
60/// [GNU] attribute-list:
61/// attrib
62/// attribute_list ',' attrib
63///
64/// [GNU] attrib:
65/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000066/// attrib-name
67/// attrib-name '(' identifier ')'
68/// attrib-name '(' identifier ',' nonempty-expr-list ')'
69/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000070///
Steve Naroff0f2fe172007-06-01 17:11:19 +000071/// [GNU] attrib-name:
72/// identifier
73/// typespec
74/// typequal
75/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000076///
Steve Naroff0f2fe172007-06-01 17:11:19 +000077/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000078/// token lookahead. Comment from gcc: "If they start with an identifier
79/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000080/// start with that identifier; otherwise they are an expression list."
81///
82/// At the moment, I am not doing 2 token lookahead. I am also unaware of
83/// any attributes that don't work (based on my limited testing). Most
84/// attributes are very simple in practice. Until we find a bug, I don't see
85/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000086
John McCall53fa7142010-12-24 02:08:15 +000087void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
88 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +000089 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattner76c72282007-10-09 17:33:22 +000091 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000092 ConsumeToken();
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
94 "attribute")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000096 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +000097 }
98 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
99 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000100 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101 }
102 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000103 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
104 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000105
106 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
108 ConsumeToken();
109 continue;
110 }
111 // we have an identifier or declaration specifier (const, int, etc.)
112 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
113 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000114
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000115 // Availability attributes have their own grammar.
116 if (AttrName->isStr("availability"))
117 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregora2f49452010-03-16 19:09:18 +0000118 // check if we have a "parameterized" attribute
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000119 else if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000120 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattner76c72282007-10-09 17:33:22 +0000122 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
124 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000125
126 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000128 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000129 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
130 ParmName, ParmLoc, 0, 0);
Chris Lattner76c72282007-10-09 17:33:22 +0000131 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 ConsumeToken();
133 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000134 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000136
Steve Naroff0f2fe172007-06-01 17:11:19 +0000137 // now parse the non-empty comma separated list of expressions
138 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000139 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000140 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 ArgExprsOk = false;
142 SkipUntil(tok::r_paren);
143 break;
144 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000145 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000146 }
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000148 break;
149 ConsumeToken(); // Eat the comma, move to the next argument
150 }
Chris Lattner76c72282007-10-09 17:33:22 +0000151 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000152 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000153 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
154 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000155 }
156 }
157 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000158 switch (Tok.getKind()) {
159 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000161 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000162 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
164 0, SourceLocation(), 0, 0);
Nate Begemanf2758702009-06-26 06:32:41 +0000165 break;
166 case tok::kw_char:
167 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000168 case tok::kw_char16_t:
169 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000170 case tok::kw_bool:
171 case tok::kw_short:
172 case tok::kw_int:
173 case tok::kw_long:
174 case tok::kw_signed:
175 case tok::kw_unsigned:
176 case tok::kw_float:
177 case tok::kw_double:
178 case tok::kw_void:
John McCall53fa7142010-12-24 02:08:15 +0000179 case tok::kw_typeof: {
180 AttributeList *attr
John McCall084e83d2011-03-24 11:26:52 +0000181 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
182 0, SourceLocation(), 0, 0);
John McCall53fa7142010-12-24 02:08:15 +0000183 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000184 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000185 // If it's a builtin type name, eat it and expect a rparen
186 // __attribute__(( vec_type_hint(char) ))
187 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000188 if (Tok.is(tok::r_paren))
189 ConsumeParen();
190 break;
John McCall53fa7142010-12-24 02:08:15 +0000191 }
Nate Begemanf2758702009-06-26 06:32:41 +0000192 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000193 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000194 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000195 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Steve Naroff0f2fe172007-06-01 17:11:19 +0000197 // now parse the list of expressions
198 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000199 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000200 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000201 ArgExprsOk = false;
202 SkipUntil(tok::r_paren);
203 break;
204 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000205 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000206 }
Chris Lattner76c72282007-10-09 17:33:22 +0000207 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000208 break;
209 ConsumeToken(); // Eat the comma, move to the next argument
210 }
211 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000212 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000213 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000214 attrs.addNew(AttrName, AttrNameLoc, 0,
215 AttrNameLoc, 0, SourceLocation(),
216 ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000217 }
Nate Begemanf2758702009-06-26 06:32:41 +0000218 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000219 }
220 }
221 } else {
John McCall084e83d2011-03-24 11:26:52 +0000222 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
223 0, SourceLocation(), 0, 0);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000224 }
225 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000226 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000227 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000228 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000229 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
230 SkipUntil(tok::r_paren, false);
231 }
John McCall53fa7142010-12-24 02:08:15 +0000232 if (endLoc)
233 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000234 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000235}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000236
Eli Friedman06de2b52009-06-08 07:21:15 +0000237/// ParseMicrosoftDeclSpec - Parse an __declspec construct
238///
239/// [MS] decl-specifier:
240/// __declspec ( extended-decl-modifier-seq )
241///
242/// [MS] extended-decl-modifier-seq:
243/// extended-decl-modifier[opt]
244/// extended-decl-modifier extended-decl-modifier-seq
245
John McCall53fa7142010-12-24 02:08:15 +0000246void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000247 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000248
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000249 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000250 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
251 "declspec")) {
252 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000253 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000254 }
Eli Friedman53339e02009-06-08 23:27:34 +0000255 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000256 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
257 SourceLocation AttrNameLoc = ConsumeToken();
258 if (Tok.is(tok::l_paren)) {
259 ConsumeParen();
260 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
261 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000262 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000263 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000264 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000265 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
266 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000267 }
268 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
269 SkipUntil(tok::r_paren, false);
270 } else {
John McCall084e83d2011-03-24 11:26:52 +0000271 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
272 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000273 }
274 }
275 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
276 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000277 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000278}
279
John McCall53fa7142010-12-24 02:08:15 +0000280void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000281 // Treat these like attributes
282 // FIXME: Allow Sema to distinguish between these and real attributes!
283 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000284 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
285 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000286 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
287 SourceLocation AttrNameLoc = ConsumeToken();
288 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
289 // FIXME: Support these properly!
290 continue;
John McCall084e83d2011-03-24 11:26:52 +0000291 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
292 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000293 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000294}
295
John McCall53fa7142010-12-24 02:08:15 +0000296void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000297 // Treat these like attributes
298 while (Tok.is(tok::kw___pascal)) {
299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000301 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000303 }
John McCall53fa7142010-12-24 02:08:15 +0000304}
305
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000306void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
307 // Treat these like attributes
308 while (Tok.is(tok::kw___kernel)) {
309 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000310 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
311 AttrNameLoc, 0, AttrNameLoc, 0,
312 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000313 }
314}
315
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000316void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
317 SourceLocation Loc = Tok.getLocation();
318 switch(Tok.getKind()) {
319 // OpenCL qualifiers:
320 case tok::kw___private:
321 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000322 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000323 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000324 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000325 break;
326
327 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000328 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000329 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000330 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000331 break;
332
333 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000334 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000335 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000336 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000337 break;
338
339 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000340 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000341 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000342 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000343 break;
344
345 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000346 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000347 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000348 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000349 break;
350
351 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000352 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000353 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000354 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000355 break;
356
357 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000358 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000359 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000360 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000361 break;
362 default: break;
363 }
364}
365
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000366/// \brief Parse a version number.
367///
368/// version:
369/// simple-integer
370/// simple-integer ',' simple-integer
371/// simple-integer ',' simple-integer ',' simple-integer
372VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
373 Range = Tok.getLocation();
374
375 if (!Tok.is(tok::numeric_constant)) {
376 Diag(Tok, diag::err_expected_version);
377 SkipUntil(tok::comma, tok::r_paren, true, true, true);
378 return VersionTuple();
379 }
380
381 // Parse the major (and possibly minor and subminor) versions, which
382 // are stored in the numeric constant. We utilize a quirk of the
383 // lexer, which is that it handles something like 1.2.3 as a single
384 // numeric constant, rather than two separate tokens.
385 llvm::SmallString<512> Buffer;
386 Buffer.resize(Tok.getLength()+1);
387 const char *ThisTokBegin = &Buffer[0];
388
389 // Get the spelling of the token, which eliminates trigraphs, etc.
390 bool Invalid = false;
391 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
392 if (Invalid)
393 return VersionTuple();
394
395 // Parse the major version.
396 unsigned AfterMajor = 0;
397 unsigned Major = 0;
398 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
399 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
400 ++AfterMajor;
401 }
402
403 if (AfterMajor == 0) {
404 Diag(Tok, diag::err_expected_version);
405 SkipUntil(tok::comma, tok::r_paren, true, true, true);
406 return VersionTuple();
407 }
408
409 if (AfterMajor == ActualLength) {
410 ConsumeToken();
411
412 // We only had a single version component.
413 if (Major == 0) {
414 Diag(Tok, diag::err_zero_version);
415 return VersionTuple();
416 }
417
418 return VersionTuple(Major);
419 }
420
421 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
422 Diag(Tok, diag::err_expected_version);
423 SkipUntil(tok::comma, tok::r_paren, true, true, true);
424 return VersionTuple();
425 }
426
427 // Parse the minor version.
428 unsigned AfterMinor = AfterMajor + 1;
429 unsigned Minor = 0;
430 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
431 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
432 ++AfterMinor;
433 }
434
435 if (AfterMinor == ActualLength) {
436 ConsumeToken();
437
438 // We had major.minor.
439 if (Major == 0 && Minor == 0) {
440 Diag(Tok, diag::err_zero_version);
441 return VersionTuple();
442 }
443
444 return VersionTuple(Major, Minor);
445 }
446
447 // If what follows is not a '.', we have a problem.
448 if (ThisTokBegin[AfterMinor] != '.') {
449 Diag(Tok, diag::err_expected_version);
450 SkipUntil(tok::comma, tok::r_paren, true, true, true);
451 return VersionTuple();
452 }
453
454 // Parse the subminor version.
455 unsigned AfterSubminor = AfterMinor + 1;
456 unsigned Subminor = 0;
457 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
458 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
459 ++AfterSubminor;
460 }
461
462 if (AfterSubminor != ActualLength) {
463 Diag(Tok, diag::err_expected_version);
464 SkipUntil(tok::comma, tok::r_paren, true, true, true);
465 return VersionTuple();
466 }
467 ConsumeToken();
468 return VersionTuple(Major, Minor, Subminor);
469}
470
471/// \brief Parse the contents of the "availability" attribute.
472///
473/// availability-attribute:
474/// 'availability' '(' platform ',' version-arg-list ')'
475///
476/// platform:
477/// identifier
478///
479/// version-arg-list:
480/// version-arg
481/// version-arg ',' version-arg-list
482///
483/// version-arg:
484/// 'introduced' '=' version
485/// 'deprecated' '=' version
486/// 'removed' = version
487void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
488 SourceLocation AvailabilityLoc,
489 ParsedAttributes &attrs,
490 SourceLocation *endLoc) {
491 SourceLocation PlatformLoc;
492 IdentifierInfo *Platform = 0;
493
494 enum { Introduced, Deprecated, Obsoleted, Unknown };
495 AvailabilityChange Changes[Unknown];
496
497 // Opening '('.
498 SourceLocation LParenLoc;
499 if (!Tok.is(tok::l_paren)) {
500 Diag(Tok, diag::err_expected_lparen);
501 return;
502 }
503 LParenLoc = ConsumeParen();
504
505 // Parse the platform name,
506 if (Tok.isNot(tok::identifier)) {
507 Diag(Tok, diag::err_availability_expected_platform);
508 SkipUntil(tok::r_paren);
509 return;
510 }
511 Platform = Tok.getIdentifierInfo();
512 PlatformLoc = ConsumeToken();
513
514 // Parse the ',' following the platform name.
515 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
516 return;
517
518 // If we haven't grabbed the pointers for the identifiers
519 // "introduced", "deprecated", and "obsoleted", do so now.
520 if (!Ident_introduced) {
521 Ident_introduced = PP.getIdentifierInfo("introduced");
522 Ident_deprecated = PP.getIdentifierInfo("deprecated");
523 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
524 }
525
526 // Parse the set of introductions/deprecations/removals.
527 do {
528 if (Tok.isNot(tok::identifier)) {
529 Diag(Tok, diag::err_availability_expected_change);
530 SkipUntil(tok::r_paren);
531 return;
532 }
533 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
534 SourceLocation KeywordLoc = ConsumeToken();
535
536 if (Tok.isNot(tok::equal)) {
537 Diag(Tok, diag::err_expected_equal_after)
538 << Keyword;
539 SkipUntil(tok::r_paren);
540 return;
541 }
542 ConsumeToken();
543
544 SourceRange VersionRange;
545 VersionTuple Version = ParseVersionTuple(VersionRange);
546
547 if (Version.empty()) {
548 SkipUntil(tok::r_paren);
549 return;
550 }
551
552 unsigned Index;
553 if (Keyword == Ident_introduced)
554 Index = Introduced;
555 else if (Keyword == Ident_deprecated)
556 Index = Deprecated;
557 else if (Keyword == Ident_obsoleted)
558 Index = Obsoleted;
559 else
560 Index = Unknown;
561
562 if (Index < Unknown) {
563 if (!Changes[Index].KeywordLoc.isInvalid()) {
564 Diag(KeywordLoc, diag::err_availability_redundant)
565 << Keyword
566 << SourceRange(Changes[Index].KeywordLoc,
567 Changes[Index].VersionRange.getEnd());
568 }
569
570 Changes[Index].KeywordLoc = KeywordLoc;
571 Changes[Index].Version = Version;
572 Changes[Index].VersionRange = VersionRange;
573 } else {
574 Diag(KeywordLoc, diag::err_availability_unknown_change)
575 << Keyword << VersionRange;
576 }
577
578 if (Tok.isNot(tok::comma))
579 break;
580
581 ConsumeToken();
582 } while (true);
583
584 // Closing ')'.
585 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
586 if (RParenLoc.isInvalid())
587 return;
588
589 if (endLoc)
590 *endLoc = RParenLoc;
591
592 // Record this attribute
John McCall084e83d2011-03-24 11:26:52 +0000593 attrs.addNew(&Availability, AvailabilityLoc,
594 0, SourceLocation(),
595 Platform, PlatformLoc,
596 Changes[Introduced],
597 Changes[Deprecated],
598 Changes[Obsoleted], false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000599}
600
John McCall53fa7142010-12-24 02:08:15 +0000601void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
602 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
603 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000604}
605
Chris Lattner53361ac2006-08-10 05:19:57 +0000606/// ParseDeclaration - Parse a full 'declaration', which consists of
607/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000608/// 'Context' should be a Declarator::TheContext value. This returns the
609/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000610///
611/// declaration: [C99 6.7]
612/// block-declaration ->
613/// simple-declaration
614/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000615/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000616/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000617/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000618/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000619/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000620/// others... [FIXME]
621///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000622Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
623 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000624 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000625 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000626 ParenBraceBracketBalancer BalancerRAIIObj(*this);
627
John McCall48871652010-08-21 09:40:31 +0000628 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000629 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000630 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000631 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000632 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000633 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000634 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000635 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000636 // Could be the start of an inline namespace. Allowed as an ext in C++03.
637 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000638 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000639 SourceLocation InlineLoc = ConsumeToken();
640 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
641 break;
642 }
John McCall53fa7142010-12-24 02:08:15 +0000643 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000644 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000645 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000646 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000647 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000648 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000649 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000650 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000651 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000652 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000653 case tok::kw_static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000654 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000655 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000656 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000657 default:
John McCall53fa7142010-12-24 02:08:15 +0000658 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000659 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000660
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000661 // This routine returns a DeclGroup, if the thing we parsed only contains a
662 // single decl, convert it now.
663 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000664}
665
666/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
667/// declaration-specifiers init-declarator-list[opt] ';'
668///[C90/C++]init-declarator-list ';' [TODO]
669/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000670///
671/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000672/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000673Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
674 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000675 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000676 ParsedAttributes &attrs,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000677 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000678 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000679 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000680 DS.takeAttributesFrom(attrs);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000681 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000682 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000683 StmtResult R = Actions.ActOnVlaStmt(DS);
684 if (R.isUsable())
685 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000686
Chris Lattner0e894622006-08-13 19:58:17 +0000687 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
688 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000689 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000690 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000691 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000692 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000693 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000694 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000695 }
Mike Stump11289f42009-09-09 15:08:12 +0000696
Chris Lattner005fc1b2010-04-05 18:18:31 +0000697 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000698}
Mike Stump11289f42009-09-09 15:08:12 +0000699
John McCalld5a36322009-11-03 19:26:08 +0000700/// ParseDeclGroup - Having concluded that this is either a function
701/// definition or a group of object declarations, actually parse the
702/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000703Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
704 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000705 bool AllowFunctionDefinitions,
706 SourceLocation *DeclEnd) {
707 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000708 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000709 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000710
John McCalld5a36322009-11-03 19:26:08 +0000711 // Bail out if the first declarator didn't seem well-formed.
712 if (!D.hasName() && !D.mayOmitIdentifier()) {
713 // Skip until ; or }.
714 SkipUntil(tok::r_brace, true, true);
715 if (Tok.is(tok::semi))
716 ConsumeToken();
717 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000718 }
Mike Stump11289f42009-09-09 15:08:12 +0000719
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000720 // Check to see if we have a function *definition* which must have a body.
721 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
722 // Look at the next token to make sure that this isn't a function
723 // declaration. We have to check this because __attribute__ might be the
724 // start of a function definition in GCC-extended K&R C.
725 !isDeclarationAfterDeclarator()) {
726
Chris Lattner13901342010-07-11 22:42:07 +0000727 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000728 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
729 Diag(Tok, diag::err_function_declared_typedef);
730
731 // Recover by treating the 'typedef' as spurious.
732 DS.ClearStorageClassSpecs();
733 }
734
John McCall48871652010-08-21 09:40:31 +0000735 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000736 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000737 }
738
739 if (isDeclarationSpecifier()) {
740 // If there is an invalid declaration specifier right after the function
741 // prototype, then we must be in a missing semicolon case where this isn't
742 // actually a body. Just fall through into the code that handles it as a
743 // prototype, and let the top-level code handle the erroneous declspec
744 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000745 } else {
746 Diag(Tok, diag::err_expected_fn_body);
747 SkipUntil(tok::semi);
748 return DeclGroupPtrTy();
749 }
750 }
751
John McCall48871652010-08-21 09:40:31 +0000752 llvm::SmallVector<Decl *, 8> DeclsInGroup;
753 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000754 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000755 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000756 DeclsInGroup.push_back(FirstDecl);
757
758 // If we don't have a comma, it is either the end of the list (a ';') or an
759 // error, bail out.
760 while (Tok.is(tok::comma)) {
761 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000762 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000763
764 // Parse the next declarator.
765 D.clear();
766
767 // Accept attributes in an init-declarator. In the first declarator in a
768 // declaration, these would be part of the declspec. In subsequent
769 // declarators, they become part of the declarator itself, so that they
770 // don't apply to declarators after *this* one. Examples:
771 // short __attribute__((common)) var; -> declspec
772 // short var __attribute__((common)); -> declarator
773 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000774 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000775
776 ParseDeclarator(D);
777
John McCall48871652010-08-21 09:40:31 +0000778 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000779 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000780 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000781 DeclsInGroup.push_back(ThisDecl);
782 }
783
784 if (DeclEnd)
785 *DeclEnd = Tok.getLocation();
786
787 if (Context != Declarator::ForContext &&
788 ExpectAndConsume(tok::semi,
789 Context == Declarator::FileContext
790 ? diag::err_invalid_token_after_toplevel_declarator
791 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000792 // Okay, there was no semicolon and one was expected. If we see a
793 // declaration specifier, just assume it was missing and continue parsing.
794 // Otherwise things are very confused and we skip to recover.
795 if (!isDeclarationSpecifier()) {
796 SkipUntil(tok::r_brace, true, true);
797 if (Tok.is(tok::semi))
798 ConsumeToken();
799 }
John McCalld5a36322009-11-03 19:26:08 +0000800 }
801
Douglas Gregor0be31a22010-07-02 17:43:08 +0000802 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000803 DeclsInGroup.data(),
804 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000805}
806
Douglas Gregor23996282009-05-12 21:31:51 +0000807/// \brief Parse 'declaration' after parsing 'declaration-specifiers
808/// declarator'. This method parses the remainder of the declaration
809/// (including any attributes or initializer, among other things) and
810/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000811///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000812/// init-declarator: [C99 6.7]
813/// declarator
814/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000815/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
816/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000817/// [C++] declarator initializer[opt]
818///
819/// [C++] initializer:
820/// [C++] '=' initializer-clause
821/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000822/// [C++0x] '=' 'default' [TODO]
823/// [C++0x] '=' 'delete'
824///
825/// According to the standard grammar, =default and =delete are function
826/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000827///
John McCall48871652010-08-21 09:40:31 +0000828Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000829 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000830 // If a simple-asm-expr is present, parse it.
831 if (Tok.is(tok::kw_asm)) {
832 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000833 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000834 if (AsmLabel.isInvalid()) {
835 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000836 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000837 }
Mike Stump11289f42009-09-09 15:08:12 +0000838
Douglas Gregor23996282009-05-12 21:31:51 +0000839 D.setAsmLabel(AsmLabel.release());
840 D.SetRangeEnd(Loc);
841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
John McCall53fa7142010-12-24 02:08:15 +0000843 MaybeParseGNUAttributes(D);
Mike Stump11289f42009-09-09 15:08:12 +0000844
Douglas Gregor23996282009-05-12 21:31:51 +0000845 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000846 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000847 switch (TemplateInfo.Kind) {
848 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000849 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000850 break;
851
852 case ParsedTemplateInfo::Template:
853 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000854 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000855 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000856 TemplateInfo.TemplateParams->data(),
857 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000858 D);
859 break;
860
861 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000862 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000863 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000864 TemplateInfo.ExternLoc,
865 TemplateInfo.TemplateLoc,
866 D);
867 if (ThisRes.isInvalid()) {
868 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000869 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000870 }
871
872 ThisDecl = ThisRes.get();
873 break;
874 }
875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
Richard Smith30482bc2011-02-20 03:19:35 +0000877 bool TypeContainsAuto =
878 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
879
Douglas Gregor23996282009-05-12 21:31:51 +0000880 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000881 if (isTokenEqualOrMistypedEqualEqual(
882 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000883 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000884 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000885 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000886
887 if (!getLang().CPlusPlus0x)
888 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
889
Douglas Gregor23996282009-05-12 21:31:51 +0000890 Actions.SetDeclDeleted(ThisDecl, DelLoc);
891 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000892 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
893 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000894 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000895 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000896
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000897 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000898 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000899 ConsumeCodeCompletionToken();
900 SkipUntil(tok::comma, true, true);
901 return ThisDecl;
902 }
903
John McCalldadc5752010-08-24 06:29:42 +0000904 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000905
John McCall1f4ee7b2009-12-19 09:28:58 +0000906 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000907 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000908 ExitScope();
909 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000910
Douglas Gregor23996282009-05-12 21:31:51 +0000911 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000912 SkipUntil(tok::comma, true, true);
913 Actions.ActOnInitializerError(ThisDecl);
914 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000915 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
916 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000917 }
918 } else if (Tok.is(tok::l_paren)) {
919 // Parse C++ direct initializer: '(' expression-list ')'
920 SourceLocation LParenLoc = ConsumeParen();
921 ExprVector Exprs(Actions);
922 CommaLocsTy CommaLocs;
923
Douglas Gregor613bf102009-12-22 17:47:17 +0000924 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
925 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000926 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000927 }
928
Douglas Gregor23996282009-05-12 21:31:51 +0000929 if (ParseExpressionList(Exprs, CommaLocs)) {
930 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000931
932 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000933 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000934 ExitScope();
935 }
Douglas Gregor23996282009-05-12 21:31:51 +0000936 } else {
937 // Match the ')'.
938 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
939
940 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
941 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000942
943 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000944 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000945 ExitScope();
946 }
947
Douglas Gregor23996282009-05-12 21:31:51 +0000948 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
949 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +0000950 RParenLoc,
951 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000952 }
953 } else {
Richard Smith30482bc2011-02-20 03:19:35 +0000954 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000955 }
956
Richard Smithb2bc2e62011-02-21 20:05:19 +0000957 Actions.FinalizeDeclaration(ThisDecl);
958
Douglas Gregor23996282009-05-12 21:31:51 +0000959 return ThisDecl;
960}
961
Chris Lattner1890ac82006-08-13 01:16:23 +0000962/// ParseSpecifierQualifierList
963/// specifier-qualifier-list:
964/// type-specifier specifier-qualifier-list[opt]
965/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000966/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000967///
968void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
969 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
970 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000971 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000972
Chris Lattner1890ac82006-08-13 01:16:23 +0000973 // Validate declspec for type-name.
974 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000975 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +0000976 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000977 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000978
Chris Lattner1b22eed2006-11-28 05:12:07 +0000979 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000980 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000981 if (DS.getStorageClassSpecLoc().isValid())
982 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
983 else
984 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000985 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Chris Lattner1b22eed2006-11-28 05:12:07 +0000988 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000989 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000990 if (DS.isInlineSpecified())
991 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
992 if (DS.isVirtualSpecified())
993 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
994 if (DS.isExplicitSpecified())
995 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000996 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000997 }
998}
Chris Lattner53361ac2006-08-10 05:19:57 +0000999
Chris Lattner6cc055a2009-04-12 20:42:31 +00001000/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1001/// specified token is valid after the identifier in a declarator which
1002/// immediately follows the declspec. For example, these things are valid:
1003///
1004/// int x [ 4]; // direct-declarator
1005/// int x ( int y); // direct-declarator
1006/// int(int x ) // direct-declarator
1007/// int x ; // simple-declaration
1008/// int x = 17; // init-declarator-list
1009/// int x , y; // init-declarator-list
1010/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001011/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001012/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001013///
1014/// This is not, because 'x' does not immediately follow the declspec (though
1015/// ')' happens to be valid anyway).
1016/// int (x)
1017///
1018static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1019 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1020 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001021 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001022}
1023
Chris Lattner20a0c612009-04-14 21:34:55 +00001024
1025/// ParseImplicitInt - This method is called when we have an non-typename
1026/// identifier in a declspec (which normally terminates the decl spec) when
1027/// the declspec has no type specifier. In this case, the declspec is either
1028/// malformed or is "implicit int" (in K&R and C89).
1029///
1030/// This method handles diagnosing this prettily and returns false if the
1031/// declspec is done being processed. If it recovers and thinks there may be
1032/// other pieces of declspec after it, it returns true.
1033///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001034bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001035 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001036 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001037 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattner20a0c612009-04-14 21:34:55 +00001039 SourceLocation Loc = Tok.getLocation();
1040 // If we see an identifier that is not a type name, we normally would
1041 // parse it as the identifer being declared. However, when a typename
1042 // is typo'd or the definition is not included, this will incorrectly
1043 // parse the typename as the identifier name and fall over misparsing
1044 // later parts of the diagnostic.
1045 //
1046 // As such, we try to do some look-ahead in cases where this would
1047 // otherwise be an "implicit-int" case to see if this is invalid. For
1048 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1049 // an identifier with implicit int, we'd get a parse error because the
1050 // next token is obviously invalid for a type. Parse these as a case
1051 // with an invalid type specifier.
1052 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001053
Chris Lattner20a0c612009-04-14 21:34:55 +00001054 // Since we know that this either implicit int (which is rare) or an
1055 // error, we'd do lookahead to try to do better recovery.
1056 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1057 // If this token is valid for implicit int, e.g. "static x = 4", then
1058 // we just avoid eating the identifier, so it will be parsed as the
1059 // identifier in the declarator.
1060 return false;
1061 }
Mike Stump11289f42009-09-09 15:08:12 +00001062
Chris Lattner20a0c612009-04-14 21:34:55 +00001063 // Otherwise, if we don't consume this token, we are going to emit an
1064 // error anyway. Try to recover from various common problems. Check
1065 // to see if this was a reference to a tag name without a tag specified.
1066 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001067 //
1068 // C++ doesn't need this, and isTagName doesn't take SS.
1069 if (SS == 0) {
1070 const char *TagName = 0;
1071 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001072
Douglas Gregor0be31a22010-07-02 17:43:08 +00001073 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001074 default: break;
1075 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
1076 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
1077 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
1078 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
1079 }
Mike Stump11289f42009-09-09 15:08:12 +00001080
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001081 if (TagName) {
1082 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001083 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00001084 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +00001085
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001086 // Parse this as a tag as if the missing tag were present.
1087 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001088 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001089 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001090 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001091 return true;
1092 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
Douglas Gregor15e56022009-10-13 23:27:22 +00001095 // This is almost certainly an invalid type name. Let the action emit a
1096 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001097 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001098 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001099 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001100 // The action emitted a diagnostic, so we don't have to.
1101 if (T) {
1102 // The action has suggested that the type T could be used. Set that as
1103 // the type in the declaration specifiers, consume the would-be type
1104 // name token, and we're done.
1105 const char *PrevSpec;
1106 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001107 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001108 DS.SetRangeEnd(Tok.getLocation());
1109 ConsumeToken();
1110
1111 // There may be other declaration specifiers after this.
1112 return true;
1113 }
1114
1115 // Fall through; the action had no suggestion for us.
1116 } else {
1117 // The action did not emit a diagnostic, so emit one now.
1118 SourceRange R;
1119 if (SS) R = SS->getRange();
1120 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregor15e56022009-10-13 23:27:22 +00001123 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001124 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001125 unsigned DiagID;
1126 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001127 DS.SetRangeEnd(Tok.getLocation());
1128 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattner20a0c612009-04-14 21:34:55 +00001130 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1131 // avoid rippling error messages on subsequent uses of the same type,
1132 // could be useful if #include was forgotten.
1133 return false;
1134}
1135
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001136/// \brief Determine the declaration specifier context from the declarator
1137/// context.
1138///
1139/// \param Context the declarator context, which is one of the
1140/// Declarator::TheContext enumerator values.
1141Parser::DeclSpecContext
1142Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1143 if (Context == Declarator::MemberContext)
1144 return DSC_class;
1145 if (Context == Declarator::FileContext)
1146 return DSC_top_level;
1147 return DSC_normal;
1148}
1149
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001150/// ParseDeclarationSpecifiers
1151/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001152/// storage-class-specifier declaration-specifiers[opt]
1153/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001154/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001155/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001156///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001157/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001158/// 'typedef'
1159/// 'extern'
1160/// 'static'
1161/// 'auto'
1162/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001163/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001164/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001165/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001166/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001167/// [C++] 'virtual'
1168/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001169/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001170/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001171/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001172
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001173///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001174void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001175 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001176 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001177 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +00001178 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +00001179 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001180 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001181 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001182 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001183 unsigned DiagID = 0;
1184
Chris Lattner4d8f8732006-11-28 05:05:08 +00001185 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001186
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001187 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001188 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001189 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001190 // If this is not a declaration specifier token, we're done reading decl
1191 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001192 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001193 return;
Mike Stump11289f42009-09-09 15:08:12 +00001194
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001195 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001196 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001197 if (DS.hasTypeSpecifier()) {
1198 bool AllowNonIdentifiers
1199 = (getCurScope()->getFlags() & (Scope::ControlScope |
1200 Scope::BlockScope |
1201 Scope::TemplateParamScope |
1202 Scope::FunctionPrototypeScope |
1203 Scope::AtCatchScope)) == 0;
1204 bool AllowNestedNameSpecifiers
1205 = DSContext == DSC_top_level ||
1206 (DSContext == DSC_class && DS.isFriendSpecified());
1207
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001208 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1209 AllowNonIdentifiers,
1210 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001211 ConsumeCodeCompletionToken();
1212 return;
1213 }
1214
Douglas Gregor80039242011-02-15 20:33:25 +00001215 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1216 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1217 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001218 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1219 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001220 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001221 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001222 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001223 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001224
1225 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1226 ConsumeCodeCompletionToken();
1227 return;
1228 }
1229
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001230 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001231 // C++ scope specifier. Annotate and loop, or bail out on error.
1232 if (TryAnnotateCXXScopeToken(true)) {
1233 if (!DS.hasTypeSpecifier())
1234 DS.SetTypeSpecError();
1235 goto DoneWithDeclSpec;
1236 }
John McCall8bc2a702010-03-01 18:20:46 +00001237 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1238 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001239 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001240
1241 case tok::annot_cxxscope: {
1242 if (DS.hasTypeSpecifier())
1243 goto DoneWithDeclSpec;
1244
John McCall9dab4e62009-12-12 11:40:51 +00001245 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001246 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1247 Tok.getAnnotationRange(),
1248 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001249
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001250 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001251 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001252 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001253 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001254 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001255 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001256
1257 // C++ [class.qual]p2:
1258 // In a lookup in which the constructor is an acceptable lookup
1259 // result and the nested-name-specifier nominates a class C:
1260 //
1261 // - if the name specified after the
1262 // nested-name-specifier, when looked up in C, is the
1263 // injected-class-name of C (Clause 9), or
1264 //
1265 // - if the name specified after the nested-name-specifier
1266 // is the same as the identifier or the
1267 // simple-template-id's template-name in the last
1268 // component of the nested-name-specifier,
1269 //
1270 // the name is instead considered to name the constructor of
1271 // class C.
1272 //
1273 // Thus, if the template-name is actually the constructor
1274 // name, then the code is ill-formed; this interpretation is
1275 // reinforced by the NAD status of core issue 635.
1276 TemplateIdAnnotation *TemplateId
1277 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001278 if ((DSContext == DSC_top_level ||
1279 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1280 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001281 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001282 if (isConstructorDeclarator()) {
1283 // The user meant this to be an out-of-line constructor
1284 // definition, but template arguments are not allowed
1285 // there. Just allow this as a constructor; we'll
1286 // complain about it later.
1287 goto DoneWithDeclSpec;
1288 }
1289
1290 // The user meant this to name a type, but it actually names
1291 // a constructor with some extraneous template
1292 // arguments. Complain, then parse it as a type as the user
1293 // intended.
1294 Diag(TemplateId->TemplateNameLoc,
1295 diag::err_out_of_line_template_id_names_constructor)
1296 << TemplateId->Name;
1297 }
1298
John McCall9dab4e62009-12-12 11:40:51 +00001299 DS.getTypeSpecScope() = SS;
1300 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001301 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001302 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001303 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001304 continue;
1305 }
1306
Douglas Gregorc5790df2009-09-28 07:26:33 +00001307 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001308 DS.getTypeSpecScope() = SS;
1309 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001310 if (Tok.getAnnotationValue()) {
1311 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001312 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1313 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001314 PrevSpec, DiagID, T);
1315 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001316 else
1317 DS.SetTypeSpecError();
1318 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1319 ConsumeToken(); // The typename
1320 }
1321
Douglas Gregor167fa622009-03-25 15:40:00 +00001322 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001323 goto DoneWithDeclSpec;
1324
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001325 // If we're in a context where the identifier could be a class name,
1326 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001327 if ((DSContext == DSC_top_level ||
1328 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001329 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001330 &SS)) {
1331 if (isConstructorDeclarator())
1332 goto DoneWithDeclSpec;
1333
1334 // As noted in C++ [class.qual]p2 (cited above), when the name
1335 // of the class is qualified in a context where it could name
1336 // a constructor, its a constructor name. However, we've
1337 // looked at the declarator, and the user probably meant this
1338 // to be a type. Complain that it isn't supposed to be treated
1339 // as a type, then proceed to parse it as a type.
1340 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1341 << Next.getIdentifierInfo();
1342 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001343
John McCallba7bf592010-08-24 05:47:05 +00001344 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1345 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001346 getCurScope(), &SS,
1347 false, false, ParsedType(),
1348 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001349
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001350 // If the referenced identifier is not a type, then this declspec is
1351 // erroneous: We already checked about that it has no type specifier, and
1352 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001353 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001354 if (TypeRep == 0) {
1355 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001356 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001357 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001358 }
Mike Stump11289f42009-09-09 15:08:12 +00001359
John McCall9dab4e62009-12-12 11:40:51 +00001360 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001361 ConsumeToken(); // The C++ scope.
1362
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001364 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001365 if (isInvalid)
1366 break;
Mike Stump11289f42009-09-09 15:08:12 +00001367
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001368 DS.SetRangeEnd(Tok.getLocation());
1369 ConsumeToken(); // The typename.
1370
1371 continue;
1372 }
Mike Stump11289f42009-09-09 15:08:12 +00001373
Chris Lattnere387d9e2009-01-21 19:48:37 +00001374 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001375 if (Tok.getAnnotationValue()) {
1376 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001377 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001378 DiagID, T);
1379 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001380 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001381
1382 if (isInvalid)
1383 break;
1384
Chris Lattnere387d9e2009-01-21 19:48:37 +00001385 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1386 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001387
Chris Lattnere387d9e2009-01-21 19:48:37 +00001388 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1389 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001390 // Objective-C interface.
1391 if (Tok.is(tok::less) && getLang().ObjC1)
1392 ParseObjCProtocolQualifiers(DS);
1393
Chris Lattnere387d9e2009-01-21 19:48:37 +00001394 continue;
1395 }
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattner16fac4f2008-07-26 01:18:38 +00001397 // typedef-name
1398 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001399 // In C++, check to see if this is a scope specifier like foo::bar::, if
1400 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001401 if (getLang().CPlusPlus) {
1402 if (TryAnnotateCXXScopeToken(true)) {
1403 if (!DS.hasTypeSpecifier())
1404 DS.SetTypeSpecError();
1405 goto DoneWithDeclSpec;
1406 }
1407 if (!Tok.is(tok::identifier))
1408 continue;
1409 }
Mike Stump11289f42009-09-09 15:08:12 +00001410
Chris Lattner16fac4f2008-07-26 01:18:38 +00001411 // This identifier can only be a typedef name if we haven't already seen
1412 // a type-specifier. Without this check we misparse:
1413 // typedef int X; struct Y { short X; }; as 'short int'.
1414 if (DS.hasTypeSpecifier())
1415 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001416
John Thompson22334602010-02-05 00:12:22 +00001417 // Check for need to substitute AltiVec keyword tokens.
1418 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1419 break;
1420
Chris Lattner16fac4f2008-07-26 01:18:38 +00001421 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001422 ParsedType TypeRep =
1423 Actions.getTypeName(*Tok.getIdentifierInfo(),
1424 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001425
Chris Lattner6cc055a2009-04-12 20:42:31 +00001426 // If this is not a typedef name, don't parse it as part of the declspec,
1427 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001428 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001429 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001430 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001431 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001432
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001433 // If we're in a context where the identifier could be a class name,
1434 // check whether this is a constructor declaration.
1435 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001436 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001437 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001438 goto DoneWithDeclSpec;
1439
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001440 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001441 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001442 if (isInvalid)
1443 break;
Mike Stump11289f42009-09-09 15:08:12 +00001444
Chris Lattner16fac4f2008-07-26 01:18:38 +00001445 DS.SetRangeEnd(Tok.getLocation());
1446 ConsumeToken(); // The identifier
1447
1448 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1449 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001450 // Objective-C interface.
1451 if (Tok.is(tok::less) && getLang().ObjC1)
1452 ParseObjCProtocolQualifiers(DS);
1453
Steve Naroffcd5e7822008-09-22 10:28:57 +00001454 // Need to support trailing type qualifiers (e.g. "id<p> const").
1455 // If a type specifier follows, it will be diagnosed elsewhere.
1456 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001457 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001458
1459 // type-name
1460 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001461 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001462 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001463 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001464 // This template-id does not refer to a type name, so we're
1465 // done with the type-specifiers.
1466 goto DoneWithDeclSpec;
1467 }
1468
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001469 // If we're in a context where the template-id could be a
1470 // constructor name or specialization, check whether this is a
1471 // constructor declaration.
1472 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001473 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001474 isConstructorDeclarator())
1475 goto DoneWithDeclSpec;
1476
Douglas Gregor7f741122009-02-25 19:37:18 +00001477 // Turn the template-id annotation token into a type annotation
1478 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001479 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001480 continue;
1481 }
1482
Chris Lattnere37e2332006-08-15 04:50:22 +00001483 // GNU attributes support.
1484 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001485 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001486 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001487
1488 // Microsoft declspec support.
1489 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001490 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001491 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001492
Steve Naroff44ac7772008-12-25 14:16:32 +00001493 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001494 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001495 // FIXME: Add handling here!
1496 break;
1497
1498 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001499 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001500 case tok::kw___cdecl:
1501 case tok::kw___stdcall:
1502 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001503 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001504 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001505 continue;
1506
Dawn Perchik335e16b2010-09-03 01:29:35 +00001507 // Borland single token adornments.
1508 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001509 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001510 continue;
1511
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001512 // OpenCL single token adornments.
1513 case tok::kw___kernel:
1514 ParseOpenCLAttributes(DS.getAttributes());
1515 continue;
1516
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001517 // storage-class-specifier
1518 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001519 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001520 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001521 break;
1522 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001523 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001524 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001525 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001526 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001527 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001528 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001529 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001530 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001531 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001532 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001533 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001534 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001535 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001536 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001537 break;
1538 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001539 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001540 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1541 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1542 DiagID, getLang());
1543 if (!isInvalid)
1544 Diag(Tok, diag::auto_storage_class)
1545 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1546 }
1547 else
1548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1549 DiagID);
1550 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001551 else
John McCall49bfce42009-08-03 20:12:06 +00001552 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001553 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001554 break;
1555 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001556 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001557 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001558 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001559 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001560 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001561 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001562 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001563 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001564 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001565 break;
Mike Stump11289f42009-09-09 15:08:12 +00001566
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001567 // function-specifier
1568 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001569 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001570 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001571 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001572 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001573 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001574 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001575 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001576 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001577
Anders Carlssoncd8db412009-05-06 04:46:28 +00001578 // friend
1579 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001580 if (DSContext == DSC_class)
1581 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1582 else {
1583 PrevSpec = ""; // not actually used by the diagnostic
1584 DiagID = diag::err_friend_invalid_in_context;
1585 isInvalid = true;
1586 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001587 break;
Mike Stump11289f42009-09-09 15:08:12 +00001588
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001589 // constexpr
1590 case tok::kw_constexpr:
1591 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1592 break;
1593
Chris Lattnere387d9e2009-01-21 19:48:37 +00001594 // type-specifier
1595 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1597 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001598 break;
1599 case tok::kw_long:
1600 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1602 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001603 else
John McCall49bfce42009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1605 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001606 break;
1607 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1609 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001610 break;
1611 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001612 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1613 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001614 break;
1615 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001616 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1617 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001618 break;
1619 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001620 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1621 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001622 break;
1623 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1625 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001626 break;
1627 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1629 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001630 break;
1631 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001632 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1633 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001634 break;
1635 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1637 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001638 break;
1639 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001640 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1641 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001642 break;
1643 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001644 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1645 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001646 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001647 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001648 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1649 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001650 break;
1651 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001652 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1653 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001654 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001655 case tok::kw_bool:
1656 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001657 if (Tok.is(tok::kw_bool) &&
1658 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1659 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1660 PrevSpec = ""; // Not used by the diagnostic.
1661 DiagID = diag::err_bool_redeclaration;
1662 isInvalid = true;
1663 } else {
1664 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1665 DiagID);
1666 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001667 break;
1668 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1670 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001671 break;
1672 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1674 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001675 break;
1676 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001677 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1678 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001679 break;
John Thompson22334602010-02-05 00:12:22 +00001680 case tok::kw___vector:
1681 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1682 break;
1683 case tok::kw___pixel:
1684 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1685 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001686
1687 // class-specifier:
1688 case tok::kw_class:
1689 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001690 case tok::kw_union: {
1691 tok::TokenKind Kind = Tok.getKind();
1692 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001693 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001694 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001695 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001696
1697 // enum-specifier:
1698 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001699 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001700 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001701 continue;
1702
1703 // cv-qualifier:
1704 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001705 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1706 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001707 break;
1708 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001709 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1710 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001711 break;
1712 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001713 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1714 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001715 break;
1716
Douglas Gregor333489b2009-03-27 23:10:48 +00001717 // C++ typename-specifier:
1718 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001719 if (TryAnnotateTypeOrScopeToken()) {
1720 DS.SetTypeSpecError();
1721 goto DoneWithDeclSpec;
1722 }
1723 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001724 continue;
1725 break;
1726
Chris Lattnere387d9e2009-01-21 19:48:37 +00001727 // GNU typeof support.
1728 case tok::kw_typeof:
1729 ParseTypeofSpecifier(DS);
1730 continue;
1731
Anders Carlsson74948d02009-06-24 17:47:40 +00001732 case tok::kw_decltype:
1733 ParseDecltypeSpecifier(DS);
1734 continue;
1735
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001736 // OpenCL qualifiers:
1737 case tok::kw_private:
1738 if (!getLang().OpenCL)
1739 goto DoneWithDeclSpec;
1740 case tok::kw___private:
1741 case tok::kw___global:
1742 case tok::kw___local:
1743 case tok::kw___constant:
1744 case tok::kw___read_only:
1745 case tok::kw___write_only:
1746 case tok::kw___read_write:
1747 ParseOpenCLQualifiers(DS);
1748 break;
1749
Steve Naroffcfdf6162008-06-05 00:02:44 +00001750 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001751 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001752 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1753 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001754 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001755 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001756
Douglas Gregor3a001f42010-11-19 17:10:50 +00001757 if (!ParseObjCProtocolQualifiers(DS))
1758 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1759 << FixItHint::CreateInsertion(Loc, "id")
1760 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001761
1762 // Need to support trailing type qualifiers (e.g. "id<p> const").
1763 // If a type specifier follows, it will be diagnosed elsewhere.
1764 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001765 }
John McCall49bfce42009-08-03 20:12:06 +00001766 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001767 if (isInvalid) {
1768 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001769 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001770
1771 if (DiagID == diag::ext_duplicate_declspec)
1772 Diag(Tok, DiagID)
1773 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1774 else
1775 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001776 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001777
Chris Lattner2e232092008-03-13 06:29:04 +00001778 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001779 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001780 }
1781}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001782
Chris Lattnera448d752009-01-06 06:59:53 +00001783/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001784/// primarily follow the C++ grammar with additions for C99 and GNU,
1785/// which together subsume the C grammar. Note that the C++
1786/// type-specifier also includes the C type-qualifier (for const,
1787/// volatile, and C99 restrict). Returns true if a type-specifier was
1788/// found (and parsed), false otherwise.
1789///
1790/// type-specifier: [C++ 7.1.5]
1791/// simple-type-specifier
1792/// class-specifier
1793/// enum-specifier
1794/// elaborated-type-specifier [TODO]
1795/// cv-qualifier
1796///
1797/// cv-qualifier: [C++ 7.1.5.1]
1798/// 'const'
1799/// 'volatile'
1800/// [C99] 'restrict'
1801///
1802/// simple-type-specifier: [ C++ 7.1.5.2]
1803/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1804/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1805/// 'char'
1806/// 'wchar_t'
1807/// 'bool'
1808/// 'short'
1809/// 'int'
1810/// 'long'
1811/// 'signed'
1812/// 'unsigned'
1813/// 'float'
1814/// 'double'
1815/// 'void'
1816/// [C99] '_Bool'
1817/// [C99] '_Complex'
1818/// [C99] '_Imaginary' // Removed in TC2?
1819/// [GNU] '_Decimal32'
1820/// [GNU] '_Decimal64'
1821/// [GNU] '_Decimal128'
1822/// [GNU] typeof-specifier
1823/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1824/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001825/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001826/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001827bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001828 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001829 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001830 const ParsedTemplateInfo &TemplateInfo,
1831 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001832 SourceLocation Loc = Tok.getLocation();
1833
1834 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001835 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001836 // If we already have a type specifier, this identifier is not a type.
1837 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1838 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1839 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1840 return false;
John Thompson22334602010-02-05 00:12:22 +00001841 // Check for need to substitute AltiVec keyword tokens.
1842 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1843 break;
1844 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001845 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001846 // Annotate typenames and C++ scope specifiers. If we get one, just
1847 // recurse to handle whatever we get.
1848 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001849 return true;
1850 if (Tok.is(tok::identifier))
1851 return false;
1852 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1853 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001854 case tok::coloncolon: // ::foo::bar
1855 if (NextToken().is(tok::kw_new) || // ::new
1856 NextToken().is(tok::kw_delete)) // ::delete
1857 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001858
Chris Lattner020bab92009-01-04 23:41:41 +00001859 // Annotate typenames and C++ scope specifiers. If we get one, just
1860 // recurse to handle whatever we get.
1861 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001862 return true;
1863 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1864 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001865
Douglas Gregor450c75a2008-11-07 15:42:26 +00001866 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001867 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001868 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001869 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1870 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001871 DiagID, T);
1872 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001873 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001874 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1875 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001876
Douglas Gregor450c75a2008-11-07 15:42:26 +00001877 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1878 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1879 // Objective-C interface. If we don't have Objective-C or a '<', this is
1880 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001881 if (Tok.is(tok::less) && getLang().ObjC1)
1882 ParseObjCProtocolQualifiers(DS);
1883
Douglas Gregor450c75a2008-11-07 15:42:26 +00001884 return true;
1885 }
1886
1887 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001888 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001889 break;
1890 case tok::kw_long:
1891 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001892 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1893 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001894 else
John McCall49bfce42009-08-03 20:12:06 +00001895 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1896 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001897 break;
1898 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001899 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001900 break;
1901 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001902 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1903 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001904 break;
1905 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001906 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1907 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001908 break;
1909 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001910 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1911 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001912 break;
1913 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001914 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001915 break;
1916 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001917 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001918 break;
1919 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001921 break;
1922 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001923 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001924 break;
1925 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001926 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001927 break;
1928 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001929 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001930 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001931 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001933 break;
1934 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001935 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001936 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001937 case tok::kw_bool:
1938 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001939 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001940 break;
1941 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1943 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001944 break;
1945 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1947 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001948 break;
1949 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1951 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001952 break;
John Thompson22334602010-02-05 00:12:22 +00001953 case tok::kw___vector:
1954 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1955 break;
1956 case tok::kw___pixel:
1957 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1958 break;
1959
Douglas Gregor450c75a2008-11-07 15:42:26 +00001960 // class-specifier:
1961 case tok::kw_class:
1962 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001963 case tok::kw_union: {
1964 tok::TokenKind Kind = Tok.getKind();
1965 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001966 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1967 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001968 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001969 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001970
1971 // enum-specifier:
1972 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001973 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001974 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001975 return true;
1976
1977 // cv-qualifier:
1978 case tok::kw_const:
1979 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001980 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001981 break;
1982 case tok::kw_volatile:
1983 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001984 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001985 break;
1986 case tok::kw_restrict:
1987 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001988 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001989 break;
1990
1991 // GNU typeof support.
1992 case tok::kw_typeof:
1993 ParseTypeofSpecifier(DS);
1994 return true;
1995
Anders Carlsson74948d02009-06-24 17:47:40 +00001996 // C++0x decltype support.
1997 case tok::kw_decltype:
1998 ParseDecltypeSpecifier(DS);
1999 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002000
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002001 // OpenCL qualifiers:
2002 case tok::kw_private:
2003 if (!getLang().OpenCL)
2004 return false;
2005 case tok::kw___private:
2006 case tok::kw___global:
2007 case tok::kw___local:
2008 case tok::kw___constant:
2009 case tok::kw___read_only:
2010 case tok::kw___write_only:
2011 case tok::kw___read_write:
2012 ParseOpenCLQualifiers(DS);
2013 break;
2014
Anders Carlssonbae27372009-06-26 23:44:14 +00002015 // C++0x auto support.
2016 case tok::kw_auto:
2017 if (!getLang().CPlusPlus0x)
2018 return false;
2019
John McCall49bfce42009-08-03 20:12:06 +00002020 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002021 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002022
Eli Friedman53339e02009-06-08 23:27:34 +00002023 case tok::kw___ptr64:
2024 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002025 case tok::kw___cdecl:
2026 case tok::kw___stdcall:
2027 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002028 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002029 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002030 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002031
Dawn Perchik335e16b2010-09-03 01:29:35 +00002032 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002033 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002034 return true;
2035
Douglas Gregor450c75a2008-11-07 15:42:26 +00002036 default:
2037 // Not a type-specifier; do nothing.
2038 return false;
2039 }
2040
2041 // If the specifier combination wasn't legal, issue a diagnostic.
2042 if (isInvalid) {
2043 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002044 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002045 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002046 }
2047 DS.SetRangeEnd(Tok.getLocation());
2048 ConsumeToken(); // whatever we parsed above.
2049 return true;
2050}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002051
Chris Lattner70ae4912007-10-29 04:42:53 +00002052/// ParseStructDeclaration - Parse a struct declaration without the terminating
2053/// semicolon.
2054///
Chris Lattner90a26b02007-01-23 04:38:16 +00002055/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002056/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002057/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002058/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002059/// struct-declarator-list:
2060/// struct-declarator
2061/// struct-declarator-list ',' struct-declarator
2062/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2063/// struct-declarator:
2064/// declarator
2065/// [GNU] declarator attributes[opt]
2066/// declarator[opt] ':' constant-expression
2067/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2068///
Chris Lattnera12405b2008-04-10 06:46:29 +00002069void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002070ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002071 if (Tok.is(tok::kw___extension__)) {
2072 // __extension__ silences extension warnings in the subexpression.
2073 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002074 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002075 return ParseStructDeclaration(DS, Fields);
2076 }
Mike Stump11289f42009-09-09 15:08:12 +00002077
Steve Naroff97170802007-08-20 22:28:22 +00002078 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002079 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002080
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002081 // If there are no declarators, this is a free-standing declaration
2082 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002083 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002084 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002085 return;
2086 }
2087
2088 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002089 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002090 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002091 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002092 FieldDeclarator DeclaratorInfo(DS);
2093
2094 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002095 if (!FirstDeclarator)
2096 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002097
Steve Naroff97170802007-08-20 22:28:22 +00002098 /// struct-declarator: declarator
2099 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002100 if (Tok.isNot(tok::colon)) {
2101 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2102 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002103 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002104 }
Mike Stump11289f42009-09-09 15:08:12 +00002105
Chris Lattner76c72282007-10-09 17:33:22 +00002106 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002107 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002108 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002109 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002110 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002111 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002112 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002113 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002114
Steve Naroff97170802007-08-20 22:28:22 +00002115 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002116 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002117
John McCallcfefb6d2009-11-03 02:38:08 +00002118 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002119 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002120 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002121
Steve Naroff97170802007-08-20 22:28:22 +00002122 // If we don't have a comma, it is either the end of the list (a ';')
2123 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002124 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002125 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002126
Steve Naroff97170802007-08-20 22:28:22 +00002127 // Consume the comma.
2128 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002129
John McCallcfefb6d2009-11-03 02:38:08 +00002130 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002131 }
Steve Naroff97170802007-08-20 22:28:22 +00002132}
2133
2134/// ParseStructUnionBody
2135/// struct-contents:
2136/// struct-declaration-list
2137/// [EXT] empty
2138/// [GNU] "struct-declaration-list" without terminatoring ';'
2139/// struct-declaration-list:
2140/// struct-declaration
2141/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002142/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002143///
Chris Lattner1300fb92007-01-23 23:42:53 +00002144void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002145 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002146 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2147 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002148
Chris Lattner90a26b02007-01-23 04:38:16 +00002149 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002150
Douglas Gregor658b9552009-01-09 22:42:13 +00002151 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002152 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002153
Chris Lattner7b9ace62007-01-23 20:11:08 +00002154 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2155 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002156 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002157 Diag(Tok, diag::ext_empty_struct_union)
2158 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002159
John McCall48871652010-08-21 09:40:31 +00002160 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002161
Chris Lattner7b9ace62007-01-23 20:11:08 +00002162 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002163 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002164 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002165
Chris Lattner736ed5d2007-06-09 05:59:07 +00002166 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002167 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002168 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002169 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002170 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002171 ConsumeToken();
2172 continue;
2173 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002174
2175 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002176 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002177
John McCallcfefb6d2009-11-03 02:38:08 +00002178 if (!Tok.is(tok::at)) {
2179 struct CFieldCallback : FieldCallback {
2180 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002181 Decl *TagDecl;
2182 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002183
John McCall48871652010-08-21 09:40:31 +00002184 CFieldCallback(Parser &P, Decl *TagDecl,
2185 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002186 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2187
John McCall48871652010-08-21 09:40:31 +00002188 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002189 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002190 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002191 FD.D.getDeclSpec().getSourceRange().getBegin(),
2192 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002193 FieldDecls.push_back(Field);
2194 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002195 }
John McCallcfefb6d2009-11-03 02:38:08 +00002196 } Callback(*this, TagDecl, FieldDecls);
2197
2198 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002199 } else { // Handle @defs
2200 ConsumeToken();
2201 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2202 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002203 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002204 continue;
2205 }
2206 ConsumeToken();
2207 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2208 if (!Tok.is(tok::identifier)) {
2209 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002210 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002211 continue;
2212 }
John McCall48871652010-08-21 09:40:31 +00002213 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002214 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002215 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002216 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2217 ConsumeToken();
2218 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002219 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002220
Chris Lattner76c72282007-10-09 17:33:22 +00002221 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002222 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002223 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002224 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002225 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002226 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002227 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2228 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002229 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002230 // If we stopped at a ';', eat it.
2231 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002232 }
2233 }
Mike Stump11289f42009-09-09 15:08:12 +00002234
Steve Naroff33a1e802007-10-29 21:38:07 +00002235 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002236
John McCall084e83d2011-03-24 11:26:52 +00002237 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002238 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002239 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002240
Douglas Gregor0be31a22010-07-02 17:43:08 +00002241 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002242 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002243 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002244 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002245 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002246 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002247}
2248
Chris Lattner3b561a32006-08-13 00:12:11 +00002249/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002250/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002251/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002252///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002253/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2254/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002255/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002256/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002257///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002258/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2259/// [C++0x] enum-head '{' enumerator-list ',' '}'
2260///
2261/// enum-head: [C++0x]
2262/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2263/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2264///
2265/// enum-key: [C++0x]
2266/// 'enum'
2267/// 'enum' 'class'
2268/// 'enum' 'struct'
2269///
2270/// enum-base: [C++0x]
2271/// ':' type-specifier-seq
2272///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002273/// [C++] elaborated-type-specifier:
2274/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2275///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002276void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002277 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002278 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002279 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002280 if (Tok.is(tok::code_completion)) {
2281 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002282 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002283 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002284 }
2285
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002286 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002287 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002288 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002289
Abramo Bagnarad7548482010-05-19 21:37:53 +00002290 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002291 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002292 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002293 return;
2294
2295 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002296 Diag(Tok, diag::err_expected_ident);
2297 if (Tok.isNot(tok::l_brace)) {
2298 // Has no name and is not a definition.
2299 // Skip the rest of this declarator, up until the comma or semicolon.
2300 SkipUntil(tok::comma, true);
2301 return;
2302 }
2303 }
2304 }
Mike Stump11289f42009-09-09 15:08:12 +00002305
Douglas Gregora1aec292011-02-22 20:32:04 +00002306 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002307 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002308 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002309
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002310 if (getLang().CPlusPlus0x &&
2311 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002312 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002313 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2314 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002315 }
2316
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002317 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002318 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2319 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002320 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002321
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002322 // Skip the rest of this declarator, up until the comma or semicolon.
2323 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002324 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002325 }
Mike Stump11289f42009-09-09 15:08:12 +00002326
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002327 // If an identifier is present, consume and remember it.
2328 IdentifierInfo *Name = 0;
2329 SourceLocation NameLoc;
2330 if (Tok.is(tok::identifier)) {
2331 Name = Tok.getIdentifierInfo();
2332 NameLoc = ConsumeToken();
2333 }
Mike Stump11289f42009-09-09 15:08:12 +00002334
Douglas Gregor0bf31402010-10-08 23:50:27 +00002335 if (!Name && IsScopedEnum) {
2336 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2337 // declaration of a scoped enumeration.
2338 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2339 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002340 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002341 }
2342
2343 TypeResult BaseType;
2344
Douglas Gregord1f69f62010-12-01 17:42:47 +00002345 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002346 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002347 bool PossibleBitfield = false;
2348 if (getCurScope()->getFlags() & Scope::ClassScope) {
2349 // If we're in class scope, this can either be an enum declaration with
2350 // an underlying type, or a declaration of a bitfield member. We try to
2351 // use a simple disambiguation scheme first to catch the common cases
2352 // (integer literal, sizeof); if it's still ambiguous, we then consider
2353 // anything that's a simple-type-specifier followed by '(' as an
2354 // expression. This suffices because function types are not valid
2355 // underlying types anyway.
2356 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2357 // If the next token starts an expression, we know we're parsing a
2358 // bit-field. This is the common case.
2359 if (TPR == TPResult::True())
2360 PossibleBitfield = true;
2361 // If the next token starts a type-specifier-seq, it may be either a
2362 // a fixed underlying type or the start of a function-style cast in C++;
2363 // lookahead one more token to see if it's obvious that we have a
2364 // fixed underlying type.
2365 else if (TPR == TPResult::False() &&
2366 GetLookAheadToken(2).getKind() == tok::semi) {
2367 // Consume the ':'.
2368 ConsumeToken();
2369 } else {
2370 // We have the start of a type-specifier-seq, so we have to perform
2371 // tentative parsing to determine whether we have an expression or a
2372 // type.
2373 TentativeParsingAction TPA(*this);
2374
2375 // Consume the ':'.
2376 ConsumeToken();
2377
Douglas Gregora1aec292011-02-22 20:32:04 +00002378 if ((getLang().CPlusPlus &&
2379 isCXXDeclarationSpecifier() != TPResult::True()) ||
2380 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002381 // We'll parse this as a bitfield later.
2382 PossibleBitfield = true;
2383 TPA.Revert();
2384 } else {
2385 // We have a type-specifier-seq.
2386 TPA.Commit();
2387 }
2388 }
2389 } else {
2390 // Consume the ':'.
2391 ConsumeToken();
2392 }
2393
2394 if (!PossibleBitfield) {
2395 SourceRange Range;
2396 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002397
2398 if (!getLang().CPlusPlus0x)
2399 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2400 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002401 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002402 }
2403
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002404 // There are three options here. If we have 'enum foo;', then this is a
2405 // forward declaration. If we have 'enum foo {...' then this is a
2406 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2407 //
2408 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2409 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2410 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2411 //
John McCallfaf5fb42010-08-26 23:41:50 +00002412 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002413 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002414 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002415 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002416 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002417 else
John McCallfaf5fb42010-08-26 23:41:50 +00002418 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002419
2420 // enums cannot be templates, although they can be referenced from a
2421 // template.
2422 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002423 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002424 Diag(Tok, diag::err_enum_template);
2425
2426 // Skip the rest of this declarator, up until the comma or semicolon.
2427 SkipUntil(tok::comma, true);
2428 return;
2429 }
2430
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002431 if (!Name && TUK != Sema::TUK_Definition) {
2432 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2433
2434 // Skip the rest of this declarator, up until the comma or semicolon.
2435 SkipUntil(tok::comma, true);
2436 return;
2437 }
2438
Douglas Gregord6ab8742009-05-28 23:31:59 +00002439 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002440 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002441 const char *PrevSpec = 0;
2442 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002443 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002444 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002445 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002446 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002447 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002448 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002449
Douglas Gregorba41d012010-04-24 16:38:41 +00002450 if (IsDependent) {
2451 // This enum has a dependent nested-name-specifier. Handle it as a
2452 // dependent tag.
2453 if (!Name) {
2454 DS.SetTypeSpecError();
2455 Diag(Tok, diag::err_expected_type_name_after_typename);
2456 return;
2457 }
2458
Douglas Gregor0be31a22010-07-02 17:43:08 +00002459 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002460 TUK, SS, Name, StartLoc,
2461 NameLoc);
2462 if (Type.isInvalid()) {
2463 DS.SetTypeSpecError();
2464 return;
2465 }
2466
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002467 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2468 NameLoc.isValid() ? NameLoc : StartLoc,
2469 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002470 Diag(StartLoc, DiagID) << PrevSpec;
2471
2472 return;
2473 }
Mike Stump11289f42009-09-09 15:08:12 +00002474
John McCall48871652010-08-21 09:40:31 +00002475 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002476 // The action failed to produce an enumeration tag. If this is a
2477 // definition, consume the entire definition.
2478 if (Tok.is(tok::l_brace)) {
2479 ConsumeBrace();
2480 SkipUntil(tok::r_brace);
2481 }
2482
2483 DS.SetTypeSpecError();
2484 return;
2485 }
2486
Chris Lattner76c72282007-10-09 17:33:22 +00002487 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002488 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002489
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002490 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2491 NameLoc.isValid() ? NameLoc : StartLoc,
2492 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002493 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002494}
2495
Chris Lattnerc1915e22007-01-25 07:29:02 +00002496/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2497/// enumerator-list:
2498/// enumerator
2499/// enumerator-list ',' enumerator
2500/// enumerator:
2501/// enumeration-constant
2502/// enumeration-constant '=' constant-expression
2503/// enumeration-constant:
2504/// identifier
2505///
John McCall48871652010-08-21 09:40:31 +00002506void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002507 // Enter the scope of the enum body and start the definition.
2508 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002509 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002510
Chris Lattnerc1915e22007-01-25 07:29:02 +00002511 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002512
Chris Lattner37256fb2007-08-27 17:24:30 +00002513 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002514 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002515 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002516
John McCall48871652010-08-21 09:40:31 +00002517 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002518
John McCall48871652010-08-21 09:40:31 +00002519 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002520
Chris Lattnerc1915e22007-01-25 07:29:02 +00002521 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002522 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002523 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2524 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002525
John McCall811a0f52010-10-22 23:36:17 +00002526 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002527 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002528 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002529
Chris Lattnerc1915e22007-01-25 07:29:02 +00002530 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002531 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002532 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002533 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002534 AssignedVal = ParseConstantExpression();
2535 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002536 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002537 }
Mike Stump11289f42009-09-09 15:08:12 +00002538
Chris Lattnerc1915e22007-01-25 07:29:02 +00002539 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002540 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2541 LastEnumConstDecl,
2542 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002543 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002544 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002545 EnumConstantDecls.push_back(EnumConstDecl);
2546 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002547
Douglas Gregorce66d022010-09-07 14:51:08 +00002548 if (Tok.is(tok::identifier)) {
2549 // We're missing a comma between enumerators.
2550 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2551 Diag(Loc, diag::err_enumerator_list_missing_comma)
2552 << FixItHint::CreateInsertion(Loc, ", ");
2553 continue;
2554 }
2555
Chris Lattner76c72282007-10-09 17:33:22 +00002556 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002557 break;
2558 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002559
2560 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002561 !(getLang().C99 || getLang().CPlusPlus0x))
2562 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2563 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002564 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002565 }
Mike Stump11289f42009-09-09 15:08:12 +00002566
Chris Lattnerc1915e22007-01-25 07:29:02 +00002567 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002568 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002569
Chris Lattnerc1915e22007-01-25 07:29:02 +00002570 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002571 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002572 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002573
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002574 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2575 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002576 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002577
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002578 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002579 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002580}
Chris Lattner3b561a32006-08-13 00:12:11 +00002581
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002582/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002583/// start of a type-qualifier-list.
2584bool Parser::isTypeQualifier() const {
2585 switch (Tok.getKind()) {
2586 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002587
2588 // type-qualifier only in OpenCL
2589 case tok::kw_private:
2590 return getLang().OpenCL;
2591
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002592 // type-qualifier
2593 case tok::kw_const:
2594 case tok::kw_volatile:
2595 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002596 case tok::kw___private:
2597 case tok::kw___local:
2598 case tok::kw___global:
2599 case tok::kw___constant:
2600 case tok::kw___read_only:
2601 case tok::kw___read_write:
2602 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002603 return true;
2604 }
2605}
2606
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002607/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2608/// is definitely a type-specifier. Return false if it isn't part of a type
2609/// specifier or if we're not sure.
2610bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2611 switch (Tok.getKind()) {
2612 default: return false;
2613 // type-specifiers
2614 case tok::kw_short:
2615 case tok::kw_long:
2616 case tok::kw_signed:
2617 case tok::kw_unsigned:
2618 case tok::kw__Complex:
2619 case tok::kw__Imaginary:
2620 case tok::kw_void:
2621 case tok::kw_char:
2622 case tok::kw_wchar_t:
2623 case tok::kw_char16_t:
2624 case tok::kw_char32_t:
2625 case tok::kw_int:
2626 case tok::kw_float:
2627 case tok::kw_double:
2628 case tok::kw_bool:
2629 case tok::kw__Bool:
2630 case tok::kw__Decimal32:
2631 case tok::kw__Decimal64:
2632 case tok::kw__Decimal128:
2633 case tok::kw___vector:
2634
2635 // struct-or-union-specifier (C99) or class-specifier (C++)
2636 case tok::kw_class:
2637 case tok::kw_struct:
2638 case tok::kw_union:
2639 // enum-specifier
2640 case tok::kw_enum:
2641
2642 // typedef-name
2643 case tok::annot_typename:
2644 return true;
2645 }
2646}
2647
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002648/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002649/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002650bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002651 switch (Tok.getKind()) {
2652 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002653
Chris Lattner020bab92009-01-04 23:41:41 +00002654 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002655 if (TryAltiVecVectorToken())
2656 return true;
2657 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002658 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002659 // Annotate typenames and C++ scope specifiers. If we get one, just
2660 // recurse to handle whatever we get.
2661 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002662 return true;
2663 if (Tok.is(tok::identifier))
2664 return false;
2665 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002666
Chris Lattner020bab92009-01-04 23:41:41 +00002667 case tok::coloncolon: // ::foo::bar
2668 if (NextToken().is(tok::kw_new) || // ::new
2669 NextToken().is(tok::kw_delete)) // ::delete
2670 return false;
2671
Chris Lattner020bab92009-01-04 23:41:41 +00002672 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002673 return true;
2674 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002675
Chris Lattnere37e2332006-08-15 04:50:22 +00002676 // GNU attributes support.
2677 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002678 // GNU typeof support.
2679 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002680
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002681 // type-specifiers
2682 case tok::kw_short:
2683 case tok::kw_long:
2684 case tok::kw_signed:
2685 case tok::kw_unsigned:
2686 case tok::kw__Complex:
2687 case tok::kw__Imaginary:
2688 case tok::kw_void:
2689 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002690 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002691 case tok::kw_char16_t:
2692 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002693 case tok::kw_int:
2694 case tok::kw_float:
2695 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002696 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002697 case tok::kw__Bool:
2698 case tok::kw__Decimal32:
2699 case tok::kw__Decimal64:
2700 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002701 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002702
Chris Lattner861a2262008-04-13 18:59:07 +00002703 // struct-or-union-specifier (C99) or class-specifier (C++)
2704 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002705 case tok::kw_struct:
2706 case tok::kw_union:
2707 // enum-specifier
2708 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002709
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002710 // type-qualifier
2711 case tok::kw_const:
2712 case tok::kw_volatile:
2713 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002714
2715 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002716 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002717 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002718
Chris Lattner409bf7d2008-10-20 00:25:30 +00002719 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2720 case tok::less:
2721 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002722
Steve Naroff44ac7772008-12-25 14:16:32 +00002723 case tok::kw___cdecl:
2724 case tok::kw___stdcall:
2725 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002726 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002727 case tok::kw___w64:
2728 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002729 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002730
2731 case tok::kw___private:
2732 case tok::kw___local:
2733 case tok::kw___global:
2734 case tok::kw___constant:
2735 case tok::kw___read_only:
2736 case tok::kw___read_write:
2737 case tok::kw___write_only:
2738
Eli Friedman53339e02009-06-08 23:27:34 +00002739 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002740
2741 case tok::kw_private:
2742 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002743 }
2744}
2745
Chris Lattneracd58a32006-08-06 17:24:14 +00002746/// isDeclarationSpecifier() - Return true if the current token is part of a
2747/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002748///
2749/// \param DisambiguatingWithExpression True to indicate that the purpose of
2750/// this check is to disambiguate between an expression and a declaration.
2751bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002752 switch (Tok.getKind()) {
2753 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002754
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002755 case tok::kw_private:
2756 return getLang().OpenCL;
2757
Chris Lattner020bab92009-01-04 23:41:41 +00002758 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002759 // Unfortunate hack to support "Class.factoryMethod" notation.
2760 if (getLang().ObjC1 && NextToken().is(tok::period))
2761 return false;
John Thompson22334602010-02-05 00:12:22 +00002762 if (TryAltiVecVectorToken())
2763 return true;
2764 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002765 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002766 // Annotate typenames and C++ scope specifiers. If we get one, just
2767 // recurse to handle whatever we get.
2768 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002769 return true;
2770 if (Tok.is(tok::identifier))
2771 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002772
2773 // If we're in Objective-C and we have an Objective-C class type followed
2774 // by an identifier and then either ':' or ']', in a place where an
2775 // expression is permitted, then this is probably a class message send
2776 // missing the initial '['. In this case, we won't consider this to be
2777 // the start of a declaration.
2778 if (DisambiguatingWithExpression &&
2779 isStartOfObjCClassMessageMissingOpenBracket())
2780 return false;
2781
John McCall1f476a12010-02-26 08:45:28 +00002782 return isDeclarationSpecifier();
2783
Chris Lattner020bab92009-01-04 23:41:41 +00002784 case tok::coloncolon: // ::foo::bar
2785 if (NextToken().is(tok::kw_new) || // ::new
2786 NextToken().is(tok::kw_delete)) // ::delete
2787 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002788
Chris Lattner020bab92009-01-04 23:41:41 +00002789 // Annotate typenames and C++ scope specifiers. If we get one, just
2790 // recurse to handle whatever we get.
2791 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002792 return true;
2793 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattneracd58a32006-08-06 17:24:14 +00002795 // storage-class-specifier
2796 case tok::kw_typedef:
2797 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002798 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002799 case tok::kw_static:
2800 case tok::kw_auto:
2801 case tok::kw_register:
2802 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002803
Chris Lattneracd58a32006-08-06 17:24:14 +00002804 // type-specifiers
2805 case tok::kw_short:
2806 case tok::kw_long:
2807 case tok::kw_signed:
2808 case tok::kw_unsigned:
2809 case tok::kw__Complex:
2810 case tok::kw__Imaginary:
2811 case tok::kw_void:
2812 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002813 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002814 case tok::kw_char16_t:
2815 case tok::kw_char32_t:
2816
Chris Lattneracd58a32006-08-06 17:24:14 +00002817 case tok::kw_int:
2818 case tok::kw_float:
2819 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002820 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002821 case tok::kw__Bool:
2822 case tok::kw__Decimal32:
2823 case tok::kw__Decimal64:
2824 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002825 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002826
Chris Lattner861a2262008-04-13 18:59:07 +00002827 // struct-or-union-specifier (C99) or class-specifier (C++)
2828 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002829 case tok::kw_struct:
2830 case tok::kw_union:
2831 // enum-specifier
2832 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002833
Chris Lattneracd58a32006-08-06 17:24:14 +00002834 // type-qualifier
2835 case tok::kw_const:
2836 case tok::kw_volatile:
2837 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002838
Chris Lattneracd58a32006-08-06 17:24:14 +00002839 // function-specifier
2840 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002841 case tok::kw_virtual:
2842 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002843
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002844 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002845 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002846
Chris Lattner599e47e2007-08-09 17:01:07 +00002847 // GNU typeof support.
2848 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002849
Chris Lattner599e47e2007-08-09 17:01:07 +00002850 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002851 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002852 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002853
Chris Lattner8b2ec162008-07-26 03:38:44 +00002854 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2855 case tok::less:
2856 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002857
Steve Narofff192fab2009-01-06 19:34:12 +00002858 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002859 case tok::kw___cdecl:
2860 case tok::kw___stdcall:
2861 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002862 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002863 case tok::kw___w64:
2864 case tok::kw___ptr64:
2865 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002866 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002867
2868 case tok::kw___private:
2869 case tok::kw___local:
2870 case tok::kw___global:
2871 case tok::kw___constant:
2872 case tok::kw___read_only:
2873 case tok::kw___read_write:
2874 case tok::kw___write_only:
2875
Eli Friedman53339e02009-06-08 23:27:34 +00002876 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002877 }
2878}
2879
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002880bool Parser::isConstructorDeclarator() {
2881 TentativeParsingAction TPA(*this);
2882
2883 // Parse the C++ scope specifier.
2884 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002885 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002886 TPA.Revert();
2887 return false;
2888 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002889
2890 // Parse the constructor name.
2891 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2892 // We already know that we have a constructor name; just consume
2893 // the token.
2894 ConsumeToken();
2895 } else {
2896 TPA.Revert();
2897 return false;
2898 }
2899
2900 // Current class name must be followed by a left parentheses.
2901 if (Tok.isNot(tok::l_paren)) {
2902 TPA.Revert();
2903 return false;
2904 }
2905 ConsumeParen();
2906
2907 // A right parentheses or ellipsis signals that we have a constructor.
2908 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2909 TPA.Revert();
2910 return true;
2911 }
2912
2913 // If we need to, enter the specified scope.
2914 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002915 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002916 DeclScopeObj.EnterDeclaratorScope();
2917
Francois Pichet79f3a872011-01-31 04:54:32 +00002918 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00002919 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00002920 MaybeParseMicrosoftAttributes(Attrs);
2921
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002922 // Check whether the next token(s) are part of a declaration
2923 // specifier, in which case we have the start of a parameter and,
2924 // therefore, we know that this is a constructor.
2925 bool IsConstructor = isDeclarationSpecifier();
2926 TPA.Revert();
2927 return IsConstructor;
2928}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002929
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002930/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002931/// type-qualifier-list: [C99 6.7.5]
2932/// type-qualifier
2933/// [vendor] attributes
2934/// [ only if VendorAttributesAllowed=true ]
2935/// type-qualifier-list type-qualifier
2936/// [vendor] type-qualifier-list attributes
2937/// [ only if VendorAttributesAllowed=true ]
2938/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2939/// [ only if CXX0XAttributesAllowed=true ]
2940/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002941///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002942void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2943 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002944 bool CXX0XAttributesAllowed) {
2945 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2946 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00002947 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002948 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002949 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00002950 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002951 else
2952 Diag(Loc, diag::err_attributes_not_allowed);
2953 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00002954
2955 SourceLocation EndLoc;
2956
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002957 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002958 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002959 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002960 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002961 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002962
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002963 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002964 case tok::code_completion:
2965 Actions.CodeCompleteTypeQualifiers(DS);
2966 ConsumeCodeCompletionToken();
2967 break;
2968
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002969 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002970 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2971 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002972 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002973 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002974 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2975 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002976 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002977 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002978 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2979 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002980 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002981
2982 // OpenCL qualifiers:
2983 case tok::kw_private:
2984 if (!getLang().OpenCL)
2985 goto DoneWithTypeQuals;
2986 case tok::kw___private:
2987 case tok::kw___global:
2988 case tok::kw___local:
2989 case tok::kw___constant:
2990 case tok::kw___read_only:
2991 case tok::kw___write_only:
2992 case tok::kw___read_write:
2993 ParseOpenCLQualifiers(DS);
2994 break;
2995
Eli Friedman53339e02009-06-08 23:27:34 +00002996 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002997 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002998 case tok::kw___cdecl:
2999 case tok::kw___stdcall:
3000 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003001 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003002 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003003 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003004 continue;
3005 }
3006 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003007 case tok::kw___pascal:
3008 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003009 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003010 continue;
3011 }
3012 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003013 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003014 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003015 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003016 continue; // do *not* consume the next token!
3017 }
3018 // otherwise, FALL THROUGH!
3019 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003020 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003021 // If this is not a type-qualifier token, we're done reading type
3022 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003023 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003024 if (EndLoc.isValid())
3025 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003026 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003027 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003028
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003029 // If the specifier combination wasn't legal, issue a diagnostic.
3030 if (isInvalid) {
3031 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003032 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003033 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003034 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003035 }
3036}
3037
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003038
3039/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3040///
3041void Parser::ParseDeclarator(Declarator &D) {
3042 /// This implements the 'declarator' production in the C grammar, then checks
3043 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003044 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003045}
3046
Sebastian Redlbd150f42008-11-21 19:14:01 +00003047/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3048/// is parsed by the function passed to it. Pass null, and the direct-declarator
3049/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003050/// ptr-operator production.
3051///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003052/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3053/// [C] pointer[opt] direct-declarator
3054/// [C++] direct-declarator
3055/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003056///
3057/// pointer: [C99 6.7.5]
3058/// '*' type-qualifier-list[opt]
3059/// '*' type-qualifier-list[opt] pointer
3060///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003061/// ptr-operator:
3062/// '*' cv-qualifier-seq[opt]
3063/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003064/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003065/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003066/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003067/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003068void Parser::ParseDeclaratorInternal(Declarator &D,
3069 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003070 if (Diags.hasAllExtensionsSilenced())
3071 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003072
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003073 // C++ member pointers start with a '::' or a nested-name.
3074 // Member pointers get special handling, since there's no place for the
3075 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003076 if (getLang().CPlusPlus &&
3077 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3078 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003079 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003080 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003081
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003082 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003083 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003084 // The scope spec really belongs to the direct-declarator.
3085 D.getCXXScopeSpec() = SS;
3086 if (DirectDeclParser)
3087 (this->*DirectDeclParser)(D);
3088 return;
3089 }
3090
3091 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003092 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003093 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003094 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003095 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003096
3097 // Recurse to parse whatever is left.
3098 ParseDeclaratorInternal(D, DirectDeclParser);
3099
3100 // Sema will have to catch (syntactically invalid) pointers into global
3101 // scope. It has to catch pointers into namespace scope anyway.
3102 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003103 Loc),
3104 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003105 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003106 return;
3107 }
3108 }
3109
3110 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003111 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003112 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003113 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003114 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003115 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003116 if (DirectDeclParser)
3117 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003118 return;
3119 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003120
Sebastian Redled0f3b02009-03-15 22:02:01 +00003121 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3122 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003123 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003124 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003125
Chris Lattner9eac9312009-03-27 04:18:06 +00003126 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003127 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003128 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003129
Bill Wendling3708c182007-05-27 10:15:43 +00003130 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003131 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003132
Bill Wendling3708c182007-05-27 10:15:43 +00003133 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003134 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003135 if (Kind == tok::star)
3136 // Remember that we parsed a pointer type, and remember the type-quals.
3137 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003138 DS.getConstSpecLoc(),
3139 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003140 DS.getRestrictSpecLoc()),
3141 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003142 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003143 else
3144 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003145 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003146 Loc),
3147 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003148 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003149 } else {
3150 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003151 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003152
Sebastian Redl3b27be62009-03-23 00:00:23 +00003153 // Complain about rvalue references in C++03, but then go on and build
3154 // the declarator.
3155 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003156 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003157
Bill Wendling93efb222007-06-02 23:28:54 +00003158 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3159 // cv-qualifiers are introduced through the use of a typedef or of a
3160 // template type argument, in which case the cv-qualifiers are ignored.
3161 //
3162 // [GNU] Retricted references are allowed.
3163 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003164 // [C++0x] Attributes on references are not allowed.
3165 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003166 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003167
3168 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3169 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3170 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003171 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003172 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3173 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003174 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003175 }
Bill Wendling3708c182007-05-27 10:15:43 +00003176
3177 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003178 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003179
Douglas Gregor66583c52008-11-03 15:51:28 +00003180 if (D.getNumTypeObjects() > 0) {
3181 // C++ [dcl.ref]p4: There shall be no references to references.
3182 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3183 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003184 if (const IdentifierInfo *II = D.getIdentifier())
3185 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3186 << II;
3187 else
3188 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3189 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003190
Sebastian Redlbd150f42008-11-21 19:14:01 +00003191 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003192 // can go ahead and build the (technically ill-formed)
3193 // declarator: reference collapsing will take care of it.
3194 }
3195 }
3196
Bill Wendling3708c182007-05-27 10:15:43 +00003197 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003198 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003199 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003200 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003201 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003202 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003203}
3204
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003205/// ParseDirectDeclarator
3206/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003207/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003208/// '(' declarator ')'
3209/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003210/// [C90] direct-declarator '[' constant-expression[opt] ']'
3211/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3212/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3213/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3214/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003215/// direct-declarator '(' parameter-type-list ')'
3216/// direct-declarator '(' identifier-list[opt] ')'
3217/// [GNU] direct-declarator '(' parameter-forward-declarations
3218/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003219/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3220/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003221/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003222///
3223/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003224/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003225/// '::'[opt] nested-name-specifier[opt] type-name
3226///
3227/// id-expression: [C++ 5.1]
3228/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003229/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003230///
3231/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003232/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003233/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003234/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003235/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003236/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003237///
Chris Lattneracd58a32006-08-06 17:24:14 +00003238void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003239 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003240
Douglas Gregor7861a802009-11-03 01:35:08 +00003241 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3242 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003243 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003244 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003245 }
3246
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003247 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003248 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003249 // Change the declaration context for name lookup, until this function
3250 // is exited (and the declarator has been parsed).
3251 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003252 }
3253
Douglas Gregor27b4c162010-12-23 22:44:42 +00003254 // C++0x [dcl.fct]p14:
3255 // There is a syntactic ambiguity when an ellipsis occurs at the end
3256 // of a parameter-declaration-clause without a preceding comma. In
3257 // this case, the ellipsis is parsed as part of the
3258 // abstract-declarator if the type of the parameter names a template
3259 // parameter pack that has not been expanded; otherwise, it is parsed
3260 // as part of the parameter-declaration-clause.
3261 if (Tok.is(tok::ellipsis) &&
3262 !((D.getContext() == Declarator::PrototypeContext ||
3263 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003264 NextToken().is(tok::r_paren) &&
3265 !Actions.containsUnexpandedParameterPacks(D)))
3266 D.setEllipsisLoc(ConsumeToken());
3267
Douglas Gregor7861a802009-11-03 01:35:08 +00003268 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3269 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3270 // We found something that indicates the start of an unqualified-id.
3271 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003272 bool AllowConstructorName;
3273 if (D.getDeclSpec().hasTypeSpecifier())
3274 AllowConstructorName = false;
3275 else if (D.getCXXScopeSpec().isSet())
3276 AllowConstructorName =
3277 (D.getContext() == Declarator::FileContext ||
3278 (D.getContext() == Declarator::MemberContext &&
3279 D.getDeclSpec().isFriendSpecified()));
3280 else
3281 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3282
Douglas Gregor7861a802009-11-03 01:35:08 +00003283 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3284 /*EnteringContext=*/true,
3285 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003286 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003287 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003288 D.getName()) ||
3289 // Once we're past the identifier, if the scope was bad, mark the
3290 // whole declarator bad.
3291 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003292 D.SetIdentifier(0, Tok.getLocation());
3293 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003294 } else {
3295 // Parsed the unqualified-id; update range information and move along.
3296 if (D.getSourceRange().getBegin().isInvalid())
3297 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3298 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003299 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003300 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003301 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003302 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003303 assert(!getLang().CPlusPlus &&
3304 "There's a C++-specific check for tok::identifier above");
3305 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3306 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3307 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003308 goto PastIdentifier;
3309 }
3310
3311 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003312 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003313 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003314 // Example: 'char (*X)' or 'int (*XX)(void)'
3315 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003316
3317 // If the declarator was parenthesized, we entered the declarator
3318 // scope when parsing the parenthesized declarator, then exited
3319 // the scope already. Re-enter the scope, if we need to.
3320 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003321 // If there was an error parsing parenthesized declarator, declarator
3322 // scope may have been enterred before. Don't do it again.
3323 if (!D.isInvalidType() &&
3324 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003325 // Change the declaration context for name lookup, until this function
3326 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003327 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003328 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003329 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003330 // This could be something simple like "int" (in which case the declarator
3331 // portion is empty), if an abstract-declarator is allowed.
3332 D.SetIdentifier(0, Tok.getLocation());
3333 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003334 if (D.getContext() == Declarator::MemberContext)
3335 Diag(Tok, diag::err_expected_member_name_or_semi)
3336 << D.getDeclSpec().getSourceRange();
3337 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003338 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003339 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003340 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003341 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003342 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003343 }
Mike Stump11289f42009-09-09 15:08:12 +00003344
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003345 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003346 assert(D.isPastIdentifier() &&
3347 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003348
Alexis Hunt96d5c762009-11-21 08:43:09 +00003349 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003350 if (D.getIdentifier())
3351 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003352
Chris Lattneracd58a32006-08-06 17:24:14 +00003353 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003354 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003355 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3356 // In such a case, check if we actually have a function declarator; if it
3357 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003358 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3359 // When not in file scope, warn for ambiguous function declarators, just
3360 // in case the author intended it as a variable definition.
3361 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3362 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3363 break;
3364 }
John McCall084e83d2011-03-24 11:26:52 +00003365 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003366 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003367 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003368 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003369 } else {
3370 break;
3371 }
3372 }
3373}
3374
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003375/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3376/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003377/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003378/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3379///
3380/// direct-declarator:
3381/// '(' declarator ')'
3382/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003383/// direct-declarator '(' parameter-type-list ')'
3384/// direct-declarator '(' identifier-list[opt] ')'
3385/// [GNU] direct-declarator '(' parameter-forward-declarations
3386/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003387///
3388void Parser::ParseParenDeclarator(Declarator &D) {
3389 SourceLocation StartLoc = ConsumeParen();
3390 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003391
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003392 // Eat any attributes before we look at whether this is a grouping or function
3393 // declarator paren. If this is a grouping paren, the attribute applies to
3394 // the type being built up, for example:
3395 // int (__attribute__(()) *x)(long y)
3396 // If this ends up not being a grouping paren, the attribute applies to the
3397 // first argument, for example:
3398 // int (__attribute__(()) int x)
3399 // In either case, we need to eat any attributes to be able to determine what
3400 // sort of paren this is.
3401 //
John McCall084e83d2011-03-24 11:26:52 +00003402 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003403 bool RequiresArg = false;
3404 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003405 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003406
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003407 // We require that the argument list (if this is a non-grouping paren) be
3408 // present even if the attribute list was empty.
3409 RequiresArg = true;
3410 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003411 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003412 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003413 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3414 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003415 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003416 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003417 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003418 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003419 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003420
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003421 // If we haven't past the identifier yet (or where the identifier would be
3422 // stored, if this is an abstract declarator), then this is probably just
3423 // grouping parens. However, if this could be an abstract-declarator, then
3424 // this could also be the start of function arguments (consider 'void()').
3425 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003426
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003427 if (!D.mayOmitIdentifier()) {
3428 // If this can't be an abstract-declarator, this *must* be a grouping
3429 // paren, because we haven't seen the identifier yet.
3430 isGrouping = true;
3431 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003432 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003433 isDeclarationSpecifier()) { // 'int(int)' is a function.
3434 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3435 // considered to be a type, not a K&R identifier-list.
3436 isGrouping = false;
3437 } else {
3438 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3439 isGrouping = true;
3440 }
Mike Stump11289f42009-09-09 15:08:12 +00003441
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003442 // If this is a grouping paren, handle:
3443 // direct-declarator: '(' declarator ')'
3444 // direct-declarator: '(' attributes declarator ')'
3445 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003446 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003447 D.setGroupingParens(true);
3448
Sebastian Redlbd150f42008-11-21 19:14:01 +00003449 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003450 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003451 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003452 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3453 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003454
3455 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003456 return;
3457 }
Mike Stump11289f42009-09-09 15:08:12 +00003458
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003459 // Okay, if this wasn't a grouping paren, it must be the start of a function
3460 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003461 // identifier (and remember where it would have been), then call into
3462 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003463 D.SetIdentifier(0, Tok.getLocation());
3464
John McCall53fa7142010-12-24 02:08:15 +00003465 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003466}
3467
3468/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3469/// declarator D up to a paren, which indicates that we are parsing function
3470/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003471///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003472/// If AttrList is non-null, then the caller parsed those arguments immediately
3473/// after the open paren - they should be considered to be the first argument of
3474/// a parameter. If RequiresArg is true, then the first argument of the
3475/// function is required to be present and required to not be an identifier
3476/// list.
3477///
Chris Lattneracd58a32006-08-06 17:24:14 +00003478/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003479/// parameter-type-list: [C99 6.7.5]
3480/// parameter-list
3481/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003482/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003483///
3484/// parameter-list: [C99 6.7.5]
3485/// parameter-declaration
3486/// parameter-list ',' parameter-declaration
3487///
3488/// parameter-declaration: [C99 6.7.5]
3489/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003490/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003491/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003492/// declaration-specifiers abstract-declarator[opt]
3493/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003494/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003495/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003496///
Douglas Gregor54992352011-01-26 03:43:54 +00003497/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3498/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003499///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003500/// [C++0x] exception-specification:
3501/// dynamic-exception-specification
3502/// noexcept-specification
3503///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003504void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003505 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003506 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003507 // lparen is already consumed!
3508 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003509
Douglas Gregor7fb25412010-10-01 18:44:50 +00003510 ParsedType TrailingReturnType;
3511
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003512 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003513 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003514 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003515 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003516
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003517 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003518
3519 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003520 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003521 SourceLocation RefQualifierLoc;
3522 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003523 ExceptionSpecificationType ESpecType = EST_None;
3524 SourceRange ESpecRange;
3525 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3526 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3527 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003528 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003529 MaybeParseCXX0XAttributes(attrs);
3530
Chris Lattnercf0bab22008-12-18 07:02:59 +00003531 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003532 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003533 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003534
Douglas Gregor54992352011-01-26 03:43:54 +00003535 // Parse ref-qualifier[opt]
3536 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3537 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003538 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003539
Douglas Gregor54992352011-01-26 03:43:54 +00003540 RefQualifierIsLValueRef = Tok.is(tok::amp);
3541 RefQualifierLoc = ConsumeToken();
3542 EndLoc = RefQualifierLoc;
3543 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003544
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003545 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003546 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3547 DynamicExceptions,
3548 DynamicExceptionRanges,
3549 NoexceptExpr);
3550 if (ESpecType != EST_None)
3551 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003552
3553 // Parse trailing-return-type.
3554 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3555 TrailingReturnType = ParseTrailingReturnType().get();
3556 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003557 }
3558
Chris Lattner371ed4e2008-04-06 06:57:35 +00003559 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003560 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003561 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003562 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003563 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003564 /*arglist*/ 0, 0,
3565 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003566 RefQualifierIsLValueRef,
3567 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003568 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003569 DynamicExceptions.data(),
3570 DynamicExceptionRanges.data(),
3571 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003572 NoexceptExpr.isUsable() ?
3573 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003574 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003575 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003576 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003577 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003578 }
3579
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003580 // Alternatively, this parameter list may be an identifier list form for a
3581 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003582 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3583 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003584 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003585 // K&R identifier lists can't have typedefs as identifiers, per
3586 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003587 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003588 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003589
Steve Naroffb0486722009-01-28 19:16:40 +00003590 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003591 // normal declarators, not for abstract-declarators. Get the first
3592 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003593 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003594 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003595
3596 // Identifier lists follow a really simple grammar: the identifiers can
3597 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3598 // identifier lists are really rare in the brave new modern world, and it
3599 // is very common for someone to typo a type in a non-k&r style list. If
3600 // we are presented with something like: "void foo(intptr x, float y)",
3601 // we don't want to start parsing the function declarator as though it is
3602 // a K&R style declarator just because intptr is an invalid type.
3603 //
3604 // To handle this, we check to see if the token after the first identifier
3605 // is a "," or ")". Only if so, do we parse it as an identifier list.
3606 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3607 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3608 FirstTok.getIdentifierInfo(),
3609 FirstTok.getLocation(), D);
3610
3611 // If we get here, the code is invalid. Push the first identifier back
3612 // into the token stream and parse the first argument as an (invalid)
3613 // normal argument declarator.
3614 PP.EnterToken(Tok);
3615 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003616 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003617 }
Mike Stump11289f42009-09-09 15:08:12 +00003618
Chris Lattner371ed4e2008-04-06 06:57:35 +00003619 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003620
Chris Lattner371ed4e2008-04-06 06:57:35 +00003621 // Build up an array of information about the parsed arguments.
3622 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003623
3624 // Enter function-declaration scope, limiting any declarators to the
3625 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003626 ParseScope PrototypeScope(this,
3627 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003628
Chris Lattner371ed4e2008-04-06 06:57:35 +00003629 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003630 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003631 while (1) {
3632 if (Tok.is(tok::ellipsis)) {
3633 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003634 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003635 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003636 }
Mike Stump11289f42009-09-09 15:08:12 +00003637
Chris Lattner371ed4e2008-04-06 06:57:35 +00003638 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003639 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003640 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003641
3642 // Skip any Microsoft attributes before a param.
3643 if (getLang().Microsoft && Tok.is(tok::l_square))
3644 ParseMicrosoftAttributes(DS.getAttributes());
3645
3646 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003647
3648 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003649 // Take them so that we only apply the attributes to the first parameter.
3650 DS.takeAttributesFrom(attrs);
3651
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003652 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003653
Chris Lattner371ed4e2008-04-06 06:57:35 +00003654 // Parse the declarator. This is "PrototypeContext", because we must
3655 // accept either 'declarator' or 'abstract-declarator' here.
3656 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3657 ParseDeclarator(ParmDecl);
3658
3659 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003660 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003661
Chris Lattner371ed4e2008-04-06 06:57:35 +00003662 // Remember this parsed parameter in ParamInfo.
3663 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003664
Douglas Gregor4d87df52008-12-16 21:30:33 +00003665 // DefArgToks is used when the parsing of default arguments needs
3666 // to be delayed.
3667 CachedTokens *DefArgToks = 0;
3668
Chris Lattner371ed4e2008-04-06 06:57:35 +00003669 // If no parameter was specified, verify that *something* was specified,
3670 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003671 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3672 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003673 // Completely missing, emit error.
3674 Diag(DSStart, diag::err_missing_param);
3675 } else {
3676 // Otherwise, we have something. Add it and let semantic analysis try
3677 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003678
Chris Lattner371ed4e2008-04-06 06:57:35 +00003679 // Inform the actions module about the parameter declarator, so it gets
3680 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003681 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003682
3683 // Parse the default argument, if any. We parse the default
3684 // arguments in all dialects; the semantic analysis in
3685 // ActOnParamDefaultArgument will reject the default argument in
3686 // C.
3687 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003688 SourceLocation EqualLoc = Tok.getLocation();
3689
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003690 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003691 if (D.getContext() == Declarator::MemberContext) {
3692 // If we're inside a class definition, cache the tokens
3693 // corresponding to the default argument. We'll actually parse
3694 // them when we see the end of the class definition.
3695 // FIXME: Templates will require something similar.
3696 // FIXME: Can we use a smart pointer for Toks?
3697 DefArgToks = new CachedTokens;
3698
Mike Stump11289f42009-09-09 15:08:12 +00003699 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003700 /*StopAtSemi=*/true,
3701 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003702 delete DefArgToks;
3703 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003704 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003705 } else {
3706 // Mark the end of the default argument so that we know when to
3707 // stop when we parse it later on.
3708 Token DefArgEnd;
3709 DefArgEnd.startToken();
3710 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3711 DefArgEnd.setLocation(Tok.getLocation());
3712 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003713 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003714 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003715 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003716 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003717 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003718 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003719
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003720 // The argument isn't actually potentially evaluated unless it is
3721 // used.
3722 EnterExpressionEvaluationContext Eval(Actions,
3723 Sema::PotentiallyEvaluatedIfUsed);
3724
John McCalldadc5752010-08-24 06:29:42 +00003725 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003726 if (DefArgResult.isInvalid()) {
3727 Actions.ActOnParamDefaultArgumentError(Param);
3728 SkipUntil(tok::comma, tok::r_paren, true, true);
3729 } else {
3730 // Inform the actions module about the default argument
3731 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003732 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003733 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003734 }
3735 }
Mike Stump11289f42009-09-09 15:08:12 +00003736
3737 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3738 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003739 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003740 }
3741
3742 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003743 if (Tok.isNot(tok::comma)) {
3744 if (Tok.is(tok::ellipsis)) {
3745 IsVariadic = true;
3746 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3747
3748 if (!getLang().CPlusPlus) {
3749 // We have ellipsis without a preceding ',', which is ill-formed
3750 // in C. Complain and provide the fix.
3751 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003752 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003753 }
3754 }
3755
3756 break;
3757 }
Mike Stump11289f42009-09-09 15:08:12 +00003758
Chris Lattner371ed4e2008-04-06 06:57:35 +00003759 // Consume the comma.
3760 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003761 }
Mike Stump11289f42009-09-09 15:08:12 +00003762
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003763 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003764 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003765
John McCall084e83d2011-03-24 11:26:52 +00003766 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003767 SourceLocation RefQualifierLoc;
3768 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003769 ExceptionSpecificationType ESpecType = EST_None;
3770 SourceRange ESpecRange;
3771 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3772 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3773 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003774
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003775 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003776 MaybeParseCXX0XAttributes(attrs);
3777
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003778 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003779 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003780 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003781 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003782
Douglas Gregor54992352011-01-26 03:43:54 +00003783 // Parse ref-qualifier[opt]
3784 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3785 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003786 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003787
3788 RefQualifierIsLValueRef = Tok.is(tok::amp);
3789 RefQualifierLoc = ConsumeToken();
3790 EndLoc = RefQualifierLoc;
3791 }
3792
Sebastian Redl965b0e32011-03-05 14:45:16 +00003793 // FIXME: We should leave the prototype scope before parsing the exception
3794 // specification, and then reenter it when parsing the trailing return type.
3795 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3796
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003797 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003798 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3799 DynamicExceptions,
3800 DynamicExceptionRanges,
3801 NoexceptExpr);
3802 if (ESpecType != EST_None)
3803 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003804
3805 // Parse trailing-return-type.
3806 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3807 TrailingReturnType = ParseTrailingReturnType().get();
3808 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003809 }
3810
Douglas Gregor7fb25412010-10-01 18:44:50 +00003811 // Leave prototype scope.
3812 PrototypeScope.Exit();
3813
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003814 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003815 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003816 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003817 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003818 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003819 RefQualifierIsLValueRef,
3820 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003821 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003822 DynamicExceptions.data(),
3823 DynamicExceptionRanges.data(),
3824 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003825 NoexceptExpr.isUsable() ?
3826 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003827 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003828 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003829 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003830}
Chris Lattneracd58a32006-08-06 17:24:14 +00003831
Chris Lattner6c940e62008-04-06 06:34:08 +00003832/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3833/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003834/// first identifier has already been consumed, and the current token is the
3835/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003836///
3837/// identifier-list: [C99 6.7.5]
3838/// identifier
3839/// identifier-list ',' identifier
3840///
3841void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003842 IdentifierInfo *FirstIdent,
3843 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003844 Declarator &D) {
3845 // Build up an array of information about the parsed arguments.
3846 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3847 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003848
Chris Lattner6c940e62008-04-06 06:34:08 +00003849 // If there was no identifier specified for the declarator, either we are in
3850 // an abstract-declarator, or we are in a parameter declarator which was found
3851 // to be abstract. In abstract-declarators, identifier lists are not valid:
3852 // diagnose this.
3853 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003854 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003855
Chris Lattner9453ab82010-05-14 17:23:36 +00003856 // The first identifier was already read, and is known to be the first
3857 // identifier in the list. Remember this identifier in ParamInfo.
3858 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003859 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003860
Chris Lattner6c940e62008-04-06 06:34:08 +00003861 while (Tok.is(tok::comma)) {
3862 // Eat the comma.
3863 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003864
Chris Lattner9186f552008-04-06 06:39:19 +00003865 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003866 if (Tok.isNot(tok::identifier)) {
3867 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003868 SkipUntil(tok::r_paren);
3869 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003870 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003871
Chris Lattner6c940e62008-04-06 06:34:08 +00003872 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003873
3874 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003875 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003876 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003877
Chris Lattner6c940e62008-04-06 06:34:08 +00003878 // Verify that the argument identifier has not already been mentioned.
3879 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003880 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003881 } else {
3882 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003883 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003884 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003885 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003886 }
Mike Stump11289f42009-09-09 15:08:12 +00003887
Chris Lattner6c940e62008-04-06 06:34:08 +00003888 // Eat the identifier.
3889 ConsumeToken();
3890 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003891
3892 // If we have the closing ')', eat it and we're done.
3893 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3894
Chris Lattner9186f552008-04-06 06:39:19 +00003895 // Remember that we parsed a function type, and remember the attributes. This
3896 // function type is always a K&R style function type, which is not varargs and
3897 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00003898 ParsedAttributes attrs(AttrFactory);
3899 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003900 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003901 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003902 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003903 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003904 EST_None, SourceLocation(), 0, 0,
3905 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00003906 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003907}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003908
Chris Lattnere8074e62006-08-06 18:30:15 +00003909/// [C90] direct-declarator '[' constant-expression[opt] ']'
3910/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3911/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3912/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3913/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3914void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003915 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003916
Chris Lattner84a11622008-12-18 07:27:21 +00003917 // C array syntax has many features, but by-far the most common is [] and [4].
3918 // This code does a fast path to handle some of the most obvious cases.
3919 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003920 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003921 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003922 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003923
Chris Lattner84a11622008-12-18 07:27:21 +00003924 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003925 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00003926 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00003927 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00003928 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003929 return;
3930 } else if (Tok.getKind() == tok::numeric_constant &&
3931 GetLookAheadToken(1).is(tok::r_square)) {
3932 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003933 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003934 ConsumeToken();
3935
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003936 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003937 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003938 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003939
Chris Lattner84a11622008-12-18 07:27:21 +00003940 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00003941 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00003942 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00003943 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00003944 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003945 return;
3946 }
Mike Stump11289f42009-09-09 15:08:12 +00003947
Chris Lattnere8074e62006-08-06 18:30:15 +00003948 // If valid, this location is the position where we read the 'static' keyword.
3949 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003950 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003951 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003952
Chris Lattnere8074e62006-08-06 18:30:15 +00003953 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003954 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00003955 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003956 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003957
Chris Lattnere8074e62006-08-06 18:30:15 +00003958 // If we haven't already read 'static', check to see if there is one after the
3959 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003960 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003961 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003962
Chris Lattnere8074e62006-08-06 18:30:15 +00003963 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003964 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003965 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003966
Chris Lattner521ff2b2008-04-06 05:26:30 +00003967 // Handle the case where we have '[*]' as the array size. However, a leading
3968 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3969 // the the token after the star is a ']'. Since stars in arrays are
3970 // infrequent, use of lookahead is not costly here.
3971 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003972 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003973
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003974 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003975 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003976 StaticLoc = SourceLocation(); // Drop the static.
3977 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003978 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003979 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003980 // Note, in C89, this production uses the constant-expr production instead
3981 // of assignment-expr. The only difference is that assignment-expr allows
3982 // things like '=' and '*='. Sema rejects these in C89 mode because they
3983 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003984
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003985 // Parse the constant-expression or assignment-expression now (depending
3986 // on dialect).
3987 if (getLang().CPlusPlus)
3988 NumElements = ParseConstantExpression();
3989 else
3990 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003991 }
Mike Stump11289f42009-09-09 15:08:12 +00003992
Chris Lattner62591722006-08-12 18:40:58 +00003993 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003994 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003995 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003996 // If the expression was invalid, skip it.
3997 SkipUntil(tok::r_square);
3998 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003999 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004000
4001 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4002
John McCall084e83d2011-03-24 11:26:52 +00004003 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004004 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004005
Chris Lattner84a11622008-12-18 07:27:21 +00004006 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004007 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004008 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004009 NumElements.release(),
4010 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004011 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004012}
4013
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004014/// [GNU] typeof-specifier:
4015/// typeof ( expressions )
4016/// typeof ( type-name )
4017/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004018///
4019void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004020 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004021 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004022 SourceLocation StartLoc = ConsumeToken();
4023
John McCalle8595032010-01-13 20:03:27 +00004024 const bool hasParens = Tok.is(tok::l_paren);
4025
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004026 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004027 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004028 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004029 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4030 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004031 if (hasParens)
4032 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004033
4034 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004035 // FIXME: Not accurate, the range gets one token more than it should.
4036 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004037 else
4038 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004039
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004040 if (isCastExpr) {
4041 if (!CastTy) {
4042 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004043 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004044 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004045
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004046 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004047 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004048 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4049 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004050 DiagID, CastTy))
4051 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004052 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004053 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004054
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004055 // If we get here, the operand to the typeof was an expresion.
4056 if (Operand.isInvalid()) {
4057 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004058 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004059 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004060
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004061 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004062 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004063 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4064 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004065 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004066 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004067}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004068
4069
4070/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4071/// from TryAltiVecVectorToken.
4072bool Parser::TryAltiVecVectorTokenOutOfLine() {
4073 Token Next = NextToken();
4074 switch (Next.getKind()) {
4075 default: return false;
4076 case tok::kw_short:
4077 case tok::kw_long:
4078 case tok::kw_signed:
4079 case tok::kw_unsigned:
4080 case tok::kw_void:
4081 case tok::kw_char:
4082 case tok::kw_int:
4083 case tok::kw_float:
4084 case tok::kw_double:
4085 case tok::kw_bool:
4086 case tok::kw___pixel:
4087 Tok.setKind(tok::kw___vector);
4088 return true;
4089 case tok::identifier:
4090 if (Next.getIdentifierInfo() == Ident_pixel) {
4091 Tok.setKind(tok::kw___vector);
4092 return true;
4093 }
4094 return false;
4095 }
4096}
4097
4098bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4099 const char *&PrevSpec, unsigned &DiagID,
4100 bool &isInvalid) {
4101 if (Tok.getIdentifierInfo() == Ident_vector) {
4102 Token Next = NextToken();
4103 switch (Next.getKind()) {
4104 case tok::kw_short:
4105 case tok::kw_long:
4106 case tok::kw_signed:
4107 case tok::kw_unsigned:
4108 case tok::kw_void:
4109 case tok::kw_char:
4110 case tok::kw_int:
4111 case tok::kw_float:
4112 case tok::kw_double:
4113 case tok::kw_bool:
4114 case tok::kw___pixel:
4115 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4116 return true;
4117 case tok::identifier:
4118 if (Next.getIdentifierInfo() == Ident_pixel) {
4119 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4120 return true;
4121 }
4122 break;
4123 default:
4124 break;
4125 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004126 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004127 DS.isTypeAltiVecVector()) {
4128 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4129 return true;
4130 }
4131 return false;
4132}