blob: 5e6c51c1ee92fc81e81533cad0b058067722600c [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
Sebastian Redlf769df52009-03-24 22:27:57 +0000656/// [C++0x] 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:
John McCall53fa7142010-12-24 02:08:15 +0000691 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000692 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000693 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000694 default:
John McCall53fa7142010-12-24 02:08:15 +0000695 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000696 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000697
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000698 // This routine returns a DeclGroup, if the thing we parsed only contains a
699 // single decl, convert it now.
700 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000701}
702
703/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
704/// declaration-specifiers init-declarator-list[opt] ';'
705///[C90/C++]init-declarator-list ';' [TODO]
706/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000707///
Richard Smith02e85f32011-04-14 22:09:26 +0000708/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
709/// attribute-specifier-seq[opt] type-specifier-seq declarator
710///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000711/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000712/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000713///
714/// If FRI is non-null, we might be parsing a for-range-declaration instead
715/// of a simple-declaration. If we find that we are, we also parse the
716/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000717Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
718 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000719 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000720 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000721 bool RequireSemi,
722 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000723 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000724 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000725 DS.takeAttributesFrom(attrs);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000726 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000727 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000728 StmtResult R = Actions.ActOnVlaStmt(DS);
729 if (R.isUsable())
730 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000731
Chris Lattner0e894622006-08-13 19:58:17 +0000732 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
733 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000734 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000735 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000736 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000737 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000738 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000739 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000740 }
Mike Stump11289f42009-09-09 15:08:12 +0000741
Richard Smith02e85f32011-04-14 22:09:26 +0000742 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000743}
Mike Stump11289f42009-09-09 15:08:12 +0000744
John McCalld5a36322009-11-03 19:26:08 +0000745/// ParseDeclGroup - Having concluded that this is either a function
746/// definition or a group of object declarations, actually parse the
747/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000748Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
749 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000750 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000751 SourceLocation *DeclEnd,
752 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000753 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000754 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000755 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000756
John McCalld5a36322009-11-03 19:26:08 +0000757 // Bail out if the first declarator didn't seem well-formed.
758 if (!D.hasName() && !D.mayOmitIdentifier()) {
759 // Skip until ; or }.
760 SkipUntil(tok::r_brace, true, true);
761 if (Tok.is(tok::semi))
762 ConsumeToken();
763 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000766 // Check to see if we have a function *definition* which must have a body.
767 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
768 // Look at the next token to make sure that this isn't a function
769 // declaration. We have to check this because __attribute__ might be the
770 // start of a function definition in GCC-extended K&R C.
771 !isDeclarationAfterDeclarator()) {
772
Chris Lattner13901342010-07-11 22:42:07 +0000773 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000774 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
775 Diag(Tok, diag::err_function_declared_typedef);
776
777 // Recover by treating the 'typedef' as spurious.
778 DS.ClearStorageClassSpecs();
779 }
780
John McCall48871652010-08-21 09:40:31 +0000781 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000782 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000783 }
784
785 if (isDeclarationSpecifier()) {
786 // If there is an invalid declaration specifier right after the function
787 // prototype, then we must be in a missing semicolon case where this isn't
788 // actually a body. Just fall through into the code that handles it as a
789 // prototype, and let the top-level code handle the erroneous declspec
790 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000791 } else {
792 Diag(Tok, diag::err_expected_fn_body);
793 SkipUntil(tok::semi);
794 return DeclGroupPtrTy();
795 }
796 }
797
Richard Smith02e85f32011-04-14 22:09:26 +0000798 if (ParseAttributesAfterDeclarator(D))
799 return DeclGroupPtrTy();
800
801 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
802 // must parse and analyze the for-range-initializer before the declaration is
803 // analyzed.
804 if (FRI && Tok.is(tok::colon)) {
805 FRI->ColonLoc = ConsumeToken();
806 // FIXME: handle braced-init-list here.
807 FRI->RangeExpr = ParseExpression();
808 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
809 Actions.ActOnCXXForRangeDecl(ThisDecl);
810 Actions.FinalizeDeclaration(ThisDecl);
811 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
812 }
813
John McCall48871652010-08-21 09:40:31 +0000814 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +0000815 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +0000816 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000817 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000818 DeclsInGroup.push_back(FirstDecl);
819
820 // If we don't have a comma, it is either the end of the list (a ';') or an
821 // error, bail out.
822 while (Tok.is(tok::comma)) {
823 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000824 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000825
826 // Parse the next declarator.
827 D.clear();
828
829 // Accept attributes in an init-declarator. In the first declarator in a
830 // declaration, these would be part of the declspec. In subsequent
831 // declarators, they become part of the declarator itself, so that they
832 // don't apply to declarators after *this* one. Examples:
833 // short __attribute__((common)) var; -> declspec
834 // short var __attribute__((common)); -> declarator
835 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000836 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000837
838 ParseDeclarator(D);
839
John McCall48871652010-08-21 09:40:31 +0000840 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000841 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000842 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000843 DeclsInGroup.push_back(ThisDecl);
844 }
845
846 if (DeclEnd)
847 *DeclEnd = Tok.getLocation();
848
849 if (Context != Declarator::ForContext &&
850 ExpectAndConsume(tok::semi,
851 Context == Declarator::FileContext
852 ? diag::err_invalid_token_after_toplevel_declarator
853 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000854 // Okay, there was no semicolon and one was expected. If we see a
855 // declaration specifier, just assume it was missing and continue parsing.
856 // Otherwise things are very confused and we skip to recover.
857 if (!isDeclarationSpecifier()) {
858 SkipUntil(tok::r_brace, true, true);
859 if (Tok.is(tok::semi))
860 ConsumeToken();
861 }
John McCalld5a36322009-11-03 19:26:08 +0000862 }
863
Douglas Gregor0be31a22010-07-02 17:43:08 +0000864 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000865 DeclsInGroup.data(),
866 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000867}
868
Richard Smith02e85f32011-04-14 22:09:26 +0000869/// Parse an optional simple-asm-expr and attributes, and attach them to a
870/// declarator. Returns true on an error.
871bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
872 // If a simple-asm-expr is present, parse it.
873 if (Tok.is(tok::kw_asm)) {
874 SourceLocation Loc;
875 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
876 if (AsmLabel.isInvalid()) {
877 SkipUntil(tok::semi, true, true);
878 return true;
879 }
880
881 D.setAsmLabel(AsmLabel.release());
882 D.SetRangeEnd(Loc);
883 }
884
885 MaybeParseGNUAttributes(D);
886 return false;
887}
888
Douglas Gregor23996282009-05-12 21:31:51 +0000889/// \brief Parse 'declaration' after parsing 'declaration-specifiers
890/// declarator'. This method parses the remainder of the declaration
891/// (including any attributes or initializer, among other things) and
892/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000893///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000894/// init-declarator: [C99 6.7]
895/// declarator
896/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000897/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
898/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000899/// [C++] declarator initializer[opt]
900///
901/// [C++] initializer:
902/// [C++] '=' initializer-clause
903/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000904/// [C++0x] '=' 'default' [TODO]
905/// [C++0x] '=' 'delete'
906///
907/// According to the standard grammar, =default and =delete are function
908/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000909///
John McCall48871652010-08-21 09:40:31 +0000910Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000911 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +0000912 if (ParseAttributesAfterDeclarator(D))
913 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000914
Richard Smith02e85f32011-04-14 22:09:26 +0000915 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
916}
Mike Stump11289f42009-09-09 15:08:12 +0000917
Richard Smith02e85f32011-04-14 22:09:26 +0000918Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
919 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000920 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000921 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000922 switch (TemplateInfo.Kind) {
923 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000924 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000925 break;
926
927 case ParsedTemplateInfo::Template:
928 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000929 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000930 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000931 TemplateInfo.TemplateParams->data(),
932 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000933 D);
934 break;
935
936 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000937 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000938 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000939 TemplateInfo.ExternLoc,
940 TemplateInfo.TemplateLoc,
941 D);
942 if (ThisRes.isInvalid()) {
943 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000944 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000945 }
946
947 ThisDecl = ThisRes.get();
948 break;
949 }
950 }
Mike Stump11289f42009-09-09 15:08:12 +0000951
Richard Smith30482bc2011-02-20 03:19:35 +0000952 bool TypeContainsAuto =
953 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
954
Douglas Gregor23996282009-05-12 21:31:51 +0000955 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000956 if (isTokenEqualOrMistypedEqualEqual(
957 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000958 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000959 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000960 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000961
962 if (!getLang().CPlusPlus0x)
963 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
964
Douglas Gregor23996282009-05-12 21:31:51 +0000965 Actions.SetDeclDeleted(ThisDecl, DelLoc);
966 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000967 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
968 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000969 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000970 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000971
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000972 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000973 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000974 ConsumeCodeCompletionToken();
975 SkipUntil(tok::comma, true, true);
976 return ThisDecl;
977 }
978
John McCalldadc5752010-08-24 06:29:42 +0000979 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000980
John McCall1f4ee7b2009-12-19 09:28:58 +0000981 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000982 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000983 ExitScope();
984 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000985
Douglas Gregor23996282009-05-12 21:31:51 +0000986 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000987 SkipUntil(tok::comma, true, true);
988 Actions.ActOnInitializerError(ThisDecl);
989 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000990 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
991 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000992 }
993 } else if (Tok.is(tok::l_paren)) {
994 // Parse C++ direct initializer: '(' expression-list ')'
995 SourceLocation LParenLoc = ConsumeParen();
996 ExprVector Exprs(Actions);
997 CommaLocsTy CommaLocs;
998
Douglas Gregor613bf102009-12-22 17:47:17 +0000999 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1000 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001001 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001002 }
1003
Douglas Gregor23996282009-05-12 21:31:51 +00001004 if (ParseExpressionList(Exprs, CommaLocs)) {
1005 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001006
1007 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001008 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001009 ExitScope();
1010 }
Douglas Gregor23996282009-05-12 21:31:51 +00001011 } else {
1012 // Match the ')'.
1013 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1014
1015 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1016 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001017
1018 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001019 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001020 ExitScope();
1021 }
1022
Douglas Gregor23996282009-05-12 21:31:51 +00001023 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1024 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001025 RParenLoc,
1026 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001027 }
1028 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001029 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001030 }
1031
Richard Smithb2bc2e62011-02-21 20:05:19 +00001032 Actions.FinalizeDeclaration(ThisDecl);
1033
Douglas Gregor23996282009-05-12 21:31:51 +00001034 return ThisDecl;
1035}
1036
Chris Lattner1890ac82006-08-13 01:16:23 +00001037/// ParseSpecifierQualifierList
1038/// specifier-qualifier-list:
1039/// type-specifier specifier-qualifier-list[opt]
1040/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001041/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001042///
1043void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1044 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1045 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +00001046 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001047
Chris Lattner1890ac82006-08-13 01:16:23 +00001048 // Validate declspec for type-name.
1049 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001050 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001051 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001052 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001053
Chris Lattner1b22eed2006-11-28 05:12:07 +00001054 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001055 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001056 if (DS.getStorageClassSpecLoc().isValid())
1057 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1058 else
1059 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001060 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001061 }
Mike Stump11289f42009-09-09 15:08:12 +00001062
Chris Lattner1b22eed2006-11-28 05:12:07 +00001063 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001064 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001065 if (DS.isInlineSpecified())
1066 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1067 if (DS.isVirtualSpecified())
1068 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1069 if (DS.isExplicitSpecified())
1070 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001071 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001072 }
1073}
Chris Lattner53361ac2006-08-10 05:19:57 +00001074
Chris Lattner6cc055a2009-04-12 20:42:31 +00001075/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1076/// specified token is valid after the identifier in a declarator which
1077/// immediately follows the declspec. For example, these things are valid:
1078///
1079/// int x [ 4]; // direct-declarator
1080/// int x ( int y); // direct-declarator
1081/// int(int x ) // direct-declarator
1082/// int x ; // simple-declaration
1083/// int x = 17; // init-declarator-list
1084/// int x , y; // init-declarator-list
1085/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001086/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001087/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001088///
1089/// This is not, because 'x' does not immediately follow the declspec (though
1090/// ')' happens to be valid anyway).
1091/// int (x)
1092///
1093static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1094 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1095 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001096 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001097}
1098
Chris Lattner20a0c612009-04-14 21:34:55 +00001099
1100/// ParseImplicitInt - This method is called when we have an non-typename
1101/// identifier in a declspec (which normally terminates the decl spec) when
1102/// the declspec has no type specifier. In this case, the declspec is either
1103/// malformed or is "implicit int" (in K&R and C89).
1104///
1105/// This method handles diagnosing this prettily and returns false if the
1106/// declspec is done being processed. If it recovers and thinks there may be
1107/// other pieces of declspec after it, it returns true.
1108///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001109bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001110 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001111 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001112 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattner20a0c612009-04-14 21:34:55 +00001114 SourceLocation Loc = Tok.getLocation();
1115 // If we see an identifier that is not a type name, we normally would
1116 // parse it as the identifer being declared. However, when a typename
1117 // is typo'd or the definition is not included, this will incorrectly
1118 // parse the typename as the identifier name and fall over misparsing
1119 // later parts of the diagnostic.
1120 //
1121 // As such, we try to do some look-ahead in cases where this would
1122 // otherwise be an "implicit-int" case to see if this is invalid. For
1123 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1124 // an identifier with implicit int, we'd get a parse error because the
1125 // next token is obviously invalid for a type. Parse these as a case
1126 // with an invalid type specifier.
1127 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001128
Chris Lattner20a0c612009-04-14 21:34:55 +00001129 // Since we know that this either implicit int (which is rare) or an
1130 // error, we'd do lookahead to try to do better recovery.
1131 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1132 // If this token is valid for implicit int, e.g. "static x = 4", then
1133 // we just avoid eating the identifier, so it will be parsed as the
1134 // identifier in the declarator.
1135 return false;
1136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137
Chris Lattner20a0c612009-04-14 21:34:55 +00001138 // Otherwise, if we don't consume this token, we are going to emit an
1139 // error anyway. Try to recover from various common problems. Check
1140 // to see if this was a reference to a tag name without a tag specified.
1141 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001142 //
1143 // C++ doesn't need this, and isTagName doesn't take SS.
1144 if (SS == 0) {
1145 const char *TagName = 0;
1146 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001147
Douglas Gregor0be31a22010-07-02 17:43:08 +00001148 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001149 default: break;
1150 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
1151 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
1152 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
1153 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
1154 }
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001156 if (TagName) {
1157 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001158 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00001159 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +00001160
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001161 // Parse this as a tag as if the missing tag were present.
1162 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001163 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001164 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001165 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001166 return true;
1167 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001168 }
Mike Stump11289f42009-09-09 15:08:12 +00001169
Douglas Gregor15e56022009-10-13 23:27:22 +00001170 // This is almost certainly an invalid type name. Let the action emit a
1171 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001172 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001173 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001174 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001175 // The action emitted a diagnostic, so we don't have to.
1176 if (T) {
1177 // The action has suggested that the type T could be used. Set that as
1178 // the type in the declaration specifiers, consume the would-be type
1179 // name token, and we're done.
1180 const char *PrevSpec;
1181 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001182 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001183 DS.SetRangeEnd(Tok.getLocation());
1184 ConsumeToken();
1185
1186 // There may be other declaration specifiers after this.
1187 return true;
1188 }
1189
1190 // Fall through; the action had no suggestion for us.
1191 } else {
1192 // The action did not emit a diagnostic, so emit one now.
1193 SourceRange R;
1194 if (SS) R = SS->getRange();
1195 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Douglas Gregor15e56022009-10-13 23:27:22 +00001198 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001199 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001200 unsigned DiagID;
1201 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001202 DS.SetRangeEnd(Tok.getLocation());
1203 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001204
Chris Lattner20a0c612009-04-14 21:34:55 +00001205 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1206 // avoid rippling error messages on subsequent uses of the same type,
1207 // could be useful if #include was forgotten.
1208 return false;
1209}
1210
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001211/// \brief Determine the declaration specifier context from the declarator
1212/// context.
1213///
1214/// \param Context the declarator context, which is one of the
1215/// Declarator::TheContext enumerator values.
1216Parser::DeclSpecContext
1217Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1218 if (Context == Declarator::MemberContext)
1219 return DSC_class;
1220 if (Context == Declarator::FileContext)
1221 return DSC_top_level;
1222 return DSC_normal;
1223}
1224
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001225/// ParseDeclarationSpecifiers
1226/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001227/// storage-class-specifier declaration-specifiers[opt]
1228/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001229/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001230/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001231///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001232/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001233/// 'typedef'
1234/// 'extern'
1235/// 'static'
1236/// 'auto'
1237/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001238/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001239/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001240/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001241/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001242/// [C++] 'virtual'
1243/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001244/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001245/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001246/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001247
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001248///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001249void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001250 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001251 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001252 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +00001253 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +00001254 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001255 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001256 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001257 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001258 unsigned DiagID = 0;
1259
Chris Lattner4d8f8732006-11-28 05:05:08 +00001260 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001261
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001262 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001263 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001264 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001265 // If this is not a declaration specifier token, we're done reading decl
1266 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001267 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001268 return;
Mike Stump11289f42009-09-09 15:08:12 +00001269
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001270 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001271 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001272 if (DS.hasTypeSpecifier()) {
1273 bool AllowNonIdentifiers
1274 = (getCurScope()->getFlags() & (Scope::ControlScope |
1275 Scope::BlockScope |
1276 Scope::TemplateParamScope |
1277 Scope::FunctionPrototypeScope |
1278 Scope::AtCatchScope)) == 0;
1279 bool AllowNestedNameSpecifiers
1280 = DSContext == DSC_top_level ||
1281 (DSContext == DSC_class && DS.isFriendSpecified());
1282
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001283 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1284 AllowNonIdentifiers,
1285 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001286 ConsumeCodeCompletionToken();
1287 return;
1288 }
1289
Douglas Gregor80039242011-02-15 20:33:25 +00001290 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1291 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1292 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001293 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1294 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001295 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001296 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001297 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001298 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001299
1300 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1301 ConsumeCodeCompletionToken();
1302 return;
1303 }
1304
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001305 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001306 // C++ scope specifier. Annotate and loop, or bail out on error.
1307 if (TryAnnotateCXXScopeToken(true)) {
1308 if (!DS.hasTypeSpecifier())
1309 DS.SetTypeSpecError();
1310 goto DoneWithDeclSpec;
1311 }
John McCall8bc2a702010-03-01 18:20:46 +00001312 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1313 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001314 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001315
1316 case tok::annot_cxxscope: {
1317 if (DS.hasTypeSpecifier())
1318 goto DoneWithDeclSpec;
1319
John McCall9dab4e62009-12-12 11:40:51 +00001320 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001321 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1322 Tok.getAnnotationRange(),
1323 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001324
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001325 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001326 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001327 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001328 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001329 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001330 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001331
1332 // C++ [class.qual]p2:
1333 // In a lookup in which the constructor is an acceptable lookup
1334 // result and the nested-name-specifier nominates a class C:
1335 //
1336 // - if the name specified after the
1337 // nested-name-specifier, when looked up in C, is the
1338 // injected-class-name of C (Clause 9), or
1339 //
1340 // - if the name specified after the nested-name-specifier
1341 // is the same as the identifier or the
1342 // simple-template-id's template-name in the last
1343 // component of the nested-name-specifier,
1344 //
1345 // the name is instead considered to name the constructor of
1346 // class C.
1347 //
1348 // Thus, if the template-name is actually the constructor
1349 // name, then the code is ill-formed; this interpretation is
1350 // reinforced by the NAD status of core issue 635.
1351 TemplateIdAnnotation *TemplateId
1352 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001353 if ((DSContext == DSC_top_level ||
1354 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1355 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001356 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001357 if (isConstructorDeclarator()) {
1358 // The user meant this to be an out-of-line constructor
1359 // definition, but template arguments are not allowed
1360 // there. Just allow this as a constructor; we'll
1361 // complain about it later.
1362 goto DoneWithDeclSpec;
1363 }
1364
1365 // The user meant this to name a type, but it actually names
1366 // a constructor with some extraneous template
1367 // arguments. Complain, then parse it as a type as the user
1368 // intended.
1369 Diag(TemplateId->TemplateNameLoc,
1370 diag::err_out_of_line_template_id_names_constructor)
1371 << TemplateId->Name;
1372 }
1373
John McCall9dab4e62009-12-12 11:40:51 +00001374 DS.getTypeSpecScope() = SS;
1375 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001376 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001377 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001378 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001379 continue;
1380 }
1381
Douglas Gregorc5790df2009-09-28 07:26:33 +00001382 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001383 DS.getTypeSpecScope() = SS;
1384 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001385 if (Tok.getAnnotationValue()) {
1386 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001387 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1388 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001389 PrevSpec, DiagID, T);
1390 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001391 else
1392 DS.SetTypeSpecError();
1393 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1394 ConsumeToken(); // The typename
1395 }
1396
Douglas Gregor167fa622009-03-25 15:40:00 +00001397 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001398 goto DoneWithDeclSpec;
1399
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001400 // If we're in a context where the identifier could be a class name,
1401 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001402 if ((DSContext == DSC_top_level ||
1403 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001404 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001405 &SS)) {
1406 if (isConstructorDeclarator())
1407 goto DoneWithDeclSpec;
1408
1409 // As noted in C++ [class.qual]p2 (cited above), when the name
1410 // of the class is qualified in a context where it could name
1411 // a constructor, its a constructor name. However, we've
1412 // looked at the declarator, and the user probably meant this
1413 // to be a type. Complain that it isn't supposed to be treated
1414 // as a type, then proceed to parse it as a type.
1415 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1416 << Next.getIdentifierInfo();
1417 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001418
John McCallba7bf592010-08-24 05:47:05 +00001419 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1420 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001421 getCurScope(), &SS,
1422 false, false, ParsedType(),
1423 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001424
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001425 // If the referenced identifier is not a type, then this declspec is
1426 // erroneous: We already checked about that it has no type specifier, and
1427 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001428 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001429 if (TypeRep == 0) {
1430 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001431 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001432 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001433 }
Mike Stump11289f42009-09-09 15:08:12 +00001434
John McCall9dab4e62009-12-12 11:40:51 +00001435 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001436 ConsumeToken(); // The C++ scope.
1437
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001438 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001439 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001440 if (isInvalid)
1441 break;
Mike Stump11289f42009-09-09 15:08:12 +00001442
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001443 DS.SetRangeEnd(Tok.getLocation());
1444 ConsumeToken(); // The typename.
1445
1446 continue;
1447 }
Mike Stump11289f42009-09-09 15:08:12 +00001448
Chris Lattnere387d9e2009-01-21 19:48:37 +00001449 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001450 if (Tok.getAnnotationValue()) {
1451 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001452 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001453 DiagID, T);
1454 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001455 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001456
1457 if (isInvalid)
1458 break;
1459
Chris Lattnere387d9e2009-01-21 19:48:37 +00001460 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1461 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001462
Chris Lattnere387d9e2009-01-21 19:48:37 +00001463 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1464 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001465 // Objective-C interface.
1466 if (Tok.is(tok::less) && getLang().ObjC1)
1467 ParseObjCProtocolQualifiers(DS);
1468
Chris Lattnere387d9e2009-01-21 19:48:37 +00001469 continue;
1470 }
Mike Stump11289f42009-09-09 15:08:12 +00001471
Chris Lattner16fac4f2008-07-26 01:18:38 +00001472 // typedef-name
1473 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001474 // In C++, check to see if this is a scope specifier like foo::bar::, if
1475 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001476 if (getLang().CPlusPlus) {
1477 if (TryAnnotateCXXScopeToken(true)) {
1478 if (!DS.hasTypeSpecifier())
1479 DS.SetTypeSpecError();
1480 goto DoneWithDeclSpec;
1481 }
1482 if (!Tok.is(tok::identifier))
1483 continue;
1484 }
Mike Stump11289f42009-09-09 15:08:12 +00001485
Chris Lattner16fac4f2008-07-26 01:18:38 +00001486 // This identifier can only be a typedef name if we haven't already seen
1487 // a type-specifier. Without this check we misparse:
1488 // typedef int X; struct Y { short X; }; as 'short int'.
1489 if (DS.hasTypeSpecifier())
1490 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001491
John Thompson22334602010-02-05 00:12:22 +00001492 // Check for need to substitute AltiVec keyword tokens.
1493 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1494 break;
1495
Chris Lattner16fac4f2008-07-26 01:18:38 +00001496 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001497 ParsedType TypeRep =
1498 Actions.getTypeName(*Tok.getIdentifierInfo(),
1499 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001500
Chris Lattner6cc055a2009-04-12 20:42:31 +00001501 // If this is not a typedef name, don't parse it as part of the declspec,
1502 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001503 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001504 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001505 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001506 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001507
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001508 // If we're in a context where the identifier could be a class name,
1509 // check whether this is a constructor declaration.
1510 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001511 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001512 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001513 goto DoneWithDeclSpec;
1514
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001516 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001517 if (isInvalid)
1518 break;
Mike Stump11289f42009-09-09 15:08:12 +00001519
Chris Lattner16fac4f2008-07-26 01:18:38 +00001520 DS.SetRangeEnd(Tok.getLocation());
1521 ConsumeToken(); // The identifier
1522
1523 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1524 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001525 // Objective-C interface.
1526 if (Tok.is(tok::less) && getLang().ObjC1)
1527 ParseObjCProtocolQualifiers(DS);
1528
Steve Naroffcd5e7822008-09-22 10:28:57 +00001529 // Need to support trailing type qualifiers (e.g. "id<p> const").
1530 // If a type specifier follows, it will be diagnosed elsewhere.
1531 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001532 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001533
1534 // type-name
1535 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001536 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001537 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001538 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001539 // This template-id does not refer to a type name, so we're
1540 // done with the type-specifiers.
1541 goto DoneWithDeclSpec;
1542 }
1543
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001544 // If we're in a context where the template-id could be a
1545 // constructor name or specialization, check whether this is a
1546 // constructor declaration.
1547 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001548 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001549 isConstructorDeclarator())
1550 goto DoneWithDeclSpec;
1551
Douglas Gregor7f741122009-02-25 19:37:18 +00001552 // Turn the template-id annotation token into a type annotation
1553 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001554 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001555 continue;
1556 }
1557
Chris Lattnere37e2332006-08-15 04:50:22 +00001558 // GNU attributes support.
1559 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001560 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001561 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001562
1563 // Microsoft declspec support.
1564 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001565 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001566 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001567
Steve Naroff44ac7772008-12-25 14:16:32 +00001568 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001569 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001570 // FIXME: Add handling here!
1571 break;
1572
1573 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001574 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001575 case tok::kw___cdecl:
1576 case tok::kw___stdcall:
1577 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001578 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001579 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001580 continue;
1581
Dawn Perchik335e16b2010-09-03 01:29:35 +00001582 // Borland single token adornments.
1583 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001584 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001585 continue;
1586
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001587 // OpenCL single token adornments.
1588 case tok::kw___kernel:
1589 ParseOpenCLAttributes(DS.getAttributes());
1590 continue;
1591
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001592 // storage-class-specifier
1593 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001594 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001595 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001596 break;
1597 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001598 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001599 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001600 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001601 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001602 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001603 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001604 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001605 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001606 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001607 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001608 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001609 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001610 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001611 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001612 break;
1613 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001614 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001615 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1616 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1617 DiagID, getLang());
1618 if (!isInvalid)
1619 Diag(Tok, diag::auto_storage_class)
1620 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1621 }
1622 else
1623 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1624 DiagID);
1625 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001626 else
John McCall49bfce42009-08-03 20:12:06 +00001627 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001628 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001629 break;
1630 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001631 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001632 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001633 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001634 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001635 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001636 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001637 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001638 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001639 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001640 break;
Mike Stump11289f42009-09-09 15:08:12 +00001641
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001642 // function-specifier
1643 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001644 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001645 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001646 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001647 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001648 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001649 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001650 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001651 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001652
Anders Carlssoncd8db412009-05-06 04:46:28 +00001653 // friend
1654 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001655 if (DSContext == DSC_class)
1656 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1657 else {
1658 PrevSpec = ""; // not actually used by the diagnostic
1659 DiagID = diag::err_friend_invalid_in_context;
1660 isInvalid = true;
1661 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001662 break;
Mike Stump11289f42009-09-09 15:08:12 +00001663
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001664 // constexpr
1665 case tok::kw_constexpr:
1666 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1667 break;
1668
Chris Lattnere387d9e2009-01-21 19:48:37 +00001669 // type-specifier
1670 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001671 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1672 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001673 break;
1674 case tok::kw_long:
1675 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001676 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1677 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001678 else
John McCall49bfce42009-08-03 20:12:06 +00001679 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1680 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001681 break;
1682 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001683 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1684 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001685 break;
1686 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001687 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1688 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001689 break;
1690 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001691 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1692 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001693 break;
1694 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001695 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1696 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001697 break;
1698 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001699 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1700 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001701 break;
1702 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001703 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1704 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001705 break;
1706 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1708 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001709 break;
1710 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001711 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1712 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001713 break;
1714 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001715 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1716 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001717 break;
1718 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1720 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001721 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001722 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001723 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1724 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001725 break;
1726 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001727 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1728 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001729 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001730 case tok::kw_bool:
1731 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001732 if (Tok.is(tok::kw_bool) &&
1733 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1734 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1735 PrevSpec = ""; // Not used by the diagnostic.
1736 DiagID = diag::err_bool_redeclaration;
1737 isInvalid = true;
1738 } else {
1739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1740 DiagID);
1741 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001742 break;
1743 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1745 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001746 break;
1747 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1749 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001750 break;
1751 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1753 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001754 break;
John Thompson22334602010-02-05 00:12:22 +00001755 case tok::kw___vector:
1756 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1757 break;
1758 case tok::kw___pixel:
1759 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1760 break;
John McCall39439732011-04-09 22:50:59 +00001761 case tok::kw___unknown_anytype:
1762 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1763 PrevSpec, DiagID);
1764 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001765
1766 // class-specifier:
1767 case tok::kw_class:
1768 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001769 case tok::kw_union: {
1770 tok::TokenKind Kind = Tok.getKind();
1771 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001772 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001773 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001774 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001775
1776 // enum-specifier:
1777 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001778 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001779 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001780 continue;
1781
1782 // cv-qualifier:
1783 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001784 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1785 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001786 break;
1787 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001788 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1789 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001790 break;
1791 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001792 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1793 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001794 break;
1795
Douglas Gregor333489b2009-03-27 23:10:48 +00001796 // C++ typename-specifier:
1797 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001798 if (TryAnnotateTypeOrScopeToken()) {
1799 DS.SetTypeSpecError();
1800 goto DoneWithDeclSpec;
1801 }
1802 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001803 continue;
1804 break;
1805
Chris Lattnere387d9e2009-01-21 19:48:37 +00001806 // GNU typeof support.
1807 case tok::kw_typeof:
1808 ParseTypeofSpecifier(DS);
1809 continue;
1810
Anders Carlsson74948d02009-06-24 17:47:40 +00001811 case tok::kw_decltype:
1812 ParseDecltypeSpecifier(DS);
1813 continue;
1814
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001815 // OpenCL qualifiers:
1816 case tok::kw_private:
1817 if (!getLang().OpenCL)
1818 goto DoneWithDeclSpec;
1819 case tok::kw___private:
1820 case tok::kw___global:
1821 case tok::kw___local:
1822 case tok::kw___constant:
1823 case tok::kw___read_only:
1824 case tok::kw___write_only:
1825 case tok::kw___read_write:
1826 ParseOpenCLQualifiers(DS);
1827 break;
1828
Steve Naroffcfdf6162008-06-05 00:02:44 +00001829 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001830 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001831 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1832 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001833 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001834 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001835
Douglas Gregor3a001f42010-11-19 17:10:50 +00001836 if (!ParseObjCProtocolQualifiers(DS))
1837 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1838 << FixItHint::CreateInsertion(Loc, "id")
1839 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001840
1841 // Need to support trailing type qualifiers (e.g. "id<p> const").
1842 // If a type specifier follows, it will be diagnosed elsewhere.
1843 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001844 }
John McCall49bfce42009-08-03 20:12:06 +00001845 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001846 if (isInvalid) {
1847 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001848 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001849
1850 if (DiagID == diag::ext_duplicate_declspec)
1851 Diag(Tok, DiagID)
1852 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1853 else
1854 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001855 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001856
Chris Lattner2e232092008-03-13 06:29:04 +00001857 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001858 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001859 }
1860}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001861
Chris Lattnera448d752009-01-06 06:59:53 +00001862/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001863/// primarily follow the C++ grammar with additions for C99 and GNU,
1864/// which together subsume the C grammar. Note that the C++
1865/// type-specifier also includes the C type-qualifier (for const,
1866/// volatile, and C99 restrict). Returns true if a type-specifier was
1867/// found (and parsed), false otherwise.
1868///
1869/// type-specifier: [C++ 7.1.5]
1870/// simple-type-specifier
1871/// class-specifier
1872/// enum-specifier
1873/// elaborated-type-specifier [TODO]
1874/// cv-qualifier
1875///
1876/// cv-qualifier: [C++ 7.1.5.1]
1877/// 'const'
1878/// 'volatile'
1879/// [C99] 'restrict'
1880///
1881/// simple-type-specifier: [ C++ 7.1.5.2]
1882/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1883/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1884/// 'char'
1885/// 'wchar_t'
1886/// 'bool'
1887/// 'short'
1888/// 'int'
1889/// 'long'
1890/// 'signed'
1891/// 'unsigned'
1892/// 'float'
1893/// 'double'
1894/// 'void'
1895/// [C99] '_Bool'
1896/// [C99] '_Complex'
1897/// [C99] '_Imaginary' // Removed in TC2?
1898/// [GNU] '_Decimal32'
1899/// [GNU] '_Decimal64'
1900/// [GNU] '_Decimal128'
1901/// [GNU] typeof-specifier
1902/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1903/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001904/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001905/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001906bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001907 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001908 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001909 const ParsedTemplateInfo &TemplateInfo,
1910 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001911 SourceLocation Loc = Tok.getLocation();
1912
1913 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001914 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001915 // If we already have a type specifier, this identifier is not a type.
1916 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1917 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1918 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1919 return false;
John Thompson22334602010-02-05 00:12:22 +00001920 // Check for need to substitute AltiVec keyword tokens.
1921 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1922 break;
1923 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001924 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001925 // Annotate typenames and C++ scope specifiers. If we get one, just
1926 // recurse to handle whatever we get.
1927 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001928 return true;
1929 if (Tok.is(tok::identifier))
1930 return false;
1931 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1932 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001933 case tok::coloncolon: // ::foo::bar
1934 if (NextToken().is(tok::kw_new) || // ::new
1935 NextToken().is(tok::kw_delete)) // ::delete
1936 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001937
Chris Lattner020bab92009-01-04 23:41:41 +00001938 // Annotate typenames and C++ scope specifiers. If we get one, just
1939 // recurse to handle whatever we get.
1940 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001941 return true;
1942 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1943 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001944
Douglas Gregor450c75a2008-11-07 15:42:26 +00001945 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001946 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001947 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001948 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1949 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001950 DiagID, T);
1951 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001952 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001953 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1954 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001955
Douglas Gregor450c75a2008-11-07 15:42:26 +00001956 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1957 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1958 // Objective-C interface. If we don't have Objective-C or a '<', this is
1959 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001960 if (Tok.is(tok::less) && getLang().ObjC1)
1961 ParseObjCProtocolQualifiers(DS);
1962
Douglas Gregor450c75a2008-11-07 15:42:26 +00001963 return true;
1964 }
1965
1966 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001967 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001968 break;
1969 case tok::kw_long:
1970 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001971 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1972 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001973 else
John McCall49bfce42009-08-03 20:12:06 +00001974 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1975 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001976 break;
1977 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001978 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001979 break;
1980 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001981 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1982 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001983 break;
1984 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001985 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1986 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001987 break;
1988 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001989 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1990 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001991 break;
1992 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001994 break;
1995 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001996 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001997 break;
1998 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001999 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002000 break;
2001 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002002 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002003 break;
2004 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002005 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002006 break;
2007 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002009 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002010 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002011 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002012 break;
2013 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002015 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002016 case tok::kw_bool:
2017 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002018 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002019 break;
2020 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002021 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2022 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002023 break;
2024 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2026 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002027 break;
2028 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2030 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002031 break;
John Thompson22334602010-02-05 00:12:22 +00002032 case tok::kw___vector:
2033 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2034 break;
2035 case tok::kw___pixel:
2036 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2037 break;
2038
Douglas Gregor450c75a2008-11-07 15:42:26 +00002039 // class-specifier:
2040 case tok::kw_class:
2041 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002042 case tok::kw_union: {
2043 tok::TokenKind Kind = Tok.getKind();
2044 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002045 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2046 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002047 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002048 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002049
2050 // enum-specifier:
2051 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002052 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002053 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002054 return true;
2055
2056 // cv-qualifier:
2057 case tok::kw_const:
2058 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002059 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002060 break;
2061 case tok::kw_volatile:
2062 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, 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_restrict:
2066 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002067 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002068 break;
2069
2070 // GNU typeof support.
2071 case tok::kw_typeof:
2072 ParseTypeofSpecifier(DS);
2073 return true;
2074
Anders Carlsson74948d02009-06-24 17:47:40 +00002075 // C++0x decltype support.
2076 case tok::kw_decltype:
2077 ParseDecltypeSpecifier(DS);
2078 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002079
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002080 // OpenCL qualifiers:
2081 case tok::kw_private:
2082 if (!getLang().OpenCL)
2083 return false;
2084 case tok::kw___private:
2085 case tok::kw___global:
2086 case tok::kw___local:
2087 case tok::kw___constant:
2088 case tok::kw___read_only:
2089 case tok::kw___write_only:
2090 case tok::kw___read_write:
2091 ParseOpenCLQualifiers(DS);
2092 break;
2093
Anders Carlssonbae27372009-06-26 23:44:14 +00002094 // C++0x auto support.
2095 case tok::kw_auto:
2096 if (!getLang().CPlusPlus0x)
2097 return false;
2098
John McCall49bfce42009-08-03 20:12:06 +00002099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002100 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002101
Eli Friedman53339e02009-06-08 23:27:34 +00002102 case tok::kw___ptr64:
2103 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002104 case tok::kw___cdecl:
2105 case tok::kw___stdcall:
2106 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002107 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002108 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002109 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002110
Dawn Perchik335e16b2010-09-03 01:29:35 +00002111 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002112 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002113 return true;
2114
Douglas Gregor450c75a2008-11-07 15:42:26 +00002115 default:
2116 // Not a type-specifier; do nothing.
2117 return false;
2118 }
2119
2120 // If the specifier combination wasn't legal, issue a diagnostic.
2121 if (isInvalid) {
2122 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002123 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002124 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002125 }
2126 DS.SetRangeEnd(Tok.getLocation());
2127 ConsumeToken(); // whatever we parsed above.
2128 return true;
2129}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002130
Chris Lattner70ae4912007-10-29 04:42:53 +00002131/// ParseStructDeclaration - Parse a struct declaration without the terminating
2132/// semicolon.
2133///
Chris Lattner90a26b02007-01-23 04:38:16 +00002134/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002135/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002136/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002137/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002138/// struct-declarator-list:
2139/// struct-declarator
2140/// struct-declarator-list ',' struct-declarator
2141/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2142/// struct-declarator:
2143/// declarator
2144/// [GNU] declarator attributes[opt]
2145/// declarator[opt] ':' constant-expression
2146/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2147///
Chris Lattnera12405b2008-04-10 06:46:29 +00002148void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002149ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002150 if (Tok.is(tok::kw___extension__)) {
2151 // __extension__ silences extension warnings in the subexpression.
2152 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002153 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002154 return ParseStructDeclaration(DS, Fields);
2155 }
Mike Stump11289f42009-09-09 15:08:12 +00002156
Steve Naroff97170802007-08-20 22:28:22 +00002157 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002158 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002159
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002160 // If there are no declarators, this is a free-standing declaration
2161 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002162 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002163 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002164 return;
2165 }
2166
2167 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002168 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002169 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002170 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002171 FieldDeclarator DeclaratorInfo(DS);
2172
2173 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002174 if (!FirstDeclarator)
2175 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002176
Steve Naroff97170802007-08-20 22:28:22 +00002177 /// struct-declarator: declarator
2178 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002179 if (Tok.isNot(tok::colon)) {
2180 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2181 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002182 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002183 }
Mike Stump11289f42009-09-09 15:08:12 +00002184
Chris Lattner76c72282007-10-09 17:33:22 +00002185 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002186 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002187 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002188 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002189 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002190 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002191 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002192 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002193
Steve Naroff97170802007-08-20 22:28:22 +00002194 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002195 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002196
John McCallcfefb6d2009-11-03 02:38:08 +00002197 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002198 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002199 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002200
Steve Naroff97170802007-08-20 22:28:22 +00002201 // If we don't have a comma, it is either the end of the list (a ';')
2202 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002203 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002204 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002205
Steve Naroff97170802007-08-20 22:28:22 +00002206 // Consume the comma.
2207 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002208
John McCallcfefb6d2009-11-03 02:38:08 +00002209 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002210 }
Steve Naroff97170802007-08-20 22:28:22 +00002211}
2212
2213/// ParseStructUnionBody
2214/// struct-contents:
2215/// struct-declaration-list
2216/// [EXT] empty
2217/// [GNU] "struct-declaration-list" without terminatoring ';'
2218/// struct-declaration-list:
2219/// struct-declaration
2220/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002221/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002222///
Chris Lattner1300fb92007-01-23 23:42:53 +00002223void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002224 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002225 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2226 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002227
Chris Lattner90a26b02007-01-23 04:38:16 +00002228 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002229
Douglas Gregor658b9552009-01-09 22:42:13 +00002230 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002231 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002232
Chris Lattner7b9ace62007-01-23 20:11:08 +00002233 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2234 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002235 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002236 Diag(Tok, diag::ext_empty_struct_union)
2237 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002238
John McCall48871652010-08-21 09:40:31 +00002239 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002240
Chris Lattner7b9ace62007-01-23 20:11:08 +00002241 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002242 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002243 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002244
Chris Lattner736ed5d2007-06-09 05:59:07 +00002245 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002246 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002247 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002248 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002249 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002250 ConsumeToken();
2251 continue;
2252 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002253
2254 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002255 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002256
John McCallcfefb6d2009-11-03 02:38:08 +00002257 if (!Tok.is(tok::at)) {
2258 struct CFieldCallback : FieldCallback {
2259 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002260 Decl *TagDecl;
2261 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002262
John McCall48871652010-08-21 09:40:31 +00002263 CFieldCallback(Parser &P, Decl *TagDecl,
2264 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002265 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2266
John McCall48871652010-08-21 09:40:31 +00002267 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002268 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002269 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002270 FD.D.getDeclSpec().getSourceRange().getBegin(),
2271 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002272 FieldDecls.push_back(Field);
2273 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002274 }
John McCallcfefb6d2009-11-03 02:38:08 +00002275 } Callback(*this, TagDecl, FieldDecls);
2276
2277 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002278 } else { // Handle @defs
2279 ConsumeToken();
2280 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2281 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002282 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002283 continue;
2284 }
2285 ConsumeToken();
2286 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2287 if (!Tok.is(tok::identifier)) {
2288 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002289 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002290 continue;
2291 }
John McCall48871652010-08-21 09:40:31 +00002292 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002293 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002294 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002295 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2296 ConsumeToken();
2297 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002298 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002299
Chris Lattner76c72282007-10-09 17:33:22 +00002300 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002301 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002302 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002303 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002304 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002305 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002306 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2307 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002308 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002309 // If we stopped at a ';', eat it.
2310 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002311 }
2312 }
Mike Stump11289f42009-09-09 15:08:12 +00002313
Steve Naroff33a1e802007-10-29 21:38:07 +00002314 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002315
John McCall084e83d2011-03-24 11:26:52 +00002316 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002317 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002318 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002319
Douglas Gregor0be31a22010-07-02 17:43:08 +00002320 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002321 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002322 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002323 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002324 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002325 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002326}
2327
Chris Lattner3b561a32006-08-13 00:12:11 +00002328/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002329/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002330/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002331///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002332/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2333/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002334/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002335/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002336///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002337/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2338/// [C++0x] enum-head '{' enumerator-list ',' '}'
2339///
2340/// enum-head: [C++0x]
2341/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2342/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2343///
2344/// enum-key: [C++0x]
2345/// 'enum'
2346/// 'enum' 'class'
2347/// 'enum' 'struct'
2348///
2349/// enum-base: [C++0x]
2350/// ':' type-specifier-seq
2351///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002352/// [C++] elaborated-type-specifier:
2353/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2354///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002355void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002356 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002357 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002358 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002359 if (Tok.is(tok::code_completion)) {
2360 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002361 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002362 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002363 }
2364
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002365 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002366 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002367 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002368
Abramo Bagnarad7548482010-05-19 21:37:53 +00002369 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002370 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002371 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002372 return;
2373
2374 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002375 Diag(Tok, diag::err_expected_ident);
2376 if (Tok.isNot(tok::l_brace)) {
2377 // Has no name and is not a definition.
2378 // Skip the rest of this declarator, up until the comma or semicolon.
2379 SkipUntil(tok::comma, true);
2380 return;
2381 }
2382 }
2383 }
Mike Stump11289f42009-09-09 15:08:12 +00002384
Douglas Gregora1aec292011-02-22 20:32:04 +00002385 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002386 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002387 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002388
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002389 if (getLang().CPlusPlus0x &&
2390 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002391 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002392 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2393 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002394 }
2395
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002396 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002397 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2398 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002399 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002400
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002401 // Skip the rest of this declarator, up until the comma or semicolon.
2402 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002403 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002404 }
Mike Stump11289f42009-09-09 15:08:12 +00002405
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002406 // If an identifier is present, consume and remember it.
2407 IdentifierInfo *Name = 0;
2408 SourceLocation NameLoc;
2409 if (Tok.is(tok::identifier)) {
2410 Name = Tok.getIdentifierInfo();
2411 NameLoc = ConsumeToken();
2412 }
Mike Stump11289f42009-09-09 15:08:12 +00002413
Douglas Gregor0bf31402010-10-08 23:50:27 +00002414 if (!Name && IsScopedEnum) {
2415 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2416 // declaration of a scoped enumeration.
2417 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2418 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002419 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002420 }
2421
2422 TypeResult BaseType;
2423
Douglas Gregord1f69f62010-12-01 17:42:47 +00002424 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002425 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002426 bool PossibleBitfield = false;
2427 if (getCurScope()->getFlags() & Scope::ClassScope) {
2428 // If we're in class scope, this can either be an enum declaration with
2429 // an underlying type, or a declaration of a bitfield member. We try to
2430 // use a simple disambiguation scheme first to catch the common cases
2431 // (integer literal, sizeof); if it's still ambiguous, we then consider
2432 // anything that's a simple-type-specifier followed by '(' as an
2433 // expression. This suffices because function types are not valid
2434 // underlying types anyway.
2435 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2436 // If the next token starts an expression, we know we're parsing a
2437 // bit-field. This is the common case.
2438 if (TPR == TPResult::True())
2439 PossibleBitfield = true;
2440 // If the next token starts a type-specifier-seq, it may be either a
2441 // a fixed underlying type or the start of a function-style cast in C++;
2442 // lookahead one more token to see if it's obvious that we have a
2443 // fixed underlying type.
2444 else if (TPR == TPResult::False() &&
2445 GetLookAheadToken(2).getKind() == tok::semi) {
2446 // Consume the ':'.
2447 ConsumeToken();
2448 } else {
2449 // We have the start of a type-specifier-seq, so we have to perform
2450 // tentative parsing to determine whether we have an expression or a
2451 // type.
2452 TentativeParsingAction TPA(*this);
2453
2454 // Consume the ':'.
2455 ConsumeToken();
2456
Douglas Gregora1aec292011-02-22 20:32:04 +00002457 if ((getLang().CPlusPlus &&
2458 isCXXDeclarationSpecifier() != TPResult::True()) ||
2459 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002460 // We'll parse this as a bitfield later.
2461 PossibleBitfield = true;
2462 TPA.Revert();
2463 } else {
2464 // We have a type-specifier-seq.
2465 TPA.Commit();
2466 }
2467 }
2468 } else {
2469 // Consume the ':'.
2470 ConsumeToken();
2471 }
2472
2473 if (!PossibleBitfield) {
2474 SourceRange Range;
2475 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002476
2477 if (!getLang().CPlusPlus0x)
2478 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2479 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002480 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002481 }
2482
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002483 // There are three options here. If we have 'enum foo;', then this is a
2484 // forward declaration. If we have 'enum foo {...' then this is a
2485 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2486 //
2487 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2488 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2489 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2490 //
John McCallfaf5fb42010-08-26 23:41:50 +00002491 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002492 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002493 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002494 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002495 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002496 else
John McCallfaf5fb42010-08-26 23:41:50 +00002497 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002498
2499 // enums cannot be templates, although they can be referenced from a
2500 // template.
2501 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002502 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002503 Diag(Tok, diag::err_enum_template);
2504
2505 // Skip the rest of this declarator, up until the comma or semicolon.
2506 SkipUntil(tok::comma, true);
2507 return;
2508 }
2509
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002510 if (!Name && TUK != Sema::TUK_Definition) {
2511 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2512
2513 // Skip the rest of this declarator, up until the comma or semicolon.
2514 SkipUntil(tok::comma, true);
2515 return;
2516 }
2517
Douglas Gregord6ab8742009-05-28 23:31:59 +00002518 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002519 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002520 const char *PrevSpec = 0;
2521 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002522 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002523 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002524 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002525 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002526 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002527 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002528
Douglas Gregorba41d012010-04-24 16:38:41 +00002529 if (IsDependent) {
2530 // This enum has a dependent nested-name-specifier. Handle it as a
2531 // dependent tag.
2532 if (!Name) {
2533 DS.SetTypeSpecError();
2534 Diag(Tok, diag::err_expected_type_name_after_typename);
2535 return;
2536 }
2537
Douglas Gregor0be31a22010-07-02 17:43:08 +00002538 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002539 TUK, SS, Name, StartLoc,
2540 NameLoc);
2541 if (Type.isInvalid()) {
2542 DS.SetTypeSpecError();
2543 return;
2544 }
2545
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002546 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2547 NameLoc.isValid() ? NameLoc : StartLoc,
2548 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002549 Diag(StartLoc, DiagID) << PrevSpec;
2550
2551 return;
2552 }
Mike Stump11289f42009-09-09 15:08:12 +00002553
John McCall48871652010-08-21 09:40:31 +00002554 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002555 // The action failed to produce an enumeration tag. If this is a
2556 // definition, consume the entire definition.
2557 if (Tok.is(tok::l_brace)) {
2558 ConsumeBrace();
2559 SkipUntil(tok::r_brace);
2560 }
2561
2562 DS.SetTypeSpecError();
2563 return;
2564 }
2565
Chris Lattner76c72282007-10-09 17:33:22 +00002566 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002567 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002568
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002569 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2570 NameLoc.isValid() ? NameLoc : StartLoc,
2571 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002572 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002573}
2574
Chris Lattnerc1915e22007-01-25 07:29:02 +00002575/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2576/// enumerator-list:
2577/// enumerator
2578/// enumerator-list ',' enumerator
2579/// enumerator:
2580/// enumeration-constant
2581/// enumeration-constant '=' constant-expression
2582/// enumeration-constant:
2583/// identifier
2584///
John McCall48871652010-08-21 09:40:31 +00002585void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002586 // Enter the scope of the enum body and start the definition.
2587 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002588 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002589
Chris Lattnerc1915e22007-01-25 07:29:02 +00002590 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002591
Chris Lattner37256fb2007-08-27 17:24:30 +00002592 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002593 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002594 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002595
John McCall48871652010-08-21 09:40:31 +00002596 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002597
John McCall48871652010-08-21 09:40:31 +00002598 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002599
Chris Lattnerc1915e22007-01-25 07:29:02 +00002600 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002601 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002602 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2603 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002604
John McCall811a0f52010-10-22 23:36:17 +00002605 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002606 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002607 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002608
Chris Lattnerc1915e22007-01-25 07:29:02 +00002609 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002610 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002611 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002612 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002613 AssignedVal = ParseConstantExpression();
2614 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002615 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002616 }
Mike Stump11289f42009-09-09 15:08:12 +00002617
Chris Lattnerc1915e22007-01-25 07:29:02 +00002618 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002619 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2620 LastEnumConstDecl,
2621 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002622 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002623 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002624 EnumConstantDecls.push_back(EnumConstDecl);
2625 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002626
Douglas Gregorce66d022010-09-07 14:51:08 +00002627 if (Tok.is(tok::identifier)) {
2628 // We're missing a comma between enumerators.
2629 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2630 Diag(Loc, diag::err_enumerator_list_missing_comma)
2631 << FixItHint::CreateInsertion(Loc, ", ");
2632 continue;
2633 }
2634
Chris Lattner76c72282007-10-09 17:33:22 +00002635 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002636 break;
2637 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002638
2639 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002640 !(getLang().C99 || getLang().CPlusPlus0x))
2641 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2642 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002643 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002644 }
Mike Stump11289f42009-09-09 15:08:12 +00002645
Chris Lattnerc1915e22007-01-25 07:29:02 +00002646 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002647 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002648
Chris Lattnerc1915e22007-01-25 07:29:02 +00002649 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002650 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002651 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002652
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002653 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2654 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002655 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002656
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002657 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002658 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002659}
Chris Lattner3b561a32006-08-13 00:12:11 +00002660
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002661/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002662/// start of a type-qualifier-list.
2663bool Parser::isTypeQualifier() const {
2664 switch (Tok.getKind()) {
2665 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002666
2667 // type-qualifier only in OpenCL
2668 case tok::kw_private:
2669 return getLang().OpenCL;
2670
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002671 // type-qualifier
2672 case tok::kw_const:
2673 case tok::kw_volatile:
2674 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002675 case tok::kw___private:
2676 case tok::kw___local:
2677 case tok::kw___global:
2678 case tok::kw___constant:
2679 case tok::kw___read_only:
2680 case tok::kw___read_write:
2681 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002682 return true;
2683 }
2684}
2685
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002686/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2687/// is definitely a type-specifier. Return false if it isn't part of a type
2688/// specifier or if we're not sure.
2689bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2690 switch (Tok.getKind()) {
2691 default: return false;
2692 // type-specifiers
2693 case tok::kw_short:
2694 case tok::kw_long:
2695 case tok::kw_signed:
2696 case tok::kw_unsigned:
2697 case tok::kw__Complex:
2698 case tok::kw__Imaginary:
2699 case tok::kw_void:
2700 case tok::kw_char:
2701 case tok::kw_wchar_t:
2702 case tok::kw_char16_t:
2703 case tok::kw_char32_t:
2704 case tok::kw_int:
2705 case tok::kw_float:
2706 case tok::kw_double:
2707 case tok::kw_bool:
2708 case tok::kw__Bool:
2709 case tok::kw__Decimal32:
2710 case tok::kw__Decimal64:
2711 case tok::kw__Decimal128:
2712 case tok::kw___vector:
2713
2714 // struct-or-union-specifier (C99) or class-specifier (C++)
2715 case tok::kw_class:
2716 case tok::kw_struct:
2717 case tok::kw_union:
2718 // enum-specifier
2719 case tok::kw_enum:
2720
2721 // typedef-name
2722 case tok::annot_typename:
2723 return true;
2724 }
2725}
2726
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002727/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002728/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002729bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002730 switch (Tok.getKind()) {
2731 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002732
Chris Lattner020bab92009-01-04 23:41:41 +00002733 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002734 if (TryAltiVecVectorToken())
2735 return true;
2736 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002737 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002738 // Annotate typenames and C++ scope specifiers. If we get one, just
2739 // recurse to handle whatever we get.
2740 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002741 return true;
2742 if (Tok.is(tok::identifier))
2743 return false;
2744 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002745
Chris Lattner020bab92009-01-04 23:41:41 +00002746 case tok::coloncolon: // ::foo::bar
2747 if (NextToken().is(tok::kw_new) || // ::new
2748 NextToken().is(tok::kw_delete)) // ::delete
2749 return false;
2750
Chris Lattner020bab92009-01-04 23:41:41 +00002751 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002752 return true;
2753 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002754
Chris Lattnere37e2332006-08-15 04:50:22 +00002755 // GNU attributes support.
2756 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002757 // GNU typeof support.
2758 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002759
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002760 // type-specifiers
2761 case tok::kw_short:
2762 case tok::kw_long:
2763 case tok::kw_signed:
2764 case tok::kw_unsigned:
2765 case tok::kw__Complex:
2766 case tok::kw__Imaginary:
2767 case tok::kw_void:
2768 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002769 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002770 case tok::kw_char16_t:
2771 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002772 case tok::kw_int:
2773 case tok::kw_float:
2774 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002775 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002776 case tok::kw__Bool:
2777 case tok::kw__Decimal32:
2778 case tok::kw__Decimal64:
2779 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002780 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002781
Chris Lattner861a2262008-04-13 18:59:07 +00002782 // struct-or-union-specifier (C99) or class-specifier (C++)
2783 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002784 case tok::kw_struct:
2785 case tok::kw_union:
2786 // enum-specifier
2787 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002788
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002789 // type-qualifier
2790 case tok::kw_const:
2791 case tok::kw_volatile:
2792 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002793
2794 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002795 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002796 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002797
Chris Lattner409bf7d2008-10-20 00:25:30 +00002798 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2799 case tok::less:
2800 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002801
Steve Naroff44ac7772008-12-25 14:16:32 +00002802 case tok::kw___cdecl:
2803 case tok::kw___stdcall:
2804 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002805 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002806 case tok::kw___w64:
2807 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002808 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002809
2810 case tok::kw___private:
2811 case tok::kw___local:
2812 case tok::kw___global:
2813 case tok::kw___constant:
2814 case tok::kw___read_only:
2815 case tok::kw___read_write:
2816 case tok::kw___write_only:
2817
Eli Friedman53339e02009-06-08 23:27:34 +00002818 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002819
2820 case tok::kw_private:
2821 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002822 }
2823}
2824
Chris Lattneracd58a32006-08-06 17:24:14 +00002825/// isDeclarationSpecifier() - Return true if the current token is part of a
2826/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002827///
2828/// \param DisambiguatingWithExpression True to indicate that the purpose of
2829/// this check is to disambiguate between an expression and a declaration.
2830bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002831 switch (Tok.getKind()) {
2832 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002833
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002834 case tok::kw_private:
2835 return getLang().OpenCL;
2836
Chris Lattner020bab92009-01-04 23:41:41 +00002837 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002838 // Unfortunate hack to support "Class.factoryMethod" notation.
2839 if (getLang().ObjC1 && NextToken().is(tok::period))
2840 return false;
John Thompson22334602010-02-05 00:12:22 +00002841 if (TryAltiVecVectorToken())
2842 return true;
2843 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002844 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002845 // Annotate typenames and C++ scope specifiers. If we get one, just
2846 // recurse to handle whatever we get.
2847 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002848 return true;
2849 if (Tok.is(tok::identifier))
2850 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002851
2852 // If we're in Objective-C and we have an Objective-C class type followed
2853 // by an identifier and then either ':' or ']', in a place where an
2854 // expression is permitted, then this is probably a class message send
2855 // missing the initial '['. In this case, we won't consider this to be
2856 // the start of a declaration.
2857 if (DisambiguatingWithExpression &&
2858 isStartOfObjCClassMessageMissingOpenBracket())
2859 return false;
2860
John McCall1f476a12010-02-26 08:45:28 +00002861 return isDeclarationSpecifier();
2862
Chris Lattner020bab92009-01-04 23:41:41 +00002863 case tok::coloncolon: // ::foo::bar
2864 if (NextToken().is(tok::kw_new) || // ::new
2865 NextToken().is(tok::kw_delete)) // ::delete
2866 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002867
Chris Lattner020bab92009-01-04 23:41:41 +00002868 // Annotate typenames and C++ scope specifiers. If we get one, just
2869 // recurse to handle whatever we get.
2870 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002871 return true;
2872 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002873
Chris Lattneracd58a32006-08-06 17:24:14 +00002874 // storage-class-specifier
2875 case tok::kw_typedef:
2876 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002877 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002878 case tok::kw_static:
2879 case tok::kw_auto:
2880 case tok::kw_register:
2881 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002882
Chris Lattneracd58a32006-08-06 17:24:14 +00002883 // type-specifiers
2884 case tok::kw_short:
2885 case tok::kw_long:
2886 case tok::kw_signed:
2887 case tok::kw_unsigned:
2888 case tok::kw__Complex:
2889 case tok::kw__Imaginary:
2890 case tok::kw_void:
2891 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002892 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002893 case tok::kw_char16_t:
2894 case tok::kw_char32_t:
2895
Chris Lattneracd58a32006-08-06 17:24:14 +00002896 case tok::kw_int:
2897 case tok::kw_float:
2898 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002899 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002900 case tok::kw__Bool:
2901 case tok::kw__Decimal32:
2902 case tok::kw__Decimal64:
2903 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002904 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002905
Chris Lattner861a2262008-04-13 18:59:07 +00002906 // struct-or-union-specifier (C99) or class-specifier (C++)
2907 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002908 case tok::kw_struct:
2909 case tok::kw_union:
2910 // enum-specifier
2911 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002912
Chris Lattneracd58a32006-08-06 17:24:14 +00002913 // type-qualifier
2914 case tok::kw_const:
2915 case tok::kw_volatile:
2916 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002917
Chris Lattneracd58a32006-08-06 17:24:14 +00002918 // function-specifier
2919 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002920 case tok::kw_virtual:
2921 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002922
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002923 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002924 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002925
Chris Lattner599e47e2007-08-09 17:01:07 +00002926 // GNU typeof support.
2927 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002928
Chris Lattner599e47e2007-08-09 17:01:07 +00002929 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002930 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002931 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002932
Chris Lattner8b2ec162008-07-26 03:38:44 +00002933 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2934 case tok::less:
2935 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002936
Steve Narofff192fab2009-01-06 19:34:12 +00002937 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002938 case tok::kw___cdecl:
2939 case tok::kw___stdcall:
2940 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002941 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002942 case tok::kw___w64:
2943 case tok::kw___ptr64:
2944 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002945 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002946
2947 case tok::kw___private:
2948 case tok::kw___local:
2949 case tok::kw___global:
2950 case tok::kw___constant:
2951 case tok::kw___read_only:
2952 case tok::kw___read_write:
2953 case tok::kw___write_only:
2954
Eli Friedman53339e02009-06-08 23:27:34 +00002955 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002956 }
2957}
2958
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002959bool Parser::isConstructorDeclarator() {
2960 TentativeParsingAction TPA(*this);
2961
2962 // Parse the C++ scope specifier.
2963 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002964 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002965 TPA.Revert();
2966 return false;
2967 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002968
2969 // Parse the constructor name.
2970 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2971 // We already know that we have a constructor name; just consume
2972 // the token.
2973 ConsumeToken();
2974 } else {
2975 TPA.Revert();
2976 return false;
2977 }
2978
2979 // Current class name must be followed by a left parentheses.
2980 if (Tok.isNot(tok::l_paren)) {
2981 TPA.Revert();
2982 return false;
2983 }
2984 ConsumeParen();
2985
2986 // A right parentheses or ellipsis signals that we have a constructor.
2987 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2988 TPA.Revert();
2989 return true;
2990 }
2991
2992 // If we need to, enter the specified scope.
2993 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002994 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002995 DeclScopeObj.EnterDeclaratorScope();
2996
Francois Pichet79f3a872011-01-31 04:54:32 +00002997 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00002998 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00002999 MaybeParseMicrosoftAttributes(Attrs);
3000
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003001 // Check whether the next token(s) are part of a declaration
3002 // specifier, in which case we have the start of a parameter and,
3003 // therefore, we know that this is a constructor.
3004 bool IsConstructor = isDeclarationSpecifier();
3005 TPA.Revert();
3006 return IsConstructor;
3007}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003008
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003009/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003010/// type-qualifier-list: [C99 6.7.5]
3011/// type-qualifier
3012/// [vendor] attributes
3013/// [ only if VendorAttributesAllowed=true ]
3014/// type-qualifier-list type-qualifier
3015/// [vendor] type-qualifier-list attributes
3016/// [ only if VendorAttributesAllowed=true ]
3017/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3018/// [ only if CXX0XAttributesAllowed=true ]
3019/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003020///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003021void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3022 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003023 bool CXX0XAttributesAllowed) {
3024 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3025 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003026 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003027 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003028 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003029 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003030 else
3031 Diag(Loc, diag::err_attributes_not_allowed);
3032 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003033
3034 SourceLocation EndLoc;
3035
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003036 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003037 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003038 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003039 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003040 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003041
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003042 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003043 case tok::code_completion:
3044 Actions.CodeCompleteTypeQualifiers(DS);
3045 ConsumeCodeCompletionToken();
3046 break;
3047
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003048 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003049 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3050 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003051 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003052 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003053 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3054 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003055 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003056 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003057 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3058 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003059 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003060
3061 // OpenCL qualifiers:
3062 case tok::kw_private:
3063 if (!getLang().OpenCL)
3064 goto DoneWithTypeQuals;
3065 case tok::kw___private:
3066 case tok::kw___global:
3067 case tok::kw___local:
3068 case tok::kw___constant:
3069 case tok::kw___read_only:
3070 case tok::kw___write_only:
3071 case tok::kw___read_write:
3072 ParseOpenCLQualifiers(DS);
3073 break;
3074
Eli Friedman53339e02009-06-08 23:27:34 +00003075 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003076 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003077 case tok::kw___cdecl:
3078 case tok::kw___stdcall:
3079 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003080 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003081 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003082 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003083 continue;
3084 }
3085 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003086 case tok::kw___pascal:
3087 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003088 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003089 continue;
3090 }
3091 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003092 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003093 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003094 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003095 continue; // do *not* consume the next token!
3096 }
3097 // otherwise, FALL THROUGH!
3098 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003099 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003100 // If this is not a type-qualifier token, we're done reading type
3101 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003102 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003103 if (EndLoc.isValid())
3104 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003105 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003106 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003107
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003108 // If the specifier combination wasn't legal, issue a diagnostic.
3109 if (isInvalid) {
3110 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003111 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003112 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003113 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003114 }
3115}
3116
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003117
3118/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3119///
3120void Parser::ParseDeclarator(Declarator &D) {
3121 /// This implements the 'declarator' production in the C grammar, then checks
3122 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003123 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003124}
3125
Sebastian Redlbd150f42008-11-21 19:14:01 +00003126/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3127/// is parsed by the function passed to it. Pass null, and the direct-declarator
3128/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003129/// ptr-operator production.
3130///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003131/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3132/// [C] pointer[opt] direct-declarator
3133/// [C++] direct-declarator
3134/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003135///
3136/// pointer: [C99 6.7.5]
3137/// '*' type-qualifier-list[opt]
3138/// '*' type-qualifier-list[opt] pointer
3139///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003140/// ptr-operator:
3141/// '*' cv-qualifier-seq[opt]
3142/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003143/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003144/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003145/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003146/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003147void Parser::ParseDeclaratorInternal(Declarator &D,
3148 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003149 if (Diags.hasAllExtensionsSilenced())
3150 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003151
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003152 // C++ member pointers start with a '::' or a nested-name.
3153 // Member pointers get special handling, since there's no place for the
3154 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003155 if (getLang().CPlusPlus &&
3156 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3157 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003158 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003159 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003160
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003161 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003162 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003163 // The scope spec really belongs to the direct-declarator.
3164 D.getCXXScopeSpec() = SS;
3165 if (DirectDeclParser)
3166 (this->*DirectDeclParser)(D);
3167 return;
3168 }
3169
3170 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003171 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003172 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003173 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003174 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003175
3176 // Recurse to parse whatever is left.
3177 ParseDeclaratorInternal(D, DirectDeclParser);
3178
3179 // Sema will have to catch (syntactically invalid) pointers into global
3180 // scope. It has to catch pointers into namespace scope anyway.
3181 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003182 Loc),
3183 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003184 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003185 return;
3186 }
3187 }
3188
3189 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003190 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003191 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003192 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003193 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003194 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003195 if (DirectDeclParser)
3196 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003197 return;
3198 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003199
Sebastian Redled0f3b02009-03-15 22:02:01 +00003200 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3201 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003202 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003203 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003204
Chris Lattner9eac9312009-03-27 04:18:06 +00003205 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003206 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003207 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003208
Bill Wendling3708c182007-05-27 10:15:43 +00003209 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003210 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003211
Bill Wendling3708c182007-05-27 10:15:43 +00003212 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003213 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003214 if (Kind == tok::star)
3215 // Remember that we parsed a pointer type, and remember the type-quals.
3216 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003217 DS.getConstSpecLoc(),
3218 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003219 DS.getRestrictSpecLoc()),
3220 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003221 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003222 else
3223 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003224 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003225 Loc),
3226 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003227 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003228 } else {
3229 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003230 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003231
Sebastian Redl3b27be62009-03-23 00:00:23 +00003232 // Complain about rvalue references in C++03, but then go on and build
3233 // the declarator.
3234 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003235 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003236
Bill Wendling93efb222007-06-02 23:28:54 +00003237 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3238 // cv-qualifiers are introduced through the use of a typedef or of a
3239 // template type argument, in which case the cv-qualifiers are ignored.
3240 //
3241 // [GNU] Retricted references are allowed.
3242 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003243 // [C++0x] Attributes on references are not allowed.
3244 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003245 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003246
3247 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3248 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3249 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003250 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003251 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3252 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003253 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003254 }
Bill Wendling3708c182007-05-27 10:15:43 +00003255
3256 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003257 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003258
Douglas Gregor66583c52008-11-03 15:51:28 +00003259 if (D.getNumTypeObjects() > 0) {
3260 // C++ [dcl.ref]p4: There shall be no references to references.
3261 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3262 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003263 if (const IdentifierInfo *II = D.getIdentifier())
3264 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3265 << II;
3266 else
3267 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3268 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003269
Sebastian Redlbd150f42008-11-21 19:14:01 +00003270 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003271 // can go ahead and build the (technically ill-formed)
3272 // declarator: reference collapsing will take care of it.
3273 }
3274 }
3275
Bill Wendling3708c182007-05-27 10:15:43 +00003276 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003277 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003278 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003279 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003280 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003281 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003282}
3283
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003284/// ParseDirectDeclarator
3285/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003286/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003287/// '(' declarator ')'
3288/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003289/// [C90] direct-declarator '[' constant-expression[opt] ']'
3290/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3291/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3292/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3293/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003294/// direct-declarator '(' parameter-type-list ')'
3295/// direct-declarator '(' identifier-list[opt] ')'
3296/// [GNU] direct-declarator '(' parameter-forward-declarations
3297/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003298/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3299/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003300/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003301///
3302/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003303/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003304/// '::'[opt] nested-name-specifier[opt] type-name
3305///
3306/// id-expression: [C++ 5.1]
3307/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003308/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003309///
3310/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003311/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003312/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003313/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003314/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003315/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003316///
Chris Lattneracd58a32006-08-06 17:24:14 +00003317void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003318 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003319
Douglas Gregor7861a802009-11-03 01:35:08 +00003320 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3321 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003322 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003323 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003324 }
3325
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003326 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003327 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003328 // Change the declaration context for name lookup, until this function
3329 // is exited (and the declarator has been parsed).
3330 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003331 }
3332
Douglas Gregor27b4c162010-12-23 22:44:42 +00003333 // C++0x [dcl.fct]p14:
3334 // There is a syntactic ambiguity when an ellipsis occurs at the end
3335 // of a parameter-declaration-clause without a preceding comma. In
3336 // this case, the ellipsis is parsed as part of the
3337 // abstract-declarator if the type of the parameter names a template
3338 // parameter pack that has not been expanded; otherwise, it is parsed
3339 // as part of the parameter-declaration-clause.
3340 if (Tok.is(tok::ellipsis) &&
3341 !((D.getContext() == Declarator::PrototypeContext ||
3342 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003343 NextToken().is(tok::r_paren) &&
3344 !Actions.containsUnexpandedParameterPacks(D)))
3345 D.setEllipsisLoc(ConsumeToken());
3346
Douglas Gregor7861a802009-11-03 01:35:08 +00003347 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3348 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3349 // We found something that indicates the start of an unqualified-id.
3350 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003351 bool AllowConstructorName;
3352 if (D.getDeclSpec().hasTypeSpecifier())
3353 AllowConstructorName = false;
3354 else if (D.getCXXScopeSpec().isSet())
3355 AllowConstructorName =
3356 (D.getContext() == Declarator::FileContext ||
3357 (D.getContext() == Declarator::MemberContext &&
3358 D.getDeclSpec().isFriendSpecified()));
3359 else
3360 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3361
Douglas Gregor7861a802009-11-03 01:35:08 +00003362 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3363 /*EnteringContext=*/true,
3364 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003365 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003366 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003367 D.getName()) ||
3368 // Once we're past the identifier, if the scope was bad, mark the
3369 // whole declarator bad.
3370 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003371 D.SetIdentifier(0, Tok.getLocation());
3372 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003373 } else {
3374 // Parsed the unqualified-id; update range information and move along.
3375 if (D.getSourceRange().getBegin().isInvalid())
3376 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3377 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003378 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003379 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003380 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003381 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003382 assert(!getLang().CPlusPlus &&
3383 "There's a C++-specific check for tok::identifier above");
3384 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3385 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3386 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003387 goto PastIdentifier;
3388 }
3389
3390 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003391 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003392 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003393 // Example: 'char (*X)' or 'int (*XX)(void)'
3394 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003395
3396 // If the declarator was parenthesized, we entered the declarator
3397 // scope when parsing the parenthesized declarator, then exited
3398 // the scope already. Re-enter the scope, if we need to.
3399 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003400 // If there was an error parsing parenthesized declarator, declarator
3401 // scope may have been enterred before. Don't do it again.
3402 if (!D.isInvalidType() &&
3403 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003404 // Change the declaration context for name lookup, until this function
3405 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003406 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003407 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003408 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003409 // This could be something simple like "int" (in which case the declarator
3410 // portion is empty), if an abstract-declarator is allowed.
3411 D.SetIdentifier(0, Tok.getLocation());
3412 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003413 if (D.getContext() == Declarator::MemberContext)
3414 Diag(Tok, diag::err_expected_member_name_or_semi)
3415 << D.getDeclSpec().getSourceRange();
3416 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003417 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003418 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003419 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003420 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003421 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003422 }
Mike Stump11289f42009-09-09 15:08:12 +00003423
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003424 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003425 assert(D.isPastIdentifier() &&
3426 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003427
Alexis Hunt96d5c762009-11-21 08:43:09 +00003428 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003429 if (D.getIdentifier())
3430 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003431
Chris Lattneracd58a32006-08-06 17:24:14 +00003432 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003433 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003434 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3435 // In such a case, check if we actually have a function declarator; if it
3436 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003437 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3438 // When not in file scope, warn for ambiguous function declarators, just
3439 // in case the author intended it as a variable definition.
3440 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3441 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3442 break;
3443 }
John McCall084e83d2011-03-24 11:26:52 +00003444 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003445 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003446 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003447 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003448 } else {
3449 break;
3450 }
3451 }
3452}
3453
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003454/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3455/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003456/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003457/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3458///
3459/// direct-declarator:
3460/// '(' declarator ')'
3461/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003462/// direct-declarator '(' parameter-type-list ')'
3463/// direct-declarator '(' identifier-list[opt] ')'
3464/// [GNU] direct-declarator '(' parameter-forward-declarations
3465/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003466///
3467void Parser::ParseParenDeclarator(Declarator &D) {
3468 SourceLocation StartLoc = ConsumeParen();
3469 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003470
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003471 // Eat any attributes before we look at whether this is a grouping or function
3472 // declarator paren. If this is a grouping paren, the attribute applies to
3473 // the type being built up, for example:
3474 // int (__attribute__(()) *x)(long y)
3475 // If this ends up not being a grouping paren, the attribute applies to the
3476 // first argument, for example:
3477 // int (__attribute__(()) int x)
3478 // In either case, we need to eat any attributes to be able to determine what
3479 // sort of paren this is.
3480 //
John McCall084e83d2011-03-24 11:26:52 +00003481 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003482 bool RequiresArg = false;
3483 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003484 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003485
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003486 // We require that the argument list (if this is a non-grouping paren) be
3487 // present even if the attribute list was empty.
3488 RequiresArg = true;
3489 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003490 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003491 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003492 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3493 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003494 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003495 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003496 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003497 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003498 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003499
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003500 // If we haven't past the identifier yet (or where the identifier would be
3501 // stored, if this is an abstract declarator), then this is probably just
3502 // grouping parens. However, if this could be an abstract-declarator, then
3503 // this could also be the start of function arguments (consider 'void()').
3504 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003505
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003506 if (!D.mayOmitIdentifier()) {
3507 // If this can't be an abstract-declarator, this *must* be a grouping
3508 // paren, because we haven't seen the identifier yet.
3509 isGrouping = true;
3510 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003511 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003512 isDeclarationSpecifier()) { // 'int(int)' is a function.
3513 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3514 // considered to be a type, not a K&R identifier-list.
3515 isGrouping = false;
3516 } else {
3517 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3518 isGrouping = true;
3519 }
Mike Stump11289f42009-09-09 15:08:12 +00003520
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003521 // If this is a grouping paren, handle:
3522 // direct-declarator: '(' declarator ')'
3523 // direct-declarator: '(' attributes declarator ')'
3524 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003525 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003526 D.setGroupingParens(true);
3527
Sebastian Redlbd150f42008-11-21 19:14:01 +00003528 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003529 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003530 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003531 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3532 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003533
3534 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003535 return;
3536 }
Mike Stump11289f42009-09-09 15:08:12 +00003537
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003538 // Okay, if this wasn't a grouping paren, it must be the start of a function
3539 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003540 // identifier (and remember where it would have been), then call into
3541 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003542 D.SetIdentifier(0, Tok.getLocation());
3543
John McCall53fa7142010-12-24 02:08:15 +00003544 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003545}
3546
3547/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3548/// declarator D up to a paren, which indicates that we are parsing function
3549/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003550///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003551/// If AttrList is non-null, then the caller parsed those arguments immediately
3552/// after the open paren - they should be considered to be the first argument of
3553/// a parameter. If RequiresArg is true, then the first argument of the
3554/// function is required to be present and required to not be an identifier
3555/// list.
3556///
Chris Lattneracd58a32006-08-06 17:24:14 +00003557/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003558/// parameter-type-list: [C99 6.7.5]
3559/// parameter-list
3560/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003561/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003562///
3563/// parameter-list: [C99 6.7.5]
3564/// parameter-declaration
3565/// parameter-list ',' parameter-declaration
3566///
3567/// parameter-declaration: [C99 6.7.5]
3568/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003569/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003570/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003571/// declaration-specifiers abstract-declarator[opt]
3572/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003573/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003574/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003575///
Douglas Gregor54992352011-01-26 03:43:54 +00003576/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3577/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003578///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003579/// [C++0x] exception-specification:
3580/// dynamic-exception-specification
3581/// noexcept-specification
3582///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003583void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003584 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003585 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003586 // lparen is already consumed!
3587 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003588
Douglas Gregor7fb25412010-10-01 18:44:50 +00003589 ParsedType TrailingReturnType;
3590
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003591 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003592 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003593 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003594 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003595
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003596 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003597
3598 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003599 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003600 SourceLocation RefQualifierLoc;
3601 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003602 ExceptionSpecificationType ESpecType = EST_None;
3603 SourceRange ESpecRange;
3604 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3605 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3606 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003607 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003608 MaybeParseCXX0XAttributes(attrs);
3609
Chris Lattnercf0bab22008-12-18 07:02:59 +00003610 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003611 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003612 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003613
Douglas Gregor54992352011-01-26 03:43:54 +00003614 // Parse ref-qualifier[opt]
3615 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3616 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003617 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003618
Douglas Gregor54992352011-01-26 03:43:54 +00003619 RefQualifierIsLValueRef = Tok.is(tok::amp);
3620 RefQualifierLoc = ConsumeToken();
3621 EndLoc = RefQualifierLoc;
3622 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003623
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003624 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003625 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3626 DynamicExceptions,
3627 DynamicExceptionRanges,
3628 NoexceptExpr);
3629 if (ESpecType != EST_None)
3630 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003631
3632 // Parse trailing-return-type.
3633 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3634 TrailingReturnType = ParseTrailingReturnType().get();
3635 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003636 }
3637
Chris Lattner371ed4e2008-04-06 06:57:35 +00003638 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003639 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003640 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003641 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003642 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003643 /*arglist*/ 0, 0,
3644 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003645 RefQualifierIsLValueRef,
3646 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003647 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003648 DynamicExceptions.data(),
3649 DynamicExceptionRanges.data(),
3650 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003651 NoexceptExpr.isUsable() ?
3652 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003653 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003654 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003655 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003656 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003657 }
3658
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003659 // Alternatively, this parameter list may be an identifier list form for a
3660 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003661 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3662 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003663 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003664 // K&R identifier lists can't have typedefs as identifiers, per
3665 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003666 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003667 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003668
Steve Naroffb0486722009-01-28 19:16:40 +00003669 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003670 // normal declarators, not for abstract-declarators. Get the first
3671 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003672 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003673 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003674
3675 // Identifier lists follow a really simple grammar: the identifiers can
3676 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3677 // identifier lists are really rare in the brave new modern world, and it
3678 // is very common for someone to typo a type in a non-k&r style list. If
3679 // we are presented with something like: "void foo(intptr x, float y)",
3680 // we don't want to start parsing the function declarator as though it is
3681 // a K&R style declarator just because intptr is an invalid type.
3682 //
3683 // To handle this, we check to see if the token after the first identifier
3684 // is a "," or ")". Only if so, do we parse it as an identifier list.
3685 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3686 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3687 FirstTok.getIdentifierInfo(),
3688 FirstTok.getLocation(), D);
3689
3690 // If we get here, the code is invalid. Push the first identifier back
3691 // into the token stream and parse the first argument as an (invalid)
3692 // normal argument declarator.
3693 PP.EnterToken(Tok);
3694 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003695 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003696 }
Mike Stump11289f42009-09-09 15:08:12 +00003697
Chris Lattner371ed4e2008-04-06 06:57:35 +00003698 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003699
Chris Lattner371ed4e2008-04-06 06:57:35 +00003700 // Build up an array of information about the parsed arguments.
3701 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003702
3703 // Enter function-declaration scope, limiting any declarators to the
3704 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003705 ParseScope PrototypeScope(this,
3706 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003707
Chris Lattner371ed4e2008-04-06 06:57:35 +00003708 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003709 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003710 while (1) {
3711 if (Tok.is(tok::ellipsis)) {
3712 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003713 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003714 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003715 }
Mike Stump11289f42009-09-09 15:08:12 +00003716
Chris Lattner371ed4e2008-04-06 06:57:35 +00003717 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003718 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003719 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003720
3721 // Skip any Microsoft attributes before a param.
3722 if (getLang().Microsoft && Tok.is(tok::l_square))
3723 ParseMicrosoftAttributes(DS.getAttributes());
3724
3725 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003726
3727 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003728 // Take them so that we only apply the attributes to the first parameter.
3729 DS.takeAttributesFrom(attrs);
3730
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003731 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003732
Chris Lattner371ed4e2008-04-06 06:57:35 +00003733 // Parse the declarator. This is "PrototypeContext", because we must
3734 // accept either 'declarator' or 'abstract-declarator' here.
3735 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3736 ParseDeclarator(ParmDecl);
3737
3738 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003739 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003740
Chris Lattner371ed4e2008-04-06 06:57:35 +00003741 // Remember this parsed parameter in ParamInfo.
3742 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003743
Douglas Gregor4d87df52008-12-16 21:30:33 +00003744 // DefArgToks is used when the parsing of default arguments needs
3745 // to be delayed.
3746 CachedTokens *DefArgToks = 0;
3747
Chris Lattner371ed4e2008-04-06 06:57:35 +00003748 // If no parameter was specified, verify that *something* was specified,
3749 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003750 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3751 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003752 // Completely missing, emit error.
3753 Diag(DSStart, diag::err_missing_param);
3754 } else {
3755 // Otherwise, we have something. Add it and let semantic analysis try
3756 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003757
Chris Lattner371ed4e2008-04-06 06:57:35 +00003758 // Inform the actions module about the parameter declarator, so it gets
3759 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003760 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003761
3762 // Parse the default argument, if any. We parse the default
3763 // arguments in all dialects; the semantic analysis in
3764 // ActOnParamDefaultArgument will reject the default argument in
3765 // C.
3766 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003767 SourceLocation EqualLoc = Tok.getLocation();
3768
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003769 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003770 if (D.getContext() == Declarator::MemberContext) {
3771 // If we're inside a class definition, cache the tokens
3772 // corresponding to the default argument. We'll actually parse
3773 // them when we see the end of the class definition.
3774 // FIXME: Templates will require something similar.
3775 // FIXME: Can we use a smart pointer for Toks?
3776 DefArgToks = new CachedTokens;
3777
Mike Stump11289f42009-09-09 15:08:12 +00003778 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003779 /*StopAtSemi=*/true,
3780 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003781 delete DefArgToks;
3782 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003783 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003784 } else {
3785 // Mark the end of the default argument so that we know when to
3786 // stop when we parse it later on.
3787 Token DefArgEnd;
3788 DefArgEnd.startToken();
3789 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3790 DefArgEnd.setLocation(Tok.getLocation());
3791 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003792 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003793 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003794 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003795 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003796 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003797 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003798
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003799 // The argument isn't actually potentially evaluated unless it is
3800 // used.
3801 EnterExpressionEvaluationContext Eval(Actions,
3802 Sema::PotentiallyEvaluatedIfUsed);
3803
John McCalldadc5752010-08-24 06:29:42 +00003804 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003805 if (DefArgResult.isInvalid()) {
3806 Actions.ActOnParamDefaultArgumentError(Param);
3807 SkipUntil(tok::comma, tok::r_paren, true, true);
3808 } else {
3809 // Inform the actions module about the default argument
3810 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003811 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003812 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003813 }
3814 }
Mike Stump11289f42009-09-09 15:08:12 +00003815
3816 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3817 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003818 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003819 }
3820
3821 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003822 if (Tok.isNot(tok::comma)) {
3823 if (Tok.is(tok::ellipsis)) {
3824 IsVariadic = true;
3825 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3826
3827 if (!getLang().CPlusPlus) {
3828 // We have ellipsis without a preceding ',', which is ill-formed
3829 // in C. Complain and provide the fix.
3830 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003831 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003832 }
3833 }
3834
3835 break;
3836 }
Mike Stump11289f42009-09-09 15:08:12 +00003837
Chris Lattner371ed4e2008-04-06 06:57:35 +00003838 // Consume the comma.
3839 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003840 }
Mike Stump11289f42009-09-09 15:08:12 +00003841
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003842 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003843 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003844
John McCall084e83d2011-03-24 11:26:52 +00003845 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003846 SourceLocation RefQualifierLoc;
3847 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003848 ExceptionSpecificationType ESpecType = EST_None;
3849 SourceRange ESpecRange;
3850 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3851 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3852 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003853
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003854 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003855 MaybeParseCXX0XAttributes(attrs);
3856
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003857 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003858 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003859 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003860 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003861
Douglas Gregor54992352011-01-26 03:43:54 +00003862 // Parse ref-qualifier[opt]
3863 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3864 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003865 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003866
3867 RefQualifierIsLValueRef = Tok.is(tok::amp);
3868 RefQualifierLoc = ConsumeToken();
3869 EndLoc = RefQualifierLoc;
3870 }
3871
Sebastian Redl965b0e32011-03-05 14:45:16 +00003872 // FIXME: We should leave the prototype scope before parsing the exception
3873 // specification, and then reenter it when parsing the trailing return type.
3874 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3875
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003876 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003877 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3878 DynamicExceptions,
3879 DynamicExceptionRanges,
3880 NoexceptExpr);
3881 if (ESpecType != EST_None)
3882 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003883
3884 // Parse trailing-return-type.
3885 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3886 TrailingReturnType = ParseTrailingReturnType().get();
3887 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003888 }
3889
Douglas Gregor7fb25412010-10-01 18:44:50 +00003890 // Leave prototype scope.
3891 PrototypeScope.Exit();
3892
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003893 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003894 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003895 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003896 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003897 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003898 RefQualifierIsLValueRef,
3899 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003900 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003901 DynamicExceptions.data(),
3902 DynamicExceptionRanges.data(),
3903 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003904 NoexceptExpr.isUsable() ?
3905 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003906 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003907 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003908 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003909}
Chris Lattneracd58a32006-08-06 17:24:14 +00003910
Chris Lattner6c940e62008-04-06 06:34:08 +00003911/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3912/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003913/// first identifier has already been consumed, and the current token is the
3914/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003915///
3916/// identifier-list: [C99 6.7.5]
3917/// identifier
3918/// identifier-list ',' identifier
3919///
3920void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003921 IdentifierInfo *FirstIdent,
3922 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003923 Declarator &D) {
3924 // Build up an array of information about the parsed arguments.
3925 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3926 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003927
Chris Lattner6c940e62008-04-06 06:34:08 +00003928 // If there was no identifier specified for the declarator, either we are in
3929 // an abstract-declarator, or we are in a parameter declarator which was found
3930 // to be abstract. In abstract-declarators, identifier lists are not valid:
3931 // diagnose this.
3932 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003933 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003934
Chris Lattner9453ab82010-05-14 17:23:36 +00003935 // The first identifier was already read, and is known to be the first
3936 // identifier in the list. Remember this identifier in ParamInfo.
3937 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003938 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003939
Chris Lattner6c940e62008-04-06 06:34:08 +00003940 while (Tok.is(tok::comma)) {
3941 // Eat the comma.
3942 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003943
Chris Lattner9186f552008-04-06 06:39:19 +00003944 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003945 if (Tok.isNot(tok::identifier)) {
3946 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003947 SkipUntil(tok::r_paren);
3948 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003949 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003950
Chris Lattner6c940e62008-04-06 06:34:08 +00003951 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003952
3953 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003954 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003955 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003956
Chris Lattner6c940e62008-04-06 06:34:08 +00003957 // Verify that the argument identifier has not already been mentioned.
3958 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003959 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003960 } else {
3961 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003962 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003963 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003964 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003965 }
Mike Stump11289f42009-09-09 15:08:12 +00003966
Chris Lattner6c940e62008-04-06 06:34:08 +00003967 // Eat the identifier.
3968 ConsumeToken();
3969 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003970
3971 // If we have the closing ')', eat it and we're done.
3972 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3973
Chris Lattner9186f552008-04-06 06:39:19 +00003974 // Remember that we parsed a function type, and remember the attributes. This
3975 // function type is always a K&R style function type, which is not varargs and
3976 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00003977 ParsedAttributes attrs(AttrFactory);
3978 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003979 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003980 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003981 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003982 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003983 EST_None, SourceLocation(), 0, 0,
3984 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00003985 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003986}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003987
Chris Lattnere8074e62006-08-06 18:30:15 +00003988/// [C90] direct-declarator '[' constant-expression[opt] ']'
3989/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3990/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3991/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3992/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3993void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003994 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003995
Chris Lattner84a11622008-12-18 07:27:21 +00003996 // C array syntax has many features, but by-far the most common is [] and [4].
3997 // This code does a fast path to handle some of the most obvious cases.
3998 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003999 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004000 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004001 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004002
Chris Lattner84a11622008-12-18 07:27:21 +00004003 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004004 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004005 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004006 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004007 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004008 return;
4009 } else if (Tok.getKind() == tok::numeric_constant &&
4010 GetLookAheadToken(1).is(tok::r_square)) {
4011 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004012 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004013 ConsumeToken();
4014
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004015 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004016 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004017 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004018
Chris Lattner84a11622008-12-18 07:27:21 +00004019 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004020 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004021 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004022 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004023 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004024 return;
4025 }
Mike Stump11289f42009-09-09 15:08:12 +00004026
Chris Lattnere8074e62006-08-06 18:30:15 +00004027 // If valid, this location is the position where we read the 'static' keyword.
4028 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004029 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004030 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004031
Chris Lattnere8074e62006-08-06 18:30:15 +00004032 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004033 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004034 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004035 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004036
Chris Lattnere8074e62006-08-06 18:30:15 +00004037 // If we haven't already read 'static', check to see if there is one after the
4038 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004039 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004040 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004041
Chris Lattnere8074e62006-08-06 18:30:15 +00004042 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004043 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004044 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004045
Chris Lattner521ff2b2008-04-06 05:26:30 +00004046 // Handle the case where we have '[*]' as the array size. However, a leading
4047 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4048 // the the token after the star is a ']'. Since stars in arrays are
4049 // infrequent, use of lookahead is not costly here.
4050 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004051 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004052
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004053 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004054 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004055 StaticLoc = SourceLocation(); // Drop the static.
4056 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004057 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004058 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004059 // Note, in C89, this production uses the constant-expr production instead
4060 // of assignment-expr. The only difference is that assignment-expr allows
4061 // things like '=' and '*='. Sema rejects these in C89 mode because they
4062 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004063
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004064 // Parse the constant-expression or assignment-expression now (depending
4065 // on dialect).
4066 if (getLang().CPlusPlus)
4067 NumElements = ParseConstantExpression();
4068 else
4069 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004070 }
Mike Stump11289f42009-09-09 15:08:12 +00004071
Chris Lattner62591722006-08-12 18:40:58 +00004072 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004073 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004074 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004075 // If the expression was invalid, skip it.
4076 SkipUntil(tok::r_square);
4077 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004078 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004079
4080 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4081
John McCall084e83d2011-03-24 11:26:52 +00004082 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004083 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004084
Chris Lattner84a11622008-12-18 07:27:21 +00004085 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004086 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004087 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004088 NumElements.release(),
4089 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004090 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004091}
4092
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004093/// [GNU] typeof-specifier:
4094/// typeof ( expressions )
4095/// typeof ( type-name )
4096/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004097///
4098void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004099 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004100 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004101 SourceLocation StartLoc = ConsumeToken();
4102
John McCalle8595032010-01-13 20:03:27 +00004103 const bool hasParens = Tok.is(tok::l_paren);
4104
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004105 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004106 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004107 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004108 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4109 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004110 if (hasParens)
4111 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004112
4113 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004114 // FIXME: Not accurate, the range gets one token more than it should.
4115 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004116 else
4117 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004118
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004119 if (isCastExpr) {
4120 if (!CastTy) {
4121 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004122 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004123 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004124
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004125 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004126 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004127 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4128 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004129 DiagID, CastTy))
4130 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004131 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004132 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004133
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004134 // If we get here, the operand to the typeof was an expresion.
4135 if (Operand.isInvalid()) {
4136 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004137 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004138 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004139
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004140 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004141 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004142 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4143 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004144 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004145 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004146}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004147
4148
4149/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4150/// from TryAltiVecVectorToken.
4151bool Parser::TryAltiVecVectorTokenOutOfLine() {
4152 Token Next = NextToken();
4153 switch (Next.getKind()) {
4154 default: return false;
4155 case tok::kw_short:
4156 case tok::kw_long:
4157 case tok::kw_signed:
4158 case tok::kw_unsigned:
4159 case tok::kw_void:
4160 case tok::kw_char:
4161 case tok::kw_int:
4162 case tok::kw_float:
4163 case tok::kw_double:
4164 case tok::kw_bool:
4165 case tok::kw___pixel:
4166 Tok.setKind(tok::kw___vector);
4167 return true;
4168 case tok::identifier:
4169 if (Next.getIdentifierInfo() == Ident_pixel) {
4170 Tok.setKind(tok::kw___vector);
4171 return true;
4172 }
4173 return false;
4174 }
4175}
4176
4177bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4178 const char *&PrevSpec, unsigned &DiagID,
4179 bool &isInvalid) {
4180 if (Tok.getIdentifierInfo() == Ident_vector) {
4181 Token Next = NextToken();
4182 switch (Next.getKind()) {
4183 case tok::kw_short:
4184 case tok::kw_long:
4185 case tok::kw_signed:
4186 case tok::kw_unsigned:
4187 case tok::kw_void:
4188 case tok::kw_char:
4189 case tok::kw_int:
4190 case tok::kw_float:
4191 case tok::kw_double:
4192 case tok::kw_bool:
4193 case tok::kw___pixel:
4194 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4195 return true;
4196 case tok::identifier:
4197 if (Next.getIdentifierInfo() == Ident_pixel) {
4198 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4199 return true;
4200 }
4201 break;
4202 default:
4203 break;
4204 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004205 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004206 DS.isTypeAltiVecVector()) {
4207 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4208 return true;
4209 }
4210 return false;
4211}