blob: 702f8c7cfb1e88406fe82eddb98820b5566946af [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
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000487/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000488void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
489 SourceLocation AvailabilityLoc,
490 ParsedAttributes &attrs,
491 SourceLocation *endLoc) {
492 SourceLocation PlatformLoc;
493 IdentifierInfo *Platform = 0;
494
495 enum { Introduced, Deprecated, Obsoleted, Unknown };
496 AvailabilityChange Changes[Unknown];
497
498 // Opening '('.
499 SourceLocation LParenLoc;
500 if (!Tok.is(tok::l_paren)) {
501 Diag(Tok, diag::err_expected_lparen);
502 return;
503 }
504 LParenLoc = ConsumeParen();
505
506 // Parse the platform name,
507 if (Tok.isNot(tok::identifier)) {
508 Diag(Tok, diag::err_availability_expected_platform);
509 SkipUntil(tok::r_paren);
510 return;
511 }
512 Platform = Tok.getIdentifierInfo();
513 PlatformLoc = ConsumeToken();
514
515 // Parse the ',' following the platform name.
516 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
517 return;
518
519 // If we haven't grabbed the pointers for the identifiers
520 // "introduced", "deprecated", and "obsoleted", do so now.
521 if (!Ident_introduced) {
522 Ident_introduced = PP.getIdentifierInfo("introduced");
523 Ident_deprecated = PP.getIdentifierInfo("deprecated");
524 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000525 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000526 }
527
528 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000529 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000530 do {
531 if (Tok.isNot(tok::identifier)) {
532 Diag(Tok, diag::err_availability_expected_change);
533 SkipUntil(tok::r_paren);
534 return;
535 }
536 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
537 SourceLocation KeywordLoc = ConsumeToken();
538
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000539 if (Keyword == Ident_unavailable) {
540 if (UnavailableLoc.isValid()) {
541 Diag(KeywordLoc, diag::err_availability_redundant)
542 << Keyword << SourceRange(UnavailableLoc);
543 }
544 UnavailableLoc = KeywordLoc;
545
546 if (Tok.isNot(tok::comma))
547 break;
548
549 ConsumeToken();
550 continue;
551 }
552
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000553 if (Tok.isNot(tok::equal)) {
554 Diag(Tok, diag::err_expected_equal_after)
555 << Keyword;
556 SkipUntil(tok::r_paren);
557 return;
558 }
559 ConsumeToken();
560
561 SourceRange VersionRange;
562 VersionTuple Version = ParseVersionTuple(VersionRange);
563
564 if (Version.empty()) {
565 SkipUntil(tok::r_paren);
566 return;
567 }
568
569 unsigned Index;
570 if (Keyword == Ident_introduced)
571 Index = Introduced;
572 else if (Keyword == Ident_deprecated)
573 Index = Deprecated;
574 else if (Keyword == Ident_obsoleted)
575 Index = Obsoleted;
576 else
577 Index = Unknown;
578
579 if (Index < Unknown) {
580 if (!Changes[Index].KeywordLoc.isInvalid()) {
581 Diag(KeywordLoc, diag::err_availability_redundant)
582 << Keyword
583 << SourceRange(Changes[Index].KeywordLoc,
584 Changes[Index].VersionRange.getEnd());
585 }
586
587 Changes[Index].KeywordLoc = KeywordLoc;
588 Changes[Index].Version = Version;
589 Changes[Index].VersionRange = VersionRange;
590 } else {
591 Diag(KeywordLoc, diag::err_availability_unknown_change)
592 << Keyword << VersionRange;
593 }
594
595 if (Tok.isNot(tok::comma))
596 break;
597
598 ConsumeToken();
599 } while (true);
600
601 // Closing ')'.
602 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
603 if (RParenLoc.isInvalid())
604 return;
605
606 if (endLoc)
607 *endLoc = RParenLoc;
608
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000609 // The 'unavailable' availability cannot be combined with any other
610 // availability changes. Make sure that hasn't happened.
611 if (UnavailableLoc.isValid()) {
612 bool Complained = false;
613 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
614 if (Changes[Index].KeywordLoc.isValid()) {
615 if (!Complained) {
616 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
617 << SourceRange(Changes[Index].KeywordLoc,
618 Changes[Index].VersionRange.getEnd());
619 Complained = true;
620 }
621
622 // Clear out the availability.
623 Changes[Index] = AvailabilityChange();
624 }
625 }
626 }
627
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000628 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000629 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000630 0, SourceLocation(),
631 Platform, PlatformLoc,
632 Changes[Introduced],
633 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000634 Changes[Obsoleted],
635 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000636}
637
John McCall53fa7142010-12-24 02:08:15 +0000638void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
639 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
640 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000641}
642
Chris Lattner53361ac2006-08-10 05:19:57 +0000643/// ParseDeclaration - Parse a full 'declaration', which consists of
644/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000645/// 'Context' should be a Declarator::TheContext value. This returns the
646/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000647///
648/// declaration: [C99 6.7]
649/// block-declaration ->
650/// simple-declaration
651/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000652/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000653/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000654/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000655/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000656/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000657/// others... [FIXME]
658///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000659Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
660 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000661 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000662 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000663 ParenBraceBracketBalancer BalancerRAIIObj(*this);
664
John McCall48871652010-08-21 09:40:31 +0000665 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000666 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000667 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000668 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000669 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000670 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000671 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000672 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000673 // Could be the start of an inline namespace. Allowed as an ext in C++03.
674 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000675 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000676 SourceLocation InlineLoc = ConsumeToken();
677 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
678 break;
679 }
John McCall53fa7142010-12-24 02:08:15 +0000680 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000681 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000682 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000683 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000684 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000685 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000686 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000687 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000688 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000689 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000690 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000691 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000692 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000693 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000694 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000695 default:
John McCall53fa7142010-12-24 02:08:15 +0000696 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000697 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000698
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000699 // This routine returns a DeclGroup, if the thing we parsed only contains a
700 // single decl, convert it now.
701 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000702}
703
704/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
705/// declaration-specifiers init-declarator-list[opt] ';'
706///[C90/C++]init-declarator-list ';' [TODO]
707/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000708///
Richard Smith02e85f32011-04-14 22:09:26 +0000709/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
710/// attribute-specifier-seq[opt] type-specifier-seq declarator
711///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000712/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000713/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000714///
715/// If FRI is non-null, we might be parsing a for-range-declaration instead
716/// of a simple-declaration. If we find that we are, we also parse the
717/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000718Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
719 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000720 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000721 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000722 bool RequireSemi,
723 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000724 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000725 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000726 DS.takeAttributesFrom(attrs);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000727 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000728 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000729 StmtResult R = Actions.ActOnVlaStmt(DS);
730 if (R.isUsable())
731 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000732
Chris Lattner0e894622006-08-13 19:58:17 +0000733 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
734 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000735 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000736 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000737 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000738 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000739 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000740 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000741 }
Mike Stump11289f42009-09-09 15:08:12 +0000742
Richard Smith02e85f32011-04-14 22:09:26 +0000743 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000744}
Mike Stump11289f42009-09-09 15:08:12 +0000745
John McCalld5a36322009-11-03 19:26:08 +0000746/// ParseDeclGroup - Having concluded that this is either a function
747/// definition or a group of object declarations, actually parse the
748/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000749Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
750 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000751 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000752 SourceLocation *DeclEnd,
753 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000754 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000755 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000756 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000757
John McCalld5a36322009-11-03 19:26:08 +0000758 // Bail out if the first declarator didn't seem well-formed.
759 if (!D.hasName() && !D.mayOmitIdentifier()) {
760 // Skip until ; or }.
761 SkipUntil(tok::r_brace, true, true);
762 if (Tok.is(tok::semi))
763 ConsumeToken();
764 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000767 // Check to see if we have a function *definition* which must have a body.
768 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
769 // Look at the next token to make sure that this isn't a function
770 // declaration. We have to check this because __attribute__ might be the
771 // start of a function definition in GCC-extended K&R C.
772 !isDeclarationAfterDeclarator()) {
773
Chris Lattner13901342010-07-11 22:42:07 +0000774 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000775 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
776 Diag(Tok, diag::err_function_declared_typedef);
777
778 // Recover by treating the 'typedef' as spurious.
779 DS.ClearStorageClassSpecs();
780 }
781
John McCall48871652010-08-21 09:40:31 +0000782 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000783 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000784 }
785
786 if (isDeclarationSpecifier()) {
787 // If there is an invalid declaration specifier right after the function
788 // prototype, then we must be in a missing semicolon case where this isn't
789 // actually a body. Just fall through into the code that handles it as a
790 // prototype, and let the top-level code handle the erroneous declspec
791 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000792 } else {
793 Diag(Tok, diag::err_expected_fn_body);
794 SkipUntil(tok::semi);
795 return DeclGroupPtrTy();
796 }
797 }
798
Richard Smith02e85f32011-04-14 22:09:26 +0000799 if (ParseAttributesAfterDeclarator(D))
800 return DeclGroupPtrTy();
801
802 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
803 // must parse and analyze the for-range-initializer before the declaration is
804 // analyzed.
805 if (FRI && Tok.is(tok::colon)) {
806 FRI->ColonLoc = ConsumeToken();
807 // FIXME: handle braced-init-list here.
808 FRI->RangeExpr = ParseExpression();
809 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
810 Actions.ActOnCXXForRangeDecl(ThisDecl);
811 Actions.FinalizeDeclaration(ThisDecl);
812 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
813 }
814
John McCall48871652010-08-21 09:40:31 +0000815 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +0000816 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +0000817 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000818 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000819 DeclsInGroup.push_back(FirstDecl);
820
821 // If we don't have a comma, it is either the end of the list (a ';') or an
822 // error, bail out.
823 while (Tok.is(tok::comma)) {
824 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000825 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000826
827 // Parse the next declarator.
828 D.clear();
829
830 // Accept attributes in an init-declarator. In the first declarator in a
831 // declaration, these would be part of the declspec. In subsequent
832 // declarators, they become part of the declarator itself, so that they
833 // don't apply to declarators after *this* one. Examples:
834 // short __attribute__((common)) var; -> declspec
835 // short var __attribute__((common)); -> declarator
836 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000837 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000838
839 ParseDeclarator(D);
840
John McCall48871652010-08-21 09:40:31 +0000841 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000842 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000843 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000844 DeclsInGroup.push_back(ThisDecl);
845 }
846
847 if (DeclEnd)
848 *DeclEnd = Tok.getLocation();
849
850 if (Context != Declarator::ForContext &&
851 ExpectAndConsume(tok::semi,
852 Context == Declarator::FileContext
853 ? diag::err_invalid_token_after_toplevel_declarator
854 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000855 // Okay, there was no semicolon and one was expected. If we see a
856 // declaration specifier, just assume it was missing and continue parsing.
857 // Otherwise things are very confused and we skip to recover.
858 if (!isDeclarationSpecifier()) {
859 SkipUntil(tok::r_brace, true, true);
860 if (Tok.is(tok::semi))
861 ConsumeToken();
862 }
John McCalld5a36322009-11-03 19:26:08 +0000863 }
864
Douglas Gregor0be31a22010-07-02 17:43:08 +0000865 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000866 DeclsInGroup.data(),
867 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000868}
869
Richard Smith02e85f32011-04-14 22:09:26 +0000870/// Parse an optional simple-asm-expr and attributes, and attach them to a
871/// declarator. Returns true on an error.
872bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
873 // If a simple-asm-expr is present, parse it.
874 if (Tok.is(tok::kw_asm)) {
875 SourceLocation Loc;
876 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
877 if (AsmLabel.isInvalid()) {
878 SkipUntil(tok::semi, true, true);
879 return true;
880 }
881
882 D.setAsmLabel(AsmLabel.release());
883 D.SetRangeEnd(Loc);
884 }
885
886 MaybeParseGNUAttributes(D);
887 return false;
888}
889
Douglas Gregor23996282009-05-12 21:31:51 +0000890/// \brief Parse 'declaration' after parsing 'declaration-specifiers
891/// declarator'. This method parses the remainder of the declaration
892/// (including any attributes or initializer, among other things) and
893/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000894///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000895/// init-declarator: [C99 6.7]
896/// declarator
897/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000898/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
899/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000900/// [C++] declarator initializer[opt]
901///
902/// [C++] initializer:
903/// [C++] '=' initializer-clause
904/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000905/// [C++0x] '=' 'default' [TODO]
906/// [C++0x] '=' 'delete'
907///
908/// According to the standard grammar, =default and =delete are function
909/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000910///
John McCall48871652010-08-21 09:40:31 +0000911Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000912 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +0000913 if (ParseAttributesAfterDeclarator(D))
914 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Richard Smith02e85f32011-04-14 22:09:26 +0000916 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
917}
Mike Stump11289f42009-09-09 15:08:12 +0000918
Richard Smith02e85f32011-04-14 22:09:26 +0000919Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
920 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000921 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000922 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000923 switch (TemplateInfo.Kind) {
924 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000925 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000926 break;
927
928 case ParsedTemplateInfo::Template:
929 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000930 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000931 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000932 TemplateInfo.TemplateParams->data(),
933 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000934 D);
935 break;
936
937 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000938 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000939 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000940 TemplateInfo.ExternLoc,
941 TemplateInfo.TemplateLoc,
942 D);
943 if (ThisRes.isInvalid()) {
944 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000945 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000946 }
947
948 ThisDecl = ThisRes.get();
949 break;
950 }
951 }
Mike Stump11289f42009-09-09 15:08:12 +0000952
Richard Smith30482bc2011-02-20 03:19:35 +0000953 bool TypeContainsAuto =
954 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
955
Douglas Gregor23996282009-05-12 21:31:51 +0000956 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000957 if (isTokenEqualOrMistypedEqualEqual(
958 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000959 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000960 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000961 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000962
963 if (!getLang().CPlusPlus0x)
964 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
965
Douglas Gregor23996282009-05-12 21:31:51 +0000966 Actions.SetDeclDeleted(ThisDecl, DelLoc);
967 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000968 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
969 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000970 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000971 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000972
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000973 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000974 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000975 ConsumeCodeCompletionToken();
976 SkipUntil(tok::comma, true, true);
977 return ThisDecl;
978 }
979
John McCalldadc5752010-08-24 06:29:42 +0000980 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000981
John McCall1f4ee7b2009-12-19 09:28:58 +0000982 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000983 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000984 ExitScope();
985 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000986
Douglas Gregor23996282009-05-12 21:31:51 +0000987 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000988 SkipUntil(tok::comma, true, true);
989 Actions.ActOnInitializerError(ThisDecl);
990 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000991 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
992 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000993 }
994 } else if (Tok.is(tok::l_paren)) {
995 // Parse C++ direct initializer: '(' expression-list ')'
996 SourceLocation LParenLoc = ConsumeParen();
997 ExprVector Exprs(Actions);
998 CommaLocsTy CommaLocs;
999
Douglas Gregor613bf102009-12-22 17:47:17 +00001000 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1001 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001002 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001003 }
1004
Douglas Gregor23996282009-05-12 21:31:51 +00001005 if (ParseExpressionList(Exprs, CommaLocs)) {
1006 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001007
1008 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001009 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001010 ExitScope();
1011 }
Douglas Gregor23996282009-05-12 21:31:51 +00001012 } else {
1013 // Match the ')'.
1014 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1015
1016 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1017 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001018
1019 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001020 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001021 ExitScope();
1022 }
1023
Douglas Gregor23996282009-05-12 21:31:51 +00001024 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1025 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001026 RParenLoc,
1027 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001028 }
1029 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001030 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001031 }
1032
Richard Smithb2bc2e62011-02-21 20:05:19 +00001033 Actions.FinalizeDeclaration(ThisDecl);
1034
Douglas Gregor23996282009-05-12 21:31:51 +00001035 return ThisDecl;
1036}
1037
Chris Lattner1890ac82006-08-13 01:16:23 +00001038/// ParseSpecifierQualifierList
1039/// specifier-qualifier-list:
1040/// type-specifier specifier-qualifier-list[opt]
1041/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001042/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001043///
1044void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1045 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1046 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +00001047 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattner1890ac82006-08-13 01:16:23 +00001049 // Validate declspec for type-name.
1050 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001051 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001052 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001053 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001054
Chris Lattner1b22eed2006-11-28 05:12:07 +00001055 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001056 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001057 if (DS.getStorageClassSpecLoc().isValid())
1058 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1059 else
1060 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001061 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001062 }
Mike Stump11289f42009-09-09 15:08:12 +00001063
Chris Lattner1b22eed2006-11-28 05:12:07 +00001064 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001065 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001066 if (DS.isInlineSpecified())
1067 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1068 if (DS.isVirtualSpecified())
1069 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1070 if (DS.isExplicitSpecified())
1071 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001072 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001073 }
1074}
Chris Lattner53361ac2006-08-10 05:19:57 +00001075
Chris Lattner6cc055a2009-04-12 20:42:31 +00001076/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1077/// specified token is valid after the identifier in a declarator which
1078/// immediately follows the declspec. For example, these things are valid:
1079///
1080/// int x [ 4]; // direct-declarator
1081/// int x ( int y); // direct-declarator
1082/// int(int x ) // direct-declarator
1083/// int x ; // simple-declaration
1084/// int x = 17; // init-declarator-list
1085/// int x , y; // init-declarator-list
1086/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001087/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001088/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001089///
1090/// This is not, because 'x' does not immediately follow the declspec (though
1091/// ')' happens to be valid anyway).
1092/// int (x)
1093///
1094static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1095 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1096 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001097 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001098}
1099
Chris Lattner20a0c612009-04-14 21:34:55 +00001100
1101/// ParseImplicitInt - This method is called when we have an non-typename
1102/// identifier in a declspec (which normally terminates the decl spec) when
1103/// the declspec has no type specifier. In this case, the declspec is either
1104/// malformed or is "implicit int" (in K&R and C89).
1105///
1106/// This method handles diagnosing this prettily and returns false if the
1107/// declspec is done being processed. If it recovers and thinks there may be
1108/// other pieces of declspec after it, it returns true.
1109///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001110bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001111 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001112 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001113 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001114
Chris Lattner20a0c612009-04-14 21:34:55 +00001115 SourceLocation Loc = Tok.getLocation();
1116 // If we see an identifier that is not a type name, we normally would
1117 // parse it as the identifer being declared. However, when a typename
1118 // is typo'd or the definition is not included, this will incorrectly
1119 // parse the typename as the identifier name and fall over misparsing
1120 // later parts of the diagnostic.
1121 //
1122 // As such, we try to do some look-ahead in cases where this would
1123 // otherwise be an "implicit-int" case to see if this is invalid. For
1124 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1125 // an identifier with implicit int, we'd get a parse error because the
1126 // next token is obviously invalid for a type. Parse these as a case
1127 // with an invalid type specifier.
1128 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattner20a0c612009-04-14 21:34:55 +00001130 // Since we know that this either implicit int (which is rare) or an
1131 // error, we'd do lookahead to try to do better recovery.
1132 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1133 // If this token is valid for implicit int, e.g. "static x = 4", then
1134 // we just avoid eating the identifier, so it will be parsed as the
1135 // identifier in the declarator.
1136 return false;
1137 }
Mike Stump11289f42009-09-09 15:08:12 +00001138
Chris Lattner20a0c612009-04-14 21:34:55 +00001139 // Otherwise, if we don't consume this token, we are going to emit an
1140 // error anyway. Try to recover from various common problems. Check
1141 // to see if this was a reference to a tag name without a tag specified.
1142 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001143 //
1144 // C++ doesn't need this, and isTagName doesn't take SS.
1145 if (SS == 0) {
1146 const char *TagName = 0;
1147 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregor0be31a22010-07-02 17:43:08 +00001149 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001150 default: break;
1151 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
1152 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
1153 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
1154 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
1155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001157 if (TagName) {
1158 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001159 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00001160 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +00001161
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001162 // Parse this as a tag as if the missing tag were present.
1163 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001164 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001165 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001166 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001167 return true;
1168 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001169 }
Mike Stump11289f42009-09-09 15:08:12 +00001170
Douglas Gregor15e56022009-10-13 23:27:22 +00001171 // This is almost certainly an invalid type name. Let the action emit a
1172 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001173 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001174 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001175 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001176 // The action emitted a diagnostic, so we don't have to.
1177 if (T) {
1178 // The action has suggested that the type T could be used. Set that as
1179 // the type in the declaration specifiers, consume the would-be type
1180 // name token, and we're done.
1181 const char *PrevSpec;
1182 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001183 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001184 DS.SetRangeEnd(Tok.getLocation());
1185 ConsumeToken();
1186
1187 // There may be other declaration specifiers after this.
1188 return true;
1189 }
1190
1191 // Fall through; the action had no suggestion for us.
1192 } else {
1193 // The action did not emit a diagnostic, so emit one now.
1194 SourceRange R;
1195 if (SS) R = SS->getRange();
1196 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Douglas Gregor15e56022009-10-13 23:27:22 +00001199 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001200 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001201 unsigned DiagID;
1202 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001203 DS.SetRangeEnd(Tok.getLocation());
1204 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001205
Chris Lattner20a0c612009-04-14 21:34:55 +00001206 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1207 // avoid rippling error messages on subsequent uses of the same type,
1208 // could be useful if #include was forgotten.
1209 return false;
1210}
1211
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001212/// \brief Determine the declaration specifier context from the declarator
1213/// context.
1214///
1215/// \param Context the declarator context, which is one of the
1216/// Declarator::TheContext enumerator values.
1217Parser::DeclSpecContext
1218Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1219 if (Context == Declarator::MemberContext)
1220 return DSC_class;
1221 if (Context == Declarator::FileContext)
1222 return DSC_top_level;
1223 return DSC_normal;
1224}
1225
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001226/// ParseDeclarationSpecifiers
1227/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001228/// storage-class-specifier declaration-specifiers[opt]
1229/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001230/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001231/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001232///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001233/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001234/// 'typedef'
1235/// 'extern'
1236/// 'static'
1237/// 'auto'
1238/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001239/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001240/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001241/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001242/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001243/// [C++] 'virtual'
1244/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001245/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001246/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001247/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001248
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001249///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001250void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001251 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001252 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001253 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +00001254 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +00001255 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001256 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001257 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001258 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001259 unsigned DiagID = 0;
1260
Chris Lattner4d8f8732006-11-28 05:05:08 +00001261 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001262
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001263 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001264 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001265 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001266 // If this is not a declaration specifier token, we're done reading decl
1267 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001268 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001269 return;
Mike Stump11289f42009-09-09 15:08:12 +00001270
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001271 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001272 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001273 if (DS.hasTypeSpecifier()) {
1274 bool AllowNonIdentifiers
1275 = (getCurScope()->getFlags() & (Scope::ControlScope |
1276 Scope::BlockScope |
1277 Scope::TemplateParamScope |
1278 Scope::FunctionPrototypeScope |
1279 Scope::AtCatchScope)) == 0;
1280 bool AllowNestedNameSpecifiers
1281 = DSContext == DSC_top_level ||
1282 (DSContext == DSC_class && DS.isFriendSpecified());
1283
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001284 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1285 AllowNonIdentifiers,
1286 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001287 ConsumeCodeCompletionToken();
1288 return;
1289 }
1290
Douglas Gregor80039242011-02-15 20:33:25 +00001291 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1292 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1293 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001294 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1295 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001296 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001297 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001298 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001299 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001300
1301 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1302 ConsumeCodeCompletionToken();
1303 return;
1304 }
1305
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001306 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001307 // C++ scope specifier. Annotate and loop, or bail out on error.
1308 if (TryAnnotateCXXScopeToken(true)) {
1309 if (!DS.hasTypeSpecifier())
1310 DS.SetTypeSpecError();
1311 goto DoneWithDeclSpec;
1312 }
John McCall8bc2a702010-03-01 18:20:46 +00001313 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1314 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001315 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001316
1317 case tok::annot_cxxscope: {
1318 if (DS.hasTypeSpecifier())
1319 goto DoneWithDeclSpec;
1320
John McCall9dab4e62009-12-12 11:40:51 +00001321 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001322 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1323 Tok.getAnnotationRange(),
1324 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001325
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001326 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001327 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001328 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001329 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001330 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001331 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001332
1333 // C++ [class.qual]p2:
1334 // In a lookup in which the constructor is an acceptable lookup
1335 // result and the nested-name-specifier nominates a class C:
1336 //
1337 // - if the name specified after the
1338 // nested-name-specifier, when looked up in C, is the
1339 // injected-class-name of C (Clause 9), or
1340 //
1341 // - if the name specified after the nested-name-specifier
1342 // is the same as the identifier or the
1343 // simple-template-id's template-name in the last
1344 // component of the nested-name-specifier,
1345 //
1346 // the name is instead considered to name the constructor of
1347 // class C.
1348 //
1349 // Thus, if the template-name is actually the constructor
1350 // name, then the code is ill-formed; this interpretation is
1351 // reinforced by the NAD status of core issue 635.
1352 TemplateIdAnnotation *TemplateId
1353 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001354 if ((DSContext == DSC_top_level ||
1355 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1356 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001357 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001358 if (isConstructorDeclarator()) {
1359 // The user meant this to be an out-of-line constructor
1360 // definition, but template arguments are not allowed
1361 // there. Just allow this as a constructor; we'll
1362 // complain about it later.
1363 goto DoneWithDeclSpec;
1364 }
1365
1366 // The user meant this to name a type, but it actually names
1367 // a constructor with some extraneous template
1368 // arguments. Complain, then parse it as a type as the user
1369 // intended.
1370 Diag(TemplateId->TemplateNameLoc,
1371 diag::err_out_of_line_template_id_names_constructor)
1372 << TemplateId->Name;
1373 }
1374
John McCall9dab4e62009-12-12 11:40:51 +00001375 DS.getTypeSpecScope() = SS;
1376 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001377 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001378 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001379 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001380 continue;
1381 }
1382
Douglas Gregorc5790df2009-09-28 07:26:33 +00001383 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001384 DS.getTypeSpecScope() = SS;
1385 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001386 if (Tok.getAnnotationValue()) {
1387 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1389 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001390 PrevSpec, DiagID, T);
1391 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001392 else
1393 DS.SetTypeSpecError();
1394 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1395 ConsumeToken(); // The typename
1396 }
1397
Douglas Gregor167fa622009-03-25 15:40:00 +00001398 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001399 goto DoneWithDeclSpec;
1400
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001401 // If we're in a context where the identifier could be a class name,
1402 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001403 if ((DSContext == DSC_top_level ||
1404 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001405 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001406 &SS)) {
1407 if (isConstructorDeclarator())
1408 goto DoneWithDeclSpec;
1409
1410 // As noted in C++ [class.qual]p2 (cited above), when the name
1411 // of the class is qualified in a context where it could name
1412 // a constructor, its a constructor name. However, we've
1413 // looked at the declarator, and the user probably meant this
1414 // to be a type. Complain that it isn't supposed to be treated
1415 // as a type, then proceed to parse it as a type.
1416 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1417 << Next.getIdentifierInfo();
1418 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001419
John McCallba7bf592010-08-24 05:47:05 +00001420 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1421 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001422 getCurScope(), &SS,
1423 false, false, ParsedType(),
1424 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001425
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001426 // If the referenced identifier is not a type, then this declspec is
1427 // erroneous: We already checked about that it has no type specifier, and
1428 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001429 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001430 if (TypeRep == 0) {
1431 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001432 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001433 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001434 }
Mike Stump11289f42009-09-09 15:08:12 +00001435
John McCall9dab4e62009-12-12 11:40:51 +00001436 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001437 ConsumeToken(); // The C++ scope.
1438
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001439 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001440 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001441 if (isInvalid)
1442 break;
Mike Stump11289f42009-09-09 15:08:12 +00001443
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001444 DS.SetRangeEnd(Tok.getLocation());
1445 ConsumeToken(); // The typename.
1446
1447 continue;
1448 }
Mike Stump11289f42009-09-09 15:08:12 +00001449
Chris Lattnere387d9e2009-01-21 19:48:37 +00001450 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001451 if (Tok.getAnnotationValue()) {
1452 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001453 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001454 DiagID, T);
1455 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001456 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001457
1458 if (isInvalid)
1459 break;
1460
Chris Lattnere387d9e2009-01-21 19:48:37 +00001461 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1462 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001463
Chris Lattnere387d9e2009-01-21 19:48:37 +00001464 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1465 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001466 // Objective-C interface.
1467 if (Tok.is(tok::less) && getLang().ObjC1)
1468 ParseObjCProtocolQualifiers(DS);
1469
Chris Lattnere387d9e2009-01-21 19:48:37 +00001470 continue;
1471 }
Mike Stump11289f42009-09-09 15:08:12 +00001472
Chris Lattner16fac4f2008-07-26 01:18:38 +00001473 // typedef-name
1474 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001475 // In C++, check to see if this is a scope specifier like foo::bar::, if
1476 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001477 if (getLang().CPlusPlus) {
1478 if (TryAnnotateCXXScopeToken(true)) {
1479 if (!DS.hasTypeSpecifier())
1480 DS.SetTypeSpecError();
1481 goto DoneWithDeclSpec;
1482 }
1483 if (!Tok.is(tok::identifier))
1484 continue;
1485 }
Mike Stump11289f42009-09-09 15:08:12 +00001486
Chris Lattner16fac4f2008-07-26 01:18:38 +00001487 // This identifier can only be a typedef name if we haven't already seen
1488 // a type-specifier. Without this check we misparse:
1489 // typedef int X; struct Y { short X; }; as 'short int'.
1490 if (DS.hasTypeSpecifier())
1491 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001492
John Thompson22334602010-02-05 00:12:22 +00001493 // Check for need to substitute AltiVec keyword tokens.
1494 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1495 break;
1496
Chris Lattner16fac4f2008-07-26 01:18:38 +00001497 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001498 ParsedType TypeRep =
1499 Actions.getTypeName(*Tok.getIdentifierInfo(),
1500 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001501
Chris Lattner6cc055a2009-04-12 20:42:31 +00001502 // If this is not a typedef name, don't parse it as part of the declspec,
1503 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001504 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001505 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001506 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001507 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001508
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001509 // If we're in a context where the identifier could be a class name,
1510 // check whether this is a constructor declaration.
1511 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001512 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001513 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001514 goto DoneWithDeclSpec;
1515
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001517 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001518 if (isInvalid)
1519 break;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Chris Lattner16fac4f2008-07-26 01:18:38 +00001521 DS.SetRangeEnd(Tok.getLocation());
1522 ConsumeToken(); // The identifier
1523
1524 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1525 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001526 // Objective-C interface.
1527 if (Tok.is(tok::less) && getLang().ObjC1)
1528 ParseObjCProtocolQualifiers(DS);
1529
Steve Naroffcd5e7822008-09-22 10:28:57 +00001530 // Need to support trailing type qualifiers (e.g. "id<p> const").
1531 // If a type specifier follows, it will be diagnosed elsewhere.
1532 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001533 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001534
1535 // type-name
1536 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001537 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001538 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001539 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001540 // This template-id does not refer to a type name, so we're
1541 // done with the type-specifiers.
1542 goto DoneWithDeclSpec;
1543 }
1544
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001545 // If we're in a context where the template-id could be a
1546 // constructor name or specialization, check whether this is a
1547 // constructor declaration.
1548 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001549 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001550 isConstructorDeclarator())
1551 goto DoneWithDeclSpec;
1552
Douglas Gregor7f741122009-02-25 19:37:18 +00001553 // Turn the template-id annotation token into a type annotation
1554 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001555 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001556 continue;
1557 }
1558
Chris Lattnere37e2332006-08-15 04:50:22 +00001559 // GNU attributes support.
1560 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001561 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001562 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001563
1564 // Microsoft declspec support.
1565 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001566 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001567 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001568
Steve Naroff44ac7772008-12-25 14:16:32 +00001569 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001570 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001571 // FIXME: Add handling here!
1572 break;
1573
1574 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001575 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001576 case tok::kw___cdecl:
1577 case tok::kw___stdcall:
1578 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001579 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001580 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001581 continue;
1582
Dawn Perchik335e16b2010-09-03 01:29:35 +00001583 // Borland single token adornments.
1584 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001585 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001586 continue;
1587
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001588 // OpenCL single token adornments.
1589 case tok::kw___kernel:
1590 ParseOpenCLAttributes(DS.getAttributes());
1591 continue;
1592
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001593 // storage-class-specifier
1594 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001595 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001596 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001597 break;
1598 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001599 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001600 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001601 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001602 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001603 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001604 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001605 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001606 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001607 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001608 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001609 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001610 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001611 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001612 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001613 break;
1614 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001615 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001616 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1617 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1618 DiagID, getLang());
1619 if (!isInvalid)
1620 Diag(Tok, diag::auto_storage_class)
1621 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1622 }
1623 else
1624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1625 DiagID);
1626 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001627 else
John McCall49bfce42009-08-03 20:12:06 +00001628 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001629 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001630 break;
1631 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001632 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001633 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001634 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001635 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001636 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001637 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001638 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001639 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001640 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001641 break;
Mike Stump11289f42009-09-09 15:08:12 +00001642
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001643 // function-specifier
1644 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001645 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001646 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001647 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001648 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001649 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001650 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001651 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001652 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001653
Anders Carlssoncd8db412009-05-06 04:46:28 +00001654 // friend
1655 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001656 if (DSContext == DSC_class)
1657 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1658 else {
1659 PrevSpec = ""; // not actually used by the diagnostic
1660 DiagID = diag::err_friend_invalid_in_context;
1661 isInvalid = true;
1662 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001663 break;
Mike Stump11289f42009-09-09 15:08:12 +00001664
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001665 // constexpr
1666 case tok::kw_constexpr:
1667 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1668 break;
1669
Chris Lattnere387d9e2009-01-21 19:48:37 +00001670 // type-specifier
1671 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001672 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1673 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001674 break;
1675 case tok::kw_long:
1676 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001677 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1678 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001679 else
John McCall49bfce42009-08-03 20:12:06 +00001680 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1681 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001682 break;
1683 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001684 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1685 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001686 break;
1687 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001688 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1689 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001690 break;
1691 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001692 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1693 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001694 break;
1695 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001696 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1697 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001698 break;
1699 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1701 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001702 break;
1703 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1705 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001706 break;
1707 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1709 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001710 break;
1711 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1713 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001714 break;
1715 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1717 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001718 break;
1719 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1721 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001722 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001723 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1725 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001726 break;
1727 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1729 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001730 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001731 case tok::kw_bool:
1732 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001733 if (Tok.is(tok::kw_bool) &&
1734 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1735 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1736 PrevSpec = ""; // Not used by the diagnostic.
1737 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001738 // For better error recovery.
1739 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001740 isInvalid = true;
1741 } else {
1742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1743 DiagID);
1744 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001745 break;
1746 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1748 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001749 break;
1750 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1752 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001753 break;
1754 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1756 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001757 break;
John Thompson22334602010-02-05 00:12:22 +00001758 case tok::kw___vector:
1759 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1760 break;
1761 case tok::kw___pixel:
1762 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1763 break;
John McCall39439732011-04-09 22:50:59 +00001764 case tok::kw___unknown_anytype:
1765 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1766 PrevSpec, DiagID);
1767 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001768
1769 // class-specifier:
1770 case tok::kw_class:
1771 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001772 case tok::kw_union: {
1773 tok::TokenKind Kind = Tok.getKind();
1774 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001775 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001776 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001777 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001778
1779 // enum-specifier:
1780 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001781 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001782 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001783 continue;
1784
1785 // cv-qualifier:
1786 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001787 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1788 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001789 break;
1790 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001791 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1792 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001793 break;
1794 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001795 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1796 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001797 break;
1798
Douglas Gregor333489b2009-03-27 23:10:48 +00001799 // C++ typename-specifier:
1800 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001801 if (TryAnnotateTypeOrScopeToken()) {
1802 DS.SetTypeSpecError();
1803 goto DoneWithDeclSpec;
1804 }
1805 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001806 continue;
1807 break;
1808
Chris Lattnere387d9e2009-01-21 19:48:37 +00001809 // GNU typeof support.
1810 case tok::kw_typeof:
1811 ParseTypeofSpecifier(DS);
1812 continue;
1813
Anders Carlsson74948d02009-06-24 17:47:40 +00001814 case tok::kw_decltype:
1815 ParseDecltypeSpecifier(DS);
1816 continue;
1817
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001818 // OpenCL qualifiers:
1819 case tok::kw_private:
1820 if (!getLang().OpenCL)
1821 goto DoneWithDeclSpec;
1822 case tok::kw___private:
1823 case tok::kw___global:
1824 case tok::kw___local:
1825 case tok::kw___constant:
1826 case tok::kw___read_only:
1827 case tok::kw___write_only:
1828 case tok::kw___read_write:
1829 ParseOpenCLQualifiers(DS);
1830 break;
1831
Steve Naroffcfdf6162008-06-05 00:02:44 +00001832 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001833 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001834 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1835 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001836 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001837 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001838
Douglas Gregor3a001f42010-11-19 17:10:50 +00001839 if (!ParseObjCProtocolQualifiers(DS))
1840 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1841 << FixItHint::CreateInsertion(Loc, "id")
1842 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001843
1844 // Need to support trailing type qualifiers (e.g. "id<p> const").
1845 // If a type specifier follows, it will be diagnosed elsewhere.
1846 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001847 }
John McCall49bfce42009-08-03 20:12:06 +00001848 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001849 if (isInvalid) {
1850 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001851 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001852
1853 if (DiagID == diag::ext_duplicate_declspec)
1854 Diag(Tok, DiagID)
1855 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1856 else
1857 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001858 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001859
Chris Lattner2e232092008-03-13 06:29:04 +00001860 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001861 if (DiagID != diag::err_bool_redeclaration)
1862 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001863 }
1864}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001865
Chris Lattnera448d752009-01-06 06:59:53 +00001866/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001867/// primarily follow the C++ grammar with additions for C99 and GNU,
1868/// which together subsume the C grammar. Note that the C++
1869/// type-specifier also includes the C type-qualifier (for const,
1870/// volatile, and C99 restrict). Returns true if a type-specifier was
1871/// found (and parsed), false otherwise.
1872///
1873/// type-specifier: [C++ 7.1.5]
1874/// simple-type-specifier
1875/// class-specifier
1876/// enum-specifier
1877/// elaborated-type-specifier [TODO]
1878/// cv-qualifier
1879///
1880/// cv-qualifier: [C++ 7.1.5.1]
1881/// 'const'
1882/// 'volatile'
1883/// [C99] 'restrict'
1884///
1885/// simple-type-specifier: [ C++ 7.1.5.2]
1886/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1887/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1888/// 'char'
1889/// 'wchar_t'
1890/// 'bool'
1891/// 'short'
1892/// 'int'
1893/// 'long'
1894/// 'signed'
1895/// 'unsigned'
1896/// 'float'
1897/// 'double'
1898/// 'void'
1899/// [C99] '_Bool'
1900/// [C99] '_Complex'
1901/// [C99] '_Imaginary' // Removed in TC2?
1902/// [GNU] '_Decimal32'
1903/// [GNU] '_Decimal64'
1904/// [GNU] '_Decimal128'
1905/// [GNU] typeof-specifier
1906/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1907/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001908/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001909/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001910bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001911 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001912 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001913 const ParsedTemplateInfo &TemplateInfo,
1914 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001915 SourceLocation Loc = Tok.getLocation();
1916
1917 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001918 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001919 // If we already have a type specifier, this identifier is not a type.
1920 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1921 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1922 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1923 return false;
John Thompson22334602010-02-05 00:12:22 +00001924 // Check for need to substitute AltiVec keyword tokens.
1925 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1926 break;
1927 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001928 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001929 // Annotate typenames and C++ scope specifiers. If we get one, just
1930 // recurse to handle whatever we get.
1931 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001932 return true;
1933 if (Tok.is(tok::identifier))
1934 return false;
1935 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1936 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001937 case tok::coloncolon: // ::foo::bar
1938 if (NextToken().is(tok::kw_new) || // ::new
1939 NextToken().is(tok::kw_delete)) // ::delete
1940 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001941
Chris Lattner020bab92009-01-04 23:41:41 +00001942 // Annotate typenames and C++ scope specifiers. If we get one, just
1943 // recurse to handle whatever we get.
1944 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001945 return true;
1946 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1947 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001948
Douglas Gregor450c75a2008-11-07 15:42:26 +00001949 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001950 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001951 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001952 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1953 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001954 DiagID, T);
1955 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001956 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001957 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1958 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001959
Douglas Gregor450c75a2008-11-07 15:42:26 +00001960 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1961 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1962 // Objective-C interface. If we don't have Objective-C or a '<', this is
1963 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001964 if (Tok.is(tok::less) && getLang().ObjC1)
1965 ParseObjCProtocolQualifiers(DS);
1966
Douglas Gregor450c75a2008-11-07 15:42:26 +00001967 return true;
1968 }
1969
1970 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001971 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001972 break;
1973 case tok::kw_long:
1974 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001975 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1976 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001977 else
John McCall49bfce42009-08-03 20:12:06 +00001978 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1979 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001980 break;
1981 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001982 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001983 break;
1984 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001985 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1986 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001987 break;
1988 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001989 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1990 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001991 break;
1992 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001993 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1994 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001995 break;
1996 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001997 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001998 break;
1999 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002000 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002001 break;
2002 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002003 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002004 break;
2005 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002006 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002007 break;
2008 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002009 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002010 break;
2011 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002013 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002014 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002016 break;
2017 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002018 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002019 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002020 case tok::kw_bool:
2021 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002023 break;
2024 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2026 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002027 break;
2028 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2030 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002031 break;
2032 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002033 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2034 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002035 break;
John Thompson22334602010-02-05 00:12:22 +00002036 case tok::kw___vector:
2037 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2038 break;
2039 case tok::kw___pixel:
2040 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2041 break;
2042
Douglas Gregor450c75a2008-11-07 15:42:26 +00002043 // class-specifier:
2044 case tok::kw_class:
2045 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002046 case tok::kw_union: {
2047 tok::TokenKind Kind = Tok.getKind();
2048 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002049 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2050 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002051 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002052 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002053
2054 // enum-specifier:
2055 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002056 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002057 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002058 return true;
2059
2060 // cv-qualifier:
2061 case tok::kw_const:
2062 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002063 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002064 break;
2065 case tok::kw_volatile:
2066 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002067 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002068 break;
2069 case tok::kw_restrict:
2070 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002071 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002072 break;
2073
2074 // GNU typeof support.
2075 case tok::kw_typeof:
2076 ParseTypeofSpecifier(DS);
2077 return true;
2078
Anders Carlsson74948d02009-06-24 17:47:40 +00002079 // C++0x decltype support.
2080 case tok::kw_decltype:
2081 ParseDecltypeSpecifier(DS);
2082 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002083
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002084 // OpenCL qualifiers:
2085 case tok::kw_private:
2086 if (!getLang().OpenCL)
2087 return false;
2088 case tok::kw___private:
2089 case tok::kw___global:
2090 case tok::kw___local:
2091 case tok::kw___constant:
2092 case tok::kw___read_only:
2093 case tok::kw___write_only:
2094 case tok::kw___read_write:
2095 ParseOpenCLQualifiers(DS);
2096 break;
2097
Anders Carlssonbae27372009-06-26 23:44:14 +00002098 // C++0x auto support.
2099 case tok::kw_auto:
2100 if (!getLang().CPlusPlus0x)
2101 return false;
2102
John McCall49bfce42009-08-03 20:12:06 +00002103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002104 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002105
Eli Friedman53339e02009-06-08 23:27:34 +00002106 case tok::kw___ptr64:
2107 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002108 case tok::kw___cdecl:
2109 case tok::kw___stdcall:
2110 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002111 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002112 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002113 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002114
Dawn Perchik335e16b2010-09-03 01:29:35 +00002115 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002116 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002117 return true;
2118
Douglas Gregor450c75a2008-11-07 15:42:26 +00002119 default:
2120 // Not a type-specifier; do nothing.
2121 return false;
2122 }
2123
2124 // If the specifier combination wasn't legal, issue a diagnostic.
2125 if (isInvalid) {
2126 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002127 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002128 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002129 }
2130 DS.SetRangeEnd(Tok.getLocation());
2131 ConsumeToken(); // whatever we parsed above.
2132 return true;
2133}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002134
Chris Lattner70ae4912007-10-29 04:42:53 +00002135/// ParseStructDeclaration - Parse a struct declaration without the terminating
2136/// semicolon.
2137///
Chris Lattner90a26b02007-01-23 04:38:16 +00002138/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002139/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002140/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002141/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002142/// struct-declarator-list:
2143/// struct-declarator
2144/// struct-declarator-list ',' struct-declarator
2145/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2146/// struct-declarator:
2147/// declarator
2148/// [GNU] declarator attributes[opt]
2149/// declarator[opt] ':' constant-expression
2150/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2151///
Chris Lattnera12405b2008-04-10 06:46:29 +00002152void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002153ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002154 if (Tok.is(tok::kw___extension__)) {
2155 // __extension__ silences extension warnings in the subexpression.
2156 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002157 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002158 return ParseStructDeclaration(DS, Fields);
2159 }
Mike Stump11289f42009-09-09 15:08:12 +00002160
Steve Naroff97170802007-08-20 22:28:22 +00002161 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002162 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002163
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002164 // If there are no declarators, this is a free-standing declaration
2165 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002166 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002167 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002168 return;
2169 }
2170
2171 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002172 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002173 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002174 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002175 FieldDeclarator DeclaratorInfo(DS);
2176
2177 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002178 if (!FirstDeclarator)
2179 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002180
Steve Naroff97170802007-08-20 22:28:22 +00002181 /// struct-declarator: declarator
2182 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002183 if (Tok.isNot(tok::colon)) {
2184 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2185 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002186 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002187 }
Mike Stump11289f42009-09-09 15:08:12 +00002188
Chris Lattner76c72282007-10-09 17:33:22 +00002189 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002190 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002191 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002192 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002193 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002194 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002195 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002196 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002197
Steve Naroff97170802007-08-20 22:28:22 +00002198 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002199 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002200
John McCallcfefb6d2009-11-03 02:38:08 +00002201 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002202 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002203 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002204
Steve Naroff97170802007-08-20 22:28:22 +00002205 // If we don't have a comma, it is either the end of the list (a ';')
2206 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002207 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002208 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002209
Steve Naroff97170802007-08-20 22:28:22 +00002210 // Consume the comma.
2211 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002212
John McCallcfefb6d2009-11-03 02:38:08 +00002213 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002214 }
Steve Naroff97170802007-08-20 22:28:22 +00002215}
2216
2217/// ParseStructUnionBody
2218/// struct-contents:
2219/// struct-declaration-list
2220/// [EXT] empty
2221/// [GNU] "struct-declaration-list" without terminatoring ';'
2222/// struct-declaration-list:
2223/// struct-declaration
2224/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002225/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002226///
Chris Lattner1300fb92007-01-23 23:42:53 +00002227void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002228 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002229 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2230 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002231
Chris Lattner90a26b02007-01-23 04:38:16 +00002232 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002233
Douglas Gregor658b9552009-01-09 22:42:13 +00002234 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002235 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002236
Chris Lattner7b9ace62007-01-23 20:11:08 +00002237 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2238 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002239 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002240 Diag(Tok, diag::ext_empty_struct_union)
2241 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002242
John McCall48871652010-08-21 09:40:31 +00002243 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002244
Chris Lattner7b9ace62007-01-23 20:11:08 +00002245 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002246 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002247 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002248
Chris Lattner736ed5d2007-06-09 05:59:07 +00002249 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002250 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002251 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002252 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002253 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002254 ConsumeToken();
2255 continue;
2256 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002257
2258 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002259 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002260
John McCallcfefb6d2009-11-03 02:38:08 +00002261 if (!Tok.is(tok::at)) {
2262 struct CFieldCallback : FieldCallback {
2263 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002264 Decl *TagDecl;
2265 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002266
John McCall48871652010-08-21 09:40:31 +00002267 CFieldCallback(Parser &P, Decl *TagDecl,
2268 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002269 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2270
John McCall48871652010-08-21 09:40:31 +00002271 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002272 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002273 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002274 FD.D.getDeclSpec().getSourceRange().getBegin(),
2275 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002276 FieldDecls.push_back(Field);
2277 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002278 }
John McCallcfefb6d2009-11-03 02:38:08 +00002279 } Callback(*this, TagDecl, FieldDecls);
2280
2281 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002282 } else { // Handle @defs
2283 ConsumeToken();
2284 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2285 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002286 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002287 continue;
2288 }
2289 ConsumeToken();
2290 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2291 if (!Tok.is(tok::identifier)) {
2292 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002293 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002294 continue;
2295 }
John McCall48871652010-08-21 09:40:31 +00002296 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002297 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002298 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002299 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2300 ConsumeToken();
2301 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002302 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002303
Chris Lattner76c72282007-10-09 17:33:22 +00002304 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002305 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002306 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002307 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002308 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002309 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002310 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2311 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002312 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002313 // If we stopped at a ';', eat it.
2314 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002315 }
2316 }
Mike Stump11289f42009-09-09 15:08:12 +00002317
Steve Naroff33a1e802007-10-29 21:38:07 +00002318 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002319
John McCall084e83d2011-03-24 11:26:52 +00002320 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002321 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002322 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002323
Douglas Gregor0be31a22010-07-02 17:43:08 +00002324 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002325 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002326 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002327 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002328 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002329 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002330}
2331
Chris Lattner3b561a32006-08-13 00:12:11 +00002332/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002333/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002334/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002335///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002336/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2337/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002338/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002339/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002340///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002341/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2342/// [C++0x] enum-head '{' enumerator-list ',' '}'
2343///
2344/// enum-head: [C++0x]
2345/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2346/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2347///
2348/// enum-key: [C++0x]
2349/// 'enum'
2350/// 'enum' 'class'
2351/// 'enum' 'struct'
2352///
2353/// enum-base: [C++0x]
2354/// ':' type-specifier-seq
2355///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002356/// [C++] elaborated-type-specifier:
2357/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2358///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002359void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002360 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002361 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002362 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002363 if (Tok.is(tok::code_completion)) {
2364 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002365 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002366 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002367 }
2368
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002369 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002370 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002371 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002372
Abramo Bagnarad7548482010-05-19 21:37:53 +00002373 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002374 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002375 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002376 return;
2377
2378 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002379 Diag(Tok, diag::err_expected_ident);
2380 if (Tok.isNot(tok::l_brace)) {
2381 // Has no name and is not a definition.
2382 // Skip the rest of this declarator, up until the comma or semicolon.
2383 SkipUntil(tok::comma, true);
2384 return;
2385 }
2386 }
2387 }
Mike Stump11289f42009-09-09 15:08:12 +00002388
Douglas Gregora1aec292011-02-22 20:32:04 +00002389 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002390 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002391 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002392
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002393 if (getLang().CPlusPlus0x &&
2394 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002395 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002396 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2397 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002398 }
2399
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002400 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002401 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2402 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002403 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002404
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002405 // Skip the rest of this declarator, up until the comma or semicolon.
2406 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002407 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002408 }
Mike Stump11289f42009-09-09 15:08:12 +00002409
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002410 // If an identifier is present, consume and remember it.
2411 IdentifierInfo *Name = 0;
2412 SourceLocation NameLoc;
2413 if (Tok.is(tok::identifier)) {
2414 Name = Tok.getIdentifierInfo();
2415 NameLoc = ConsumeToken();
2416 }
Mike Stump11289f42009-09-09 15:08:12 +00002417
Douglas Gregor0bf31402010-10-08 23:50:27 +00002418 if (!Name && IsScopedEnum) {
2419 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2420 // declaration of a scoped enumeration.
2421 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2422 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002423 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002424 }
2425
2426 TypeResult BaseType;
2427
Douglas Gregord1f69f62010-12-01 17:42:47 +00002428 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002429 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002430 bool PossibleBitfield = false;
2431 if (getCurScope()->getFlags() & Scope::ClassScope) {
2432 // If we're in class scope, this can either be an enum declaration with
2433 // an underlying type, or a declaration of a bitfield member. We try to
2434 // use a simple disambiguation scheme first to catch the common cases
2435 // (integer literal, sizeof); if it's still ambiguous, we then consider
2436 // anything that's a simple-type-specifier followed by '(' as an
2437 // expression. This suffices because function types are not valid
2438 // underlying types anyway.
2439 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2440 // If the next token starts an expression, we know we're parsing a
2441 // bit-field. This is the common case.
2442 if (TPR == TPResult::True())
2443 PossibleBitfield = true;
2444 // If the next token starts a type-specifier-seq, it may be either a
2445 // a fixed underlying type or the start of a function-style cast in C++;
2446 // lookahead one more token to see if it's obvious that we have a
2447 // fixed underlying type.
2448 else if (TPR == TPResult::False() &&
2449 GetLookAheadToken(2).getKind() == tok::semi) {
2450 // Consume the ':'.
2451 ConsumeToken();
2452 } else {
2453 // We have the start of a type-specifier-seq, so we have to perform
2454 // tentative parsing to determine whether we have an expression or a
2455 // type.
2456 TentativeParsingAction TPA(*this);
2457
2458 // Consume the ':'.
2459 ConsumeToken();
2460
Douglas Gregora1aec292011-02-22 20:32:04 +00002461 if ((getLang().CPlusPlus &&
2462 isCXXDeclarationSpecifier() != TPResult::True()) ||
2463 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002464 // We'll parse this as a bitfield later.
2465 PossibleBitfield = true;
2466 TPA.Revert();
2467 } else {
2468 // We have a type-specifier-seq.
2469 TPA.Commit();
2470 }
2471 }
2472 } else {
2473 // Consume the ':'.
2474 ConsumeToken();
2475 }
2476
2477 if (!PossibleBitfield) {
2478 SourceRange Range;
2479 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002480
2481 if (!getLang().CPlusPlus0x)
2482 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2483 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002484 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002485 }
2486
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002487 // There are three options here. If we have 'enum foo;', then this is a
2488 // forward declaration. If we have 'enum foo {...' then this is a
2489 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2490 //
2491 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2492 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2493 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2494 //
John McCallfaf5fb42010-08-26 23:41:50 +00002495 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002496 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002497 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002498 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002499 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002500 else
John McCallfaf5fb42010-08-26 23:41:50 +00002501 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002502
2503 // enums cannot be templates, although they can be referenced from a
2504 // template.
2505 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002506 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002507 Diag(Tok, diag::err_enum_template);
2508
2509 // Skip the rest of this declarator, up until the comma or semicolon.
2510 SkipUntil(tok::comma, true);
2511 return;
2512 }
2513
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002514 if (!Name && TUK != Sema::TUK_Definition) {
2515 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2516
2517 // Skip the rest of this declarator, up until the comma or semicolon.
2518 SkipUntil(tok::comma, true);
2519 return;
2520 }
2521
Douglas Gregord6ab8742009-05-28 23:31:59 +00002522 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002523 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002524 const char *PrevSpec = 0;
2525 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002526 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002527 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002528 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002529 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002530 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002531 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002532
Douglas Gregorba41d012010-04-24 16:38:41 +00002533 if (IsDependent) {
2534 // This enum has a dependent nested-name-specifier. Handle it as a
2535 // dependent tag.
2536 if (!Name) {
2537 DS.SetTypeSpecError();
2538 Diag(Tok, diag::err_expected_type_name_after_typename);
2539 return;
2540 }
2541
Douglas Gregor0be31a22010-07-02 17:43:08 +00002542 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002543 TUK, SS, Name, StartLoc,
2544 NameLoc);
2545 if (Type.isInvalid()) {
2546 DS.SetTypeSpecError();
2547 return;
2548 }
2549
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002550 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2551 NameLoc.isValid() ? NameLoc : StartLoc,
2552 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002553 Diag(StartLoc, DiagID) << PrevSpec;
2554
2555 return;
2556 }
Mike Stump11289f42009-09-09 15:08:12 +00002557
John McCall48871652010-08-21 09:40:31 +00002558 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002559 // The action failed to produce an enumeration tag. If this is a
2560 // definition, consume the entire definition.
2561 if (Tok.is(tok::l_brace)) {
2562 ConsumeBrace();
2563 SkipUntil(tok::r_brace);
2564 }
2565
2566 DS.SetTypeSpecError();
2567 return;
2568 }
2569
Chris Lattner76c72282007-10-09 17:33:22 +00002570 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002571 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002572
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002573 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2574 NameLoc.isValid() ? NameLoc : StartLoc,
2575 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002576 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002577}
2578
Chris Lattnerc1915e22007-01-25 07:29:02 +00002579/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2580/// enumerator-list:
2581/// enumerator
2582/// enumerator-list ',' enumerator
2583/// enumerator:
2584/// enumeration-constant
2585/// enumeration-constant '=' constant-expression
2586/// enumeration-constant:
2587/// identifier
2588///
John McCall48871652010-08-21 09:40:31 +00002589void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002590 // Enter the scope of the enum body and start the definition.
2591 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002592 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002593
Chris Lattnerc1915e22007-01-25 07:29:02 +00002594 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002595
Chris Lattner37256fb2007-08-27 17:24:30 +00002596 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002597 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002598 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002599
John McCall48871652010-08-21 09:40:31 +00002600 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002601
John McCall48871652010-08-21 09:40:31 +00002602 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002603
Chris Lattnerc1915e22007-01-25 07:29:02 +00002604 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002605 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002606 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2607 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002608
John McCall811a0f52010-10-22 23:36:17 +00002609 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002610 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002611 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002612
Chris Lattnerc1915e22007-01-25 07:29:02 +00002613 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002614 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002615 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002616 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002617 AssignedVal = ParseConstantExpression();
2618 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002619 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002620 }
Mike Stump11289f42009-09-09 15:08:12 +00002621
Chris Lattnerc1915e22007-01-25 07:29:02 +00002622 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002623 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2624 LastEnumConstDecl,
2625 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002626 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002627 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002628 EnumConstantDecls.push_back(EnumConstDecl);
2629 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002630
Douglas Gregorce66d022010-09-07 14:51:08 +00002631 if (Tok.is(tok::identifier)) {
2632 // We're missing a comma between enumerators.
2633 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2634 Diag(Loc, diag::err_enumerator_list_missing_comma)
2635 << FixItHint::CreateInsertion(Loc, ", ");
2636 continue;
2637 }
2638
Chris Lattner76c72282007-10-09 17:33:22 +00002639 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002640 break;
2641 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002642
2643 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002644 !(getLang().C99 || getLang().CPlusPlus0x))
2645 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2646 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002647 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002648 }
Mike Stump11289f42009-09-09 15:08:12 +00002649
Chris Lattnerc1915e22007-01-25 07:29:02 +00002650 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002651 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002652
Chris Lattnerc1915e22007-01-25 07:29:02 +00002653 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002654 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002655 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002656
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002657 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2658 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002659 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002660
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002661 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002662 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002663}
Chris Lattner3b561a32006-08-13 00:12:11 +00002664
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002665/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002666/// start of a type-qualifier-list.
2667bool Parser::isTypeQualifier() const {
2668 switch (Tok.getKind()) {
2669 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002670
2671 // type-qualifier only in OpenCL
2672 case tok::kw_private:
2673 return getLang().OpenCL;
2674
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002675 // type-qualifier
2676 case tok::kw_const:
2677 case tok::kw_volatile:
2678 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002679 case tok::kw___private:
2680 case tok::kw___local:
2681 case tok::kw___global:
2682 case tok::kw___constant:
2683 case tok::kw___read_only:
2684 case tok::kw___read_write:
2685 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002686 return true;
2687 }
2688}
2689
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002690/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2691/// is definitely a type-specifier. Return false if it isn't part of a type
2692/// specifier or if we're not sure.
2693bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2694 switch (Tok.getKind()) {
2695 default: return false;
2696 // type-specifiers
2697 case tok::kw_short:
2698 case tok::kw_long:
2699 case tok::kw_signed:
2700 case tok::kw_unsigned:
2701 case tok::kw__Complex:
2702 case tok::kw__Imaginary:
2703 case tok::kw_void:
2704 case tok::kw_char:
2705 case tok::kw_wchar_t:
2706 case tok::kw_char16_t:
2707 case tok::kw_char32_t:
2708 case tok::kw_int:
2709 case tok::kw_float:
2710 case tok::kw_double:
2711 case tok::kw_bool:
2712 case tok::kw__Bool:
2713 case tok::kw__Decimal32:
2714 case tok::kw__Decimal64:
2715 case tok::kw__Decimal128:
2716 case tok::kw___vector:
2717
2718 // struct-or-union-specifier (C99) or class-specifier (C++)
2719 case tok::kw_class:
2720 case tok::kw_struct:
2721 case tok::kw_union:
2722 // enum-specifier
2723 case tok::kw_enum:
2724
2725 // typedef-name
2726 case tok::annot_typename:
2727 return true;
2728 }
2729}
2730
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002731/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002732/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002733bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002734 switch (Tok.getKind()) {
2735 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002736
Chris Lattner020bab92009-01-04 23:41:41 +00002737 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002738 if (TryAltiVecVectorToken())
2739 return true;
2740 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002741 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002742 // Annotate typenames and C++ scope specifiers. If we get one, just
2743 // recurse to handle whatever we get.
2744 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002745 return true;
2746 if (Tok.is(tok::identifier))
2747 return false;
2748 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002749
Chris Lattner020bab92009-01-04 23:41:41 +00002750 case tok::coloncolon: // ::foo::bar
2751 if (NextToken().is(tok::kw_new) || // ::new
2752 NextToken().is(tok::kw_delete)) // ::delete
2753 return false;
2754
Chris Lattner020bab92009-01-04 23:41:41 +00002755 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002756 return true;
2757 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002758
Chris Lattnere37e2332006-08-15 04:50:22 +00002759 // GNU attributes support.
2760 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002761 // GNU typeof support.
2762 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002763
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002764 // type-specifiers
2765 case tok::kw_short:
2766 case tok::kw_long:
2767 case tok::kw_signed:
2768 case tok::kw_unsigned:
2769 case tok::kw__Complex:
2770 case tok::kw__Imaginary:
2771 case tok::kw_void:
2772 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002773 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002774 case tok::kw_char16_t:
2775 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002776 case tok::kw_int:
2777 case tok::kw_float:
2778 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002779 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002780 case tok::kw__Bool:
2781 case tok::kw__Decimal32:
2782 case tok::kw__Decimal64:
2783 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002784 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002785
Chris Lattner861a2262008-04-13 18:59:07 +00002786 // struct-or-union-specifier (C99) or class-specifier (C++)
2787 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002788 case tok::kw_struct:
2789 case tok::kw_union:
2790 // enum-specifier
2791 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002792
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002793 // type-qualifier
2794 case tok::kw_const:
2795 case tok::kw_volatile:
2796 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002797
2798 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002799 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002800 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002801
Chris Lattner409bf7d2008-10-20 00:25:30 +00002802 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2803 case tok::less:
2804 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002805
Steve Naroff44ac7772008-12-25 14:16:32 +00002806 case tok::kw___cdecl:
2807 case tok::kw___stdcall:
2808 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002809 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002810 case tok::kw___w64:
2811 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002812 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002813
2814 case tok::kw___private:
2815 case tok::kw___local:
2816 case tok::kw___global:
2817 case tok::kw___constant:
2818 case tok::kw___read_only:
2819 case tok::kw___read_write:
2820 case tok::kw___write_only:
2821
Eli Friedman53339e02009-06-08 23:27:34 +00002822 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002823
2824 case tok::kw_private:
2825 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002826 }
2827}
2828
Chris Lattneracd58a32006-08-06 17:24:14 +00002829/// isDeclarationSpecifier() - Return true if the current token is part of a
2830/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002831///
2832/// \param DisambiguatingWithExpression True to indicate that the purpose of
2833/// this check is to disambiguate between an expression and a declaration.
2834bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002835 switch (Tok.getKind()) {
2836 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002837
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002838 case tok::kw_private:
2839 return getLang().OpenCL;
2840
Chris Lattner020bab92009-01-04 23:41:41 +00002841 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002842 // Unfortunate hack to support "Class.factoryMethod" notation.
2843 if (getLang().ObjC1 && NextToken().is(tok::period))
2844 return false;
John Thompson22334602010-02-05 00:12:22 +00002845 if (TryAltiVecVectorToken())
2846 return true;
2847 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002848 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002849 // Annotate typenames and C++ scope specifiers. If we get one, just
2850 // recurse to handle whatever we get.
2851 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002852 return true;
2853 if (Tok.is(tok::identifier))
2854 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002855
2856 // If we're in Objective-C and we have an Objective-C class type followed
2857 // by an identifier and then either ':' or ']', in a place where an
2858 // expression is permitted, then this is probably a class message send
2859 // missing the initial '['. In this case, we won't consider this to be
2860 // the start of a declaration.
2861 if (DisambiguatingWithExpression &&
2862 isStartOfObjCClassMessageMissingOpenBracket())
2863 return false;
2864
John McCall1f476a12010-02-26 08:45:28 +00002865 return isDeclarationSpecifier();
2866
Chris Lattner020bab92009-01-04 23:41:41 +00002867 case tok::coloncolon: // ::foo::bar
2868 if (NextToken().is(tok::kw_new) || // ::new
2869 NextToken().is(tok::kw_delete)) // ::delete
2870 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002871
Chris Lattner020bab92009-01-04 23:41:41 +00002872 // Annotate typenames and C++ scope specifiers. If we get one, just
2873 // recurse to handle whatever we get.
2874 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002875 return true;
2876 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002877
Chris Lattneracd58a32006-08-06 17:24:14 +00002878 // storage-class-specifier
2879 case tok::kw_typedef:
2880 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002881 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002882 case tok::kw_static:
2883 case tok::kw_auto:
2884 case tok::kw_register:
2885 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002886
Chris Lattneracd58a32006-08-06 17:24:14 +00002887 // type-specifiers
2888 case tok::kw_short:
2889 case tok::kw_long:
2890 case tok::kw_signed:
2891 case tok::kw_unsigned:
2892 case tok::kw__Complex:
2893 case tok::kw__Imaginary:
2894 case tok::kw_void:
2895 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002896 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002897 case tok::kw_char16_t:
2898 case tok::kw_char32_t:
2899
Chris Lattneracd58a32006-08-06 17:24:14 +00002900 case tok::kw_int:
2901 case tok::kw_float:
2902 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002903 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002904 case tok::kw__Bool:
2905 case tok::kw__Decimal32:
2906 case tok::kw__Decimal64:
2907 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002908 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002909
Chris Lattner861a2262008-04-13 18:59:07 +00002910 // struct-or-union-specifier (C99) or class-specifier (C++)
2911 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002912 case tok::kw_struct:
2913 case tok::kw_union:
2914 // enum-specifier
2915 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002916
Chris Lattneracd58a32006-08-06 17:24:14 +00002917 // type-qualifier
2918 case tok::kw_const:
2919 case tok::kw_volatile:
2920 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002921
Chris Lattneracd58a32006-08-06 17:24:14 +00002922 // function-specifier
2923 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002924 case tok::kw_virtual:
2925 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002926
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002927 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002928 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002929
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002930 // static_assert-declaration
2931 case tok::kw__Static_assert:
2932
Chris Lattner599e47e2007-08-09 17:01:07 +00002933 // GNU typeof support.
2934 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002935
Chris Lattner599e47e2007-08-09 17:01:07 +00002936 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002937 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002938 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002939
Chris Lattner8b2ec162008-07-26 03:38:44 +00002940 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2941 case tok::less:
2942 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002943
Steve Narofff192fab2009-01-06 19:34:12 +00002944 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002945 case tok::kw___cdecl:
2946 case tok::kw___stdcall:
2947 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002948 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002949 case tok::kw___w64:
2950 case tok::kw___ptr64:
2951 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002952 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002953
2954 case tok::kw___private:
2955 case tok::kw___local:
2956 case tok::kw___global:
2957 case tok::kw___constant:
2958 case tok::kw___read_only:
2959 case tok::kw___read_write:
2960 case tok::kw___write_only:
2961
Eli Friedman53339e02009-06-08 23:27:34 +00002962 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002963 }
2964}
2965
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002966bool Parser::isConstructorDeclarator() {
2967 TentativeParsingAction TPA(*this);
2968
2969 // Parse the C++ scope specifier.
2970 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002971 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002972 TPA.Revert();
2973 return false;
2974 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002975
2976 // Parse the constructor name.
2977 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2978 // We already know that we have a constructor name; just consume
2979 // the token.
2980 ConsumeToken();
2981 } else {
2982 TPA.Revert();
2983 return false;
2984 }
2985
2986 // Current class name must be followed by a left parentheses.
2987 if (Tok.isNot(tok::l_paren)) {
2988 TPA.Revert();
2989 return false;
2990 }
2991 ConsumeParen();
2992
2993 // A right parentheses or ellipsis signals that we have a constructor.
2994 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2995 TPA.Revert();
2996 return true;
2997 }
2998
2999 // If we need to, enter the specified scope.
3000 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003001 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003002 DeclScopeObj.EnterDeclaratorScope();
3003
Francois Pichet79f3a872011-01-31 04:54:32 +00003004 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003005 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003006 MaybeParseMicrosoftAttributes(Attrs);
3007
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003008 // Check whether the next token(s) are part of a declaration
3009 // specifier, in which case we have the start of a parameter and,
3010 // therefore, we know that this is a constructor.
3011 bool IsConstructor = isDeclarationSpecifier();
3012 TPA.Revert();
3013 return IsConstructor;
3014}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003015
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003016/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003017/// type-qualifier-list: [C99 6.7.5]
3018/// type-qualifier
3019/// [vendor] attributes
3020/// [ only if VendorAttributesAllowed=true ]
3021/// type-qualifier-list type-qualifier
3022/// [vendor] type-qualifier-list attributes
3023/// [ only if VendorAttributesAllowed=true ]
3024/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3025/// [ only if CXX0XAttributesAllowed=true ]
3026/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003027///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003028void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3029 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003030 bool CXX0XAttributesAllowed) {
3031 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3032 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003033 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003034 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003035 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003036 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003037 else
3038 Diag(Loc, diag::err_attributes_not_allowed);
3039 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003040
3041 SourceLocation EndLoc;
3042
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003043 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003044 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003045 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003046 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003047 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003048
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003049 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003050 case tok::code_completion:
3051 Actions.CodeCompleteTypeQualifiers(DS);
3052 ConsumeCodeCompletionToken();
3053 break;
3054
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003055 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003056 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3057 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003058 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003059 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003060 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3061 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003062 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003063 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003064 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3065 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003066 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003067
3068 // OpenCL qualifiers:
3069 case tok::kw_private:
3070 if (!getLang().OpenCL)
3071 goto DoneWithTypeQuals;
3072 case tok::kw___private:
3073 case tok::kw___global:
3074 case tok::kw___local:
3075 case tok::kw___constant:
3076 case tok::kw___read_only:
3077 case tok::kw___write_only:
3078 case tok::kw___read_write:
3079 ParseOpenCLQualifiers(DS);
3080 break;
3081
Eli Friedman53339e02009-06-08 23:27:34 +00003082 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003083 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003084 case tok::kw___cdecl:
3085 case tok::kw___stdcall:
3086 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003087 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003088 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003089 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003090 continue;
3091 }
3092 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003093 case tok::kw___pascal:
3094 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003095 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003096 continue;
3097 }
3098 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003099 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003100 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003101 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003102 continue; // do *not* consume the next token!
3103 }
3104 // otherwise, FALL THROUGH!
3105 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003106 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003107 // If this is not a type-qualifier token, we're done reading type
3108 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003109 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003110 if (EndLoc.isValid())
3111 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003112 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003113 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003114
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003115 // If the specifier combination wasn't legal, issue a diagnostic.
3116 if (isInvalid) {
3117 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003118 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003119 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003120 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003121 }
3122}
3123
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003124
3125/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3126///
3127void Parser::ParseDeclarator(Declarator &D) {
3128 /// This implements the 'declarator' production in the C grammar, then checks
3129 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003130 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003131}
3132
Sebastian Redlbd150f42008-11-21 19:14:01 +00003133/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3134/// is parsed by the function passed to it. Pass null, and the direct-declarator
3135/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003136/// ptr-operator production.
3137///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003138/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3139/// [C] pointer[opt] direct-declarator
3140/// [C++] direct-declarator
3141/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003142///
3143/// pointer: [C99 6.7.5]
3144/// '*' type-qualifier-list[opt]
3145/// '*' type-qualifier-list[opt] pointer
3146///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003147/// ptr-operator:
3148/// '*' cv-qualifier-seq[opt]
3149/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003150/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003151/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003152/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003153/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003154void Parser::ParseDeclaratorInternal(Declarator &D,
3155 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003156 if (Diags.hasAllExtensionsSilenced())
3157 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003158
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003159 // C++ member pointers start with a '::' or a nested-name.
3160 // Member pointers get special handling, since there's no place for the
3161 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003162 if (getLang().CPlusPlus &&
3163 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3164 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003165 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003166 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003167
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003168 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003169 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003170 // The scope spec really belongs to the direct-declarator.
3171 D.getCXXScopeSpec() = SS;
3172 if (DirectDeclParser)
3173 (this->*DirectDeclParser)(D);
3174 return;
3175 }
3176
3177 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003178 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003179 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003180 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003181 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003182
3183 // Recurse to parse whatever is left.
3184 ParseDeclaratorInternal(D, DirectDeclParser);
3185
3186 // Sema will have to catch (syntactically invalid) pointers into global
3187 // scope. It has to catch pointers into namespace scope anyway.
3188 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003189 Loc),
3190 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003191 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003192 return;
3193 }
3194 }
3195
3196 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003197 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003198 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003199 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003200 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003201 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003202 if (DirectDeclParser)
3203 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003204 return;
3205 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003206
Sebastian Redled0f3b02009-03-15 22:02:01 +00003207 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3208 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003209 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003210 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003211
Chris Lattner9eac9312009-03-27 04:18:06 +00003212 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003213 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003214 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003215
Bill Wendling3708c182007-05-27 10:15:43 +00003216 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003217 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003218
Bill Wendling3708c182007-05-27 10:15:43 +00003219 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003220 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003221 if (Kind == tok::star)
3222 // Remember that we parsed a pointer type, and remember the type-quals.
3223 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003224 DS.getConstSpecLoc(),
3225 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003226 DS.getRestrictSpecLoc()),
3227 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003228 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003229 else
3230 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003231 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003232 Loc),
3233 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003234 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003235 } else {
3236 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003237 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003238
Sebastian Redl3b27be62009-03-23 00:00:23 +00003239 // Complain about rvalue references in C++03, but then go on and build
3240 // the declarator.
3241 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003242 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003243
Bill Wendling93efb222007-06-02 23:28:54 +00003244 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3245 // cv-qualifiers are introduced through the use of a typedef or of a
3246 // template type argument, in which case the cv-qualifiers are ignored.
3247 //
3248 // [GNU] Retricted references are allowed.
3249 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003250 // [C++0x] Attributes on references are not allowed.
3251 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003252 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003253
3254 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3255 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3256 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003257 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003258 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3259 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003260 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003261 }
Bill Wendling3708c182007-05-27 10:15:43 +00003262
3263 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003264 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003265
Douglas Gregor66583c52008-11-03 15:51:28 +00003266 if (D.getNumTypeObjects() > 0) {
3267 // C++ [dcl.ref]p4: There shall be no references to references.
3268 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3269 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003270 if (const IdentifierInfo *II = D.getIdentifier())
3271 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3272 << II;
3273 else
3274 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3275 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003276
Sebastian Redlbd150f42008-11-21 19:14:01 +00003277 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003278 // can go ahead and build the (technically ill-formed)
3279 // declarator: reference collapsing will take care of it.
3280 }
3281 }
3282
Bill Wendling3708c182007-05-27 10:15:43 +00003283 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003284 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003285 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003286 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003287 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003288 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003289}
3290
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003291/// ParseDirectDeclarator
3292/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003293/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003294/// '(' declarator ')'
3295/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003296/// [C90] direct-declarator '[' constant-expression[opt] ']'
3297/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3298/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3299/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3300/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003301/// direct-declarator '(' parameter-type-list ')'
3302/// direct-declarator '(' identifier-list[opt] ')'
3303/// [GNU] direct-declarator '(' parameter-forward-declarations
3304/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003305/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3306/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003307/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003308///
3309/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003310/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003311/// '::'[opt] nested-name-specifier[opt] type-name
3312///
3313/// id-expression: [C++ 5.1]
3314/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003315/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003316///
3317/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003318/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003319/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003320/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003321/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003322/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003323///
Chris Lattneracd58a32006-08-06 17:24:14 +00003324void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003325 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003326
Douglas Gregor7861a802009-11-03 01:35:08 +00003327 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3328 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003329 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003330 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003331 }
3332
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003333 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003334 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003335 // Change the declaration context for name lookup, until this function
3336 // is exited (and the declarator has been parsed).
3337 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003338 }
3339
Douglas Gregor27b4c162010-12-23 22:44:42 +00003340 // C++0x [dcl.fct]p14:
3341 // There is a syntactic ambiguity when an ellipsis occurs at the end
3342 // of a parameter-declaration-clause without a preceding comma. In
3343 // this case, the ellipsis is parsed as part of the
3344 // abstract-declarator if the type of the parameter names a template
3345 // parameter pack that has not been expanded; otherwise, it is parsed
3346 // as part of the parameter-declaration-clause.
3347 if (Tok.is(tok::ellipsis) &&
3348 !((D.getContext() == Declarator::PrototypeContext ||
3349 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003350 NextToken().is(tok::r_paren) &&
3351 !Actions.containsUnexpandedParameterPacks(D)))
3352 D.setEllipsisLoc(ConsumeToken());
3353
Douglas Gregor7861a802009-11-03 01:35:08 +00003354 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3355 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3356 // We found something that indicates the start of an unqualified-id.
3357 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003358 bool AllowConstructorName;
3359 if (D.getDeclSpec().hasTypeSpecifier())
3360 AllowConstructorName = false;
3361 else if (D.getCXXScopeSpec().isSet())
3362 AllowConstructorName =
3363 (D.getContext() == Declarator::FileContext ||
3364 (D.getContext() == Declarator::MemberContext &&
3365 D.getDeclSpec().isFriendSpecified()));
3366 else
3367 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3368
Douglas Gregor7861a802009-11-03 01:35:08 +00003369 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3370 /*EnteringContext=*/true,
3371 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003372 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003373 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003374 D.getName()) ||
3375 // Once we're past the identifier, if the scope was bad, mark the
3376 // whole declarator bad.
3377 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003378 D.SetIdentifier(0, Tok.getLocation());
3379 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003380 } else {
3381 // Parsed the unqualified-id; update range information and move along.
3382 if (D.getSourceRange().getBegin().isInvalid())
3383 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3384 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003385 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003386 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003387 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003388 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003389 assert(!getLang().CPlusPlus &&
3390 "There's a C++-specific check for tok::identifier above");
3391 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3392 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3393 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003394 goto PastIdentifier;
3395 }
3396
3397 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003398 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003399 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003400 // Example: 'char (*X)' or 'int (*XX)(void)'
3401 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003402
3403 // If the declarator was parenthesized, we entered the declarator
3404 // scope when parsing the parenthesized declarator, then exited
3405 // the scope already. Re-enter the scope, if we need to.
3406 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003407 // If there was an error parsing parenthesized declarator, declarator
3408 // scope may have been enterred before. Don't do it again.
3409 if (!D.isInvalidType() &&
3410 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003411 // Change the declaration context for name lookup, until this function
3412 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003413 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003414 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003415 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003416 // This could be something simple like "int" (in which case the declarator
3417 // portion is empty), if an abstract-declarator is allowed.
3418 D.SetIdentifier(0, Tok.getLocation());
3419 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003420 if (D.getContext() == Declarator::MemberContext)
3421 Diag(Tok, diag::err_expected_member_name_or_semi)
3422 << D.getDeclSpec().getSourceRange();
3423 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003424 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003425 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003426 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003427 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003428 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003429 }
Mike Stump11289f42009-09-09 15:08:12 +00003430
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003431 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003432 assert(D.isPastIdentifier() &&
3433 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003434
Alexis Hunt96d5c762009-11-21 08:43:09 +00003435 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003436 if (D.getIdentifier())
3437 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003438
Chris Lattneracd58a32006-08-06 17:24:14 +00003439 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003440 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003441 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3442 // In such a case, check if we actually have a function declarator; if it
3443 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003444 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3445 // When not in file scope, warn for ambiguous function declarators, just
3446 // in case the author intended it as a variable definition.
3447 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3448 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3449 break;
3450 }
John McCall084e83d2011-03-24 11:26:52 +00003451 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003452 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003453 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003454 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003455 } else {
3456 break;
3457 }
3458 }
3459}
3460
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003461/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3462/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003463/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003464/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3465///
3466/// direct-declarator:
3467/// '(' declarator ')'
3468/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003469/// direct-declarator '(' parameter-type-list ')'
3470/// direct-declarator '(' identifier-list[opt] ')'
3471/// [GNU] direct-declarator '(' parameter-forward-declarations
3472/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003473///
3474void Parser::ParseParenDeclarator(Declarator &D) {
3475 SourceLocation StartLoc = ConsumeParen();
3476 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003477
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003478 // Eat any attributes before we look at whether this is a grouping or function
3479 // declarator paren. If this is a grouping paren, the attribute applies to
3480 // the type being built up, for example:
3481 // int (__attribute__(()) *x)(long y)
3482 // If this ends up not being a grouping paren, the attribute applies to the
3483 // first argument, for example:
3484 // int (__attribute__(()) int x)
3485 // In either case, we need to eat any attributes to be able to determine what
3486 // sort of paren this is.
3487 //
John McCall084e83d2011-03-24 11:26:52 +00003488 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003489 bool RequiresArg = false;
3490 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003491 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003492
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003493 // We require that the argument list (if this is a non-grouping paren) be
3494 // present even if the attribute list was empty.
3495 RequiresArg = true;
3496 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003497 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003498 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003499 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3500 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003501 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003502 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003503 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003504 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003505 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003506
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003507 // If we haven't past the identifier yet (or where the identifier would be
3508 // stored, if this is an abstract declarator), then this is probably just
3509 // grouping parens. However, if this could be an abstract-declarator, then
3510 // this could also be the start of function arguments (consider 'void()').
3511 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003512
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003513 if (!D.mayOmitIdentifier()) {
3514 // If this can't be an abstract-declarator, this *must* be a grouping
3515 // paren, because we haven't seen the identifier yet.
3516 isGrouping = true;
3517 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003518 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003519 isDeclarationSpecifier()) { // 'int(int)' is a function.
3520 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3521 // considered to be a type, not a K&R identifier-list.
3522 isGrouping = false;
3523 } else {
3524 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3525 isGrouping = true;
3526 }
Mike Stump11289f42009-09-09 15:08:12 +00003527
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003528 // If this is a grouping paren, handle:
3529 // direct-declarator: '(' declarator ')'
3530 // direct-declarator: '(' attributes declarator ')'
3531 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003532 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003533 D.setGroupingParens(true);
3534
Sebastian Redlbd150f42008-11-21 19:14:01 +00003535 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003536 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003537 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003538 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3539 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003540
3541 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003542 return;
3543 }
Mike Stump11289f42009-09-09 15:08:12 +00003544
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003545 // Okay, if this wasn't a grouping paren, it must be the start of a function
3546 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003547 // identifier (and remember where it would have been), then call into
3548 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003549 D.SetIdentifier(0, Tok.getLocation());
3550
John McCall53fa7142010-12-24 02:08:15 +00003551 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003552}
3553
3554/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3555/// declarator D up to a paren, which indicates that we are parsing function
3556/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003557///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003558/// If AttrList is non-null, then the caller parsed those arguments immediately
3559/// after the open paren - they should be considered to be the first argument of
3560/// a parameter. If RequiresArg is true, then the first argument of the
3561/// function is required to be present and required to not be an identifier
3562/// list.
3563///
Chris Lattneracd58a32006-08-06 17:24:14 +00003564/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003565/// parameter-type-list: [C99 6.7.5]
3566/// parameter-list
3567/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003568/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003569///
3570/// parameter-list: [C99 6.7.5]
3571/// parameter-declaration
3572/// parameter-list ',' parameter-declaration
3573///
3574/// parameter-declaration: [C99 6.7.5]
3575/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003576/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003577/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003578/// declaration-specifiers abstract-declarator[opt]
3579/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003580/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003581/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003582///
Douglas Gregor54992352011-01-26 03:43:54 +00003583/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3584/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003585///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003586/// [C++0x] exception-specification:
3587/// dynamic-exception-specification
3588/// noexcept-specification
3589///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003590void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003591 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003592 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003593 // lparen is already consumed!
3594 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003595
Douglas Gregor7fb25412010-10-01 18:44:50 +00003596 ParsedType TrailingReturnType;
3597
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003598 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003599 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003600 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003601 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003602
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003603 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003604
3605 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003606 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003607 SourceLocation RefQualifierLoc;
3608 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003609 ExceptionSpecificationType ESpecType = EST_None;
3610 SourceRange ESpecRange;
3611 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3612 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3613 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003614 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003615 MaybeParseCXX0XAttributes(attrs);
3616
Chris Lattnercf0bab22008-12-18 07:02:59 +00003617 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003618 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003619 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003620
Douglas Gregor54992352011-01-26 03:43:54 +00003621 // Parse ref-qualifier[opt]
3622 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3623 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003624 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003625
Douglas Gregor54992352011-01-26 03:43:54 +00003626 RefQualifierIsLValueRef = Tok.is(tok::amp);
3627 RefQualifierLoc = ConsumeToken();
3628 EndLoc = RefQualifierLoc;
3629 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003630
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003631 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003632 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3633 DynamicExceptions,
3634 DynamicExceptionRanges,
3635 NoexceptExpr);
3636 if (ESpecType != EST_None)
3637 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003638
3639 // Parse trailing-return-type.
3640 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3641 TrailingReturnType = ParseTrailingReturnType().get();
3642 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003643 }
3644
Chris Lattner371ed4e2008-04-06 06:57:35 +00003645 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003646 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003647 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003648 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003649 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003650 /*arglist*/ 0, 0,
3651 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003652 RefQualifierIsLValueRef,
3653 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003654 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003655 DynamicExceptions.data(),
3656 DynamicExceptionRanges.data(),
3657 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003658 NoexceptExpr.isUsable() ?
3659 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003660 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003661 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003662 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003663 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003664 }
3665
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003666 // Alternatively, this parameter list may be an identifier list form for a
3667 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003668 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3669 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003670 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003671 // K&R identifier lists can't have typedefs as identifiers, per
3672 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003673 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003674 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003675
Steve Naroffb0486722009-01-28 19:16:40 +00003676 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003677 // normal declarators, not for abstract-declarators. Get the first
3678 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003679 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003680 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003681
3682 // Identifier lists follow a really simple grammar: the identifiers can
3683 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3684 // identifier lists are really rare in the brave new modern world, and it
3685 // is very common for someone to typo a type in a non-k&r style list. If
3686 // we are presented with something like: "void foo(intptr x, float y)",
3687 // we don't want to start parsing the function declarator as though it is
3688 // a K&R style declarator just because intptr is an invalid type.
3689 //
3690 // To handle this, we check to see if the token after the first identifier
3691 // is a "," or ")". Only if so, do we parse it as an identifier list.
3692 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3693 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3694 FirstTok.getIdentifierInfo(),
3695 FirstTok.getLocation(), D);
3696
3697 // If we get here, the code is invalid. Push the first identifier back
3698 // into the token stream and parse the first argument as an (invalid)
3699 // normal argument declarator.
3700 PP.EnterToken(Tok);
3701 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003702 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003703 }
Mike Stump11289f42009-09-09 15:08:12 +00003704
Chris Lattner371ed4e2008-04-06 06:57:35 +00003705 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003706
Chris Lattner371ed4e2008-04-06 06:57:35 +00003707 // Build up an array of information about the parsed arguments.
3708 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003709
3710 // Enter function-declaration scope, limiting any declarators to the
3711 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003712 ParseScope PrototypeScope(this,
3713 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003714
Chris Lattner371ed4e2008-04-06 06:57:35 +00003715 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003716 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003717 while (1) {
3718 if (Tok.is(tok::ellipsis)) {
3719 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003720 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003721 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003722 }
Mike Stump11289f42009-09-09 15:08:12 +00003723
Chris Lattner371ed4e2008-04-06 06:57:35 +00003724 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003725 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003726 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003727
3728 // Skip any Microsoft attributes before a param.
3729 if (getLang().Microsoft && Tok.is(tok::l_square))
3730 ParseMicrosoftAttributes(DS.getAttributes());
3731
3732 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003733
3734 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003735 // Take them so that we only apply the attributes to the first parameter.
3736 DS.takeAttributesFrom(attrs);
3737
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003738 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003739
Chris Lattner371ed4e2008-04-06 06:57:35 +00003740 // Parse the declarator. This is "PrototypeContext", because we must
3741 // accept either 'declarator' or 'abstract-declarator' here.
3742 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3743 ParseDeclarator(ParmDecl);
3744
3745 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003746 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003747
Chris Lattner371ed4e2008-04-06 06:57:35 +00003748 // Remember this parsed parameter in ParamInfo.
3749 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003750
Douglas Gregor4d87df52008-12-16 21:30:33 +00003751 // DefArgToks is used when the parsing of default arguments needs
3752 // to be delayed.
3753 CachedTokens *DefArgToks = 0;
3754
Chris Lattner371ed4e2008-04-06 06:57:35 +00003755 // If no parameter was specified, verify that *something* was specified,
3756 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003757 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3758 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003759 // Completely missing, emit error.
3760 Diag(DSStart, diag::err_missing_param);
3761 } else {
3762 // Otherwise, we have something. Add it and let semantic analysis try
3763 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003764
Chris Lattner371ed4e2008-04-06 06:57:35 +00003765 // Inform the actions module about the parameter declarator, so it gets
3766 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003767 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003768
3769 // Parse the default argument, if any. We parse the default
3770 // arguments in all dialects; the semantic analysis in
3771 // ActOnParamDefaultArgument will reject the default argument in
3772 // C.
3773 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003774 SourceLocation EqualLoc = Tok.getLocation();
3775
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003776 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003777 if (D.getContext() == Declarator::MemberContext) {
3778 // If we're inside a class definition, cache the tokens
3779 // corresponding to the default argument. We'll actually parse
3780 // them when we see the end of the class definition.
3781 // FIXME: Templates will require something similar.
3782 // FIXME: Can we use a smart pointer for Toks?
3783 DefArgToks = new CachedTokens;
3784
Mike Stump11289f42009-09-09 15:08:12 +00003785 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003786 /*StopAtSemi=*/true,
3787 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003788 delete DefArgToks;
3789 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003790 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003791 } else {
3792 // Mark the end of the default argument so that we know when to
3793 // stop when we parse it later on.
3794 Token DefArgEnd;
3795 DefArgEnd.startToken();
3796 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3797 DefArgEnd.setLocation(Tok.getLocation());
3798 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003799 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003800 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003801 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003802 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003803 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003804 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003805
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003806 // The argument isn't actually potentially evaluated unless it is
3807 // used.
3808 EnterExpressionEvaluationContext Eval(Actions,
3809 Sema::PotentiallyEvaluatedIfUsed);
3810
John McCalldadc5752010-08-24 06:29:42 +00003811 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003812 if (DefArgResult.isInvalid()) {
3813 Actions.ActOnParamDefaultArgumentError(Param);
3814 SkipUntil(tok::comma, tok::r_paren, true, true);
3815 } else {
3816 // Inform the actions module about the default argument
3817 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003818 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003819 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003820 }
3821 }
Mike Stump11289f42009-09-09 15:08:12 +00003822
3823 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3824 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003825 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003826 }
3827
3828 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003829 if (Tok.isNot(tok::comma)) {
3830 if (Tok.is(tok::ellipsis)) {
3831 IsVariadic = true;
3832 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3833
3834 if (!getLang().CPlusPlus) {
3835 // We have ellipsis without a preceding ',', which is ill-formed
3836 // in C. Complain and provide the fix.
3837 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003838 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003839 }
3840 }
3841
3842 break;
3843 }
Mike Stump11289f42009-09-09 15:08:12 +00003844
Chris Lattner371ed4e2008-04-06 06:57:35 +00003845 // Consume the comma.
3846 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003847 }
Mike Stump11289f42009-09-09 15:08:12 +00003848
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003849 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003850 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003851
John McCall084e83d2011-03-24 11:26:52 +00003852 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003853 SourceLocation RefQualifierLoc;
3854 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003855 ExceptionSpecificationType ESpecType = EST_None;
3856 SourceRange ESpecRange;
3857 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3858 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3859 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003860
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003861 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003862 MaybeParseCXX0XAttributes(attrs);
3863
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003864 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003865 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003866 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003867 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003868
Douglas Gregor54992352011-01-26 03:43:54 +00003869 // Parse ref-qualifier[opt]
3870 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3871 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003872 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003873
3874 RefQualifierIsLValueRef = Tok.is(tok::amp);
3875 RefQualifierLoc = ConsumeToken();
3876 EndLoc = RefQualifierLoc;
3877 }
3878
Sebastian Redl965b0e32011-03-05 14:45:16 +00003879 // FIXME: We should leave the prototype scope before parsing the exception
3880 // specification, and then reenter it when parsing the trailing return type.
3881 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3882
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003883 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003884 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3885 DynamicExceptions,
3886 DynamicExceptionRanges,
3887 NoexceptExpr);
3888 if (ESpecType != EST_None)
3889 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003890
3891 // Parse trailing-return-type.
3892 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3893 TrailingReturnType = ParseTrailingReturnType().get();
3894 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003895 }
3896
Douglas Gregor7fb25412010-10-01 18:44:50 +00003897 // Leave prototype scope.
3898 PrototypeScope.Exit();
3899
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003900 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003901 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003902 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003903 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003904 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003905 RefQualifierIsLValueRef,
3906 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003907 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003908 DynamicExceptions.data(),
3909 DynamicExceptionRanges.data(),
3910 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003911 NoexceptExpr.isUsable() ?
3912 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003913 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003914 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003915 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003916}
Chris Lattneracd58a32006-08-06 17:24:14 +00003917
Chris Lattner6c940e62008-04-06 06:34:08 +00003918/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3919/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003920/// first identifier has already been consumed, and the current token is the
3921/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003922///
3923/// identifier-list: [C99 6.7.5]
3924/// identifier
3925/// identifier-list ',' identifier
3926///
3927void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003928 IdentifierInfo *FirstIdent,
3929 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003930 Declarator &D) {
3931 // Build up an array of information about the parsed arguments.
3932 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3933 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003934
Chris Lattner6c940e62008-04-06 06:34:08 +00003935 // If there was no identifier specified for the declarator, either we are in
3936 // an abstract-declarator, or we are in a parameter declarator which was found
3937 // to be abstract. In abstract-declarators, identifier lists are not valid:
3938 // diagnose this.
3939 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003940 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003941
Chris Lattner9453ab82010-05-14 17:23:36 +00003942 // The first identifier was already read, and is known to be the first
3943 // identifier in the list. Remember this identifier in ParamInfo.
3944 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003945 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003946
Chris Lattner6c940e62008-04-06 06:34:08 +00003947 while (Tok.is(tok::comma)) {
3948 // Eat the comma.
3949 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003950
Chris Lattner9186f552008-04-06 06:39:19 +00003951 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003952 if (Tok.isNot(tok::identifier)) {
3953 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003954 SkipUntil(tok::r_paren);
3955 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003956 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003957
Chris Lattner6c940e62008-04-06 06:34:08 +00003958 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003959
3960 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003961 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003962 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003963
Chris Lattner6c940e62008-04-06 06:34:08 +00003964 // Verify that the argument identifier has not already been mentioned.
3965 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003966 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003967 } else {
3968 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003969 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003970 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003971 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003972 }
Mike Stump11289f42009-09-09 15:08:12 +00003973
Chris Lattner6c940e62008-04-06 06:34:08 +00003974 // Eat the identifier.
3975 ConsumeToken();
3976 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003977
3978 // If we have the closing ')', eat it and we're done.
3979 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3980
Chris Lattner9186f552008-04-06 06:39:19 +00003981 // Remember that we parsed a function type, and remember the attributes. This
3982 // function type is always a K&R style function type, which is not varargs and
3983 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00003984 ParsedAttributes attrs(AttrFactory);
3985 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003986 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003987 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003988 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003989 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003990 EST_None, SourceLocation(), 0, 0,
3991 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00003992 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003993}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003994
Chris Lattnere8074e62006-08-06 18:30:15 +00003995/// [C90] direct-declarator '[' constant-expression[opt] ']'
3996/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3997/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3998/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3999/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4000void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004001 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004002
Chris Lattner84a11622008-12-18 07:27:21 +00004003 // C array syntax has many features, but by-far the most common is [] and [4].
4004 // This code does a fast path to handle some of the most obvious cases.
4005 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004006 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004007 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004008 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004009
Chris Lattner84a11622008-12-18 07:27:21 +00004010 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004011 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004012 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004013 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004014 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004015 return;
4016 } else if (Tok.getKind() == tok::numeric_constant &&
4017 GetLookAheadToken(1).is(tok::r_square)) {
4018 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004019 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004020 ConsumeToken();
4021
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004022 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004023 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004024 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004025
Chris Lattner84a11622008-12-18 07:27:21 +00004026 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004027 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004028 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004029 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004030 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004031 return;
4032 }
Mike Stump11289f42009-09-09 15:08:12 +00004033
Chris Lattnere8074e62006-08-06 18:30:15 +00004034 // If valid, this location is the position where we read the 'static' keyword.
4035 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004036 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004037 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004038
Chris Lattnere8074e62006-08-06 18:30:15 +00004039 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004040 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004041 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004042 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004043
Chris Lattnere8074e62006-08-06 18:30:15 +00004044 // If we haven't already read 'static', check to see if there is one after the
4045 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004046 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004047 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004048
Chris Lattnere8074e62006-08-06 18:30:15 +00004049 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004050 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004051 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004052
Chris Lattner521ff2b2008-04-06 05:26:30 +00004053 // Handle the case where we have '[*]' as the array size. However, a leading
4054 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4055 // the the token after the star is a ']'. Since stars in arrays are
4056 // infrequent, use of lookahead is not costly here.
4057 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004058 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004059
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004060 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004061 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004062 StaticLoc = SourceLocation(); // Drop the static.
4063 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004064 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004065 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004066 // Note, in C89, this production uses the constant-expr production instead
4067 // of assignment-expr. The only difference is that assignment-expr allows
4068 // things like '=' and '*='. Sema rejects these in C89 mode because they
4069 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004070
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004071 // Parse the constant-expression or assignment-expression now (depending
4072 // on dialect).
4073 if (getLang().CPlusPlus)
4074 NumElements = ParseConstantExpression();
4075 else
4076 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004077 }
Mike Stump11289f42009-09-09 15:08:12 +00004078
Chris Lattner62591722006-08-12 18:40:58 +00004079 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004080 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004081 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004082 // If the expression was invalid, skip it.
4083 SkipUntil(tok::r_square);
4084 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004085 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004086
4087 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4088
John McCall084e83d2011-03-24 11:26:52 +00004089 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004090 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004091
Chris Lattner84a11622008-12-18 07:27:21 +00004092 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004093 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004094 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004095 NumElements.release(),
4096 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004097 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004098}
4099
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004100/// [GNU] typeof-specifier:
4101/// typeof ( expressions )
4102/// typeof ( type-name )
4103/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004104///
4105void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004106 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004107 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004108 SourceLocation StartLoc = ConsumeToken();
4109
John McCalle8595032010-01-13 20:03:27 +00004110 const bool hasParens = Tok.is(tok::l_paren);
4111
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004112 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004113 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004114 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004115 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4116 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004117 if (hasParens)
4118 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004119
4120 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004121 // FIXME: Not accurate, the range gets one token more than it should.
4122 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004123 else
4124 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004125
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004126 if (isCastExpr) {
4127 if (!CastTy) {
4128 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004129 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004130 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004131
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004132 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004133 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004134 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4135 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004136 DiagID, CastTy))
4137 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004138 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004139 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004140
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004141 // If we get here, the operand to the typeof was an expresion.
4142 if (Operand.isInvalid()) {
4143 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004144 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004145 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004146
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004147 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004148 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004149 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4150 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004151 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004152 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004153}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004154
4155
4156/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4157/// from TryAltiVecVectorToken.
4158bool Parser::TryAltiVecVectorTokenOutOfLine() {
4159 Token Next = NextToken();
4160 switch (Next.getKind()) {
4161 default: return false;
4162 case tok::kw_short:
4163 case tok::kw_long:
4164 case tok::kw_signed:
4165 case tok::kw_unsigned:
4166 case tok::kw_void:
4167 case tok::kw_char:
4168 case tok::kw_int:
4169 case tok::kw_float:
4170 case tok::kw_double:
4171 case tok::kw_bool:
4172 case tok::kw___pixel:
4173 Tok.setKind(tok::kw___vector);
4174 return true;
4175 case tok::identifier:
4176 if (Next.getIdentifierInfo() == Ident_pixel) {
4177 Tok.setKind(tok::kw___vector);
4178 return true;
4179 }
4180 return false;
4181 }
4182}
4183
4184bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4185 const char *&PrevSpec, unsigned &DiagID,
4186 bool &isInvalid) {
4187 if (Tok.getIdentifierInfo() == Ident_vector) {
4188 Token Next = NextToken();
4189 switch (Next.getKind()) {
4190 case tok::kw_short:
4191 case tok::kw_long:
4192 case tok::kw_signed:
4193 case tok::kw_unsigned:
4194 case tok::kw_void:
4195 case tok::kw_char:
4196 case tok::kw_int:
4197 case tok::kw_float:
4198 case tok::kw_double:
4199 case tok::kw_bool:
4200 case tok::kw___pixel:
4201 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4202 return true;
4203 case tok::identifier:
4204 if (Next.getIdentifierInfo() == Ident_pixel) {
4205 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4206 return true;
4207 }
4208 break;
4209 default:
4210 break;
4211 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004212 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004213 DS.isTypeAltiVecVector()) {
4214 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4215 return true;
4216 }
4217 return false;
4218}