blob: 8674485d5f84f91fee5e7dddbb5dc9788b860127 [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///
708/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000709/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000710Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
711 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000712 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000713 ParsedAttributes &attrs,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000714 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000715 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000716 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000717 DS.takeAttributesFrom(attrs);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000718 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000719 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000720 StmtResult R = Actions.ActOnVlaStmt(DS);
721 if (R.isUsable())
722 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner0e894622006-08-13 19:58:17 +0000724 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
725 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000726 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000727 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000728 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000729 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000730 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000731 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattner005fc1b2010-04-05 18:18:31 +0000734 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000735}
Mike Stump11289f42009-09-09 15:08:12 +0000736
John McCalld5a36322009-11-03 19:26:08 +0000737/// ParseDeclGroup - Having concluded that this is either a function
738/// definition or a group of object declarations, actually parse the
739/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000740Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
741 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000742 bool AllowFunctionDefinitions,
743 SourceLocation *DeclEnd) {
744 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000745 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000746 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000747
John McCalld5a36322009-11-03 19:26:08 +0000748 // Bail out if the first declarator didn't seem well-formed.
749 if (!D.hasName() && !D.mayOmitIdentifier()) {
750 // Skip until ; or }.
751 SkipUntil(tok::r_brace, true, true);
752 if (Tok.is(tok::semi))
753 ConsumeToken();
754 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000755 }
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000757 // Check to see if we have a function *definition* which must have a body.
758 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
759 // Look at the next token to make sure that this isn't a function
760 // declaration. We have to check this because __attribute__ might be the
761 // start of a function definition in GCC-extended K&R C.
762 !isDeclarationAfterDeclarator()) {
763
Chris Lattner13901342010-07-11 22:42:07 +0000764 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000765 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
766 Diag(Tok, diag::err_function_declared_typedef);
767
768 // Recover by treating the 'typedef' as spurious.
769 DS.ClearStorageClassSpecs();
770 }
771
John McCall48871652010-08-21 09:40:31 +0000772 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000773 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000774 }
775
776 if (isDeclarationSpecifier()) {
777 // If there is an invalid declaration specifier right after the function
778 // prototype, then we must be in a missing semicolon case where this isn't
779 // actually a body. Just fall through into the code that handles it as a
780 // prototype, and let the top-level code handle the erroneous declspec
781 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000782 } else {
783 Diag(Tok, diag::err_expected_fn_body);
784 SkipUntil(tok::semi);
785 return DeclGroupPtrTy();
786 }
787 }
788
John McCall48871652010-08-21 09:40:31 +0000789 llvm::SmallVector<Decl *, 8> DeclsInGroup;
790 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000791 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000792 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000793 DeclsInGroup.push_back(FirstDecl);
794
795 // If we don't have a comma, it is either the end of the list (a ';') or an
796 // error, bail out.
797 while (Tok.is(tok::comma)) {
798 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000799 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000800
801 // Parse the next declarator.
802 D.clear();
803
804 // Accept attributes in an init-declarator. In the first declarator in a
805 // declaration, these would be part of the declspec. In subsequent
806 // declarators, they become part of the declarator itself, so that they
807 // don't apply to declarators after *this* one. Examples:
808 // short __attribute__((common)) var; -> declspec
809 // short var __attribute__((common)); -> declarator
810 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000811 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000812
813 ParseDeclarator(D);
814
John McCall48871652010-08-21 09:40:31 +0000815 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000816 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000817 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000818 DeclsInGroup.push_back(ThisDecl);
819 }
820
821 if (DeclEnd)
822 *DeclEnd = Tok.getLocation();
823
824 if (Context != Declarator::ForContext &&
825 ExpectAndConsume(tok::semi,
826 Context == Declarator::FileContext
827 ? diag::err_invalid_token_after_toplevel_declarator
828 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000829 // Okay, there was no semicolon and one was expected. If we see a
830 // declaration specifier, just assume it was missing and continue parsing.
831 // Otherwise things are very confused and we skip to recover.
832 if (!isDeclarationSpecifier()) {
833 SkipUntil(tok::r_brace, true, true);
834 if (Tok.is(tok::semi))
835 ConsumeToken();
836 }
John McCalld5a36322009-11-03 19:26:08 +0000837 }
838
Douglas Gregor0be31a22010-07-02 17:43:08 +0000839 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000840 DeclsInGroup.data(),
841 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000842}
843
Douglas Gregor23996282009-05-12 21:31:51 +0000844/// \brief Parse 'declaration' after parsing 'declaration-specifiers
845/// declarator'. This method parses the remainder of the declaration
846/// (including any attributes or initializer, among other things) and
847/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000848///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000849/// init-declarator: [C99 6.7]
850/// declarator
851/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000852/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
853/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000854/// [C++] declarator initializer[opt]
855///
856/// [C++] initializer:
857/// [C++] '=' initializer-clause
858/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000859/// [C++0x] '=' 'default' [TODO]
860/// [C++0x] '=' 'delete'
861///
862/// According to the standard grammar, =default and =delete are function
863/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000864///
John McCall48871652010-08-21 09:40:31 +0000865Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000866 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000867 // If a simple-asm-expr is present, parse it.
868 if (Tok.is(tok::kw_asm)) {
869 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000870 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000871 if (AsmLabel.isInvalid()) {
872 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000873 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Douglas Gregor23996282009-05-12 21:31:51 +0000876 D.setAsmLabel(AsmLabel.release());
877 D.SetRangeEnd(Loc);
878 }
Mike Stump11289f42009-09-09 15:08:12 +0000879
John McCall53fa7142010-12-24 02:08:15 +0000880 MaybeParseGNUAttributes(D);
Mike Stump11289f42009-09-09 15:08:12 +0000881
Douglas Gregor23996282009-05-12 21:31:51 +0000882 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000883 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000884 switch (TemplateInfo.Kind) {
885 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000886 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000887 break;
888
889 case ParsedTemplateInfo::Template:
890 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000891 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000892 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000893 TemplateInfo.TemplateParams->data(),
894 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000895 D);
896 break;
897
898 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000899 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000900 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000901 TemplateInfo.ExternLoc,
902 TemplateInfo.TemplateLoc,
903 D);
904 if (ThisRes.isInvalid()) {
905 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000906 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000907 }
908
909 ThisDecl = ThisRes.get();
910 break;
911 }
912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Richard Smith30482bc2011-02-20 03:19:35 +0000914 bool TypeContainsAuto =
915 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
916
Douglas Gregor23996282009-05-12 21:31:51 +0000917 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000918 if (isTokenEqualOrMistypedEqualEqual(
919 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000920 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000921 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000922 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000923
924 if (!getLang().CPlusPlus0x)
925 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
926
Douglas Gregor23996282009-05-12 21:31:51 +0000927 Actions.SetDeclDeleted(ThisDecl, DelLoc);
928 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000929 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
930 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000931 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000932 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000933
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000934 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000935 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000936 ConsumeCodeCompletionToken();
937 SkipUntil(tok::comma, true, true);
938 return ThisDecl;
939 }
940
John McCalldadc5752010-08-24 06:29:42 +0000941 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000942
John McCall1f4ee7b2009-12-19 09:28:58 +0000943 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000944 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000945 ExitScope();
946 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000947
Douglas Gregor23996282009-05-12 21:31:51 +0000948 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000949 SkipUntil(tok::comma, true, true);
950 Actions.ActOnInitializerError(ThisDecl);
951 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000952 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
953 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000954 }
955 } else if (Tok.is(tok::l_paren)) {
956 // Parse C++ direct initializer: '(' expression-list ')'
957 SourceLocation LParenLoc = ConsumeParen();
958 ExprVector Exprs(Actions);
959 CommaLocsTy CommaLocs;
960
Douglas Gregor613bf102009-12-22 17:47:17 +0000961 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
962 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000963 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000964 }
965
Douglas Gregor23996282009-05-12 21:31:51 +0000966 if (ParseExpressionList(Exprs, CommaLocs)) {
967 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000968
969 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000970 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000971 ExitScope();
972 }
Douglas Gregor23996282009-05-12 21:31:51 +0000973 } else {
974 // Match the ')'.
975 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
976
977 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
978 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000979
980 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000981 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000982 ExitScope();
983 }
984
Douglas Gregor23996282009-05-12 21:31:51 +0000985 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
986 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +0000987 RParenLoc,
988 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000989 }
990 } else {
Richard Smith30482bc2011-02-20 03:19:35 +0000991 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000992 }
993
Richard Smithb2bc2e62011-02-21 20:05:19 +0000994 Actions.FinalizeDeclaration(ThisDecl);
995
Douglas Gregor23996282009-05-12 21:31:51 +0000996 return ThisDecl;
997}
998
Chris Lattner1890ac82006-08-13 01:16:23 +0000999/// ParseSpecifierQualifierList
1000/// specifier-qualifier-list:
1001/// type-specifier specifier-qualifier-list[opt]
1002/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001003/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001004///
1005void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1006 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1007 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +00001008 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattner1890ac82006-08-13 01:16:23 +00001010 // Validate declspec for type-name.
1011 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001012 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001013 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001014 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattner1b22eed2006-11-28 05:12:07 +00001016 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001017 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001018 if (DS.getStorageClassSpecLoc().isValid())
1019 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1020 else
1021 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001022 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001023 }
Mike Stump11289f42009-09-09 15:08:12 +00001024
Chris Lattner1b22eed2006-11-28 05:12:07 +00001025 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001026 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001027 if (DS.isInlineSpecified())
1028 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1029 if (DS.isVirtualSpecified())
1030 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1031 if (DS.isExplicitSpecified())
1032 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001033 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001034 }
1035}
Chris Lattner53361ac2006-08-10 05:19:57 +00001036
Chris Lattner6cc055a2009-04-12 20:42:31 +00001037/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1038/// specified token is valid after the identifier in a declarator which
1039/// immediately follows the declspec. For example, these things are valid:
1040///
1041/// int x [ 4]; // direct-declarator
1042/// int x ( int y); // direct-declarator
1043/// int(int x ) // direct-declarator
1044/// int x ; // simple-declaration
1045/// int x = 17; // init-declarator-list
1046/// int x , y; // init-declarator-list
1047/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001048/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001049/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001050///
1051/// This is not, because 'x' does not immediately follow the declspec (though
1052/// ')' happens to be valid anyway).
1053/// int (x)
1054///
1055static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1056 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1057 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001058 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001059}
1060
Chris Lattner20a0c612009-04-14 21:34:55 +00001061
1062/// ParseImplicitInt - This method is called when we have an non-typename
1063/// identifier in a declspec (which normally terminates the decl spec) when
1064/// the declspec has no type specifier. In this case, the declspec is either
1065/// malformed or is "implicit int" (in K&R and C89).
1066///
1067/// This method handles diagnosing this prettily and returns false if the
1068/// declspec is done being processed. If it recovers and thinks there may be
1069/// other pieces of declspec after it, it returns true.
1070///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001071bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001072 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001073 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001074 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001075
Chris Lattner20a0c612009-04-14 21:34:55 +00001076 SourceLocation Loc = Tok.getLocation();
1077 // If we see an identifier that is not a type name, we normally would
1078 // parse it as the identifer being declared. However, when a typename
1079 // is typo'd or the definition is not included, this will incorrectly
1080 // parse the typename as the identifier name and fall over misparsing
1081 // later parts of the diagnostic.
1082 //
1083 // As such, we try to do some look-ahead in cases where this would
1084 // otherwise be an "implicit-int" case to see if this is invalid. For
1085 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1086 // an identifier with implicit int, we'd get a parse error because the
1087 // next token is obviously invalid for a type. Parse these as a case
1088 // with an invalid type specifier.
1089 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattner20a0c612009-04-14 21:34:55 +00001091 // Since we know that this either implicit int (which is rare) or an
1092 // error, we'd do lookahead to try to do better recovery.
1093 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1094 // If this token is valid for implicit int, e.g. "static x = 4", then
1095 // we just avoid eating the identifier, so it will be parsed as the
1096 // identifier in the declarator.
1097 return false;
1098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
Chris Lattner20a0c612009-04-14 21:34:55 +00001100 // Otherwise, if we don't consume this token, we are going to emit an
1101 // error anyway. Try to recover from various common problems. Check
1102 // to see if this was a reference to a tag name without a tag specified.
1103 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001104 //
1105 // C++ doesn't need this, and isTagName doesn't take SS.
1106 if (SS == 0) {
1107 const char *TagName = 0;
1108 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001109
Douglas Gregor0be31a22010-07-02 17:43:08 +00001110 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001111 default: break;
1112 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
1113 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
1114 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
1115 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001118 if (TagName) {
1119 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001120 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00001121 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +00001122
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001123 // Parse this as a tag as if the missing tag were present.
1124 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001125 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001126 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001127 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001128 return true;
1129 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001130 }
Mike Stump11289f42009-09-09 15:08:12 +00001131
Douglas Gregor15e56022009-10-13 23:27:22 +00001132 // This is almost certainly an invalid type name. Let the action emit a
1133 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001134 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001135 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001136 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001137 // The action emitted a diagnostic, so we don't have to.
1138 if (T) {
1139 // The action has suggested that the type T could be used. Set that as
1140 // the type in the declaration specifiers, consume the would-be type
1141 // name token, and we're done.
1142 const char *PrevSpec;
1143 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001144 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001145 DS.SetRangeEnd(Tok.getLocation());
1146 ConsumeToken();
1147
1148 // There may be other declaration specifiers after this.
1149 return true;
1150 }
1151
1152 // Fall through; the action had no suggestion for us.
1153 } else {
1154 // The action did not emit a diagnostic, so emit one now.
1155 SourceRange R;
1156 if (SS) R = SS->getRange();
1157 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1158 }
Mike Stump11289f42009-09-09 15:08:12 +00001159
Douglas Gregor15e56022009-10-13 23:27:22 +00001160 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001161 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001162 unsigned DiagID;
1163 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001164 DS.SetRangeEnd(Tok.getLocation());
1165 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001166
Chris Lattner20a0c612009-04-14 21:34:55 +00001167 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1168 // avoid rippling error messages on subsequent uses of the same type,
1169 // could be useful if #include was forgotten.
1170 return false;
1171}
1172
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001173/// \brief Determine the declaration specifier context from the declarator
1174/// context.
1175///
1176/// \param Context the declarator context, which is one of the
1177/// Declarator::TheContext enumerator values.
1178Parser::DeclSpecContext
1179Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1180 if (Context == Declarator::MemberContext)
1181 return DSC_class;
1182 if (Context == Declarator::FileContext)
1183 return DSC_top_level;
1184 return DSC_normal;
1185}
1186
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001187/// ParseDeclarationSpecifiers
1188/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001189/// storage-class-specifier declaration-specifiers[opt]
1190/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001191/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001192/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001193///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001194/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001195/// 'typedef'
1196/// 'extern'
1197/// 'static'
1198/// 'auto'
1199/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001200/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001201/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001202/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001203/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001204/// [C++] 'virtual'
1205/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001206/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001207/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001208/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001209
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001210///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001211void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001212 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001213 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001214 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +00001215 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +00001216 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001217 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001218 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001219 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001220 unsigned DiagID = 0;
1221
Chris Lattner4d8f8732006-11-28 05:05:08 +00001222 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001223
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001224 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001225 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001226 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001227 // If this is not a declaration specifier token, we're done reading decl
1228 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001229 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001230 return;
Mike Stump11289f42009-09-09 15:08:12 +00001231
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001232 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001233 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001234 if (DS.hasTypeSpecifier()) {
1235 bool AllowNonIdentifiers
1236 = (getCurScope()->getFlags() & (Scope::ControlScope |
1237 Scope::BlockScope |
1238 Scope::TemplateParamScope |
1239 Scope::FunctionPrototypeScope |
1240 Scope::AtCatchScope)) == 0;
1241 bool AllowNestedNameSpecifiers
1242 = DSContext == DSC_top_level ||
1243 (DSContext == DSC_class && DS.isFriendSpecified());
1244
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001245 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1246 AllowNonIdentifiers,
1247 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001248 ConsumeCodeCompletionToken();
1249 return;
1250 }
1251
Douglas Gregor80039242011-02-15 20:33:25 +00001252 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1253 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1254 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001255 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1256 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001257 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001258 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001259 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001260 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001261
1262 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1263 ConsumeCodeCompletionToken();
1264 return;
1265 }
1266
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001267 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001268 // C++ scope specifier. Annotate and loop, or bail out on error.
1269 if (TryAnnotateCXXScopeToken(true)) {
1270 if (!DS.hasTypeSpecifier())
1271 DS.SetTypeSpecError();
1272 goto DoneWithDeclSpec;
1273 }
John McCall8bc2a702010-03-01 18:20:46 +00001274 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1275 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001276 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001277
1278 case tok::annot_cxxscope: {
1279 if (DS.hasTypeSpecifier())
1280 goto DoneWithDeclSpec;
1281
John McCall9dab4e62009-12-12 11:40:51 +00001282 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001283 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1284 Tok.getAnnotationRange(),
1285 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001286
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001287 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001288 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001289 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001290 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001291 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001292 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001293
1294 // C++ [class.qual]p2:
1295 // In a lookup in which the constructor is an acceptable lookup
1296 // result and the nested-name-specifier nominates a class C:
1297 //
1298 // - if the name specified after the
1299 // nested-name-specifier, when looked up in C, is the
1300 // injected-class-name of C (Clause 9), or
1301 //
1302 // - if the name specified after the nested-name-specifier
1303 // is the same as the identifier or the
1304 // simple-template-id's template-name in the last
1305 // component of the nested-name-specifier,
1306 //
1307 // the name is instead considered to name the constructor of
1308 // class C.
1309 //
1310 // Thus, if the template-name is actually the constructor
1311 // name, then the code is ill-formed; this interpretation is
1312 // reinforced by the NAD status of core issue 635.
1313 TemplateIdAnnotation *TemplateId
1314 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001315 if ((DSContext == DSC_top_level ||
1316 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1317 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001318 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001319 if (isConstructorDeclarator()) {
1320 // The user meant this to be an out-of-line constructor
1321 // definition, but template arguments are not allowed
1322 // there. Just allow this as a constructor; we'll
1323 // complain about it later.
1324 goto DoneWithDeclSpec;
1325 }
1326
1327 // The user meant this to name a type, but it actually names
1328 // a constructor with some extraneous template
1329 // arguments. Complain, then parse it as a type as the user
1330 // intended.
1331 Diag(TemplateId->TemplateNameLoc,
1332 diag::err_out_of_line_template_id_names_constructor)
1333 << TemplateId->Name;
1334 }
1335
John McCall9dab4e62009-12-12 11:40:51 +00001336 DS.getTypeSpecScope() = SS;
1337 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001338 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001339 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001340 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001341 continue;
1342 }
1343
Douglas Gregorc5790df2009-09-28 07:26:33 +00001344 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001345 DS.getTypeSpecScope() = SS;
1346 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001347 if (Tok.getAnnotationValue()) {
1348 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1350 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001351 PrevSpec, DiagID, T);
1352 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001353 else
1354 DS.SetTypeSpecError();
1355 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1356 ConsumeToken(); // The typename
1357 }
1358
Douglas Gregor167fa622009-03-25 15:40:00 +00001359 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001360 goto DoneWithDeclSpec;
1361
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001362 // If we're in a context where the identifier could be a class name,
1363 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001364 if ((DSContext == DSC_top_level ||
1365 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001366 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001367 &SS)) {
1368 if (isConstructorDeclarator())
1369 goto DoneWithDeclSpec;
1370
1371 // As noted in C++ [class.qual]p2 (cited above), when the name
1372 // of the class is qualified in a context where it could name
1373 // a constructor, its a constructor name. However, we've
1374 // looked at the declarator, and the user probably meant this
1375 // to be a type. Complain that it isn't supposed to be treated
1376 // as a type, then proceed to parse it as a type.
1377 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1378 << Next.getIdentifierInfo();
1379 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001380
John McCallba7bf592010-08-24 05:47:05 +00001381 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1382 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001383 getCurScope(), &SS,
1384 false, false, ParsedType(),
1385 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001386
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001387 // If the referenced identifier is not a type, then this declspec is
1388 // erroneous: We already checked about that it has no type specifier, and
1389 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001390 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001391 if (TypeRep == 0) {
1392 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001393 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001394 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001395 }
Mike Stump11289f42009-09-09 15:08:12 +00001396
John McCall9dab4e62009-12-12 11:40:51 +00001397 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001398 ConsumeToken(); // The C++ scope.
1399
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001401 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001402 if (isInvalid)
1403 break;
Mike Stump11289f42009-09-09 15:08:12 +00001404
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001405 DS.SetRangeEnd(Tok.getLocation());
1406 ConsumeToken(); // The typename.
1407
1408 continue;
1409 }
Mike Stump11289f42009-09-09 15:08:12 +00001410
Chris Lattnere387d9e2009-01-21 19:48:37 +00001411 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001412 if (Tok.getAnnotationValue()) {
1413 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001415 DiagID, T);
1416 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001417 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001418
1419 if (isInvalid)
1420 break;
1421
Chris Lattnere387d9e2009-01-21 19:48:37 +00001422 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1423 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001424
Chris Lattnere387d9e2009-01-21 19:48:37 +00001425 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1426 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001427 // Objective-C interface.
1428 if (Tok.is(tok::less) && getLang().ObjC1)
1429 ParseObjCProtocolQualifiers(DS);
1430
Chris Lattnere387d9e2009-01-21 19:48:37 +00001431 continue;
1432 }
Mike Stump11289f42009-09-09 15:08:12 +00001433
Chris Lattner16fac4f2008-07-26 01:18:38 +00001434 // typedef-name
1435 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001436 // In C++, check to see if this is a scope specifier like foo::bar::, if
1437 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001438 if (getLang().CPlusPlus) {
1439 if (TryAnnotateCXXScopeToken(true)) {
1440 if (!DS.hasTypeSpecifier())
1441 DS.SetTypeSpecError();
1442 goto DoneWithDeclSpec;
1443 }
1444 if (!Tok.is(tok::identifier))
1445 continue;
1446 }
Mike Stump11289f42009-09-09 15:08:12 +00001447
Chris Lattner16fac4f2008-07-26 01:18:38 +00001448 // This identifier can only be a typedef name if we haven't already seen
1449 // a type-specifier. Without this check we misparse:
1450 // typedef int X; struct Y { short X; }; as 'short int'.
1451 if (DS.hasTypeSpecifier())
1452 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001453
John Thompson22334602010-02-05 00:12:22 +00001454 // Check for need to substitute AltiVec keyword tokens.
1455 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1456 break;
1457
Chris Lattner16fac4f2008-07-26 01:18:38 +00001458 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001459 ParsedType TypeRep =
1460 Actions.getTypeName(*Tok.getIdentifierInfo(),
1461 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001462
Chris Lattner6cc055a2009-04-12 20:42:31 +00001463 // If this is not a typedef name, don't parse it as part of the declspec,
1464 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001465 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001466 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001467 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001468 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001469
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001470 // If we're in a context where the identifier could be a class name,
1471 // check whether this is a constructor declaration.
1472 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001473 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001474 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001475 goto DoneWithDeclSpec;
1476
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001478 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001479 if (isInvalid)
1480 break;
Mike Stump11289f42009-09-09 15:08:12 +00001481
Chris Lattner16fac4f2008-07-26 01:18:38 +00001482 DS.SetRangeEnd(Tok.getLocation());
1483 ConsumeToken(); // The identifier
1484
1485 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1486 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001487 // Objective-C interface.
1488 if (Tok.is(tok::less) && getLang().ObjC1)
1489 ParseObjCProtocolQualifiers(DS);
1490
Steve Naroffcd5e7822008-09-22 10:28:57 +00001491 // Need to support trailing type qualifiers (e.g. "id<p> const").
1492 // If a type specifier follows, it will be diagnosed elsewhere.
1493 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001494 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001495
1496 // type-name
1497 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001498 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001499 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001500 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001501 // This template-id does not refer to a type name, so we're
1502 // done with the type-specifiers.
1503 goto DoneWithDeclSpec;
1504 }
1505
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001506 // If we're in a context where the template-id could be a
1507 // constructor name or specialization, check whether this is a
1508 // constructor declaration.
1509 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001510 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001511 isConstructorDeclarator())
1512 goto DoneWithDeclSpec;
1513
Douglas Gregor7f741122009-02-25 19:37:18 +00001514 // Turn the template-id annotation token into a type annotation
1515 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001516 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001517 continue;
1518 }
1519
Chris Lattnere37e2332006-08-15 04:50:22 +00001520 // GNU attributes support.
1521 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001522 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001523 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001524
1525 // Microsoft declspec support.
1526 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001527 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001528 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001529
Steve Naroff44ac7772008-12-25 14:16:32 +00001530 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001531 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001532 // FIXME: Add handling here!
1533 break;
1534
1535 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001536 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001537 case tok::kw___cdecl:
1538 case tok::kw___stdcall:
1539 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001540 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001541 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001542 continue;
1543
Dawn Perchik335e16b2010-09-03 01:29:35 +00001544 // Borland single token adornments.
1545 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001546 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001547 continue;
1548
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001549 // OpenCL single token adornments.
1550 case tok::kw___kernel:
1551 ParseOpenCLAttributes(DS.getAttributes());
1552 continue;
1553
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001554 // storage-class-specifier
1555 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001556 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001557 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001558 break;
1559 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001560 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001561 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001562 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001563 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001564 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001565 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001566 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001567 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001568 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001569 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001570 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001571 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001572 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001573 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001574 break;
1575 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001576 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001577 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1578 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1579 DiagID, getLang());
1580 if (!isInvalid)
1581 Diag(Tok, diag::auto_storage_class)
1582 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1583 }
1584 else
1585 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1586 DiagID);
1587 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001588 else
John McCall49bfce42009-08-03 20:12:06 +00001589 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001590 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001591 break;
1592 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001593 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001594 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001595 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001596 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001597 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001598 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001599 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001600 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001601 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001602 break;
Mike Stump11289f42009-09-09 15:08:12 +00001603
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001604 // function-specifier
1605 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001606 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001607 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001608 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001609 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001610 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001611 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001612 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001613 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001614
Anders Carlssoncd8db412009-05-06 04:46:28 +00001615 // friend
1616 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001617 if (DSContext == DSC_class)
1618 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1619 else {
1620 PrevSpec = ""; // not actually used by the diagnostic
1621 DiagID = diag::err_friend_invalid_in_context;
1622 isInvalid = true;
1623 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001624 break;
Mike Stump11289f42009-09-09 15:08:12 +00001625
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001626 // constexpr
1627 case tok::kw_constexpr:
1628 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1629 break;
1630
Chris Lattnere387d9e2009-01-21 19:48:37 +00001631 // type-specifier
1632 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001633 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1634 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001635 break;
1636 case tok::kw_long:
1637 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001638 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1639 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001640 else
John McCall49bfce42009-08-03 20:12:06 +00001641 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1642 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001643 break;
1644 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001645 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1646 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001647 break;
1648 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001649 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1650 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001651 break;
1652 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001653 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1654 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001655 break;
1656 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001657 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1658 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001659 break;
1660 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1662 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001663 break;
1664 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001665 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1666 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001667 break;
1668 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1670 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001671 break;
1672 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1674 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001675 break;
1676 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001677 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1678 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001679 break;
1680 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001681 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1682 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001683 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001684 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1686 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001687 break;
1688 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1690 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001691 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001692 case tok::kw_bool:
1693 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001694 if (Tok.is(tok::kw_bool) &&
1695 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1696 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1697 PrevSpec = ""; // Not used by the diagnostic.
1698 DiagID = diag::err_bool_redeclaration;
1699 isInvalid = true;
1700 } else {
1701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1702 DiagID);
1703 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001704 break;
1705 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1707 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001708 break;
1709 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1711 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001712 break;
1713 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1715 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001716 break;
John Thompson22334602010-02-05 00:12:22 +00001717 case tok::kw___vector:
1718 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1719 break;
1720 case tok::kw___pixel:
1721 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1722 break;
John McCall39439732011-04-09 22:50:59 +00001723 case tok::kw___unknown_anytype:
1724 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1725 PrevSpec, DiagID);
1726 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001727
1728 // class-specifier:
1729 case tok::kw_class:
1730 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001731 case tok::kw_union: {
1732 tok::TokenKind Kind = Tok.getKind();
1733 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001734 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001735 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001736 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001737
1738 // enum-specifier:
1739 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001740 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001741 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001742 continue;
1743
1744 // cv-qualifier:
1745 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001746 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1747 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001748 break;
1749 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001750 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1751 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001752 break;
1753 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001754 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1755 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001756 break;
1757
Douglas Gregor333489b2009-03-27 23:10:48 +00001758 // C++ typename-specifier:
1759 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001760 if (TryAnnotateTypeOrScopeToken()) {
1761 DS.SetTypeSpecError();
1762 goto DoneWithDeclSpec;
1763 }
1764 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001765 continue;
1766 break;
1767
Chris Lattnere387d9e2009-01-21 19:48:37 +00001768 // GNU typeof support.
1769 case tok::kw_typeof:
1770 ParseTypeofSpecifier(DS);
1771 continue;
1772
Anders Carlsson74948d02009-06-24 17:47:40 +00001773 case tok::kw_decltype:
1774 ParseDecltypeSpecifier(DS);
1775 continue;
1776
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001777 // OpenCL qualifiers:
1778 case tok::kw_private:
1779 if (!getLang().OpenCL)
1780 goto DoneWithDeclSpec;
1781 case tok::kw___private:
1782 case tok::kw___global:
1783 case tok::kw___local:
1784 case tok::kw___constant:
1785 case tok::kw___read_only:
1786 case tok::kw___write_only:
1787 case tok::kw___read_write:
1788 ParseOpenCLQualifiers(DS);
1789 break;
1790
Steve Naroffcfdf6162008-06-05 00:02:44 +00001791 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001792 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001793 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1794 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001795 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001796 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001797
Douglas Gregor3a001f42010-11-19 17:10:50 +00001798 if (!ParseObjCProtocolQualifiers(DS))
1799 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1800 << FixItHint::CreateInsertion(Loc, "id")
1801 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001802
1803 // Need to support trailing type qualifiers (e.g. "id<p> const").
1804 // If a type specifier follows, it will be diagnosed elsewhere.
1805 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001806 }
John McCall49bfce42009-08-03 20:12:06 +00001807 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001808 if (isInvalid) {
1809 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001810 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001811
1812 if (DiagID == diag::ext_duplicate_declspec)
1813 Diag(Tok, DiagID)
1814 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1815 else
1816 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001817 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001818
Chris Lattner2e232092008-03-13 06:29:04 +00001819 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001820 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001821 }
1822}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001823
Chris Lattnera448d752009-01-06 06:59:53 +00001824/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001825/// primarily follow the C++ grammar with additions for C99 and GNU,
1826/// which together subsume the C grammar. Note that the C++
1827/// type-specifier also includes the C type-qualifier (for const,
1828/// volatile, and C99 restrict). Returns true if a type-specifier was
1829/// found (and parsed), false otherwise.
1830///
1831/// type-specifier: [C++ 7.1.5]
1832/// simple-type-specifier
1833/// class-specifier
1834/// enum-specifier
1835/// elaborated-type-specifier [TODO]
1836/// cv-qualifier
1837///
1838/// cv-qualifier: [C++ 7.1.5.1]
1839/// 'const'
1840/// 'volatile'
1841/// [C99] 'restrict'
1842///
1843/// simple-type-specifier: [ C++ 7.1.5.2]
1844/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1845/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1846/// 'char'
1847/// 'wchar_t'
1848/// 'bool'
1849/// 'short'
1850/// 'int'
1851/// 'long'
1852/// 'signed'
1853/// 'unsigned'
1854/// 'float'
1855/// 'double'
1856/// 'void'
1857/// [C99] '_Bool'
1858/// [C99] '_Complex'
1859/// [C99] '_Imaginary' // Removed in TC2?
1860/// [GNU] '_Decimal32'
1861/// [GNU] '_Decimal64'
1862/// [GNU] '_Decimal128'
1863/// [GNU] typeof-specifier
1864/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1865/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001866/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001867/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001868bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001869 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001870 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001871 const ParsedTemplateInfo &TemplateInfo,
1872 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001873 SourceLocation Loc = Tok.getLocation();
1874
1875 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001876 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001877 // If we already have a type specifier, this identifier is not a type.
1878 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1879 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1880 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1881 return false;
John Thompson22334602010-02-05 00:12:22 +00001882 // Check for need to substitute AltiVec keyword tokens.
1883 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1884 break;
1885 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001886 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001887 // Annotate typenames and C++ scope specifiers. If we get one, just
1888 // recurse to handle whatever we get.
1889 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001890 return true;
1891 if (Tok.is(tok::identifier))
1892 return false;
1893 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1894 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001895 case tok::coloncolon: // ::foo::bar
1896 if (NextToken().is(tok::kw_new) || // ::new
1897 NextToken().is(tok::kw_delete)) // ::delete
1898 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001899
Chris Lattner020bab92009-01-04 23:41:41 +00001900 // Annotate typenames and C++ scope specifiers. If we get one, just
1901 // recurse to handle whatever we get.
1902 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001903 return true;
1904 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1905 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001906
Douglas Gregor450c75a2008-11-07 15:42:26 +00001907 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001908 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001909 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001910 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1911 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001912 DiagID, T);
1913 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001914 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001915 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1916 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001917
Douglas Gregor450c75a2008-11-07 15:42:26 +00001918 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1919 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1920 // Objective-C interface. If we don't have Objective-C or a '<', this is
1921 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001922 if (Tok.is(tok::less) && getLang().ObjC1)
1923 ParseObjCProtocolQualifiers(DS);
1924
Douglas Gregor450c75a2008-11-07 15:42:26 +00001925 return true;
1926 }
1927
1928 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001929 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001930 break;
1931 case tok::kw_long:
1932 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001933 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1934 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001935 else
John McCall49bfce42009-08-03 20:12:06 +00001936 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1937 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001938 break;
1939 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001940 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001941 break;
1942 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001943 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1944 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001945 break;
1946 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001947 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1948 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001949 break;
1950 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001951 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1952 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001953 break;
1954 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001955 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001956 break;
1957 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001959 break;
1960 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001962 break;
1963 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001965 break;
1966 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001968 break;
1969 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001971 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001972 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001973 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001974 break;
1975 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001977 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001978 case tok::kw_bool:
1979 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001981 break;
1982 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1984 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001985 break;
1986 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1988 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001989 break;
1990 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001991 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1992 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001993 break;
John Thompson22334602010-02-05 00:12:22 +00001994 case tok::kw___vector:
1995 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1996 break;
1997 case tok::kw___pixel:
1998 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1999 break;
2000
Douglas Gregor450c75a2008-11-07 15:42:26 +00002001 // class-specifier:
2002 case tok::kw_class:
2003 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002004 case tok::kw_union: {
2005 tok::TokenKind Kind = Tok.getKind();
2006 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002007 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2008 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002009 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002010 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002011
2012 // enum-specifier:
2013 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002014 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002015 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002016 return true;
2017
2018 // cv-qualifier:
2019 case tok::kw_const:
2020 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002021 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002022 break;
2023 case tok::kw_volatile:
2024 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002025 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002026 break;
2027 case tok::kw_restrict:
2028 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002029 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002030 break;
2031
2032 // GNU typeof support.
2033 case tok::kw_typeof:
2034 ParseTypeofSpecifier(DS);
2035 return true;
2036
Anders Carlsson74948d02009-06-24 17:47:40 +00002037 // C++0x decltype support.
2038 case tok::kw_decltype:
2039 ParseDecltypeSpecifier(DS);
2040 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002041
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002042 // OpenCL qualifiers:
2043 case tok::kw_private:
2044 if (!getLang().OpenCL)
2045 return false;
2046 case tok::kw___private:
2047 case tok::kw___global:
2048 case tok::kw___local:
2049 case tok::kw___constant:
2050 case tok::kw___read_only:
2051 case tok::kw___write_only:
2052 case tok::kw___read_write:
2053 ParseOpenCLQualifiers(DS);
2054 break;
2055
Anders Carlssonbae27372009-06-26 23:44:14 +00002056 // C++0x auto support.
2057 case tok::kw_auto:
2058 if (!getLang().CPlusPlus0x)
2059 return false;
2060
John McCall49bfce42009-08-03 20:12:06 +00002061 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002062 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002063
Eli Friedman53339e02009-06-08 23:27:34 +00002064 case tok::kw___ptr64:
2065 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002066 case tok::kw___cdecl:
2067 case tok::kw___stdcall:
2068 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002069 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002070 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002071 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002072
Dawn Perchik335e16b2010-09-03 01:29:35 +00002073 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002074 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002075 return true;
2076
Douglas Gregor450c75a2008-11-07 15:42:26 +00002077 default:
2078 // Not a type-specifier; do nothing.
2079 return false;
2080 }
2081
2082 // If the specifier combination wasn't legal, issue a diagnostic.
2083 if (isInvalid) {
2084 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002085 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002086 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002087 }
2088 DS.SetRangeEnd(Tok.getLocation());
2089 ConsumeToken(); // whatever we parsed above.
2090 return true;
2091}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002092
Chris Lattner70ae4912007-10-29 04:42:53 +00002093/// ParseStructDeclaration - Parse a struct declaration without the terminating
2094/// semicolon.
2095///
Chris Lattner90a26b02007-01-23 04:38:16 +00002096/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002097/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002098/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002099/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002100/// struct-declarator-list:
2101/// struct-declarator
2102/// struct-declarator-list ',' struct-declarator
2103/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2104/// struct-declarator:
2105/// declarator
2106/// [GNU] declarator attributes[opt]
2107/// declarator[opt] ':' constant-expression
2108/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2109///
Chris Lattnera12405b2008-04-10 06:46:29 +00002110void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002111ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002112 if (Tok.is(tok::kw___extension__)) {
2113 // __extension__ silences extension warnings in the subexpression.
2114 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002115 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002116 return ParseStructDeclaration(DS, Fields);
2117 }
Mike Stump11289f42009-09-09 15:08:12 +00002118
Steve Naroff97170802007-08-20 22:28:22 +00002119 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002120 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002121
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002122 // If there are no declarators, this is a free-standing declaration
2123 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002124 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002125 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002126 return;
2127 }
2128
2129 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002130 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002131 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002132 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002133 FieldDeclarator DeclaratorInfo(DS);
2134
2135 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002136 if (!FirstDeclarator)
2137 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002138
Steve Naroff97170802007-08-20 22:28:22 +00002139 /// struct-declarator: declarator
2140 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002141 if (Tok.isNot(tok::colon)) {
2142 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2143 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002144 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002145 }
Mike Stump11289f42009-09-09 15:08:12 +00002146
Chris Lattner76c72282007-10-09 17:33:22 +00002147 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002148 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002149 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002150 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002151 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002152 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002153 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002154 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002155
Steve Naroff97170802007-08-20 22:28:22 +00002156 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002157 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002158
John McCallcfefb6d2009-11-03 02:38:08 +00002159 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002160 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002161 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002162
Steve Naroff97170802007-08-20 22:28:22 +00002163 // If we don't have a comma, it is either the end of the list (a ';')
2164 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002165 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002166 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002167
Steve Naroff97170802007-08-20 22:28:22 +00002168 // Consume the comma.
2169 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002170
John McCallcfefb6d2009-11-03 02:38:08 +00002171 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002172 }
Steve Naroff97170802007-08-20 22:28:22 +00002173}
2174
2175/// ParseStructUnionBody
2176/// struct-contents:
2177/// struct-declaration-list
2178/// [EXT] empty
2179/// [GNU] "struct-declaration-list" without terminatoring ';'
2180/// struct-declaration-list:
2181/// struct-declaration
2182/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002183/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002184///
Chris Lattner1300fb92007-01-23 23:42:53 +00002185void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002186 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002187 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2188 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002189
Chris Lattner90a26b02007-01-23 04:38:16 +00002190 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002191
Douglas Gregor658b9552009-01-09 22:42:13 +00002192 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002193 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002194
Chris Lattner7b9ace62007-01-23 20:11:08 +00002195 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2196 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002197 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002198 Diag(Tok, diag::ext_empty_struct_union)
2199 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002200
John McCall48871652010-08-21 09:40:31 +00002201 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002202
Chris Lattner7b9ace62007-01-23 20:11:08 +00002203 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002204 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002205 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002206
Chris Lattner736ed5d2007-06-09 05:59:07 +00002207 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002208 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002209 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002210 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002211 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002212 ConsumeToken();
2213 continue;
2214 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002215
2216 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002217 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002218
John McCallcfefb6d2009-11-03 02:38:08 +00002219 if (!Tok.is(tok::at)) {
2220 struct CFieldCallback : FieldCallback {
2221 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002222 Decl *TagDecl;
2223 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002224
John McCall48871652010-08-21 09:40:31 +00002225 CFieldCallback(Parser &P, Decl *TagDecl,
2226 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002227 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2228
John McCall48871652010-08-21 09:40:31 +00002229 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002230 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002231 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002232 FD.D.getDeclSpec().getSourceRange().getBegin(),
2233 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002234 FieldDecls.push_back(Field);
2235 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002236 }
John McCallcfefb6d2009-11-03 02:38:08 +00002237 } Callback(*this, TagDecl, FieldDecls);
2238
2239 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002240 } else { // Handle @defs
2241 ConsumeToken();
2242 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2243 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002244 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002245 continue;
2246 }
2247 ConsumeToken();
2248 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2249 if (!Tok.is(tok::identifier)) {
2250 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002251 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002252 continue;
2253 }
John McCall48871652010-08-21 09:40:31 +00002254 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002255 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002256 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002257 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2258 ConsumeToken();
2259 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002260 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002261
Chris Lattner76c72282007-10-09 17:33:22 +00002262 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002263 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002264 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002265 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002266 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002267 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002268 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2269 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002270 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002271 // If we stopped at a ';', eat it.
2272 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002273 }
2274 }
Mike Stump11289f42009-09-09 15:08:12 +00002275
Steve Naroff33a1e802007-10-29 21:38:07 +00002276 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002277
John McCall084e83d2011-03-24 11:26:52 +00002278 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002279 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002280 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002281
Douglas Gregor0be31a22010-07-02 17:43:08 +00002282 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002283 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002284 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002285 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002286 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002287 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002288}
2289
Chris Lattner3b561a32006-08-13 00:12:11 +00002290/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002291/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002292/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002293///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002294/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2295/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002296/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002297/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002298///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002299/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2300/// [C++0x] enum-head '{' enumerator-list ',' '}'
2301///
2302/// enum-head: [C++0x]
2303/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2304/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2305///
2306/// enum-key: [C++0x]
2307/// 'enum'
2308/// 'enum' 'class'
2309/// 'enum' 'struct'
2310///
2311/// enum-base: [C++0x]
2312/// ':' type-specifier-seq
2313///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002314/// [C++] elaborated-type-specifier:
2315/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2316///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002317void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002318 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002319 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002320 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002321 if (Tok.is(tok::code_completion)) {
2322 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002323 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002324 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002325 }
2326
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002327 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002328 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002329 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002330
Abramo Bagnarad7548482010-05-19 21:37:53 +00002331 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002332 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002333 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002334 return;
2335
2336 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002337 Diag(Tok, diag::err_expected_ident);
2338 if (Tok.isNot(tok::l_brace)) {
2339 // Has no name and is not a definition.
2340 // Skip the rest of this declarator, up until the comma or semicolon.
2341 SkipUntil(tok::comma, true);
2342 return;
2343 }
2344 }
2345 }
Mike Stump11289f42009-09-09 15:08:12 +00002346
Douglas Gregora1aec292011-02-22 20:32:04 +00002347 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002348 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002349 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002350
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002351 if (getLang().CPlusPlus0x &&
2352 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002353 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002354 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2355 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002356 }
2357
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002358 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002359 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2360 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002361 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002362
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002363 // Skip the rest of this declarator, up until the comma or semicolon.
2364 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002365 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002366 }
Mike Stump11289f42009-09-09 15:08:12 +00002367
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002368 // If an identifier is present, consume and remember it.
2369 IdentifierInfo *Name = 0;
2370 SourceLocation NameLoc;
2371 if (Tok.is(tok::identifier)) {
2372 Name = Tok.getIdentifierInfo();
2373 NameLoc = ConsumeToken();
2374 }
Mike Stump11289f42009-09-09 15:08:12 +00002375
Douglas Gregor0bf31402010-10-08 23:50:27 +00002376 if (!Name && IsScopedEnum) {
2377 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2378 // declaration of a scoped enumeration.
2379 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2380 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002381 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002382 }
2383
2384 TypeResult BaseType;
2385
Douglas Gregord1f69f62010-12-01 17:42:47 +00002386 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002387 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002388 bool PossibleBitfield = false;
2389 if (getCurScope()->getFlags() & Scope::ClassScope) {
2390 // If we're in class scope, this can either be an enum declaration with
2391 // an underlying type, or a declaration of a bitfield member. We try to
2392 // use a simple disambiguation scheme first to catch the common cases
2393 // (integer literal, sizeof); if it's still ambiguous, we then consider
2394 // anything that's a simple-type-specifier followed by '(' as an
2395 // expression. This suffices because function types are not valid
2396 // underlying types anyway.
2397 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2398 // If the next token starts an expression, we know we're parsing a
2399 // bit-field. This is the common case.
2400 if (TPR == TPResult::True())
2401 PossibleBitfield = true;
2402 // If the next token starts a type-specifier-seq, it may be either a
2403 // a fixed underlying type or the start of a function-style cast in C++;
2404 // lookahead one more token to see if it's obvious that we have a
2405 // fixed underlying type.
2406 else if (TPR == TPResult::False() &&
2407 GetLookAheadToken(2).getKind() == tok::semi) {
2408 // Consume the ':'.
2409 ConsumeToken();
2410 } else {
2411 // We have the start of a type-specifier-seq, so we have to perform
2412 // tentative parsing to determine whether we have an expression or a
2413 // type.
2414 TentativeParsingAction TPA(*this);
2415
2416 // Consume the ':'.
2417 ConsumeToken();
2418
Douglas Gregora1aec292011-02-22 20:32:04 +00002419 if ((getLang().CPlusPlus &&
2420 isCXXDeclarationSpecifier() != TPResult::True()) ||
2421 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002422 // We'll parse this as a bitfield later.
2423 PossibleBitfield = true;
2424 TPA.Revert();
2425 } else {
2426 // We have a type-specifier-seq.
2427 TPA.Commit();
2428 }
2429 }
2430 } else {
2431 // Consume the ':'.
2432 ConsumeToken();
2433 }
2434
2435 if (!PossibleBitfield) {
2436 SourceRange Range;
2437 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002438
2439 if (!getLang().CPlusPlus0x)
2440 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2441 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002442 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002443 }
2444
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002445 // There are three options here. If we have 'enum foo;', then this is a
2446 // forward declaration. If we have 'enum foo {...' then this is a
2447 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2448 //
2449 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2450 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2451 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2452 //
John McCallfaf5fb42010-08-26 23:41:50 +00002453 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002454 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002455 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002456 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002457 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002458 else
John McCallfaf5fb42010-08-26 23:41:50 +00002459 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002460
2461 // enums cannot be templates, although they can be referenced from a
2462 // template.
2463 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002464 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002465 Diag(Tok, diag::err_enum_template);
2466
2467 // Skip the rest of this declarator, up until the comma or semicolon.
2468 SkipUntil(tok::comma, true);
2469 return;
2470 }
2471
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002472 if (!Name && TUK != Sema::TUK_Definition) {
2473 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2474
2475 // Skip the rest of this declarator, up until the comma or semicolon.
2476 SkipUntil(tok::comma, true);
2477 return;
2478 }
2479
Douglas Gregord6ab8742009-05-28 23:31:59 +00002480 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002481 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002482 const char *PrevSpec = 0;
2483 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002484 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002485 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002486 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002487 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002488 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002489 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002490
Douglas Gregorba41d012010-04-24 16:38:41 +00002491 if (IsDependent) {
2492 // This enum has a dependent nested-name-specifier. Handle it as a
2493 // dependent tag.
2494 if (!Name) {
2495 DS.SetTypeSpecError();
2496 Diag(Tok, diag::err_expected_type_name_after_typename);
2497 return;
2498 }
2499
Douglas Gregor0be31a22010-07-02 17:43:08 +00002500 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002501 TUK, SS, Name, StartLoc,
2502 NameLoc);
2503 if (Type.isInvalid()) {
2504 DS.SetTypeSpecError();
2505 return;
2506 }
2507
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002508 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2509 NameLoc.isValid() ? NameLoc : StartLoc,
2510 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002511 Diag(StartLoc, DiagID) << PrevSpec;
2512
2513 return;
2514 }
Mike Stump11289f42009-09-09 15:08:12 +00002515
John McCall48871652010-08-21 09:40:31 +00002516 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002517 // The action failed to produce an enumeration tag. If this is a
2518 // definition, consume the entire definition.
2519 if (Tok.is(tok::l_brace)) {
2520 ConsumeBrace();
2521 SkipUntil(tok::r_brace);
2522 }
2523
2524 DS.SetTypeSpecError();
2525 return;
2526 }
2527
Chris Lattner76c72282007-10-09 17:33:22 +00002528 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002529 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002530
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002531 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2532 NameLoc.isValid() ? NameLoc : StartLoc,
2533 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002534 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002535}
2536
Chris Lattnerc1915e22007-01-25 07:29:02 +00002537/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2538/// enumerator-list:
2539/// enumerator
2540/// enumerator-list ',' enumerator
2541/// enumerator:
2542/// enumeration-constant
2543/// enumeration-constant '=' constant-expression
2544/// enumeration-constant:
2545/// identifier
2546///
John McCall48871652010-08-21 09:40:31 +00002547void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002548 // Enter the scope of the enum body and start the definition.
2549 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002550 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002551
Chris Lattnerc1915e22007-01-25 07:29:02 +00002552 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002553
Chris Lattner37256fb2007-08-27 17:24:30 +00002554 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002555 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002556 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002557
John McCall48871652010-08-21 09:40:31 +00002558 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002559
John McCall48871652010-08-21 09:40:31 +00002560 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002561
Chris Lattnerc1915e22007-01-25 07:29:02 +00002562 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002563 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002564 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2565 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002566
John McCall811a0f52010-10-22 23:36:17 +00002567 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002568 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002569 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002570
Chris Lattnerc1915e22007-01-25 07:29:02 +00002571 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002572 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002573 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002574 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002575 AssignedVal = ParseConstantExpression();
2576 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002577 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002578 }
Mike Stump11289f42009-09-09 15:08:12 +00002579
Chris Lattnerc1915e22007-01-25 07:29:02 +00002580 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002581 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2582 LastEnumConstDecl,
2583 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002584 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002585 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002586 EnumConstantDecls.push_back(EnumConstDecl);
2587 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002588
Douglas Gregorce66d022010-09-07 14:51:08 +00002589 if (Tok.is(tok::identifier)) {
2590 // We're missing a comma between enumerators.
2591 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2592 Diag(Loc, diag::err_enumerator_list_missing_comma)
2593 << FixItHint::CreateInsertion(Loc, ", ");
2594 continue;
2595 }
2596
Chris Lattner76c72282007-10-09 17:33:22 +00002597 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002598 break;
2599 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002600
2601 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002602 !(getLang().C99 || getLang().CPlusPlus0x))
2603 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2604 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002605 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002606 }
Mike Stump11289f42009-09-09 15:08:12 +00002607
Chris Lattnerc1915e22007-01-25 07:29:02 +00002608 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002609 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002610
Chris Lattnerc1915e22007-01-25 07:29:02 +00002611 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002612 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002613 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002614
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002615 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2616 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002617 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002618
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002619 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002620 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002621}
Chris Lattner3b561a32006-08-13 00:12:11 +00002622
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002623/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002624/// start of a type-qualifier-list.
2625bool Parser::isTypeQualifier() const {
2626 switch (Tok.getKind()) {
2627 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002628
2629 // type-qualifier only in OpenCL
2630 case tok::kw_private:
2631 return getLang().OpenCL;
2632
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002633 // type-qualifier
2634 case tok::kw_const:
2635 case tok::kw_volatile:
2636 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002637 case tok::kw___private:
2638 case tok::kw___local:
2639 case tok::kw___global:
2640 case tok::kw___constant:
2641 case tok::kw___read_only:
2642 case tok::kw___read_write:
2643 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002644 return true;
2645 }
2646}
2647
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002648/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2649/// is definitely a type-specifier. Return false if it isn't part of a type
2650/// specifier or if we're not sure.
2651bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2652 switch (Tok.getKind()) {
2653 default: return false;
2654 // type-specifiers
2655 case tok::kw_short:
2656 case tok::kw_long:
2657 case tok::kw_signed:
2658 case tok::kw_unsigned:
2659 case tok::kw__Complex:
2660 case tok::kw__Imaginary:
2661 case tok::kw_void:
2662 case tok::kw_char:
2663 case tok::kw_wchar_t:
2664 case tok::kw_char16_t:
2665 case tok::kw_char32_t:
2666 case tok::kw_int:
2667 case tok::kw_float:
2668 case tok::kw_double:
2669 case tok::kw_bool:
2670 case tok::kw__Bool:
2671 case tok::kw__Decimal32:
2672 case tok::kw__Decimal64:
2673 case tok::kw__Decimal128:
2674 case tok::kw___vector:
2675
2676 // struct-or-union-specifier (C99) or class-specifier (C++)
2677 case tok::kw_class:
2678 case tok::kw_struct:
2679 case tok::kw_union:
2680 // enum-specifier
2681 case tok::kw_enum:
2682
2683 // typedef-name
2684 case tok::annot_typename:
2685 return true;
2686 }
2687}
2688
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002689/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002690/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002691bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002692 switch (Tok.getKind()) {
2693 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002694
Chris Lattner020bab92009-01-04 23:41:41 +00002695 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002696 if (TryAltiVecVectorToken())
2697 return true;
2698 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002699 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002700 // Annotate typenames and C++ scope specifiers. If we get one, just
2701 // recurse to handle whatever we get.
2702 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002703 return true;
2704 if (Tok.is(tok::identifier))
2705 return false;
2706 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002707
Chris Lattner020bab92009-01-04 23:41:41 +00002708 case tok::coloncolon: // ::foo::bar
2709 if (NextToken().is(tok::kw_new) || // ::new
2710 NextToken().is(tok::kw_delete)) // ::delete
2711 return false;
2712
Chris Lattner020bab92009-01-04 23:41:41 +00002713 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002714 return true;
2715 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002716
Chris Lattnere37e2332006-08-15 04:50:22 +00002717 // GNU attributes support.
2718 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002719 // GNU typeof support.
2720 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002721
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002722 // type-specifiers
2723 case tok::kw_short:
2724 case tok::kw_long:
2725 case tok::kw_signed:
2726 case tok::kw_unsigned:
2727 case tok::kw__Complex:
2728 case tok::kw__Imaginary:
2729 case tok::kw_void:
2730 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002731 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002732 case tok::kw_char16_t:
2733 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002734 case tok::kw_int:
2735 case tok::kw_float:
2736 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002737 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002738 case tok::kw__Bool:
2739 case tok::kw__Decimal32:
2740 case tok::kw__Decimal64:
2741 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002742 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002743
Chris Lattner861a2262008-04-13 18:59:07 +00002744 // struct-or-union-specifier (C99) or class-specifier (C++)
2745 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002746 case tok::kw_struct:
2747 case tok::kw_union:
2748 // enum-specifier
2749 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002750
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002751 // type-qualifier
2752 case tok::kw_const:
2753 case tok::kw_volatile:
2754 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002755
2756 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002757 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002758 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002759
Chris Lattner409bf7d2008-10-20 00:25:30 +00002760 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2761 case tok::less:
2762 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002763
Steve Naroff44ac7772008-12-25 14:16:32 +00002764 case tok::kw___cdecl:
2765 case tok::kw___stdcall:
2766 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002767 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002768 case tok::kw___w64:
2769 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002770 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002771
2772 case tok::kw___private:
2773 case tok::kw___local:
2774 case tok::kw___global:
2775 case tok::kw___constant:
2776 case tok::kw___read_only:
2777 case tok::kw___read_write:
2778 case tok::kw___write_only:
2779
Eli Friedman53339e02009-06-08 23:27:34 +00002780 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002781
2782 case tok::kw_private:
2783 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002784 }
2785}
2786
Chris Lattneracd58a32006-08-06 17:24:14 +00002787/// isDeclarationSpecifier() - Return true if the current token is part of a
2788/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002789///
2790/// \param DisambiguatingWithExpression True to indicate that the purpose of
2791/// this check is to disambiguate between an expression and a declaration.
2792bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002793 switch (Tok.getKind()) {
2794 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002795
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002796 case tok::kw_private:
2797 return getLang().OpenCL;
2798
Chris Lattner020bab92009-01-04 23:41:41 +00002799 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002800 // Unfortunate hack to support "Class.factoryMethod" notation.
2801 if (getLang().ObjC1 && NextToken().is(tok::period))
2802 return false;
John Thompson22334602010-02-05 00:12:22 +00002803 if (TryAltiVecVectorToken())
2804 return true;
2805 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002806 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002807 // Annotate typenames and C++ scope specifiers. If we get one, just
2808 // recurse to handle whatever we get.
2809 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002810 return true;
2811 if (Tok.is(tok::identifier))
2812 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002813
2814 // If we're in Objective-C and we have an Objective-C class type followed
2815 // by an identifier and then either ':' or ']', in a place where an
2816 // expression is permitted, then this is probably a class message send
2817 // missing the initial '['. In this case, we won't consider this to be
2818 // the start of a declaration.
2819 if (DisambiguatingWithExpression &&
2820 isStartOfObjCClassMessageMissingOpenBracket())
2821 return false;
2822
John McCall1f476a12010-02-26 08:45:28 +00002823 return isDeclarationSpecifier();
2824
Chris Lattner020bab92009-01-04 23:41:41 +00002825 case tok::coloncolon: // ::foo::bar
2826 if (NextToken().is(tok::kw_new) || // ::new
2827 NextToken().is(tok::kw_delete)) // ::delete
2828 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002829
Chris Lattner020bab92009-01-04 23:41:41 +00002830 // Annotate typenames and C++ scope specifiers. If we get one, just
2831 // recurse to handle whatever we get.
2832 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002833 return true;
2834 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002835
Chris Lattneracd58a32006-08-06 17:24:14 +00002836 // storage-class-specifier
2837 case tok::kw_typedef:
2838 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002839 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002840 case tok::kw_static:
2841 case tok::kw_auto:
2842 case tok::kw_register:
2843 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002844
Chris Lattneracd58a32006-08-06 17:24:14 +00002845 // type-specifiers
2846 case tok::kw_short:
2847 case tok::kw_long:
2848 case tok::kw_signed:
2849 case tok::kw_unsigned:
2850 case tok::kw__Complex:
2851 case tok::kw__Imaginary:
2852 case tok::kw_void:
2853 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002854 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002855 case tok::kw_char16_t:
2856 case tok::kw_char32_t:
2857
Chris Lattneracd58a32006-08-06 17:24:14 +00002858 case tok::kw_int:
2859 case tok::kw_float:
2860 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002861 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002862 case tok::kw__Bool:
2863 case tok::kw__Decimal32:
2864 case tok::kw__Decimal64:
2865 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002866 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002867
Chris Lattner861a2262008-04-13 18:59:07 +00002868 // struct-or-union-specifier (C99) or class-specifier (C++)
2869 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002870 case tok::kw_struct:
2871 case tok::kw_union:
2872 // enum-specifier
2873 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002874
Chris Lattneracd58a32006-08-06 17:24:14 +00002875 // type-qualifier
2876 case tok::kw_const:
2877 case tok::kw_volatile:
2878 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002879
Chris Lattneracd58a32006-08-06 17:24:14 +00002880 // function-specifier
2881 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002882 case tok::kw_virtual:
2883 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002884
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002885 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002886 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002887
Chris Lattner599e47e2007-08-09 17:01:07 +00002888 // GNU typeof support.
2889 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002890
Chris Lattner599e47e2007-08-09 17:01:07 +00002891 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002892 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002893 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002894
Chris Lattner8b2ec162008-07-26 03:38:44 +00002895 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2896 case tok::less:
2897 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002898
Steve Narofff192fab2009-01-06 19:34:12 +00002899 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002900 case tok::kw___cdecl:
2901 case tok::kw___stdcall:
2902 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002903 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002904 case tok::kw___w64:
2905 case tok::kw___ptr64:
2906 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002907 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002908
2909 case tok::kw___private:
2910 case tok::kw___local:
2911 case tok::kw___global:
2912 case tok::kw___constant:
2913 case tok::kw___read_only:
2914 case tok::kw___read_write:
2915 case tok::kw___write_only:
2916
Eli Friedman53339e02009-06-08 23:27:34 +00002917 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002918 }
2919}
2920
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002921bool Parser::isConstructorDeclarator() {
2922 TentativeParsingAction TPA(*this);
2923
2924 // Parse the C++ scope specifier.
2925 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002926 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002927 TPA.Revert();
2928 return false;
2929 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002930
2931 // Parse the constructor name.
2932 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2933 // We already know that we have a constructor name; just consume
2934 // the token.
2935 ConsumeToken();
2936 } else {
2937 TPA.Revert();
2938 return false;
2939 }
2940
2941 // Current class name must be followed by a left parentheses.
2942 if (Tok.isNot(tok::l_paren)) {
2943 TPA.Revert();
2944 return false;
2945 }
2946 ConsumeParen();
2947
2948 // A right parentheses or ellipsis signals that we have a constructor.
2949 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2950 TPA.Revert();
2951 return true;
2952 }
2953
2954 // If we need to, enter the specified scope.
2955 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002956 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002957 DeclScopeObj.EnterDeclaratorScope();
2958
Francois Pichet79f3a872011-01-31 04:54:32 +00002959 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00002960 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00002961 MaybeParseMicrosoftAttributes(Attrs);
2962
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002963 // Check whether the next token(s) are part of a declaration
2964 // specifier, in which case we have the start of a parameter and,
2965 // therefore, we know that this is a constructor.
2966 bool IsConstructor = isDeclarationSpecifier();
2967 TPA.Revert();
2968 return IsConstructor;
2969}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002970
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002971/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002972/// type-qualifier-list: [C99 6.7.5]
2973/// type-qualifier
2974/// [vendor] attributes
2975/// [ only if VendorAttributesAllowed=true ]
2976/// type-qualifier-list type-qualifier
2977/// [vendor] type-qualifier-list attributes
2978/// [ only if VendorAttributesAllowed=true ]
2979/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2980/// [ only if CXX0XAttributesAllowed=true ]
2981/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002982///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002983void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2984 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002985 bool CXX0XAttributesAllowed) {
2986 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2987 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00002988 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002989 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002990 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00002991 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002992 else
2993 Diag(Loc, diag::err_attributes_not_allowed);
2994 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00002995
2996 SourceLocation EndLoc;
2997
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002998 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002999 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003000 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003001 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003002 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003003
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003004 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003005 case tok::code_completion:
3006 Actions.CodeCompleteTypeQualifiers(DS);
3007 ConsumeCodeCompletionToken();
3008 break;
3009
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003010 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003011 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3012 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003013 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003014 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003015 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3016 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003017 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003018 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003019 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3020 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003021 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003022
3023 // OpenCL qualifiers:
3024 case tok::kw_private:
3025 if (!getLang().OpenCL)
3026 goto DoneWithTypeQuals;
3027 case tok::kw___private:
3028 case tok::kw___global:
3029 case tok::kw___local:
3030 case tok::kw___constant:
3031 case tok::kw___read_only:
3032 case tok::kw___write_only:
3033 case tok::kw___read_write:
3034 ParseOpenCLQualifiers(DS);
3035 break;
3036
Eli Friedman53339e02009-06-08 23:27:34 +00003037 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003038 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003039 case tok::kw___cdecl:
3040 case tok::kw___stdcall:
3041 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003042 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003043 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003044 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003045 continue;
3046 }
3047 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003048 case tok::kw___pascal:
3049 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003050 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003051 continue;
3052 }
3053 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003054 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003055 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003056 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003057 continue; // do *not* consume the next token!
3058 }
3059 // otherwise, FALL THROUGH!
3060 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003061 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003062 // If this is not a type-qualifier token, we're done reading type
3063 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003064 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003065 if (EndLoc.isValid())
3066 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003067 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003068 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003069
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003070 // If the specifier combination wasn't legal, issue a diagnostic.
3071 if (isInvalid) {
3072 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003073 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003074 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003075 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003076 }
3077}
3078
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003079
3080/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3081///
3082void Parser::ParseDeclarator(Declarator &D) {
3083 /// This implements the 'declarator' production in the C grammar, then checks
3084 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003085 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003086}
3087
Sebastian Redlbd150f42008-11-21 19:14:01 +00003088/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3089/// is parsed by the function passed to it. Pass null, and the direct-declarator
3090/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003091/// ptr-operator production.
3092///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003093/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3094/// [C] pointer[opt] direct-declarator
3095/// [C++] direct-declarator
3096/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003097///
3098/// pointer: [C99 6.7.5]
3099/// '*' type-qualifier-list[opt]
3100/// '*' type-qualifier-list[opt] pointer
3101///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003102/// ptr-operator:
3103/// '*' cv-qualifier-seq[opt]
3104/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003105/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003106/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003107/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003108/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003109void Parser::ParseDeclaratorInternal(Declarator &D,
3110 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003111 if (Diags.hasAllExtensionsSilenced())
3112 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003113
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003114 // C++ member pointers start with a '::' or a nested-name.
3115 // Member pointers get special handling, since there's no place for the
3116 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003117 if (getLang().CPlusPlus &&
3118 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3119 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003120 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003121 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003122
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003123 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003124 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003125 // The scope spec really belongs to the direct-declarator.
3126 D.getCXXScopeSpec() = SS;
3127 if (DirectDeclParser)
3128 (this->*DirectDeclParser)(D);
3129 return;
3130 }
3131
3132 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003133 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003134 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003135 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003136 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003137
3138 // Recurse to parse whatever is left.
3139 ParseDeclaratorInternal(D, DirectDeclParser);
3140
3141 // Sema will have to catch (syntactically invalid) pointers into global
3142 // scope. It has to catch pointers into namespace scope anyway.
3143 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003144 Loc),
3145 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003146 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003147 return;
3148 }
3149 }
3150
3151 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003152 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003153 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003154 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003155 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003156 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003157 if (DirectDeclParser)
3158 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003159 return;
3160 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003161
Sebastian Redled0f3b02009-03-15 22:02:01 +00003162 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3163 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003164 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003165 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003166
Chris Lattner9eac9312009-03-27 04:18:06 +00003167 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003168 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003169 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003170
Bill Wendling3708c182007-05-27 10:15:43 +00003171 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003172 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003173
Bill Wendling3708c182007-05-27 10:15:43 +00003174 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003175 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003176 if (Kind == tok::star)
3177 // Remember that we parsed a pointer type, and remember the type-quals.
3178 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003179 DS.getConstSpecLoc(),
3180 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003181 DS.getRestrictSpecLoc()),
3182 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003183 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003184 else
3185 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003186 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003187 Loc),
3188 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003189 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003190 } else {
3191 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003192 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003193
Sebastian Redl3b27be62009-03-23 00:00:23 +00003194 // Complain about rvalue references in C++03, but then go on and build
3195 // the declarator.
3196 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003197 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003198
Bill Wendling93efb222007-06-02 23:28:54 +00003199 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3200 // cv-qualifiers are introduced through the use of a typedef or of a
3201 // template type argument, in which case the cv-qualifiers are ignored.
3202 //
3203 // [GNU] Retricted references are allowed.
3204 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003205 // [C++0x] Attributes on references are not allowed.
3206 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003207 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003208
3209 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3210 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3211 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003212 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003213 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3214 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003215 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003216 }
Bill Wendling3708c182007-05-27 10:15:43 +00003217
3218 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003219 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003220
Douglas Gregor66583c52008-11-03 15:51:28 +00003221 if (D.getNumTypeObjects() > 0) {
3222 // C++ [dcl.ref]p4: There shall be no references to references.
3223 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3224 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003225 if (const IdentifierInfo *II = D.getIdentifier())
3226 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3227 << II;
3228 else
3229 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3230 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003231
Sebastian Redlbd150f42008-11-21 19:14:01 +00003232 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003233 // can go ahead and build the (technically ill-formed)
3234 // declarator: reference collapsing will take care of it.
3235 }
3236 }
3237
Bill Wendling3708c182007-05-27 10:15:43 +00003238 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003239 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003240 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003241 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003242 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003243 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003244}
3245
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003246/// ParseDirectDeclarator
3247/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003248/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003249/// '(' declarator ')'
3250/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003251/// [C90] direct-declarator '[' constant-expression[opt] ']'
3252/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3253/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3254/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3255/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003256/// direct-declarator '(' parameter-type-list ')'
3257/// direct-declarator '(' identifier-list[opt] ')'
3258/// [GNU] direct-declarator '(' parameter-forward-declarations
3259/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003260/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3261/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003262/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003263///
3264/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003265/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003266/// '::'[opt] nested-name-specifier[opt] type-name
3267///
3268/// id-expression: [C++ 5.1]
3269/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003270/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003271///
3272/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003273/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003274/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003275/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003276/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003277/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003278///
Chris Lattneracd58a32006-08-06 17:24:14 +00003279void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003280 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003281
Douglas Gregor7861a802009-11-03 01:35:08 +00003282 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3283 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003284 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003285 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003286 }
3287
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003288 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003289 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003290 // Change the declaration context for name lookup, until this function
3291 // is exited (and the declarator has been parsed).
3292 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003293 }
3294
Douglas Gregor27b4c162010-12-23 22:44:42 +00003295 // C++0x [dcl.fct]p14:
3296 // There is a syntactic ambiguity when an ellipsis occurs at the end
3297 // of a parameter-declaration-clause without a preceding comma. In
3298 // this case, the ellipsis is parsed as part of the
3299 // abstract-declarator if the type of the parameter names a template
3300 // parameter pack that has not been expanded; otherwise, it is parsed
3301 // as part of the parameter-declaration-clause.
3302 if (Tok.is(tok::ellipsis) &&
3303 !((D.getContext() == Declarator::PrototypeContext ||
3304 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003305 NextToken().is(tok::r_paren) &&
3306 !Actions.containsUnexpandedParameterPacks(D)))
3307 D.setEllipsisLoc(ConsumeToken());
3308
Douglas Gregor7861a802009-11-03 01:35:08 +00003309 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3310 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3311 // We found something that indicates the start of an unqualified-id.
3312 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003313 bool AllowConstructorName;
3314 if (D.getDeclSpec().hasTypeSpecifier())
3315 AllowConstructorName = false;
3316 else if (D.getCXXScopeSpec().isSet())
3317 AllowConstructorName =
3318 (D.getContext() == Declarator::FileContext ||
3319 (D.getContext() == Declarator::MemberContext &&
3320 D.getDeclSpec().isFriendSpecified()));
3321 else
3322 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3323
Douglas Gregor7861a802009-11-03 01:35:08 +00003324 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3325 /*EnteringContext=*/true,
3326 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003327 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003328 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003329 D.getName()) ||
3330 // Once we're past the identifier, if the scope was bad, mark the
3331 // whole declarator bad.
3332 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003333 D.SetIdentifier(0, Tok.getLocation());
3334 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003335 } else {
3336 // Parsed the unqualified-id; update range information and move along.
3337 if (D.getSourceRange().getBegin().isInvalid())
3338 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3339 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003340 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003341 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003342 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003343 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003344 assert(!getLang().CPlusPlus &&
3345 "There's a C++-specific check for tok::identifier above");
3346 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3347 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3348 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003349 goto PastIdentifier;
3350 }
3351
3352 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003353 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003354 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003355 // Example: 'char (*X)' or 'int (*XX)(void)'
3356 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003357
3358 // If the declarator was parenthesized, we entered the declarator
3359 // scope when parsing the parenthesized declarator, then exited
3360 // the scope already. Re-enter the scope, if we need to.
3361 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003362 // If there was an error parsing parenthesized declarator, declarator
3363 // scope may have been enterred before. Don't do it again.
3364 if (!D.isInvalidType() &&
3365 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003366 // Change the declaration context for name lookup, until this function
3367 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003368 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003369 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003370 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003371 // This could be something simple like "int" (in which case the declarator
3372 // portion is empty), if an abstract-declarator is allowed.
3373 D.SetIdentifier(0, Tok.getLocation());
3374 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003375 if (D.getContext() == Declarator::MemberContext)
3376 Diag(Tok, diag::err_expected_member_name_or_semi)
3377 << D.getDeclSpec().getSourceRange();
3378 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003379 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003380 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003381 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003382 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003383 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003384 }
Mike Stump11289f42009-09-09 15:08:12 +00003385
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003386 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003387 assert(D.isPastIdentifier() &&
3388 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003389
Alexis Hunt96d5c762009-11-21 08:43:09 +00003390 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003391 if (D.getIdentifier())
3392 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003393
Chris Lattneracd58a32006-08-06 17:24:14 +00003394 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003395 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003396 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3397 // In such a case, check if we actually have a function declarator; if it
3398 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003399 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3400 // When not in file scope, warn for ambiguous function declarators, just
3401 // in case the author intended it as a variable definition.
3402 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3403 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3404 break;
3405 }
John McCall084e83d2011-03-24 11:26:52 +00003406 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003407 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003408 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003409 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003410 } else {
3411 break;
3412 }
3413 }
3414}
3415
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003416/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3417/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003418/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003419/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3420///
3421/// direct-declarator:
3422/// '(' declarator ')'
3423/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003424/// direct-declarator '(' parameter-type-list ')'
3425/// direct-declarator '(' identifier-list[opt] ')'
3426/// [GNU] direct-declarator '(' parameter-forward-declarations
3427/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003428///
3429void Parser::ParseParenDeclarator(Declarator &D) {
3430 SourceLocation StartLoc = ConsumeParen();
3431 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003432
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003433 // Eat any attributes before we look at whether this is a grouping or function
3434 // declarator paren. If this is a grouping paren, the attribute applies to
3435 // the type being built up, for example:
3436 // int (__attribute__(()) *x)(long y)
3437 // If this ends up not being a grouping paren, the attribute applies to the
3438 // first argument, for example:
3439 // int (__attribute__(()) int x)
3440 // In either case, we need to eat any attributes to be able to determine what
3441 // sort of paren this is.
3442 //
John McCall084e83d2011-03-24 11:26:52 +00003443 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003444 bool RequiresArg = false;
3445 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003446 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003447
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003448 // We require that the argument list (if this is a non-grouping paren) be
3449 // present even if the attribute list was empty.
3450 RequiresArg = true;
3451 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003452 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003453 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003454 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3455 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003456 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003457 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003458 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003459 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003460 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003461
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003462 // If we haven't past the identifier yet (or where the identifier would be
3463 // stored, if this is an abstract declarator), then this is probably just
3464 // grouping parens. However, if this could be an abstract-declarator, then
3465 // this could also be the start of function arguments (consider 'void()').
3466 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003467
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003468 if (!D.mayOmitIdentifier()) {
3469 // If this can't be an abstract-declarator, this *must* be a grouping
3470 // paren, because we haven't seen the identifier yet.
3471 isGrouping = true;
3472 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003473 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003474 isDeclarationSpecifier()) { // 'int(int)' is a function.
3475 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3476 // considered to be a type, not a K&R identifier-list.
3477 isGrouping = false;
3478 } else {
3479 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3480 isGrouping = true;
3481 }
Mike Stump11289f42009-09-09 15:08:12 +00003482
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003483 // If this is a grouping paren, handle:
3484 // direct-declarator: '(' declarator ')'
3485 // direct-declarator: '(' attributes declarator ')'
3486 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003487 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003488 D.setGroupingParens(true);
3489
Sebastian Redlbd150f42008-11-21 19:14:01 +00003490 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003491 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003492 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003493 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3494 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003495
3496 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003497 return;
3498 }
Mike Stump11289f42009-09-09 15:08:12 +00003499
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003500 // Okay, if this wasn't a grouping paren, it must be the start of a function
3501 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003502 // identifier (and remember where it would have been), then call into
3503 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003504 D.SetIdentifier(0, Tok.getLocation());
3505
John McCall53fa7142010-12-24 02:08:15 +00003506 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003507}
3508
3509/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3510/// declarator D up to a paren, which indicates that we are parsing function
3511/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003512///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003513/// If AttrList is non-null, then the caller parsed those arguments immediately
3514/// after the open paren - they should be considered to be the first argument of
3515/// a parameter. If RequiresArg is true, then the first argument of the
3516/// function is required to be present and required to not be an identifier
3517/// list.
3518///
Chris Lattneracd58a32006-08-06 17:24:14 +00003519/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003520/// parameter-type-list: [C99 6.7.5]
3521/// parameter-list
3522/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003523/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003524///
3525/// parameter-list: [C99 6.7.5]
3526/// parameter-declaration
3527/// parameter-list ',' parameter-declaration
3528///
3529/// parameter-declaration: [C99 6.7.5]
3530/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003531/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003532/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003533/// declaration-specifiers abstract-declarator[opt]
3534/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003535/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003536/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003537///
Douglas Gregor54992352011-01-26 03:43:54 +00003538/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3539/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003540///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003541/// [C++0x] exception-specification:
3542/// dynamic-exception-specification
3543/// noexcept-specification
3544///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003545void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003546 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003547 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003548 // lparen is already consumed!
3549 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003550
Douglas Gregor7fb25412010-10-01 18:44:50 +00003551 ParsedType TrailingReturnType;
3552
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003553 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003554 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003555 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003556 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003557
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003558 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003559
3560 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003561 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003562 SourceLocation RefQualifierLoc;
3563 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003564 ExceptionSpecificationType ESpecType = EST_None;
3565 SourceRange ESpecRange;
3566 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3567 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3568 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003569 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003570 MaybeParseCXX0XAttributes(attrs);
3571
Chris Lattnercf0bab22008-12-18 07:02:59 +00003572 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003573 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003574 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003575
Douglas Gregor54992352011-01-26 03:43:54 +00003576 // Parse ref-qualifier[opt]
3577 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3578 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003579 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003580
Douglas Gregor54992352011-01-26 03:43:54 +00003581 RefQualifierIsLValueRef = Tok.is(tok::amp);
3582 RefQualifierLoc = ConsumeToken();
3583 EndLoc = RefQualifierLoc;
3584 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003585
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003586 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003587 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3588 DynamicExceptions,
3589 DynamicExceptionRanges,
3590 NoexceptExpr);
3591 if (ESpecType != EST_None)
3592 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003593
3594 // Parse trailing-return-type.
3595 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3596 TrailingReturnType = ParseTrailingReturnType().get();
3597 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003598 }
3599
Chris Lattner371ed4e2008-04-06 06:57:35 +00003600 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003601 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003602 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003603 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003604 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003605 /*arglist*/ 0, 0,
3606 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003607 RefQualifierIsLValueRef,
3608 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003609 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003610 DynamicExceptions.data(),
3611 DynamicExceptionRanges.data(),
3612 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003613 NoexceptExpr.isUsable() ?
3614 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003615 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003616 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003617 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003618 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003619 }
3620
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003621 // Alternatively, this parameter list may be an identifier list form for a
3622 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003623 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3624 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003625 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003626 // K&R identifier lists can't have typedefs as identifiers, per
3627 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003628 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003629 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003630
Steve Naroffb0486722009-01-28 19:16:40 +00003631 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003632 // normal declarators, not for abstract-declarators. Get the first
3633 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003634 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003635 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003636
3637 // Identifier lists follow a really simple grammar: the identifiers can
3638 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3639 // identifier lists are really rare in the brave new modern world, and it
3640 // is very common for someone to typo a type in a non-k&r style list. If
3641 // we are presented with something like: "void foo(intptr x, float y)",
3642 // we don't want to start parsing the function declarator as though it is
3643 // a K&R style declarator just because intptr is an invalid type.
3644 //
3645 // To handle this, we check to see if the token after the first identifier
3646 // is a "," or ")". Only if so, do we parse it as an identifier list.
3647 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3648 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3649 FirstTok.getIdentifierInfo(),
3650 FirstTok.getLocation(), D);
3651
3652 // If we get here, the code is invalid. Push the first identifier back
3653 // into the token stream and parse the first argument as an (invalid)
3654 // normal argument declarator.
3655 PP.EnterToken(Tok);
3656 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003657 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003658 }
Mike Stump11289f42009-09-09 15:08:12 +00003659
Chris Lattner371ed4e2008-04-06 06:57:35 +00003660 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003661
Chris Lattner371ed4e2008-04-06 06:57:35 +00003662 // Build up an array of information about the parsed arguments.
3663 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003664
3665 // Enter function-declaration scope, limiting any declarators to the
3666 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003667 ParseScope PrototypeScope(this,
3668 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003669
Chris Lattner371ed4e2008-04-06 06:57:35 +00003670 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003671 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003672 while (1) {
3673 if (Tok.is(tok::ellipsis)) {
3674 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003675 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003676 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003677 }
Mike Stump11289f42009-09-09 15:08:12 +00003678
Chris Lattner371ed4e2008-04-06 06:57:35 +00003679 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003680 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003681 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003682
3683 // Skip any Microsoft attributes before a param.
3684 if (getLang().Microsoft && Tok.is(tok::l_square))
3685 ParseMicrosoftAttributes(DS.getAttributes());
3686
3687 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003688
3689 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003690 // Take them so that we only apply the attributes to the first parameter.
3691 DS.takeAttributesFrom(attrs);
3692
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003693 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003694
Chris Lattner371ed4e2008-04-06 06:57:35 +00003695 // Parse the declarator. This is "PrototypeContext", because we must
3696 // accept either 'declarator' or 'abstract-declarator' here.
3697 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3698 ParseDeclarator(ParmDecl);
3699
3700 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003701 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003702
Chris Lattner371ed4e2008-04-06 06:57:35 +00003703 // Remember this parsed parameter in ParamInfo.
3704 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003705
Douglas Gregor4d87df52008-12-16 21:30:33 +00003706 // DefArgToks is used when the parsing of default arguments needs
3707 // to be delayed.
3708 CachedTokens *DefArgToks = 0;
3709
Chris Lattner371ed4e2008-04-06 06:57:35 +00003710 // If no parameter was specified, verify that *something* was specified,
3711 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003712 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3713 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003714 // Completely missing, emit error.
3715 Diag(DSStart, diag::err_missing_param);
3716 } else {
3717 // Otherwise, we have something. Add it and let semantic analysis try
3718 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003719
Chris Lattner371ed4e2008-04-06 06:57:35 +00003720 // Inform the actions module about the parameter declarator, so it gets
3721 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003722 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003723
3724 // Parse the default argument, if any. We parse the default
3725 // arguments in all dialects; the semantic analysis in
3726 // ActOnParamDefaultArgument will reject the default argument in
3727 // C.
3728 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003729 SourceLocation EqualLoc = Tok.getLocation();
3730
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003731 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003732 if (D.getContext() == Declarator::MemberContext) {
3733 // If we're inside a class definition, cache the tokens
3734 // corresponding to the default argument. We'll actually parse
3735 // them when we see the end of the class definition.
3736 // FIXME: Templates will require something similar.
3737 // FIXME: Can we use a smart pointer for Toks?
3738 DefArgToks = new CachedTokens;
3739
Mike Stump11289f42009-09-09 15:08:12 +00003740 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003741 /*StopAtSemi=*/true,
3742 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003743 delete DefArgToks;
3744 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003745 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003746 } else {
3747 // Mark the end of the default argument so that we know when to
3748 // stop when we parse it later on.
3749 Token DefArgEnd;
3750 DefArgEnd.startToken();
3751 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3752 DefArgEnd.setLocation(Tok.getLocation());
3753 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003754 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003755 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003756 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003757 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003758 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003759 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003760
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003761 // The argument isn't actually potentially evaluated unless it is
3762 // used.
3763 EnterExpressionEvaluationContext Eval(Actions,
3764 Sema::PotentiallyEvaluatedIfUsed);
3765
John McCalldadc5752010-08-24 06:29:42 +00003766 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003767 if (DefArgResult.isInvalid()) {
3768 Actions.ActOnParamDefaultArgumentError(Param);
3769 SkipUntil(tok::comma, tok::r_paren, true, true);
3770 } else {
3771 // Inform the actions module about the default argument
3772 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003773 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003774 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003775 }
3776 }
Mike Stump11289f42009-09-09 15:08:12 +00003777
3778 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3779 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003780 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003781 }
3782
3783 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003784 if (Tok.isNot(tok::comma)) {
3785 if (Tok.is(tok::ellipsis)) {
3786 IsVariadic = true;
3787 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3788
3789 if (!getLang().CPlusPlus) {
3790 // We have ellipsis without a preceding ',', which is ill-formed
3791 // in C. Complain and provide the fix.
3792 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003793 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003794 }
3795 }
3796
3797 break;
3798 }
Mike Stump11289f42009-09-09 15:08:12 +00003799
Chris Lattner371ed4e2008-04-06 06:57:35 +00003800 // Consume the comma.
3801 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003802 }
Mike Stump11289f42009-09-09 15:08:12 +00003803
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003804 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003805 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003806
John McCall084e83d2011-03-24 11:26:52 +00003807 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003808 SourceLocation RefQualifierLoc;
3809 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003810 ExceptionSpecificationType ESpecType = EST_None;
3811 SourceRange ESpecRange;
3812 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3813 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3814 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003815
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003816 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003817 MaybeParseCXX0XAttributes(attrs);
3818
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003819 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003820 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003821 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003822 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003823
Douglas Gregor54992352011-01-26 03:43:54 +00003824 // Parse ref-qualifier[opt]
3825 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3826 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003827 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003828
3829 RefQualifierIsLValueRef = Tok.is(tok::amp);
3830 RefQualifierLoc = ConsumeToken();
3831 EndLoc = RefQualifierLoc;
3832 }
3833
Sebastian Redl965b0e32011-03-05 14:45:16 +00003834 // FIXME: We should leave the prototype scope before parsing the exception
3835 // specification, and then reenter it when parsing the trailing return type.
3836 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3837
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003838 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003839 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3840 DynamicExceptions,
3841 DynamicExceptionRanges,
3842 NoexceptExpr);
3843 if (ESpecType != EST_None)
3844 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003845
3846 // Parse trailing-return-type.
3847 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3848 TrailingReturnType = ParseTrailingReturnType().get();
3849 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003850 }
3851
Douglas Gregor7fb25412010-10-01 18:44:50 +00003852 // Leave prototype scope.
3853 PrototypeScope.Exit();
3854
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003855 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003856 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003857 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003858 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003859 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003860 RefQualifierIsLValueRef,
3861 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003862 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003863 DynamicExceptions.data(),
3864 DynamicExceptionRanges.data(),
3865 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003866 NoexceptExpr.isUsable() ?
3867 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003868 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003869 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003870 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003871}
Chris Lattneracd58a32006-08-06 17:24:14 +00003872
Chris Lattner6c940e62008-04-06 06:34:08 +00003873/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3874/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003875/// first identifier has already been consumed, and the current token is the
3876/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003877///
3878/// identifier-list: [C99 6.7.5]
3879/// identifier
3880/// identifier-list ',' identifier
3881///
3882void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003883 IdentifierInfo *FirstIdent,
3884 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003885 Declarator &D) {
3886 // Build up an array of information about the parsed arguments.
3887 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3888 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003889
Chris Lattner6c940e62008-04-06 06:34:08 +00003890 // If there was no identifier specified for the declarator, either we are in
3891 // an abstract-declarator, or we are in a parameter declarator which was found
3892 // to be abstract. In abstract-declarators, identifier lists are not valid:
3893 // diagnose this.
3894 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003895 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003896
Chris Lattner9453ab82010-05-14 17:23:36 +00003897 // The first identifier was already read, and is known to be the first
3898 // identifier in the list. Remember this identifier in ParamInfo.
3899 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003900 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003901
Chris Lattner6c940e62008-04-06 06:34:08 +00003902 while (Tok.is(tok::comma)) {
3903 // Eat the comma.
3904 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003905
Chris Lattner9186f552008-04-06 06:39:19 +00003906 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003907 if (Tok.isNot(tok::identifier)) {
3908 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003909 SkipUntil(tok::r_paren);
3910 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003911 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003912
Chris Lattner6c940e62008-04-06 06:34:08 +00003913 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003914
3915 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003916 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003917 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003918
Chris Lattner6c940e62008-04-06 06:34:08 +00003919 // Verify that the argument identifier has not already been mentioned.
3920 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003921 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003922 } else {
3923 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003924 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003925 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003926 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003927 }
Mike Stump11289f42009-09-09 15:08:12 +00003928
Chris Lattner6c940e62008-04-06 06:34:08 +00003929 // Eat the identifier.
3930 ConsumeToken();
3931 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003932
3933 // If we have the closing ')', eat it and we're done.
3934 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3935
Chris Lattner9186f552008-04-06 06:39:19 +00003936 // Remember that we parsed a function type, and remember the attributes. This
3937 // function type is always a K&R style function type, which is not varargs and
3938 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00003939 ParsedAttributes attrs(AttrFactory);
3940 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003941 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003942 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003943 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003944 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003945 EST_None, SourceLocation(), 0, 0,
3946 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00003947 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003948}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003949
Chris Lattnere8074e62006-08-06 18:30:15 +00003950/// [C90] direct-declarator '[' constant-expression[opt] ']'
3951/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3952/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3953/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3954/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3955void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003956 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003957
Chris Lattner84a11622008-12-18 07:27:21 +00003958 // C array syntax has many features, but by-far the most common is [] and [4].
3959 // This code does a fast path to handle some of the most obvious cases.
3960 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003961 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003962 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003963 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003964
Chris Lattner84a11622008-12-18 07:27:21 +00003965 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003966 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00003967 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00003968 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00003969 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003970 return;
3971 } else if (Tok.getKind() == tok::numeric_constant &&
3972 GetLookAheadToken(1).is(tok::r_square)) {
3973 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003974 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003975 ConsumeToken();
3976
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003977 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003978 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003979 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003980
Chris Lattner84a11622008-12-18 07:27:21 +00003981 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00003982 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00003983 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00003984 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00003985 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003986 return;
3987 }
Mike Stump11289f42009-09-09 15:08:12 +00003988
Chris Lattnere8074e62006-08-06 18:30:15 +00003989 // If valid, this location is the position where we read the 'static' keyword.
3990 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003991 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003992 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003993
Chris Lattnere8074e62006-08-06 18:30:15 +00003994 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003995 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00003996 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003997 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003998
Chris Lattnere8074e62006-08-06 18:30:15 +00003999 // If we haven't already read 'static', check to see if there is one after the
4000 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004001 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004002 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004003
Chris Lattnere8074e62006-08-06 18:30:15 +00004004 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004005 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004006 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004007
Chris Lattner521ff2b2008-04-06 05:26:30 +00004008 // Handle the case where we have '[*]' as the array size. However, a leading
4009 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4010 // the the token after the star is a ']'. Since stars in arrays are
4011 // infrequent, use of lookahead is not costly here.
4012 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004013 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004014
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004015 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004016 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004017 StaticLoc = SourceLocation(); // Drop the static.
4018 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004019 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004020 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004021 // Note, in C89, this production uses the constant-expr production instead
4022 // of assignment-expr. The only difference is that assignment-expr allows
4023 // things like '=' and '*='. Sema rejects these in C89 mode because they
4024 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004025
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004026 // Parse the constant-expression or assignment-expression now (depending
4027 // on dialect).
4028 if (getLang().CPlusPlus)
4029 NumElements = ParseConstantExpression();
4030 else
4031 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004032 }
Mike Stump11289f42009-09-09 15:08:12 +00004033
Chris Lattner62591722006-08-12 18:40:58 +00004034 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004035 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004036 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004037 // If the expression was invalid, skip it.
4038 SkipUntil(tok::r_square);
4039 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004040 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004041
4042 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4043
John McCall084e83d2011-03-24 11:26:52 +00004044 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004045 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004046
Chris Lattner84a11622008-12-18 07:27:21 +00004047 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004048 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004049 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004050 NumElements.release(),
4051 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004052 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004053}
4054
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004055/// [GNU] typeof-specifier:
4056/// typeof ( expressions )
4057/// typeof ( type-name )
4058/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004059///
4060void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004061 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004062 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004063 SourceLocation StartLoc = ConsumeToken();
4064
John McCalle8595032010-01-13 20:03:27 +00004065 const bool hasParens = Tok.is(tok::l_paren);
4066
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004067 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004068 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004069 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004070 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4071 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004072 if (hasParens)
4073 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004074
4075 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004076 // FIXME: Not accurate, the range gets one token more than it should.
4077 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004078 else
4079 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004080
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004081 if (isCastExpr) {
4082 if (!CastTy) {
4083 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004084 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004085 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004086
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004087 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004088 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004089 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4090 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004091 DiagID, CastTy))
4092 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004093 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004094 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004095
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004096 // If we get here, the operand to the typeof was an expresion.
4097 if (Operand.isInvalid()) {
4098 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004099 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004100 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004101
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004102 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004103 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004104 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4105 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004106 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004107 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004108}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004109
4110
4111/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4112/// from TryAltiVecVectorToken.
4113bool Parser::TryAltiVecVectorTokenOutOfLine() {
4114 Token Next = NextToken();
4115 switch (Next.getKind()) {
4116 default: return false;
4117 case tok::kw_short:
4118 case tok::kw_long:
4119 case tok::kw_signed:
4120 case tok::kw_unsigned:
4121 case tok::kw_void:
4122 case tok::kw_char:
4123 case tok::kw_int:
4124 case tok::kw_float:
4125 case tok::kw_double:
4126 case tok::kw_bool:
4127 case tok::kw___pixel:
4128 Tok.setKind(tok::kw___vector);
4129 return true;
4130 case tok::identifier:
4131 if (Next.getIdentifierInfo() == Ident_pixel) {
4132 Tok.setKind(tok::kw___vector);
4133 return true;
4134 }
4135 return false;
4136 }
4137}
4138
4139bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4140 const char *&PrevSpec, unsigned &DiagID,
4141 bool &isInvalid) {
4142 if (Tok.getIdentifierInfo() == Ident_vector) {
4143 Token Next = NextToken();
4144 switch (Next.getKind()) {
4145 case tok::kw_short:
4146 case tok::kw_long:
4147 case tok::kw_signed:
4148 case tok::kw_unsigned:
4149 case tok::kw_void:
4150 case tok::kw_char:
4151 case tok::kw_int:
4152 case tok::kw_float:
4153 case tok::kw_double:
4154 case tok::kw_bool:
4155 case tok::kw___pixel:
4156 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4157 return true;
4158 case tok::identifier:
4159 if (Next.getIdentifierInfo() == Ident_pixel) {
4160 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4161 return true;
4162 }
4163 break;
4164 default:
4165 break;
4166 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004167 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004168 DS.isTypeAltiVecVector()) {
4169 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4170 return true;
4171 }
4172 return false;
4173}