blob: 40674ee7a98f73ff474ca4baf4a7b14df42ae83c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 ObjCDeclSpec *objcQuals,
37 AccessSpecifier AS,
38 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
John McCallf85e1932011-06-15 23:02:42 +000041 DS.setObjCQualifiers(objcQuals);
Richard Smithc89edf52011-07-01 19:46:12 +000042 ParseSpecifierQualifierList(DS, AS);
43 if (OwnedType)
44 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000047 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000049 if (Range)
50 *Range = DeclaratorInfo.getSourceRange();
51
Chris Lattnereaaebc72009-04-25 08:06:05 +000052 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000053 return true;
54
Douglas Gregor23c94db2010-07-02 17:43:08 +000055 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000056}
57
Sean Huntbbd37c62009-11-21 08:43:09 +000058/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000059///
60/// [GNU] attributes:
61/// attribute
62/// attributes attribute
63///
64/// [GNU] attribute:
65/// '__attribute__' '(' '(' attribute-list ')' ')'
66///
67/// [GNU] attribute-list:
68/// attrib
69/// attribute_list ',' attrib
70///
71/// [GNU] attrib:
72/// empty
73/// attrib-name
74/// attrib-name '(' identifier ')'
75/// attrib-name '(' identifier ',' nonempty-expr-list ')'
76/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
77///
78/// [GNU] attrib-name:
79/// identifier
80/// typespec
81/// typequal
82/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000083///
Reid Spencer5f016e22007-07-11 17:01:13 +000084/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000085/// token lookahead. Comment from gcc: "If they start with an identifier
86/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000087/// start with that identifier; otherwise they are an expression list."
88///
89/// At the moment, I am not doing 2 token lookahead. I am also unaware of
90/// any attributes that don't work (based on my limited testing). Most
91/// attributes are very simple in practice. Until we find a bug, I don't see
92/// a pressing need to implement the 2 token lookahead.
93
John McCall7f040a92010-12-24 02:08:15 +000094void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
95 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000096 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner04d66662007-10-09 17:33:22 +000098 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ConsumeToken();
100 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
101 "attribute")) {
102 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000103 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 }
105 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
106 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000107 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
109 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000110 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
111 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112
113 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
115 ConsumeToken();
116 continue;
117 }
118 // we have an identifier or declaration specifier (const, int, etc.)
119 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
120 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000122 // Availability attributes have their own grammar.
123 if (AttrName->isStr("availability"))
124 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000125 // Thread safety attributes fit into the FIXME case above, so we
126 // just parse the arguments as a list of expressions
127 else if (IsThreadSafetyAttribute(AttrName->getName()))
128 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000129 // check if we have a "parameterized" attribute
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000130 else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner04d66662007-10-09 17:33:22 +0000133 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
135 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
137 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // __attribute__(( mode(byte) ))
139 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000140 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
141 ParmName, ParmLoc, 0, 0);
Chris Lattner04d66662007-10-09 17:33:22 +0000142 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 ConsumeToken();
144 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000145 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 // now parse the non-empty comma separated list of expressions
149 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000150 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000151 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 ArgExprsOk = false;
153 SkipUntil(tok::r_paren);
154 break;
155 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000156 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 }
Chris Lattner04d66662007-10-09 17:33:22 +0000158 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 break;
160 ConsumeToken(); // Eat the comma, move to the next argument
161 }
Chris Lattner04d66662007-10-09 17:33:22 +0000162 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000164 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
165 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
167 }
168 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000169 switch (Tok.getKind()) {
170 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 // __attribute__(( nonnull() ))
173 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000174 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
175 0, SourceLocation(), 0, 0);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000176 break;
177 case tok::kw_char:
178 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000179 case tok::kw_char16_t:
180 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 case tok::kw_bool:
182 case tok::kw_short:
183 case tok::kw_int:
184 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000185 case tok::kw___int64:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000186 case tok::kw_signed:
187 case tok::kw_unsigned:
188 case tok::kw_float:
189 case tok::kw_double:
190 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000191 case tok::kw_typeof: {
192 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000193 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
194 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000195 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000196 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000197 // If it's a builtin type name, eat it and expect a rparen
198 // __attribute__(( vec_type_hint(char) ))
199 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000200 if (Tok.is(tok::r_paren))
201 ConsumeParen();
202 break;
John McCall7f040a92010-12-24 02:08:15 +0000203 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000204 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000206 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // now parse the list of expressions
210 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000211 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000212 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 ArgExprsOk = false;
214 SkipUntil(tok::r_paren);
215 break;
216 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000217 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
Chris Lattner04d66662007-10-09 17:33:22 +0000219 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 break;
221 ConsumeToken(); // Eat the comma, move to the next argument
222 }
223 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000224 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000226 attrs.addNew(AttrName, AttrNameLoc, 0,
227 AttrNameLoc, 0, SourceLocation(),
228 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000230 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 }
233 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000234 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
235 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 }
237 }
238 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000240 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000241 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
242 SkipUntil(tok::r_paren, false);
243 }
John McCall7f040a92010-12-24 02:08:15 +0000244 if (endLoc)
245 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000247}
248
Eli Friedmana23b4852009-06-08 07:21:15 +0000249/// ParseMicrosoftDeclSpec - Parse an __declspec construct
250///
251/// [MS] decl-specifier:
252/// __declspec ( extended-decl-modifier-seq )
253///
254/// [MS] extended-decl-modifier-seq:
255/// extended-decl-modifier[opt]
256/// extended-decl-modifier extended-decl-modifier-seq
257
John McCall7f040a92010-12-24 02:08:15 +0000258void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000259 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000260
Steve Narofff59e17e2008-12-24 20:59:21 +0000261 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000262 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
263 "declspec")) {
264 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000265 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000266 }
Francois Pichet373197b2011-05-07 19:04:49 +0000267
Eli Friedman290eeb02009-06-08 23:27:34 +0000268 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
270 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000271
272 // FIXME: Remove this when we have proper __declspec(property()) support.
273 // Just skip everything inside property().
274 if (AttrName->getName() == "property") {
275 ConsumeParen();
276 SkipUntil(tok::r_paren);
277 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000278 if (Tok.is(tok::l_paren)) {
279 ConsumeParen();
280 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
281 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000282 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000283 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000284 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000285 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
286 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000287 }
288 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
289 SkipUntil(tok::r_paren, false);
290 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000291 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
292 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000293 }
294 }
295 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
296 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000297 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000298}
299
John McCall7f040a92010-12-24 02:08:15 +0000300void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000301 // Treat these like attributes
302 // FIXME: Allow Sema to distinguish between these and real attributes!
303 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000304 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000305 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000306 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000307 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000308 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
309 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000310 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
311 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000312 // FIXME: Support these properly!
313 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000314 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
315 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000316 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000317}
318
John McCall7f040a92010-12-24 02:08:15 +0000319void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000320 // Treat these like attributes
321 while (Tok.is(tok::kw___pascal)) {
322 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
323 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000324 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
325 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000326 }
John McCall7f040a92010-12-24 02:08:15 +0000327}
328
Peter Collingbournef315fa82011-02-14 01:42:53 +0000329void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
330 // Treat these like attributes
331 while (Tok.is(tok::kw___kernel)) {
332 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000333 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
334 AttrNameLoc, 0, AttrNameLoc, 0,
335 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000336 }
337}
338
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000339void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
340 SourceLocation Loc = Tok.getLocation();
341 switch(Tok.getKind()) {
342 // OpenCL qualifiers:
343 case tok::kw___private:
344 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000345 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000346 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000347 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000348 break;
349
350 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000351 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000352 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000353 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000354 break;
355
356 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000357 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000358 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000359 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000360 break;
361
362 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000363 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000364 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000365 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000366 break;
367
368 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000369 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000370 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000371 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000372 break;
373
374 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000375 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000377 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000378 break;
379
380 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000381 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000383 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384 break;
385 default: break;
386 }
387}
388
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000389/// \brief Parse a version number.
390///
391/// version:
392/// simple-integer
393/// simple-integer ',' simple-integer
394/// simple-integer ',' simple-integer ',' simple-integer
395VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
396 Range = Tok.getLocation();
397
398 if (!Tok.is(tok::numeric_constant)) {
399 Diag(Tok, diag::err_expected_version);
400 SkipUntil(tok::comma, tok::r_paren, true, true, true);
401 return VersionTuple();
402 }
403
404 // Parse the major (and possibly minor and subminor) versions, which
405 // are stored in the numeric constant. We utilize a quirk of the
406 // lexer, which is that it handles something like 1.2.3 as a single
407 // numeric constant, rather than two separate tokens.
408 llvm::SmallString<512> Buffer;
409 Buffer.resize(Tok.getLength()+1);
410 const char *ThisTokBegin = &Buffer[0];
411
412 // Get the spelling of the token, which eliminates trigraphs, etc.
413 bool Invalid = false;
414 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
415 if (Invalid)
416 return VersionTuple();
417
418 // Parse the major version.
419 unsigned AfterMajor = 0;
420 unsigned Major = 0;
421 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
422 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
423 ++AfterMajor;
424 }
425
426 if (AfterMajor == 0) {
427 Diag(Tok, diag::err_expected_version);
428 SkipUntil(tok::comma, tok::r_paren, true, true, true);
429 return VersionTuple();
430 }
431
432 if (AfterMajor == ActualLength) {
433 ConsumeToken();
434
435 // We only had a single version component.
436 if (Major == 0) {
437 Diag(Tok, diag::err_zero_version);
438 return VersionTuple();
439 }
440
441 return VersionTuple(Major);
442 }
443
444 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
445 Diag(Tok, diag::err_expected_version);
446 SkipUntil(tok::comma, tok::r_paren, true, true, true);
447 return VersionTuple();
448 }
449
450 // Parse the minor version.
451 unsigned AfterMinor = AfterMajor + 1;
452 unsigned Minor = 0;
453 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
454 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
455 ++AfterMinor;
456 }
457
458 if (AfterMinor == ActualLength) {
459 ConsumeToken();
460
461 // We had major.minor.
462 if (Major == 0 && Minor == 0) {
463 Diag(Tok, diag::err_zero_version);
464 return VersionTuple();
465 }
466
467 return VersionTuple(Major, Minor);
468 }
469
470 // If what follows is not a '.', we have a problem.
471 if (ThisTokBegin[AfterMinor] != '.') {
472 Diag(Tok, diag::err_expected_version);
473 SkipUntil(tok::comma, tok::r_paren, true, true, true);
474 return VersionTuple();
475 }
476
477 // Parse the subminor version.
478 unsigned AfterSubminor = AfterMinor + 1;
479 unsigned Subminor = 0;
480 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
481 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
482 ++AfterSubminor;
483 }
484
485 if (AfterSubminor != ActualLength) {
486 Diag(Tok, diag::err_expected_version);
487 SkipUntil(tok::comma, tok::r_paren, true, true, true);
488 return VersionTuple();
489 }
490 ConsumeToken();
491 return VersionTuple(Major, Minor, Subminor);
492}
493
494/// \brief Parse the contents of the "availability" attribute.
495///
496/// availability-attribute:
497/// 'availability' '(' platform ',' version-arg-list ')'
498///
499/// platform:
500/// identifier
501///
502/// version-arg-list:
503/// version-arg
504/// version-arg ',' version-arg-list
505///
506/// version-arg:
507/// 'introduced' '=' version
508/// 'deprecated' '=' version
509/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000510/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000511void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
512 SourceLocation AvailabilityLoc,
513 ParsedAttributes &attrs,
514 SourceLocation *endLoc) {
515 SourceLocation PlatformLoc;
516 IdentifierInfo *Platform = 0;
517
518 enum { Introduced, Deprecated, Obsoleted, Unknown };
519 AvailabilityChange Changes[Unknown];
520
521 // Opening '('.
522 SourceLocation LParenLoc;
523 if (!Tok.is(tok::l_paren)) {
524 Diag(Tok, diag::err_expected_lparen);
525 return;
526 }
527 LParenLoc = ConsumeParen();
528
529 // Parse the platform name,
530 if (Tok.isNot(tok::identifier)) {
531 Diag(Tok, diag::err_availability_expected_platform);
532 SkipUntil(tok::r_paren);
533 return;
534 }
535 Platform = Tok.getIdentifierInfo();
536 PlatformLoc = ConsumeToken();
537
538 // Parse the ',' following the platform name.
539 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
540 return;
541
542 // If we haven't grabbed the pointers for the identifiers
543 // "introduced", "deprecated", and "obsoleted", do so now.
544 if (!Ident_introduced) {
545 Ident_introduced = PP.getIdentifierInfo("introduced");
546 Ident_deprecated = PP.getIdentifierInfo("deprecated");
547 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000548 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000549 }
550
551 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000552 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000553 do {
554 if (Tok.isNot(tok::identifier)) {
555 Diag(Tok, diag::err_availability_expected_change);
556 SkipUntil(tok::r_paren);
557 return;
558 }
559 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
560 SourceLocation KeywordLoc = ConsumeToken();
561
Douglas Gregorb53e4172011-03-26 03:35:55 +0000562 if (Keyword == Ident_unavailable) {
563 if (UnavailableLoc.isValid()) {
564 Diag(KeywordLoc, diag::err_availability_redundant)
565 << Keyword << SourceRange(UnavailableLoc);
566 }
567 UnavailableLoc = KeywordLoc;
568
569 if (Tok.isNot(tok::comma))
570 break;
571
572 ConsumeToken();
573 continue;
574 }
575
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000576 if (Tok.isNot(tok::equal)) {
577 Diag(Tok, diag::err_expected_equal_after)
578 << Keyword;
579 SkipUntil(tok::r_paren);
580 return;
581 }
582 ConsumeToken();
583
584 SourceRange VersionRange;
585 VersionTuple Version = ParseVersionTuple(VersionRange);
586
587 if (Version.empty()) {
588 SkipUntil(tok::r_paren);
589 return;
590 }
591
592 unsigned Index;
593 if (Keyword == Ident_introduced)
594 Index = Introduced;
595 else if (Keyword == Ident_deprecated)
596 Index = Deprecated;
597 else if (Keyword == Ident_obsoleted)
598 Index = Obsoleted;
599 else
600 Index = Unknown;
601
602 if (Index < Unknown) {
603 if (!Changes[Index].KeywordLoc.isInvalid()) {
604 Diag(KeywordLoc, diag::err_availability_redundant)
605 << Keyword
606 << SourceRange(Changes[Index].KeywordLoc,
607 Changes[Index].VersionRange.getEnd());
608 }
609
610 Changes[Index].KeywordLoc = KeywordLoc;
611 Changes[Index].Version = Version;
612 Changes[Index].VersionRange = VersionRange;
613 } else {
614 Diag(KeywordLoc, diag::err_availability_unknown_change)
615 << Keyword << VersionRange;
616 }
617
618 if (Tok.isNot(tok::comma))
619 break;
620
621 ConsumeToken();
622 } while (true);
623
624 // Closing ')'.
625 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
626 if (RParenLoc.isInvalid())
627 return;
628
629 if (endLoc)
630 *endLoc = RParenLoc;
631
Douglas Gregorb53e4172011-03-26 03:35:55 +0000632 // The 'unavailable' availability cannot be combined with any other
633 // availability changes. Make sure that hasn't happened.
634 if (UnavailableLoc.isValid()) {
635 bool Complained = false;
636 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
637 if (Changes[Index].KeywordLoc.isValid()) {
638 if (!Complained) {
639 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
640 << SourceRange(Changes[Index].KeywordLoc,
641 Changes[Index].VersionRange.getEnd());
642 Complained = true;
643 }
644
645 // Clear out the availability.
646 Changes[Index] = AvailabilityChange();
647 }
648 }
649 }
650
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000651 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000652 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000653 0, SourceLocation(),
654 Platform, PlatformLoc,
655 Changes[Introduced],
656 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000657 Changes[Obsoleted],
658 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000659}
660
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000661/// \brief Wrapper around a case statement checking if AttrName is
662/// one of the thread safety attributes
663bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
664 return llvm::StringSwitch<bool>(AttrName)
665 .Case("guarded_by", true)
666 .Case("guarded_var", true)
667 .Case("pt_guarded_by", true)
668 .Case("pt_guarded_var", true)
669 .Case("lockable", true)
670 .Case("scoped_lockable", true)
671 .Case("no_thread_safety_analysis", true)
672 .Case("acquired_after", true)
673 .Case("acquired_before", true)
674 .Case("exclusive_lock_function", true)
675 .Case("shared_lock_function", true)
676 .Case("exclusive_trylock_function", true)
677 .Case("shared_trylock_function", true)
678 .Case("unlock_function", true)
679 .Case("lock_returned", true)
680 .Case("locks_excluded", true)
681 .Case("exclusive_locks_required", true)
682 .Case("shared_locks_required", true)
683 .Default(false);
684}
685
686/// \brief Parse the contents of thread safety attributes. These
687/// should always be parsed as an expression list.
688///
689/// We need to special case the parsing due to the fact that if the first token
690/// of the first argument is an identifier, the main parse loop will store
691/// that token as a "parameter" and the rest of
692/// the arguments will be added to a list of "arguments". However,
693/// subsequent tokens in the first argument are lost. We instead parse each
694/// argument as an expression and add all arguments to the list of "arguments".
695/// In future, we will take advantage of this special case to also
696/// deal with some argument scoping issues here (for example, referring to a
697/// function parameter in the attribute on that function).
698void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
699 SourceLocation AttrNameLoc,
700 ParsedAttributes &Attrs,
701 SourceLocation *EndLoc) {
702
703 if (Tok.is(tok::l_paren)) {
704 SourceLocation LeftParenLoc = Tok.getLocation();
705 ConsumeParen(); // ignore the left paren loc for now
706
707 ExprVector ArgExprs(Actions);
708 bool ArgExprsOk = true;
709
710 // now parse the list of expressions
711 while (1) {
712 ExprResult ArgExpr(ParseAssignmentExpression());
713 if (ArgExpr.isInvalid()) {
714 ArgExprsOk = false;
715 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
716 break;
717 } else {
718 ArgExprs.push_back(ArgExpr.release());
719 }
720 if (Tok.isNot(tok::comma))
721 break;
722 ConsumeToken(); // Eat the comma, move to the next argument
723 }
724 // Match the ')'.
725 if (ArgExprsOk && Tok.is(tok::r_paren)) {
726 ConsumeParen(); // ignore the right paren loc for now
727 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
728 ArgExprs.take(), ArgExprs.size());
729 }
730 } else {
731 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc,
732 0, SourceLocation(), 0, 0);
733 }
734}
735
John McCall7f040a92010-12-24 02:08:15 +0000736void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
737 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
738 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000739}
740
Reid Spencer5f016e22007-07-11 17:01:13 +0000741/// ParseDeclaration - Parse a full 'declaration', which consists of
742/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000743/// 'Context' should be a Declarator::TheContext value. This returns the
744/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000745///
746/// declaration: [C99 6.7]
747/// block-declaration ->
748/// simple-declaration
749/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000750/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000751/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000752/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000753/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000754/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000755/// others... [FIXME]
756///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000757Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
758 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000759 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000760 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000761 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000762 // Must temporarily exit the objective-c container scope for
763 // parsing c none objective-c decls.
764 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000765
John McCalld226f652010-08-21 09:40:31 +0000766 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000767 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000768 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000769 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000770 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000771 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000772 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000773 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000774 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000775 // Could be the start of an inline namespace. Allowed as an ext in C++03.
776 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000777 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000778 SourceLocation InlineLoc = ConsumeToken();
779 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
780 break;
781 }
John McCall7f040a92010-12-24 02:08:15 +0000782 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000783 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000784 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000785 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000786 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000787 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000788 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000789 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000790 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000791 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000792 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000793 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000794 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000795 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000796 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000797 default:
John McCall7f040a92010-12-24 02:08:15 +0000798 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000799 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000800
Chris Lattner682bf922009-03-29 16:50:03 +0000801 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000802 // single decl, convert it now. Alias declarations can also declare a type;
803 // include that too if it is present.
804 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000805}
806
807/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
808/// declaration-specifiers init-declarator-list[opt] ';'
809///[C90/C++]init-declarator-list ';' [TODO]
810/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000811///
Richard Smithad762fc2011-04-14 22:09:26 +0000812/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
813/// attribute-specifier-seq[opt] type-specifier-seq declarator
814///
Chris Lattnercd147752009-03-29 17:27:48 +0000815/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000816/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000817///
818/// If FRI is non-null, we might be parsing a for-range-declaration instead
819/// of a simple-declaration. If we find that we are, we also parse the
820/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000821Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
822 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000823 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000824 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000825 bool RequireSemi,
826 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000828 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000829 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000830
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000831 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000832 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000833 StmtResult R = Actions.ActOnVlaStmt(DS);
834 if (R.isUsable())
835 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000836
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
838 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000839 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000840 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000841 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000842 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000843 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000844 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000846
847 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000848}
Mike Stump1eb44332009-09-09 15:08:12 +0000849
John McCalld8ac0572009-11-03 19:26:08 +0000850/// ParseDeclGroup - Having concluded that this is either a function
851/// definition or a group of object declarations, actually parse the
852/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000853Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
854 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000855 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000856 SourceLocation *DeclEnd,
857 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000858 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000859 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000860 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000861
John McCalld8ac0572009-11-03 19:26:08 +0000862 // Bail out if the first declarator didn't seem well-formed.
863 if (!D.hasName() && !D.mayOmitIdentifier()) {
864 // Skip until ; or }.
865 SkipUntil(tok::r_brace, true, true);
866 if (Tok.is(tok::semi))
867 ConsumeToken();
868 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Chris Lattnerc82daef2010-07-11 22:24:20 +0000871 // Check to see if we have a function *definition* which must have a body.
872 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
873 // Look at the next token to make sure that this isn't a function
874 // declaration. We have to check this because __attribute__ might be the
875 // start of a function definition in GCC-extended K&R C.
876 !isDeclarationAfterDeclarator()) {
877
Chris Lattner004659a2010-07-11 22:42:07 +0000878 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000879 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
880 Diag(Tok, diag::err_function_declared_typedef);
881
882 // Recover by treating the 'typedef' as spurious.
883 DS.ClearStorageClassSpecs();
884 }
885
John McCalld226f652010-08-21 09:40:31 +0000886 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000887 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000888 }
889
890 if (isDeclarationSpecifier()) {
891 // If there is an invalid declaration specifier right after the function
892 // prototype, then we must be in a missing semicolon case where this isn't
893 // actually a body. Just fall through into the code that handles it as a
894 // prototype, and let the top-level code handle the erroneous declspec
895 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000896 } else {
897 Diag(Tok, diag::err_expected_fn_body);
898 SkipUntil(tok::semi);
899 return DeclGroupPtrTy();
900 }
901 }
902
Richard Smithad762fc2011-04-14 22:09:26 +0000903 if (ParseAttributesAfterDeclarator(D))
904 return DeclGroupPtrTy();
905
906 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
907 // must parse and analyze the for-range-initializer before the declaration is
908 // analyzed.
909 if (FRI && Tok.is(tok::colon)) {
910 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000911 if (Tok.is(tok::l_brace))
912 FRI->RangeExpr = ParseBraceInitializer();
913 else
914 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +0000915 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
916 Actions.ActOnCXXForRangeDecl(ThisDecl);
917 Actions.FinalizeDeclaration(ThisDecl);
918 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
919 }
920
Chris Lattner5f9e2722011-07-23 10:55:15 +0000921 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000922 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000923 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000924 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000925 DeclsInGroup.push_back(FirstDecl);
926
927 // If we don't have a comma, it is either the end of the list (a ';') or an
928 // error, bail out.
929 while (Tok.is(tok::comma)) {
930 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000931 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000932
933 // Parse the next declarator.
934 D.clear();
935
936 // Accept attributes in an init-declarator. In the first declarator in a
937 // declaration, these would be part of the declspec. In subsequent
938 // declarators, they become part of the declarator itself, so that they
939 // don't apply to declarators after *this* one. Examples:
940 // short __attribute__((common)) var; -> declspec
941 // short var __attribute__((common)); -> declarator
942 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000943 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000944
945 ParseDeclarator(D);
946
John McCalld226f652010-08-21 09:40:31 +0000947 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000948 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000949 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000950 DeclsInGroup.push_back(ThisDecl);
951 }
952
953 if (DeclEnd)
954 *DeclEnd = Tok.getLocation();
955
956 if (Context != Declarator::ForContext &&
957 ExpectAndConsume(tok::semi,
958 Context == Declarator::FileContext
959 ? diag::err_invalid_token_after_toplevel_declarator
960 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000961 // Okay, there was no semicolon and one was expected. If we see a
962 // declaration specifier, just assume it was missing and continue parsing.
963 // Otherwise things are very confused and we skip to recover.
964 if (!isDeclarationSpecifier()) {
965 SkipUntil(tok::r_brace, true, true);
966 if (Tok.is(tok::semi))
967 ConsumeToken();
968 }
John McCalld8ac0572009-11-03 19:26:08 +0000969 }
970
Douglas Gregor23c94db2010-07-02 17:43:08 +0000971 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000972 DeclsInGroup.data(),
973 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000974}
975
Richard Smithad762fc2011-04-14 22:09:26 +0000976/// Parse an optional simple-asm-expr and attributes, and attach them to a
977/// declarator. Returns true on an error.
978bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
979 // If a simple-asm-expr is present, parse it.
980 if (Tok.is(tok::kw_asm)) {
981 SourceLocation Loc;
982 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
983 if (AsmLabel.isInvalid()) {
984 SkipUntil(tok::semi, true, true);
985 return true;
986 }
987
988 D.setAsmLabel(AsmLabel.release());
989 D.SetRangeEnd(Loc);
990 }
991
992 MaybeParseGNUAttributes(D);
993 return false;
994}
995
Douglas Gregor1426e532009-05-12 21:31:51 +0000996/// \brief Parse 'declaration' after parsing 'declaration-specifiers
997/// declarator'. This method parses the remainder of the declaration
998/// (including any attributes or initializer, among other things) and
999/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001000///
Reid Spencer5f016e22007-07-11 17:01:13 +00001001/// init-declarator: [C99 6.7]
1002/// declarator
1003/// declarator '=' initializer
1004/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1005/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001006/// [C++] declarator initializer[opt]
1007///
1008/// [C++] initializer:
1009/// [C++] '=' initializer-clause
1010/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001011/// [C++0x] '=' 'default' [TODO]
1012/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001013/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001014///
1015/// According to the standard grammar, =default and =delete are function
1016/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001017///
John McCalld226f652010-08-21 09:40:31 +00001018Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001019 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001020 if (ParseAttributesAfterDeclarator(D))
1021 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Richard Smithad762fc2011-04-14 22:09:26 +00001023 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1024}
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Richard Smithad762fc2011-04-14 22:09:26 +00001026Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1027 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001028 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001029 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001030 switch (TemplateInfo.Kind) {
1031 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001032 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001033 break;
1034
1035 case ParsedTemplateInfo::Template:
1036 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001037 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001038 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001039 TemplateInfo.TemplateParams->data(),
1040 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001041 D);
1042 break;
1043
1044 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001045 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001046 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001047 TemplateInfo.ExternLoc,
1048 TemplateInfo.TemplateLoc,
1049 D);
1050 if (ThisRes.isInvalid()) {
1051 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001052 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001053 }
1054
1055 ThisDecl = ThisRes.get();
1056 break;
1057 }
1058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Richard Smith34b41d92011-02-20 03:19:35 +00001060 bool TypeContainsAuto =
1061 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1062
Douglas Gregor1426e532009-05-12 21:31:51 +00001063 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001064 if (isTokenEqualOrMistypedEqualEqual(
1065 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001066 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001067 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001068 if (D.isFunctionDeclarator())
1069 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1070 << 1 /* delete */;
1071 else
1072 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001073 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001074 if (D.isFunctionDeclarator())
1075 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1076 << 1 /* delete */;
1077 else
1078 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001079 } else {
John McCall731ad842009-12-19 09:28:58 +00001080 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1081 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001082 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001083 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001084
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001085 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001086 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001087 cutOffParsing();
1088 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001089 }
1090
John McCall60d7b3a2010-08-24 06:29:42 +00001091 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001092
John McCall731ad842009-12-19 09:28:58 +00001093 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001094 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001095 ExitScope();
1096 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001097
Douglas Gregor1426e532009-05-12 21:31:51 +00001098 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001099 SkipUntil(tok::comma, true, true);
1100 Actions.ActOnInitializerError(ThisDecl);
1101 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001102 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1103 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001104 }
1105 } else if (Tok.is(tok::l_paren)) {
1106 // Parse C++ direct initializer: '(' expression-list ')'
1107 SourceLocation LParenLoc = ConsumeParen();
1108 ExprVector Exprs(Actions);
1109 CommaLocsTy CommaLocs;
1110
Douglas Gregorb4debae2009-12-22 17:47:17 +00001111 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1112 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001113 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001114 }
1115
Douglas Gregor1426e532009-05-12 21:31:51 +00001116 if (ParseExpressionList(Exprs, CommaLocs)) {
1117 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001118
1119 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001120 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001121 ExitScope();
1122 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001123 } else {
1124 // Match the ')'.
1125 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1126
1127 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1128 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001129
1130 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001131 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001132 ExitScope();
1133 }
1134
Douglas Gregor1426e532009-05-12 21:31:51 +00001135 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1136 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001137 RParenLoc,
1138 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001139 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001140 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1141 // Parse C++0x braced-init-list.
1142 if (D.getCXXScopeSpec().isSet()) {
1143 EnterScope(0);
1144 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1145 }
1146
1147 ExprResult Init(ParseBraceInitializer());
1148
1149 if (D.getCXXScopeSpec().isSet()) {
1150 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1151 ExitScope();
1152 }
1153
1154 if (Init.isInvalid()) {
1155 Actions.ActOnInitializerError(ThisDecl);
1156 } else
1157 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1158 /*DirectInit=*/true, TypeContainsAuto);
1159
Douglas Gregor1426e532009-05-12 21:31:51 +00001160 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001161 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001162 }
1163
Richard Smith483b9f32011-02-21 20:05:19 +00001164 Actions.FinalizeDeclaration(ThisDecl);
1165
Douglas Gregor1426e532009-05-12 21:31:51 +00001166 return ThisDecl;
1167}
1168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169/// ParseSpecifierQualifierList
1170/// specifier-qualifier-list:
1171/// type-specifier specifier-qualifier-list[opt]
1172/// type-qualifier specifier-qualifier-list[opt]
1173/// [GNU] attributes specifier-qualifier-list[opt]
1174///
Richard Smithc89edf52011-07-01 19:46:12 +00001175void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1177 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithc89edf52011-07-01 19:46:12 +00001178 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // Validate declspec for type-name.
1181 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001182 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001183 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 // Issue diagnostic and remove storage class if present.
1187 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1188 if (DS.getStorageClassSpecLoc().isValid())
1189 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1190 else
1191 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1192 DS.ClearStorageClassSpecs();
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 // Issue diagnostic and remove function specfier if present.
1196 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001197 if (DS.isInlineSpecified())
1198 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1199 if (DS.isVirtualSpecified())
1200 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1201 if (DS.isExplicitSpecified())
1202 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 DS.ClearFunctionSpecs();
1204 }
1205}
1206
Chris Lattnerc199ab32009-04-12 20:42:31 +00001207/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1208/// specified token is valid after the identifier in a declarator which
1209/// immediately follows the declspec. For example, these things are valid:
1210///
1211/// int x [ 4]; // direct-declarator
1212/// int x ( int y); // direct-declarator
1213/// int(int x ) // direct-declarator
1214/// int x ; // simple-declaration
1215/// int x = 17; // init-declarator-list
1216/// int x , y; // init-declarator-list
1217/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001218/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001219/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001220///
1221/// This is not, because 'x' does not immediately follow the declspec (though
1222/// ')' happens to be valid anyway).
1223/// int (x)
1224///
1225static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1226 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1227 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001228 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001229}
1230
Chris Lattnere40c2952009-04-14 21:34:55 +00001231
1232/// ParseImplicitInt - This method is called when we have an non-typename
1233/// identifier in a declspec (which normally terminates the decl spec) when
1234/// the declspec has no type specifier. In this case, the declspec is either
1235/// malformed or is "implicit int" (in K&R and C89).
1236///
1237/// This method handles diagnosing this prettily and returns false if the
1238/// declspec is done being processed. If it recovers and thinks there may be
1239/// other pieces of declspec after it, it returns true.
1240///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001241bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001242 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001243 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001244 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattnere40c2952009-04-14 21:34:55 +00001246 SourceLocation Loc = Tok.getLocation();
1247 // If we see an identifier that is not a type name, we normally would
1248 // parse it as the identifer being declared. However, when a typename
1249 // is typo'd or the definition is not included, this will incorrectly
1250 // parse the typename as the identifier name and fall over misparsing
1251 // later parts of the diagnostic.
1252 //
1253 // As such, we try to do some look-ahead in cases where this would
1254 // otherwise be an "implicit-int" case to see if this is invalid. For
1255 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1256 // an identifier with implicit int, we'd get a parse error because the
1257 // next token is obviously invalid for a type. Parse these as a case
1258 // with an invalid type specifier.
1259 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Chris Lattnere40c2952009-04-14 21:34:55 +00001261 // Since we know that this either implicit int (which is rare) or an
1262 // error, we'd do lookahead to try to do better recovery.
1263 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1264 // If this token is valid for implicit int, e.g. "static x = 4", then
1265 // we just avoid eating the identifier, so it will be parsed as the
1266 // identifier in the declarator.
1267 return false;
1268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Chris Lattnere40c2952009-04-14 21:34:55 +00001270 // Otherwise, if we don't consume this token, we are going to emit an
1271 // error anyway. Try to recover from various common problems. Check
1272 // to see if this was a reference to a tag name without a tag specified.
1273 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001274 //
1275 // C++ doesn't need this, and isTagName doesn't take SS.
1276 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001277 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001278 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Douglas Gregor23c94db2010-07-02 17:43:08 +00001280 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001281 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001282 case DeclSpec::TST_enum:
1283 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1284 case DeclSpec::TST_union:
1285 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1286 case DeclSpec::TST_struct:
1287 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1288 case DeclSpec::TST_class:
1289 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattnerf4382f52009-04-14 22:17:06 +00001292 if (TagName) {
1293 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001294 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001295 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Chris Lattnerf4382f52009-04-14 22:17:06 +00001297 // Parse this as a tag as if the missing tag were present.
1298 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001299 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001300 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001301 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001302 return true;
1303 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001304 }
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Douglas Gregora786fdb2009-10-13 23:27:22 +00001306 // This is almost certainly an invalid type name. Let the action emit a
1307 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001308 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001309 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001310 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001311 // The action emitted a diagnostic, so we don't have to.
1312 if (T) {
1313 // The action has suggested that the type T could be used. Set that as
1314 // the type in the declaration specifiers, consume the would-be type
1315 // name token, and we're done.
1316 const char *PrevSpec;
1317 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001318 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001319 DS.SetRangeEnd(Tok.getLocation());
1320 ConsumeToken();
1321
1322 // There may be other declaration specifiers after this.
1323 return true;
1324 }
1325
1326 // Fall through; the action had no suggestion for us.
1327 } else {
1328 // The action did not emit a diagnostic, so emit one now.
1329 SourceRange R;
1330 if (SS) R = SS->getRange();
1331 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1332 }
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Douglas Gregora786fdb2009-10-13 23:27:22 +00001334 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001335 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001336 unsigned DiagID;
1337 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001338 DS.SetRangeEnd(Tok.getLocation());
1339 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Chris Lattnere40c2952009-04-14 21:34:55 +00001341 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1342 // avoid rippling error messages on subsequent uses of the same type,
1343 // could be useful if #include was forgotten.
1344 return false;
1345}
1346
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001347/// \brief Determine the declaration specifier context from the declarator
1348/// context.
1349///
1350/// \param Context the declarator context, which is one of the
1351/// Declarator::TheContext enumerator values.
1352Parser::DeclSpecContext
1353Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1354 if (Context == Declarator::MemberContext)
1355 return DSC_class;
1356 if (Context == Declarator::FileContext)
1357 return DSC_top_level;
1358 return DSC_normal;
1359}
1360
Reid Spencer5f016e22007-07-11 17:01:13 +00001361/// ParseDeclarationSpecifiers
1362/// declaration-specifiers: [C99 6.7]
1363/// storage-class-specifier declaration-specifiers[opt]
1364/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001365/// [C99] function-specifier declaration-specifiers[opt]
1366/// [GNU] attributes declaration-specifiers[opt]
1367///
1368/// storage-class-specifier: [C99 6.7.1]
1369/// 'typedef'
1370/// 'extern'
1371/// 'static'
1372/// 'auto'
1373/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001374/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001375/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001376/// function-specifier: [C99 6.7.4]
1377/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001378/// [C++] 'virtual'
1379/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001380/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001381/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001382/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001385void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001386 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001387 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001388 DeclSpecContext DSContext) {
1389 if (DS.getSourceRange().isInvalid()) {
1390 DS.SetRangeStart(Tok.getLocation());
1391 DS.SetRangeEnd(Tok.getLocation());
1392 }
1393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001395 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001397 unsigned DiagID = 0;
1398
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001400
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001402 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001403 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 // If this is not a declaration specifier token, we're done reading decl
1405 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001406 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001409 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001410 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001411 if (DS.hasTypeSpecifier()) {
1412 bool AllowNonIdentifiers
1413 = (getCurScope()->getFlags() & (Scope::ControlScope |
1414 Scope::BlockScope |
1415 Scope::TemplateParamScope |
1416 Scope::FunctionPrototypeScope |
1417 Scope::AtCatchScope)) == 0;
1418 bool AllowNestedNameSpecifiers
1419 = DSContext == DSC_top_level ||
1420 (DSContext == DSC_class && DS.isFriendSpecified());
1421
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001422 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1423 AllowNonIdentifiers,
1424 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001425 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001426 }
1427
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001428 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1429 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1430 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001431 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1432 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001433 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001434 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001435 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001436 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001437
1438 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001439 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001440 }
1441
Chris Lattner5e02c472009-01-05 00:07:25 +00001442 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001443 // C++ scope specifier. Annotate and loop, or bail out on error.
1444 if (TryAnnotateCXXScopeToken(true)) {
1445 if (!DS.hasTypeSpecifier())
1446 DS.SetTypeSpecError();
1447 goto DoneWithDeclSpec;
1448 }
John McCall2e0a7152010-03-01 18:20:46 +00001449 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1450 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001451 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001452
1453 case tok::annot_cxxscope: {
1454 if (DS.hasTypeSpecifier())
1455 goto DoneWithDeclSpec;
1456
John McCallaa87d332009-12-12 11:40:51 +00001457 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001458 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1459 Tok.getAnnotationRange(),
1460 SS);
John McCallaa87d332009-12-12 11:40:51 +00001461
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001462 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001463 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001464 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001465 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001466 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001467 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001468
1469 // C++ [class.qual]p2:
1470 // In a lookup in which the constructor is an acceptable lookup
1471 // result and the nested-name-specifier nominates a class C:
1472 //
1473 // - if the name specified after the
1474 // nested-name-specifier, when looked up in C, is the
1475 // injected-class-name of C (Clause 9), or
1476 //
1477 // - if the name specified after the nested-name-specifier
1478 // is the same as the identifier or the
1479 // simple-template-id's template-name in the last
1480 // component of the nested-name-specifier,
1481 //
1482 // the name is instead considered to name the constructor of
1483 // class C.
1484 //
1485 // Thus, if the template-name is actually the constructor
1486 // name, then the code is ill-formed; this interpretation is
1487 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001488 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001489 if ((DSContext == DSC_top_level ||
1490 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1491 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001492 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001493 if (isConstructorDeclarator()) {
1494 // The user meant this to be an out-of-line constructor
1495 // definition, but template arguments are not allowed
1496 // there. Just allow this as a constructor; we'll
1497 // complain about it later.
1498 goto DoneWithDeclSpec;
1499 }
1500
1501 // The user meant this to name a type, but it actually names
1502 // a constructor with some extraneous template
1503 // arguments. Complain, then parse it as a type as the user
1504 // intended.
1505 Diag(TemplateId->TemplateNameLoc,
1506 diag::err_out_of_line_template_id_names_constructor)
1507 << TemplateId->Name;
1508 }
1509
John McCallaa87d332009-12-12 11:40:51 +00001510 DS.getTypeSpecScope() = SS;
1511 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001512 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001513 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001514 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001515 continue;
1516 }
1517
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001518 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001519 DS.getTypeSpecScope() = SS;
1520 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001521 if (Tok.getAnnotationValue()) {
1522 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1524 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001525 PrevSpec, DiagID, T);
1526 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001527 else
1528 DS.SetTypeSpecError();
1529 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1530 ConsumeToken(); // The typename
1531 }
1532
Douglas Gregor9135c722009-03-25 15:40:00 +00001533 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001534 goto DoneWithDeclSpec;
1535
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001536 // If we're in a context where the identifier could be a class name,
1537 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001538 if ((DSContext == DSC_top_level ||
1539 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001540 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001541 &SS)) {
1542 if (isConstructorDeclarator())
1543 goto DoneWithDeclSpec;
1544
1545 // As noted in C++ [class.qual]p2 (cited above), when the name
1546 // of the class is qualified in a context where it could name
1547 // a constructor, its a constructor name. However, we've
1548 // looked at the declarator, and the user probably meant this
1549 // to be a type. Complain that it isn't supposed to be treated
1550 // as a type, then proceed to parse it as a type.
1551 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1552 << Next.getIdentifierInfo();
1553 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001554
John McCallb3d87482010-08-24 05:47:05 +00001555 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1556 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001557 getCurScope(), &SS,
1558 false, false, ParsedType(),
1559 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001560
Chris Lattnerf4382f52009-04-14 22:17:06 +00001561 // If the referenced identifier is not a type, then this declspec is
1562 // erroneous: We already checked about that it has no type specifier, and
1563 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001564 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001565 if (TypeRep == 0) {
1566 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001567 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001568 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
John McCallaa87d332009-12-12 11:40:51 +00001571 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001572 ConsumeToken(); // The C++ scope.
1573
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001575 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001576 if (isInvalid)
1577 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001579 DS.SetRangeEnd(Tok.getLocation());
1580 ConsumeToken(); // The typename.
1581
1582 continue;
1583 }
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Chris Lattner80d0c892009-01-21 19:48:37 +00001585 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001586 if (Tok.getAnnotationValue()) {
1587 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001588 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001589 DiagID, T);
1590 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001591 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001592
1593 if (isInvalid)
1594 break;
1595
Chris Lattner80d0c892009-01-21 19:48:37 +00001596 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1597 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Chris Lattner80d0c892009-01-21 19:48:37 +00001599 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1600 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001601 // Objective-C interface.
1602 if (Tok.is(tok::less) && getLang().ObjC1)
1603 ParseObjCProtocolQualifiers(DS);
1604
Chris Lattner80d0c892009-01-21 19:48:37 +00001605 continue;
1606 }
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Douglas Gregorbfad9152011-04-28 15:48:45 +00001608 case tok::kw___is_signed:
1609 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1610 // typically treats it as a trait. If we see __is_signed as it appears
1611 // in libstdc++, e.g.,
1612 //
1613 // static const bool __is_signed;
1614 //
1615 // then treat __is_signed as an identifier rather than as a keyword.
1616 if (DS.getTypeSpecType() == TST_bool &&
1617 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1618 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1619 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1620 Tok.setKind(tok::identifier);
1621 }
1622
1623 // We're done with the declaration-specifiers.
1624 goto DoneWithDeclSpec;
1625
Chris Lattner3bd934a2008-07-26 01:18:38 +00001626 // typedef-name
1627 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001628 // In C++, check to see if this is a scope specifier like foo::bar::, if
1629 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001630 if (getLang().CPlusPlus) {
1631 if (TryAnnotateCXXScopeToken(true)) {
1632 if (!DS.hasTypeSpecifier())
1633 DS.SetTypeSpecError();
1634 goto DoneWithDeclSpec;
1635 }
1636 if (!Tok.is(tok::identifier))
1637 continue;
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Chris Lattner3bd934a2008-07-26 01:18:38 +00001640 // This identifier can only be a typedef name if we haven't already seen
1641 // a type-specifier. Without this check we misparse:
1642 // typedef int X; struct Y { short X; }; as 'short int'.
1643 if (DS.hasTypeSpecifier())
1644 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001645
John Thompson82287d12010-02-05 00:12:22 +00001646 // Check for need to substitute AltiVec keyword tokens.
1647 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1648 break;
1649
Chris Lattner3bd934a2008-07-26 01:18:38 +00001650 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001651 ParsedType TypeRep =
1652 Actions.getTypeName(*Tok.getIdentifierInfo(),
1653 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001654
Chris Lattnerc199ab32009-04-12 20:42:31 +00001655 // If this is not a typedef name, don't parse it as part of the declspec,
1656 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001657 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001658 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001659 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001660 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001661
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001662 // If we're in a context where the identifier could be a class name,
1663 // check whether this is a constructor declaration.
1664 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001665 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001666 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001667 goto DoneWithDeclSpec;
1668
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001670 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001671 if (isInvalid)
1672 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Chris Lattner3bd934a2008-07-26 01:18:38 +00001674 DS.SetRangeEnd(Tok.getLocation());
1675 ConsumeToken(); // The identifier
1676
1677 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1678 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001679 // Objective-C interface.
1680 if (Tok.is(tok::less) && getLang().ObjC1)
1681 ParseObjCProtocolQualifiers(DS);
1682
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001683 // Need to support trailing type qualifiers (e.g. "id<p> const").
1684 // If a type specifier follows, it will be diagnosed elsewhere.
1685 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001686 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001687
1688 // type-name
1689 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001690 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001691 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001692 // This template-id does not refer to a type name, so we're
1693 // done with the type-specifiers.
1694 goto DoneWithDeclSpec;
1695 }
1696
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001697 // If we're in a context where the template-id could be a
1698 // constructor name or specialization, check whether this is a
1699 // constructor declaration.
1700 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001701 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001702 isConstructorDeclarator())
1703 goto DoneWithDeclSpec;
1704
Douglas Gregor39a8de12009-02-25 19:37:18 +00001705 // Turn the template-id annotation token into a type annotation
1706 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001707 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001708 continue;
1709 }
1710
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 // GNU attributes support.
1712 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001713 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001715
1716 // Microsoft declspec support.
1717 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001718 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001719 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Steve Naroff239f0732008-12-25 14:16:32 +00001721 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001722 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001723 // FIXME: Add handling here!
1724 break;
1725
1726 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001727 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001728 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001729 case tok::kw___cdecl:
1730 case tok::kw___stdcall:
1731 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001732 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001733 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001734 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001735 continue;
1736
Dawn Perchik52fc3142010-09-03 01:29:35 +00001737 // Borland single token adornments.
1738 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001739 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001740 continue;
1741
Peter Collingbournef315fa82011-02-14 01:42:53 +00001742 // OpenCL single token adornments.
1743 case tok::kw___kernel:
1744 ParseOpenCLAttributes(DS.getAttributes());
1745 continue;
1746
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 // storage-class-specifier
1748 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001749 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001750 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 break;
1752 case tok::kw_extern:
1753 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001754 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001755 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001756 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001758 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001759 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001760 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001761 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 case tok::kw_static:
1763 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001764 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001765 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001766 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 break;
1768 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001769 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001770 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1771 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Richard Smith8f4fb192011-09-04 19:54:14 +00001772 DiagID, getLang());
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001773 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00001774 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001775 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00001776 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1778 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00001779 } else
John McCallfec54012009-08-03 20:12:06 +00001780 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001781 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 break;
1783 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001784 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001785 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001787 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001788 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001789 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001790 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001792 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 // function-specifier
1796 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001797 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001799 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001800 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001801 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001802 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001803 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001804 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001805
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001806 // friend
1807 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001808 if (DSContext == DSC_class)
1809 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1810 else {
1811 PrevSpec = ""; // not actually used by the diagnostic
1812 DiagID = diag::err_friend_invalid_in_context;
1813 isInvalid = true;
1814 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001815 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Sebastian Redl2ac67232009-11-05 15:47:02 +00001817 // constexpr
1818 case tok::kw_constexpr:
1819 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1820 break;
1821
Chris Lattner80d0c892009-01-21 19:48:37 +00001822 // type-specifier
1823 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001824 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1825 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001826 break;
1827 case tok::kw_long:
1828 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001829 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1830 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001831 else
John McCallfec54012009-08-03 20:12:06 +00001832 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1833 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001834 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001835 case tok::kw___int64:
1836 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1837 DiagID);
1838 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001839 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001840 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1841 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001842 break;
1843 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001844 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1845 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001846 break;
1847 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001848 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1849 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001850 break;
1851 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001852 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1853 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001854 break;
1855 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001856 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1857 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001858 break;
1859 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001860 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1861 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001862 break;
1863 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001864 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1865 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001866 break;
1867 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001868 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1869 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001870 break;
1871 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001872 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1873 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001874 break;
1875 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001876 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1877 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001878 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001879 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001880 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1881 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001882 break;
1883 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001884 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1885 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001886 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001887 case tok::kw_bool:
1888 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001889 if (Tok.is(tok::kw_bool) &&
1890 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1891 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1892 PrevSpec = ""; // Not used by the diagnostic.
1893 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001894 // For better error recovery.
1895 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001896 isInvalid = true;
1897 } else {
1898 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1899 DiagID);
1900 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001901 break;
1902 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1904 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001905 break;
1906 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1908 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001909 break;
1910 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001911 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1912 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001913 break;
John Thompson82287d12010-02-05 00:12:22 +00001914 case tok::kw___vector:
1915 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1916 break;
1917 case tok::kw___pixel:
1918 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1919 break;
John McCalla5fc4722011-04-09 22:50:59 +00001920 case tok::kw___unknown_anytype:
1921 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1922 PrevSpec, DiagID);
1923 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001924
1925 // class-specifier:
1926 case tok::kw_class:
1927 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001928 case tok::kw_union: {
1929 tok::TokenKind Kind = Tok.getKind();
1930 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001931 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001932 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001933 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001934
1935 // enum-specifier:
1936 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001937 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001938 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001939 continue;
1940
1941 // cv-qualifier:
1942 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001943 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1944 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001945 break;
1946 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001947 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1948 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001949 break;
1950 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001951 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1952 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001953 break;
1954
Douglas Gregord57959a2009-03-27 23:10:48 +00001955 // C++ typename-specifier:
1956 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001957 if (TryAnnotateTypeOrScopeToken()) {
1958 DS.SetTypeSpecError();
1959 goto DoneWithDeclSpec;
1960 }
1961 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001962 continue;
1963 break;
1964
Chris Lattner80d0c892009-01-21 19:48:37 +00001965 // GNU typeof support.
1966 case tok::kw_typeof:
1967 ParseTypeofSpecifier(DS);
1968 continue;
1969
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001970 case tok::kw_decltype:
1971 ParseDecltypeSpecifier(DS);
1972 continue;
1973
Sean Huntdb5d44b2011-05-19 05:37:45 +00001974 case tok::kw___underlying_type:
1975 ParseUnderlyingTypeSpecifier(DS);
1976
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001977 // OpenCL qualifiers:
1978 case tok::kw_private:
1979 if (!getLang().OpenCL)
1980 goto DoneWithDeclSpec;
1981 case tok::kw___private:
1982 case tok::kw___global:
1983 case tok::kw___local:
1984 case tok::kw___constant:
1985 case tok::kw___read_only:
1986 case tok::kw___write_only:
1987 case tok::kw___read_write:
1988 ParseOpenCLQualifiers(DS);
1989 break;
1990
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001991 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001992 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001993 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1994 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001995 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001996 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Douglas Gregor46f936e2010-11-19 17:10:50 +00001998 if (!ParseObjCProtocolQualifiers(DS))
1999 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2000 << FixItHint::CreateInsertion(Loc, "id")
2001 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002002
2003 // Need to support trailing type qualifiers (e.g. "id<p> const").
2004 // If a type specifier follows, it will be diagnosed elsewhere.
2005 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 }
John McCallfec54012009-08-03 20:12:06 +00002007 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002008 if (isInvalid) {
2009 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002010 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002011
2012 if (DiagID == diag::ext_duplicate_declspec)
2013 Diag(Tok, DiagID)
2014 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2015 else
2016 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002018
Chris Lattner81c018d2008-03-13 06:29:04 +00002019 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002020 if (DiagID != diag::err_bool_redeclaration)
2021 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002022 }
2023}
Douglas Gregoradcac882008-12-01 23:54:00 +00002024
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002025/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002026/// primarily follow the C++ grammar with additions for C99 and GNU,
2027/// which together subsume the C grammar. Note that the C++
2028/// type-specifier also includes the C type-qualifier (for const,
2029/// volatile, and C99 restrict). Returns true if a type-specifier was
2030/// found (and parsed), false otherwise.
2031///
2032/// type-specifier: [C++ 7.1.5]
2033/// simple-type-specifier
2034/// class-specifier
2035/// enum-specifier
2036/// elaborated-type-specifier [TODO]
2037/// cv-qualifier
2038///
2039/// cv-qualifier: [C++ 7.1.5.1]
2040/// 'const'
2041/// 'volatile'
2042/// [C99] 'restrict'
2043///
2044/// simple-type-specifier: [ C++ 7.1.5.2]
2045/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2046/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2047/// 'char'
2048/// 'wchar_t'
2049/// 'bool'
2050/// 'short'
2051/// 'int'
2052/// 'long'
2053/// 'signed'
2054/// 'unsigned'
2055/// 'float'
2056/// 'double'
2057/// 'void'
2058/// [C99] '_Bool'
2059/// [C99] '_Complex'
2060/// [C99] '_Imaginary' // Removed in TC2?
2061/// [GNU] '_Decimal32'
2062/// [GNU] '_Decimal64'
2063/// [GNU] '_Decimal128'
2064/// [GNU] typeof-specifier
2065/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2066/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002067/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002068/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002069bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002070 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002071 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002072 const ParsedTemplateInfo &TemplateInfo,
2073 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002074 SourceLocation Loc = Tok.getLocation();
2075
2076 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002077 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002078 // If we already have a type specifier, this identifier is not a type.
2079 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2080 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2081 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2082 return false;
John Thompson82287d12010-02-05 00:12:22 +00002083 // Check for need to substitute AltiVec keyword tokens.
2084 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2085 break;
2086 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002087 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002088 // Annotate typenames and C++ scope specifiers. If we get one, just
2089 // recurse to handle whatever we get.
2090 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002091 return true;
2092 if (Tok.is(tok::identifier))
2093 return false;
2094 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2095 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002096 case tok::coloncolon: // ::foo::bar
2097 if (NextToken().is(tok::kw_new) || // ::new
2098 NextToken().is(tok::kw_delete)) // ::delete
2099 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Chris Lattner166a8fc2009-01-04 23:41:41 +00002101 // Annotate typenames and C++ scope specifiers. If we get one, just
2102 // recurse to handle whatever we get.
2103 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002104 return true;
2105 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2106 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Douglas Gregor12e083c2008-11-07 15:42:26 +00002108 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002109 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002110 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2112 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002113 DiagID, T);
2114 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002115 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002116 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2117 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002118
Douglas Gregor12e083c2008-11-07 15:42:26 +00002119 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2120 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2121 // Objective-C interface. If we don't have Objective-C or a '<', this is
2122 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002123 if (Tok.is(tok::less) && getLang().ObjC1)
2124 ParseObjCProtocolQualifiers(DS);
2125
Douglas Gregor12e083c2008-11-07 15:42:26 +00002126 return true;
2127 }
2128
2129 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002130 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002131 break;
2132 case tok::kw_long:
2133 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002134 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2135 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002136 else
John McCallfec54012009-08-03 20:12:06 +00002137 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2138 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002139 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002140 case tok::kw___int64:
2141 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2142 DiagID);
2143 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002144 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002145 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002146 break;
2147 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002148 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2149 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002150 break;
2151 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002152 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2153 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002154 break;
2155 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002156 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2157 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002158 break;
2159 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002161 break;
2162 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002163 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002164 break;
2165 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002167 break;
2168 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002169 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002170 break;
2171 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002173 break;
2174 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002175 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002176 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002177 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002178 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002179 break;
2180 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002181 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002182 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002183 case tok::kw_bool:
2184 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002185 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002186 break;
2187 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002188 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2189 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002190 break;
2191 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002192 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2193 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002194 break;
2195 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002196 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2197 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002198 break;
John Thompson82287d12010-02-05 00:12:22 +00002199 case tok::kw___vector:
2200 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2201 break;
2202 case tok::kw___pixel:
2203 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2204 break;
2205
Douglas Gregor12e083c2008-11-07 15:42:26 +00002206 // class-specifier:
2207 case tok::kw_class:
2208 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002209 case tok::kw_union: {
2210 tok::TokenKind Kind = Tok.getKind();
2211 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002212 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2213 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002214 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002215 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002216
2217 // enum-specifier:
2218 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002219 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002220 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002221 return true;
2222
2223 // cv-qualifier:
2224 case tok::kw_const:
2225 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002226 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002227 break;
2228 case tok::kw_volatile:
2229 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002230 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002231 break;
2232 case tok::kw_restrict:
2233 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002234 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002235 break;
2236
2237 // GNU typeof support.
2238 case tok::kw_typeof:
2239 ParseTypeofSpecifier(DS);
2240 return true;
2241
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002242 // C++0x decltype support.
2243 case tok::kw_decltype:
2244 ParseDecltypeSpecifier(DS);
2245 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Sean Huntdb5d44b2011-05-19 05:37:45 +00002247 // C++0x type traits support.
2248 case tok::kw___underlying_type:
2249 ParseUnderlyingTypeSpecifier(DS);
2250 return true;
2251
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002252 // OpenCL qualifiers:
2253 case tok::kw_private:
2254 if (!getLang().OpenCL)
2255 return false;
2256 case tok::kw___private:
2257 case tok::kw___global:
2258 case tok::kw___local:
2259 case tok::kw___constant:
2260 case tok::kw___read_only:
2261 case tok::kw___write_only:
2262 case tok::kw___read_write:
2263 ParseOpenCLQualifiers(DS);
2264 break;
2265
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002266 // C++0x auto support.
2267 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002268 // This is only called in situations where a storage-class specifier is
2269 // illegal, so we can assume an auto type specifier was intended even in
2270 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2271 // extension diagnostic.
2272 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002273 return false;
2274
John McCallfec54012009-08-03 20:12:06 +00002275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002276 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002277
Eli Friedman290eeb02009-06-08 23:27:34 +00002278 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002279 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002280 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002281 case tok::kw___cdecl:
2282 case tok::kw___stdcall:
2283 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002284 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002285 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002286 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002287 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002288
Dawn Perchik52fc3142010-09-03 01:29:35 +00002289 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002290 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002291 return true;
2292
Douglas Gregor12e083c2008-11-07 15:42:26 +00002293 default:
2294 // Not a type-specifier; do nothing.
2295 return false;
2296 }
2297
2298 // If the specifier combination wasn't legal, issue a diagnostic.
2299 if (isInvalid) {
2300 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002301 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002302 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002303 }
2304 DS.SetRangeEnd(Tok.getLocation());
2305 ConsumeToken(); // whatever we parsed above.
2306 return true;
2307}
Reid Spencer5f016e22007-07-11 17:01:13 +00002308
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002309/// ParseStructDeclaration - Parse a struct declaration without the terminating
2310/// semicolon.
2311///
Reid Spencer5f016e22007-07-11 17:01:13 +00002312/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002313/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002314/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002315/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002316/// struct-declarator-list:
2317/// struct-declarator
2318/// struct-declarator-list ',' struct-declarator
2319/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2320/// struct-declarator:
2321/// declarator
2322/// [GNU] declarator attributes[opt]
2323/// declarator[opt] ':' constant-expression
2324/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2325///
Chris Lattnere1359422008-04-10 06:46:29 +00002326void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002327ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002328
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002329 if (Tok.is(tok::kw___extension__)) {
2330 // __extension__ silences extension warnings in the subexpression.
2331 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002332 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002333 return ParseStructDeclaration(DS, Fields);
2334 }
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Steve Naroff28a7ca82007-08-20 22:28:22 +00002336 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002337 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002338
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002339 // If there are no declarators, this is a free-standing declaration
2340 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002341 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002342 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002343 return;
2344 }
2345
2346 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002347 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002348 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002349 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002350 FieldDeclarator DeclaratorInfo(DS);
2351
2352 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002353 if (!FirstDeclarator)
2354 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Steve Naroff28a7ca82007-08-20 22:28:22 +00002356 /// struct-declarator: declarator
2357 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002358 if (Tok.isNot(tok::colon)) {
2359 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2360 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002361 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002362 }
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Chris Lattner04d66662007-10-09 17:33:22 +00002364 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002365 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002366 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002367 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002368 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002369 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002370 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002371 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002372
Steve Naroff28a7ca82007-08-20 22:28:22 +00002373 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002374 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002375
John McCallbdd563e2009-11-03 02:38:08 +00002376 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002377 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002378 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002379
Steve Naroff28a7ca82007-08-20 22:28:22 +00002380 // If we don't have a comma, it is either the end of the list (a ';')
2381 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002382 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002383 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002384
Steve Naroff28a7ca82007-08-20 22:28:22 +00002385 // Consume the comma.
2386 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002387
John McCallbdd563e2009-11-03 02:38:08 +00002388 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002389 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002390}
2391
2392/// ParseStructUnionBody
2393/// struct-contents:
2394/// struct-declaration-list
2395/// [EXT] empty
2396/// [GNU] "struct-declaration-list" without terminatoring ';'
2397/// struct-declaration-list:
2398/// struct-declaration
2399/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002400/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002401///
Reid Spencer5f016e22007-07-11 17:01:13 +00002402void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002403 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002404 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2405 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Reid Spencer5f016e22007-07-11 17:01:13 +00002407 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002408
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002409 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002410 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002411
Reid Spencer5f016e22007-07-11 17:01:13 +00002412 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2413 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002414 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002415 Diag(Tok, diag::ext_empty_struct_union)
2416 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002417
Chris Lattner5f9e2722011-07-23 10:55:15 +00002418 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002419
Reid Spencer5f016e22007-07-11 17:01:13 +00002420 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002421 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002422 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002425 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002426 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002427 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002428 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002429 ConsumeToken();
2430 continue;
2431 }
Chris Lattnere1359422008-04-10 06:46:29 +00002432
2433 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002434 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002435
John McCallbdd563e2009-11-03 02:38:08 +00002436 if (!Tok.is(tok::at)) {
2437 struct CFieldCallback : FieldCallback {
2438 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002439 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002440 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002441
John McCalld226f652010-08-21 09:40:31 +00002442 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002443 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002444 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2445
John McCalld226f652010-08-21 09:40:31 +00002446 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002447 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002448 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002449 FD.D.getDeclSpec().getSourceRange().getBegin(),
2450 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002451 FieldDecls.push_back(Field);
2452 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002453 }
John McCallbdd563e2009-11-03 02:38:08 +00002454 } Callback(*this, TagDecl, FieldDecls);
2455
2456 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002457 } else { // Handle @defs
2458 ConsumeToken();
2459 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2460 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002461 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002462 continue;
2463 }
2464 ConsumeToken();
2465 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2466 if (!Tok.is(tok::identifier)) {
2467 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002468 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002469 continue;
2470 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002471 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002472 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002473 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002474 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2475 ConsumeToken();
2476 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002477 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002478
Chris Lattner04d66662007-10-09 17:33:22 +00002479 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002480 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002481 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002482 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002483 break;
2484 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002485 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2486 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002487 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002488 // If we stopped at a ';', eat it.
2489 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002490 }
2491 }
Mike Stump1eb44332009-09-09 15:08:12 +00002492
Steve Naroff60fccee2007-10-29 21:38:07 +00002493 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002494
John McCall0b7e6782011-03-24 11:26:52 +00002495 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002496 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002497 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002498
Douglas Gregor23c94db2010-07-02 17:43:08 +00002499 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002500 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002501 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002502 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002503 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002504 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002505}
2506
Reid Spencer5f016e22007-07-11 17:01:13 +00002507/// ParseEnumSpecifier
2508/// enum-specifier: [C99 6.7.2.2]
2509/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002510///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002511/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2512/// '}' attributes[opt]
2513/// 'enum' identifier
2514/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002515///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002516/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2517/// [C++0x] enum-head '{' enumerator-list ',' '}'
2518///
2519/// enum-head: [C++0x]
2520/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2521/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2522///
2523/// enum-key: [C++0x]
2524/// 'enum'
2525/// 'enum' 'class'
2526/// 'enum' 'struct'
2527///
2528/// enum-base: [C++0x]
2529/// ':' type-specifier-seq
2530///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002531/// [C++] elaborated-type-specifier:
2532/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2533///
Chris Lattner4c97d762009-04-12 21:49:30 +00002534void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002535 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002536 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002537 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002538 if (Tok.is(tok::code_completion)) {
2539 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002540 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002541 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002542 }
John McCall57c13002011-07-06 05:58:41 +00002543
2544 bool IsScopedEnum = false;
2545 bool IsScopedUsingClassTag = false;
2546
2547 if (getLang().CPlusPlus0x &&
2548 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2549 IsScopedEnum = true;
2550 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2551 ConsumeToken();
2552 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002553
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002554 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002555 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002556 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002557
Douglas Gregor5471bc82011-09-08 17:18:35 +00002558 bool AllowFixedUnderlyingType
2559 = getLang().CPlusPlus0x || getLang().Microsoft || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002560
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002561 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002562 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002563 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2564 // if a fixed underlying type is allowed.
2565 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2566
John McCallb3d87482010-08-24 05:47:05 +00002567 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002568 return;
2569
2570 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002571 Diag(Tok, diag::err_expected_ident);
2572 if (Tok.isNot(tok::l_brace)) {
2573 // Has no name and is not a definition.
2574 // Skip the rest of this declarator, up until the comma or semicolon.
2575 SkipUntil(tok::comma, true);
2576 return;
2577 }
2578 }
2579 }
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002581 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002582 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2583 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002584 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002586 // Skip the rest of this declarator, up until the comma or semicolon.
2587 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002588 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002589 }
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002591 // If an identifier is present, consume and remember it.
2592 IdentifierInfo *Name = 0;
2593 SourceLocation NameLoc;
2594 if (Tok.is(tok::identifier)) {
2595 Name = Tok.getIdentifierInfo();
2596 NameLoc = ConsumeToken();
2597 }
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002599 if (!Name && IsScopedEnum) {
2600 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2601 // declaration of a scoped enumeration.
2602 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2603 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002604 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002605 }
2606
2607 TypeResult BaseType;
2608
Douglas Gregora61b3e72010-12-01 17:42:47 +00002609 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002610 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002611 bool PossibleBitfield = false;
2612 if (getCurScope()->getFlags() & Scope::ClassScope) {
2613 // If we're in class scope, this can either be an enum declaration with
2614 // an underlying type, or a declaration of a bitfield member. We try to
2615 // use a simple disambiguation scheme first to catch the common cases
2616 // (integer literal, sizeof); if it's still ambiguous, we then consider
2617 // anything that's a simple-type-specifier followed by '(' as an
2618 // expression. This suffices because function types are not valid
2619 // underlying types anyway.
2620 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2621 // If the next token starts an expression, we know we're parsing a
2622 // bit-field. This is the common case.
2623 if (TPR == TPResult::True())
2624 PossibleBitfield = true;
2625 // If the next token starts a type-specifier-seq, it may be either a
2626 // a fixed underlying type or the start of a function-style cast in C++;
2627 // lookahead one more token to see if it's obvious that we have a
2628 // fixed underlying type.
2629 else if (TPR == TPResult::False() &&
2630 GetLookAheadToken(2).getKind() == tok::semi) {
2631 // Consume the ':'.
2632 ConsumeToken();
2633 } else {
2634 // We have the start of a type-specifier-seq, so we have to perform
2635 // tentative parsing to determine whether we have an expression or a
2636 // type.
2637 TentativeParsingAction TPA(*this);
2638
2639 // Consume the ':'.
2640 ConsumeToken();
2641
Douglas Gregor86f208c2011-02-22 20:32:04 +00002642 if ((getLang().CPlusPlus &&
2643 isCXXDeclarationSpecifier() != TPResult::True()) ||
2644 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002645 // We'll parse this as a bitfield later.
2646 PossibleBitfield = true;
2647 TPA.Revert();
2648 } else {
2649 // We have a type-specifier-seq.
2650 TPA.Commit();
2651 }
2652 }
2653 } else {
2654 // Consume the ':'.
2655 ConsumeToken();
2656 }
2657
2658 if (!PossibleBitfield) {
2659 SourceRange Range;
2660 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002661
Douglas Gregor5471bc82011-09-08 17:18:35 +00002662 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002663 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2664 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002665 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002666 }
2667
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002668 // There are three options here. If we have 'enum foo;', then this is a
2669 // forward declaration. If we have 'enum foo {...' then this is a
2670 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2671 //
2672 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2673 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2674 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2675 //
John McCallf312b1e2010-08-26 23:41:50 +00002676 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002677 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002678 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002679 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002680 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002681 else
John McCallf312b1e2010-08-26 23:41:50 +00002682 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002683
2684 // enums cannot be templates, although they can be referenced from a
2685 // template.
2686 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002687 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002688 Diag(Tok, diag::err_enum_template);
2689
2690 // Skip the rest of this declarator, up until the comma or semicolon.
2691 SkipUntil(tok::comma, true);
2692 return;
2693 }
2694
Douglas Gregorb9075602011-02-22 02:55:24 +00002695 if (!Name && TUK != Sema::TUK_Definition) {
2696 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2697
2698 // Skip the rest of this declarator, up until the comma or semicolon.
2699 SkipUntil(tok::comma, true);
2700 return;
2701 }
2702
Douglas Gregor402abb52009-05-28 23:31:59 +00002703 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002704 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002705 const char *PrevSpec = 0;
2706 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002707 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002708 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002709 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002710 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002711 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002712 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002713
Douglas Gregor48c89f42010-04-24 16:38:41 +00002714 if (IsDependent) {
2715 // This enum has a dependent nested-name-specifier. Handle it as a
2716 // dependent tag.
2717 if (!Name) {
2718 DS.SetTypeSpecError();
2719 Diag(Tok, diag::err_expected_type_name_after_typename);
2720 return;
2721 }
2722
Douglas Gregor23c94db2010-07-02 17:43:08 +00002723 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002724 TUK, SS, Name, StartLoc,
2725 NameLoc);
2726 if (Type.isInvalid()) {
2727 DS.SetTypeSpecError();
2728 return;
2729 }
2730
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002731 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2732 NameLoc.isValid() ? NameLoc : StartLoc,
2733 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002734 Diag(StartLoc, DiagID) << PrevSpec;
2735
2736 return;
2737 }
Mike Stump1eb44332009-09-09 15:08:12 +00002738
John McCalld226f652010-08-21 09:40:31 +00002739 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002740 // The action failed to produce an enumeration tag. If this is a
2741 // definition, consume the entire definition.
2742 if (Tok.is(tok::l_brace)) {
2743 ConsumeBrace();
2744 SkipUntil(tok::r_brace);
2745 }
2746
2747 DS.SetTypeSpecError();
2748 return;
2749 }
2750
Chris Lattner04d66662007-10-09 17:33:22 +00002751 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002752 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002753
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002754 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2755 NameLoc.isValid() ? NameLoc : StartLoc,
2756 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002757 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002758}
2759
2760/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2761/// enumerator-list:
2762/// enumerator
2763/// enumerator-list ',' enumerator
2764/// enumerator:
2765/// enumeration-constant
2766/// enumeration-constant '=' constant-expression
2767/// enumeration-constant:
2768/// identifier
2769///
John McCalld226f652010-08-21 09:40:31 +00002770void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002771 // Enter the scope of the enum body and start the definition.
2772 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002773 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002774
Reid Spencer5f016e22007-07-11 17:01:13 +00002775 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Chris Lattner7946dd32007-08-27 17:24:30 +00002777 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002778 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002779 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002780
Chris Lattner5f9e2722011-07-23 10:55:15 +00002781 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002782
John McCalld226f652010-08-21 09:40:31 +00002783 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002784
Reid Spencer5f016e22007-07-11 17:01:13 +00002785 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002786 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002787 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2788 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002789
John McCall5b629aa2010-10-22 23:36:17 +00002790 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002791 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002792 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002793
Reid Spencer5f016e22007-07-11 17:01:13 +00002794 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002795 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002796 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002798 AssignedVal = ParseConstantExpression();
2799 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002800 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002801 }
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Reid Spencer5f016e22007-07-11 17:01:13 +00002803 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002804 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2805 LastEnumConstDecl,
2806 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002807 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002808 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002809 EnumConstantDecls.push_back(EnumConstDecl);
2810 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Douglas Gregor751f6922010-09-07 14:51:08 +00002812 if (Tok.is(tok::identifier)) {
2813 // We're missing a comma between enumerators.
2814 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2815 Diag(Loc, diag::err_enumerator_list_missing_comma)
2816 << FixItHint::CreateInsertion(Loc, ", ");
2817 continue;
2818 }
2819
Chris Lattner04d66662007-10-09 17:33:22 +00002820 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002821 break;
2822 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002823
2824 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002825 !(getLang().C99 || getLang().CPlusPlus0x))
2826 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2827 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002828 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002829 }
Mike Stump1eb44332009-09-09 15:08:12 +00002830
Reid Spencer5f016e22007-07-11 17:01:13 +00002831 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002832 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002833
Reid Spencer5f016e22007-07-11 17:01:13 +00002834 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002835 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002836 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002837
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002838 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2839 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002840 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Douglas Gregor72de6672009-01-08 20:45:30 +00002842 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002843 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002844}
2845
2846/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002847/// start of a type-qualifier-list.
2848bool Parser::isTypeQualifier() const {
2849 switch (Tok.getKind()) {
2850 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002851
2852 // type-qualifier only in OpenCL
2853 case tok::kw_private:
2854 return getLang().OpenCL;
2855
Steve Naroff5f8aa692008-02-11 23:15:56 +00002856 // type-qualifier
2857 case tok::kw_const:
2858 case tok::kw_volatile:
2859 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002860 case tok::kw___private:
2861 case tok::kw___local:
2862 case tok::kw___global:
2863 case tok::kw___constant:
2864 case tok::kw___read_only:
2865 case tok::kw___read_write:
2866 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002867 return true;
2868 }
2869}
2870
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002871/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2872/// is definitely a type-specifier. Return false if it isn't part of a type
2873/// specifier or if we're not sure.
2874bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2875 switch (Tok.getKind()) {
2876 default: return false;
2877 // type-specifiers
2878 case tok::kw_short:
2879 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002880 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002881 case tok::kw_signed:
2882 case tok::kw_unsigned:
2883 case tok::kw__Complex:
2884 case tok::kw__Imaginary:
2885 case tok::kw_void:
2886 case tok::kw_char:
2887 case tok::kw_wchar_t:
2888 case tok::kw_char16_t:
2889 case tok::kw_char32_t:
2890 case tok::kw_int:
2891 case tok::kw_float:
2892 case tok::kw_double:
2893 case tok::kw_bool:
2894 case tok::kw__Bool:
2895 case tok::kw__Decimal32:
2896 case tok::kw__Decimal64:
2897 case tok::kw__Decimal128:
2898 case tok::kw___vector:
2899
2900 // struct-or-union-specifier (C99) or class-specifier (C++)
2901 case tok::kw_class:
2902 case tok::kw_struct:
2903 case tok::kw_union:
2904 // enum-specifier
2905 case tok::kw_enum:
2906
2907 // typedef-name
2908 case tok::annot_typename:
2909 return true;
2910 }
2911}
2912
Steve Naroff5f8aa692008-02-11 23:15:56 +00002913/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002914/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002915bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 switch (Tok.getKind()) {
2917 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Chris Lattner166a8fc2009-01-04 23:41:41 +00002919 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002920 if (TryAltiVecVectorToken())
2921 return true;
2922 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002923 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002924 // Annotate typenames and C++ scope specifiers. If we get one, just
2925 // recurse to handle whatever we get.
2926 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002927 return true;
2928 if (Tok.is(tok::identifier))
2929 return false;
2930 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002931
Chris Lattner166a8fc2009-01-04 23:41:41 +00002932 case tok::coloncolon: // ::foo::bar
2933 if (NextToken().is(tok::kw_new) || // ::new
2934 NextToken().is(tok::kw_delete)) // ::delete
2935 return false;
2936
Chris Lattner166a8fc2009-01-04 23:41:41 +00002937 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002938 return true;
2939 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Reid Spencer5f016e22007-07-11 17:01:13 +00002941 // GNU attributes support.
2942 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002943 // GNU typeof support.
2944 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002945
Reid Spencer5f016e22007-07-11 17:01:13 +00002946 // type-specifiers
2947 case tok::kw_short:
2948 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002949 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002950 case tok::kw_signed:
2951 case tok::kw_unsigned:
2952 case tok::kw__Complex:
2953 case tok::kw__Imaginary:
2954 case tok::kw_void:
2955 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002956 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002957 case tok::kw_char16_t:
2958 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 case tok::kw_int:
2960 case tok::kw_float:
2961 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002962 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002963 case tok::kw__Bool:
2964 case tok::kw__Decimal32:
2965 case tok::kw__Decimal64:
2966 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002967 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002968
Chris Lattner99dc9142008-04-13 18:59:07 +00002969 // struct-or-union-specifier (C99) or class-specifier (C++)
2970 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002971 case tok::kw_struct:
2972 case tok::kw_union:
2973 // enum-specifier
2974 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002975
Reid Spencer5f016e22007-07-11 17:01:13 +00002976 // type-qualifier
2977 case tok::kw_const:
2978 case tok::kw_volatile:
2979 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002980
2981 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002982 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002983 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002984
Chris Lattner7c186be2008-10-20 00:25:30 +00002985 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2986 case tok::less:
2987 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002988
Steve Naroff239f0732008-12-25 14:16:32 +00002989 case tok::kw___cdecl:
2990 case tok::kw___stdcall:
2991 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002992 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002993 case tok::kw___w64:
2994 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002995 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002996 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002997 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002998
2999 case tok::kw___private:
3000 case tok::kw___local:
3001 case tok::kw___global:
3002 case tok::kw___constant:
3003 case tok::kw___read_only:
3004 case tok::kw___read_write:
3005 case tok::kw___write_only:
3006
Eli Friedman290eeb02009-06-08 23:27:34 +00003007 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003008
3009 case tok::kw_private:
3010 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00003011 }
3012}
3013
3014/// isDeclarationSpecifier() - Return true if the current token is part of a
3015/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003016///
3017/// \param DisambiguatingWithExpression True to indicate that the purpose of
3018/// this check is to disambiguate between an expression and a declaration.
3019bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003020 switch (Tok.getKind()) {
3021 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003023 case tok::kw_private:
3024 return getLang().OpenCL;
3025
Chris Lattner166a8fc2009-01-04 23:41:41 +00003026 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003027 // Unfortunate hack to support "Class.factoryMethod" notation.
3028 if (getLang().ObjC1 && NextToken().is(tok::period))
3029 return false;
John Thompson82287d12010-02-05 00:12:22 +00003030 if (TryAltiVecVectorToken())
3031 return true;
3032 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003033 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003034 // Annotate typenames and C++ scope specifiers. If we get one, just
3035 // recurse to handle whatever we get.
3036 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003037 return true;
3038 if (Tok.is(tok::identifier))
3039 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003040
3041 // If we're in Objective-C and we have an Objective-C class type followed
3042 // by an identifier and then either ':' or ']', in a place where an
3043 // expression is permitted, then this is probably a class message send
3044 // missing the initial '['. In this case, we won't consider this to be
3045 // the start of a declaration.
3046 if (DisambiguatingWithExpression &&
3047 isStartOfObjCClassMessageMissingOpenBracket())
3048 return false;
3049
John McCall9ba61662010-02-26 08:45:28 +00003050 return isDeclarationSpecifier();
3051
Chris Lattner166a8fc2009-01-04 23:41:41 +00003052 case tok::coloncolon: // ::foo::bar
3053 if (NextToken().is(tok::kw_new) || // ::new
3054 NextToken().is(tok::kw_delete)) // ::delete
3055 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003056
Chris Lattner166a8fc2009-01-04 23:41:41 +00003057 // Annotate typenames and C++ scope specifiers. If we get one, just
3058 // recurse to handle whatever we get.
3059 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003060 return true;
3061 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003062
Reid Spencer5f016e22007-07-11 17:01:13 +00003063 // storage-class-specifier
3064 case tok::kw_typedef:
3065 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003066 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003067 case tok::kw_static:
3068 case tok::kw_auto:
3069 case tok::kw_register:
3070 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003071
Reid Spencer5f016e22007-07-11 17:01:13 +00003072 // type-specifiers
3073 case tok::kw_short:
3074 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003075 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003076 case tok::kw_signed:
3077 case tok::kw_unsigned:
3078 case tok::kw__Complex:
3079 case tok::kw__Imaginary:
3080 case tok::kw_void:
3081 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003082 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003083 case tok::kw_char16_t:
3084 case tok::kw_char32_t:
3085
Reid Spencer5f016e22007-07-11 17:01:13 +00003086 case tok::kw_int:
3087 case tok::kw_float:
3088 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003089 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003090 case tok::kw__Bool:
3091 case tok::kw__Decimal32:
3092 case tok::kw__Decimal64:
3093 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003094 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003095
Chris Lattner99dc9142008-04-13 18:59:07 +00003096 // struct-or-union-specifier (C99) or class-specifier (C++)
3097 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003098 case tok::kw_struct:
3099 case tok::kw_union:
3100 // enum-specifier
3101 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Reid Spencer5f016e22007-07-11 17:01:13 +00003103 // type-qualifier
3104 case tok::kw_const:
3105 case tok::kw_volatile:
3106 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003107
Reid Spencer5f016e22007-07-11 17:01:13 +00003108 // function-specifier
3109 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003110 case tok::kw_virtual:
3111 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003112
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003113 // static_assert-declaration
3114 case tok::kw__Static_assert:
3115
Chris Lattner1ef08762007-08-09 17:01:07 +00003116 // GNU typeof support.
3117 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003118
Chris Lattner1ef08762007-08-09 17:01:07 +00003119 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003120 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003121 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Francois Pichete3d49b42011-06-19 08:02:06 +00003123 // C++0x decltype.
3124 case tok::kw_decltype:
3125 return true;
3126
Chris Lattnerf3948c42008-07-26 03:38:44 +00003127 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3128 case tok::less:
3129 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Douglas Gregord9d75e52011-04-27 05:41:15 +00003131 // typedef-name
3132 case tok::annot_typename:
3133 return !DisambiguatingWithExpression ||
3134 !isStartOfObjCClassMessageMissingOpenBracket();
3135
Steve Naroff47f52092009-01-06 19:34:12 +00003136 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003137 case tok::kw___cdecl:
3138 case tok::kw___stdcall:
3139 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003140 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003141 case tok::kw___w64:
3142 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003143 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003144 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003145 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003146 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003147
3148 case tok::kw___private:
3149 case tok::kw___local:
3150 case tok::kw___global:
3151 case tok::kw___constant:
3152 case tok::kw___read_only:
3153 case tok::kw___read_write:
3154 case tok::kw___write_only:
3155
Eli Friedman290eeb02009-06-08 23:27:34 +00003156 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 }
3158}
3159
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003160bool Parser::isConstructorDeclarator() {
3161 TentativeParsingAction TPA(*this);
3162
3163 // Parse the C++ scope specifier.
3164 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003165 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003166 TPA.Revert();
3167 return false;
3168 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003169
3170 // Parse the constructor name.
3171 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3172 // We already know that we have a constructor name; just consume
3173 // the token.
3174 ConsumeToken();
3175 } else {
3176 TPA.Revert();
3177 return false;
3178 }
3179
3180 // Current class name must be followed by a left parentheses.
3181 if (Tok.isNot(tok::l_paren)) {
3182 TPA.Revert();
3183 return false;
3184 }
3185 ConsumeParen();
3186
3187 // A right parentheses or ellipsis signals that we have a constructor.
3188 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3189 TPA.Revert();
3190 return true;
3191 }
3192
3193 // If we need to, enter the specified scope.
3194 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003195 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003196 DeclScopeObj.EnterDeclaratorScope();
3197
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003198 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003199 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003200 MaybeParseMicrosoftAttributes(Attrs);
3201
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003202 // Check whether the next token(s) are part of a declaration
3203 // specifier, in which case we have the start of a parameter and,
3204 // therefore, we know that this is a constructor.
3205 bool IsConstructor = isDeclarationSpecifier();
3206 TPA.Revert();
3207 return IsConstructor;
3208}
Reid Spencer5f016e22007-07-11 17:01:13 +00003209
3210/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003211/// type-qualifier-list: [C99 6.7.5]
3212/// type-qualifier
3213/// [vendor] attributes
3214/// [ only if VendorAttributesAllowed=true ]
3215/// type-qualifier-list type-qualifier
3216/// [vendor] type-qualifier-list attributes
3217/// [ only if VendorAttributesAllowed=true ]
3218/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3219/// [ only if CXX0XAttributesAllowed=true ]
3220/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003221///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003222void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3223 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003224 bool CXX0XAttributesAllowed) {
3225 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3226 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003227 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003228 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003229 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003230 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003231 else
3232 Diag(Loc, diag::err_attributes_not_allowed);
3233 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003234
3235 SourceLocation EndLoc;
3236
Reid Spencer5f016e22007-07-11 17:01:13 +00003237 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003238 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003239 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003240 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003241 SourceLocation Loc = Tok.getLocation();
3242
3243 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003244 case tok::code_completion:
3245 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003246 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003247
Reid Spencer5f016e22007-07-11 17:01:13 +00003248 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003249 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3250 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003251 break;
3252 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003253 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3254 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 break;
3256 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003257 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3258 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003259 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003260
3261 // OpenCL qualifiers:
3262 case tok::kw_private:
3263 if (!getLang().OpenCL)
3264 goto DoneWithTypeQuals;
3265 case tok::kw___private:
3266 case tok::kw___global:
3267 case tok::kw___local:
3268 case tok::kw___constant:
3269 case tok::kw___read_only:
3270 case tok::kw___write_only:
3271 case tok::kw___read_write:
3272 ParseOpenCLQualifiers(DS);
3273 break;
3274
Eli Friedman290eeb02009-06-08 23:27:34 +00003275 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003276 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003277 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003278 case tok::kw___cdecl:
3279 case tok::kw___stdcall:
3280 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003281 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003282 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003283 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003284 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003285 continue;
3286 }
3287 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003288 case tok::kw___pascal:
3289 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003290 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003291 continue;
3292 }
3293 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003294 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003295 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003296 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003297 continue; // do *not* consume the next token!
3298 }
3299 // otherwise, FALL THROUGH!
3300 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003301 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003302 // If this is not a type-qualifier token, we're done reading type
3303 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003304 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003305 if (EndLoc.isValid())
3306 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003307 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003309
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 // If the specifier combination wasn't legal, issue a diagnostic.
3311 if (isInvalid) {
3312 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003313 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003314 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003315 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003316 }
3317}
3318
3319
3320/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3321///
3322void Parser::ParseDeclarator(Declarator &D) {
3323 /// This implements the 'declarator' production in the C grammar, then checks
3324 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003325 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003326}
3327
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003328/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3329/// is parsed by the function passed to it. Pass null, and the direct-declarator
3330/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003331/// ptr-operator production.
3332///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003333/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3334/// [C] pointer[opt] direct-declarator
3335/// [C++] direct-declarator
3336/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003337///
3338/// pointer: [C99 6.7.5]
3339/// '*' type-qualifier-list[opt]
3340/// '*' type-qualifier-list[opt] pointer
3341///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003342/// ptr-operator:
3343/// '*' cv-qualifier-seq[opt]
3344/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003345/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003346/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003347/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003348/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003349void Parser::ParseDeclaratorInternal(Declarator &D,
3350 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003351 if (Diags.hasAllExtensionsSilenced())
3352 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003353
Sebastian Redlf30208a2009-01-24 21:16:55 +00003354 // C++ member pointers start with a '::' or a nested-name.
3355 // Member pointers get special handling, since there's no place for the
3356 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003357 if (getLang().CPlusPlus &&
3358 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3359 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003360 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003361 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003362
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003363 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003364 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003365 // The scope spec really belongs to the direct-declarator.
3366 D.getCXXScopeSpec() = SS;
3367 if (DirectDeclParser)
3368 (this->*DirectDeclParser)(D);
3369 return;
3370 }
3371
3372 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003373 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003374 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003375 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003376 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003377
3378 // Recurse to parse whatever is left.
3379 ParseDeclaratorInternal(D, DirectDeclParser);
3380
3381 // Sema will have to catch (syntactically invalid) pointers into global
3382 // scope. It has to catch pointers into namespace scope anyway.
3383 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003384 Loc),
3385 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003386 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003387 return;
3388 }
3389 }
3390
3391 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003392 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003393 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003394 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003395 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003396 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003397 if (DirectDeclParser)
3398 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003399 return;
3400 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003401
Sebastian Redl05532f22009-03-15 22:02:01 +00003402 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3403 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003404 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003405 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003406
Chris Lattner9af55002009-03-27 04:18:06 +00003407 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003408 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003409 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003410
Reid Spencer5f016e22007-07-11 17:01:13 +00003411 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003412 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003413
Reid Spencer5f016e22007-07-11 17:01:13 +00003414 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003415 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003416 if (Kind == tok::star)
3417 // Remember that we parsed a pointer type, and remember the type-quals.
3418 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003419 DS.getConstSpecLoc(),
3420 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003421 DS.getRestrictSpecLoc()),
3422 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003423 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003424 else
3425 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003426 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003427 Loc),
3428 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003429 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003430 } else {
3431 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003432 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003433
Sebastian Redl743de1f2009-03-23 00:00:23 +00003434 // Complain about rvalue references in C++03, but then go on and build
3435 // the declarator.
3436 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003437 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003438
Reid Spencer5f016e22007-07-11 17:01:13 +00003439 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3440 // cv-qualifiers are introduced through the use of a typedef or of a
3441 // template type argument, in which case the cv-qualifiers are ignored.
3442 //
3443 // [GNU] Retricted references are allowed.
3444 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003445 // [C++0x] Attributes on references are not allowed.
3446 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003447 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003448
3449 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3450 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3451 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003452 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003453 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3454 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003455 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003456 }
3457
3458 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003459 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003460
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003461 if (D.getNumTypeObjects() > 0) {
3462 // C++ [dcl.ref]p4: There shall be no references to references.
3463 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3464 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003465 if (const IdentifierInfo *II = D.getIdentifier())
3466 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3467 << II;
3468 else
3469 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3470 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003471
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003472 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003473 // can go ahead and build the (technically ill-formed)
3474 // declarator: reference collapsing will take care of it.
3475 }
3476 }
3477
Reid Spencer5f016e22007-07-11 17:01:13 +00003478 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003479 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003480 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003481 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003482 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003483 }
3484}
3485
3486/// ParseDirectDeclarator
3487/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003488/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003489/// '(' declarator ')'
3490/// [GNU] '(' attributes declarator ')'
3491/// [C90] direct-declarator '[' constant-expression[opt] ']'
3492/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3493/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3494/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3495/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3496/// direct-declarator '(' parameter-type-list ')'
3497/// direct-declarator '(' identifier-list[opt] ')'
3498/// [GNU] direct-declarator '(' parameter-forward-declarations
3499/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003500/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3501/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003502/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003503///
3504/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003505/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003506/// '::'[opt] nested-name-specifier[opt] type-name
3507///
3508/// id-expression: [C++ 5.1]
3509/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003510/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003511///
3512/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003513/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003514/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003515/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003516/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003517/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003518///
Reid Spencer5f016e22007-07-11 17:01:13 +00003519void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003520 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003521
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003522 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3523 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003524 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003525 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003526 }
3527
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003528 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003529 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003530 // Change the declaration context for name lookup, until this function
3531 // is exited (and the declarator has been parsed).
3532 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003533 }
3534
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003535 // C++0x [dcl.fct]p14:
3536 // There is a syntactic ambiguity when an ellipsis occurs at the end
3537 // of a parameter-declaration-clause without a preceding comma. In
3538 // this case, the ellipsis is parsed as part of the
3539 // abstract-declarator if the type of the parameter names a template
3540 // parameter pack that has not been expanded; otherwise, it is parsed
3541 // as part of the parameter-declaration-clause.
3542 if (Tok.is(tok::ellipsis) &&
3543 !((D.getContext() == Declarator::PrototypeContext ||
3544 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003545 NextToken().is(tok::r_paren) &&
3546 !Actions.containsUnexpandedParameterPacks(D)))
3547 D.setEllipsisLoc(ConsumeToken());
3548
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003549 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3550 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3551 // We found something that indicates the start of an unqualified-id.
3552 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003553 bool AllowConstructorName;
3554 if (D.getDeclSpec().hasTypeSpecifier())
3555 AllowConstructorName = false;
3556 else if (D.getCXXScopeSpec().isSet())
3557 AllowConstructorName =
3558 (D.getContext() == Declarator::FileContext ||
3559 (D.getContext() == Declarator::MemberContext &&
3560 D.getDeclSpec().isFriendSpecified()));
3561 else
3562 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3563
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003564 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3565 /*EnteringContext=*/true,
3566 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003567 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003568 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003569 D.getName()) ||
3570 // Once we're past the identifier, if the scope was bad, mark the
3571 // whole declarator bad.
3572 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003573 D.SetIdentifier(0, Tok.getLocation());
3574 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003575 } else {
3576 // Parsed the unqualified-id; update range information and move along.
3577 if (D.getSourceRange().getBegin().isInvalid())
3578 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3579 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003580 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003581 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003582 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003583 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003584 assert(!getLang().CPlusPlus &&
3585 "There's a C++-specific check for tok::identifier above");
3586 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3587 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3588 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003589 goto PastIdentifier;
3590 }
3591
3592 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003593 // direct-declarator: '(' declarator ')'
3594 // direct-declarator: '(' attributes declarator ')'
3595 // Example: 'char (*X)' or 'int (*XX)(void)'
3596 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003597
3598 // If the declarator was parenthesized, we entered the declarator
3599 // scope when parsing the parenthesized declarator, then exited
3600 // the scope already. Re-enter the scope, if we need to.
3601 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003602 // If there was an error parsing parenthesized declarator, declarator
3603 // scope may have been enterred before. Don't do it again.
3604 if (!D.isInvalidType() &&
3605 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003606 // Change the declaration context for name lookup, until this function
3607 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003608 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003609 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003610 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003611 // This could be something simple like "int" (in which case the declarator
3612 // portion is empty), if an abstract-declarator is allowed.
3613 D.SetIdentifier(0, Tok.getLocation());
3614 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003615 if (D.getContext() == Declarator::MemberContext)
3616 Diag(Tok, diag::err_expected_member_name_or_semi)
3617 << D.getDeclSpec().getSourceRange();
3618 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003619 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003620 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003621 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003622 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003623 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003624 }
Mike Stump1eb44332009-09-09 15:08:12 +00003625
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003626 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003627 assert(D.isPastIdentifier() &&
3628 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003629
Sean Huntbbd37c62009-11-21 08:43:09 +00003630 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003631 if (D.getIdentifier())
3632 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003633
Reid Spencer5f016e22007-07-11 17:01:13 +00003634 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003635 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003636 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3637 // In such a case, check if we actually have a function declarator; if it
3638 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003639 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3640 // When not in file scope, warn for ambiguous function declarators, just
3641 // in case the author intended it as a variable definition.
3642 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3643 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3644 break;
3645 }
John McCall0b7e6782011-03-24 11:26:52 +00003646 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003647 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003648 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003649 ParseBracketDeclarator(D);
3650 } else {
3651 break;
3652 }
3653 }
3654}
3655
Chris Lattneref4715c2008-04-06 05:45:57 +00003656/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3657/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003658/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003659/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3660///
3661/// direct-declarator:
3662/// '(' declarator ')'
3663/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003664/// direct-declarator '(' parameter-type-list ')'
3665/// direct-declarator '(' identifier-list[opt] ')'
3666/// [GNU] direct-declarator '(' parameter-forward-declarations
3667/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003668///
3669void Parser::ParseParenDeclarator(Declarator &D) {
3670 SourceLocation StartLoc = ConsumeParen();
3671 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003672
Chris Lattner7399ee02008-10-20 02:05:46 +00003673 // Eat any attributes before we look at whether this is a grouping or function
3674 // declarator paren. If this is a grouping paren, the attribute applies to
3675 // the type being built up, for example:
3676 // int (__attribute__(()) *x)(long y)
3677 // If this ends up not being a grouping paren, the attribute applies to the
3678 // first argument, for example:
3679 // int (__attribute__(()) int x)
3680 // In either case, we need to eat any attributes to be able to determine what
3681 // sort of paren this is.
3682 //
John McCall0b7e6782011-03-24 11:26:52 +00003683 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003684 bool RequiresArg = false;
3685 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003686 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003687
Chris Lattner7399ee02008-10-20 02:05:46 +00003688 // We require that the argument list (if this is a non-grouping paren) be
3689 // present even if the attribute list was empty.
3690 RequiresArg = true;
3691 }
Steve Naroff239f0732008-12-25 14:16:32 +00003692 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003693 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003694 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003695 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003696 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003697 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003698 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003699 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003700 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003701 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003702
Chris Lattneref4715c2008-04-06 05:45:57 +00003703 // If we haven't past the identifier yet (or where the identifier would be
3704 // stored, if this is an abstract declarator), then this is probably just
3705 // grouping parens. However, if this could be an abstract-declarator, then
3706 // this could also be the start of function arguments (consider 'void()').
3707 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003708
Chris Lattneref4715c2008-04-06 05:45:57 +00003709 if (!D.mayOmitIdentifier()) {
3710 // If this can't be an abstract-declarator, this *must* be a grouping
3711 // paren, because we haven't seen the identifier yet.
3712 isGrouping = true;
3713 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003714 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003715 isDeclarationSpecifier()) { // 'int(int)' is a function.
3716 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3717 // considered to be a type, not a K&R identifier-list.
3718 isGrouping = false;
3719 } else {
3720 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3721 isGrouping = true;
3722 }
Mike Stump1eb44332009-09-09 15:08:12 +00003723
Chris Lattneref4715c2008-04-06 05:45:57 +00003724 // If this is a grouping paren, handle:
3725 // direct-declarator: '(' declarator ')'
3726 // direct-declarator: '(' attributes declarator ')'
3727 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003728 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003729 D.setGroupingParens(true);
3730
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003731 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003732 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003733 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003734 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3735 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003736
3737 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003738 return;
3739 }
Mike Stump1eb44332009-09-09 15:08:12 +00003740
Chris Lattneref4715c2008-04-06 05:45:57 +00003741 // Okay, if this wasn't a grouping paren, it must be the start of a function
3742 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003743 // identifier (and remember where it would have been), then call into
3744 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003745 D.SetIdentifier(0, Tok.getLocation());
3746
John McCall7f040a92010-12-24 02:08:15 +00003747 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003748}
3749
3750/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3751/// declarator D up to a paren, which indicates that we are parsing function
3752/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003753///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003754/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003755/// after the open paren - they should be considered to be the first argument of
3756/// a parameter. If RequiresArg is true, then the first argument of the
3757/// function is required to be present and required to not be an identifier
3758/// list.
3759///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003760/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3761/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3762/// (C++0x) trailing-return-type[opt].
3763///
3764/// [C++0x] exception-specification:
3765/// dynamic-exception-specification
3766/// noexcept-specification
3767///
3768void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3769 ParsedAttributes &attrs,
3770 bool RequiresArg) {
3771 // lparen is already consumed!
3772 assert(D.isPastIdentifier() && "Should not call before identifier!");
3773
3774 // This should be true when the function has typed arguments.
3775 // Otherwise, it is treated as a K&R-style function.
3776 bool HasProto = false;
3777 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003778 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003779 // Remember where we see an ellipsis, if any.
3780 SourceLocation EllipsisLoc;
3781
3782 DeclSpec DS(AttrFactory);
3783 bool RefQualifierIsLValueRef = true;
3784 SourceLocation RefQualifierLoc;
3785 ExceptionSpecificationType ESpecType = EST_None;
3786 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003787 SmallVector<ParsedType, 2> DynamicExceptions;
3788 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003789 ExprResult NoexceptExpr;
3790 ParsedType TrailingReturnType;
3791
3792 SourceLocation EndLoc;
3793
3794 if (isFunctionDeclaratorIdentifierList()) {
3795 if (RequiresArg)
3796 Diag(Tok, diag::err_argument_required_after_attribute);
3797
3798 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3799
3800 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3801 } else {
3802 // Enter function-declaration scope, limiting any declarators to the
3803 // function prototype scope, including parameter declarators.
3804 ParseScope PrototypeScope(this,
3805 Scope::FunctionPrototypeScope|Scope::DeclScope);
3806
3807 if (Tok.isNot(tok::r_paren))
3808 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3809 else if (RequiresArg)
3810 Diag(Tok, diag::err_argument_required_after_attribute);
3811
3812 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3813
3814 // If we have the closing ')', eat it.
3815 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3816
3817 if (getLang().CPlusPlus) {
3818 MaybeParseCXX0XAttributes(attrs);
3819
3820 // Parse cv-qualifier-seq[opt].
3821 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3822 if (!DS.getSourceRange().getEnd().isInvalid())
3823 EndLoc = DS.getSourceRange().getEnd();
3824
3825 // Parse ref-qualifier[opt].
3826 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3827 if (!getLang().CPlusPlus0x)
3828 Diag(Tok, diag::ext_ref_qualifier);
3829
3830 RefQualifierIsLValueRef = Tok.is(tok::amp);
3831 RefQualifierLoc = ConsumeToken();
3832 EndLoc = RefQualifierLoc;
3833 }
3834
3835 // Parse exception-specification[opt].
3836 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3837 DynamicExceptions,
3838 DynamicExceptionRanges,
3839 NoexceptExpr);
3840 if (ESpecType != EST_None)
3841 EndLoc = ESpecRange.getEnd();
3842
3843 // Parse trailing-return-type[opt].
3844 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00003845 SourceRange Range;
3846 TrailingReturnType = ParseTrailingReturnType(Range).get();
3847 if (Range.getEnd().isValid())
3848 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003849 }
3850 }
3851
3852 // Leave prototype scope.
3853 PrototypeScope.Exit();
3854 }
3855
3856 // Remember that we parsed a function type, and remember the attributes.
3857 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3858 /*isVariadic=*/EllipsisLoc.isValid(),
3859 EllipsisLoc,
3860 ParamInfo.data(), ParamInfo.size(),
3861 DS.getTypeQualifiers(),
3862 RefQualifierIsLValueRef,
3863 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00003864 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003865 ESpecType, ESpecRange.getBegin(),
3866 DynamicExceptions.data(),
3867 DynamicExceptionRanges.data(),
3868 DynamicExceptions.size(),
3869 NoexceptExpr.isUsable() ?
3870 NoexceptExpr.get() : 0,
3871 LParenLoc, EndLoc, D,
3872 TrailingReturnType),
3873 attrs, EndLoc);
3874}
3875
3876/// isFunctionDeclaratorIdentifierList - This parameter list may have an
3877/// identifier list form for a K&R-style function: void foo(a,b,c)
3878///
3879/// Note that identifier-lists are only allowed for normal declarators, not for
3880/// abstract-declarators.
3881bool Parser::isFunctionDeclaratorIdentifierList() {
3882 return !getLang().CPlusPlus
3883 && Tok.is(tok::identifier)
3884 && !TryAltiVecVectorToken()
3885 // K&R identifier lists can't have typedefs as identifiers, per C99
3886 // 6.7.5.3p11.
3887 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
3888 // Identifier lists follow a really simple grammar: the identifiers can
3889 // be followed *only* by a ", identifier" or ")". However, K&R
3890 // identifier lists are really rare in the brave new modern world, and
3891 // it is very common for someone to typo a type in a non-K&R style
3892 // list. If we are presented with something like: "void foo(intptr x,
3893 // float y)", we don't want to start parsing the function declarator as
3894 // though it is a K&R style declarator just because intptr is an
3895 // invalid type.
3896 //
3897 // To handle this, we check to see if the token after the first
3898 // identifier is a "," or ")". Only then do we parse it as an
3899 // identifier list.
3900 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
3901}
3902
3903/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3904/// we found a K&R-style identifier list instead of a typed parameter list.
3905///
3906/// After returning, ParamInfo will hold the parsed parameters.
3907///
3908/// identifier-list: [C99 6.7.5]
3909/// identifier
3910/// identifier-list ',' identifier
3911///
3912void Parser::ParseFunctionDeclaratorIdentifierList(
3913 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003914 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003915 // If there was no identifier specified for the declarator, either we are in
3916 // an abstract-declarator, or we are in a parameter declarator which was found
3917 // to be abstract. In abstract-declarators, identifier lists are not valid:
3918 // diagnose this.
3919 if (!D.getIdentifier())
3920 Diag(Tok, diag::ext_ident_list_in_param);
3921
3922 // Maintain an efficient lookup of params we have seen so far.
3923 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3924
3925 while (1) {
3926 // If this isn't an identifier, report the error and skip until ')'.
3927 if (Tok.isNot(tok::identifier)) {
3928 Diag(Tok, diag::err_expected_ident);
3929 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
3930 // Forget we parsed anything.
3931 ParamInfo.clear();
3932 return;
3933 }
3934
3935 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3936
3937 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3938 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
3939 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3940
3941 // Verify that the argument identifier has not already been mentioned.
3942 if (!ParamsSoFar.insert(ParmII)) {
3943 Diag(Tok, diag::err_param_redefinition) << ParmII;
3944 } else {
3945 // Remember this identifier in ParamInfo.
3946 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3947 Tok.getLocation(),
3948 0));
3949 }
3950
3951 // Eat the identifier.
3952 ConsumeToken();
3953
3954 // The list continues if we see a comma.
3955 if (Tok.isNot(tok::comma))
3956 break;
3957 ConsumeToken();
3958 }
3959}
3960
3961/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
3962/// after the opening parenthesis. This function will not parse a K&R-style
3963/// identifier list.
3964///
3965/// D is the declarator being parsed. If attrs is non-null, then the caller
3966/// parsed those arguments immediately after the open paren - they should be
3967/// considered to be the first argument of a parameter.
3968///
3969/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
3970/// be the location of the ellipsis, if any was parsed.
3971///
Reid Spencer5f016e22007-07-11 17:01:13 +00003972/// parameter-type-list: [C99 6.7.5]
3973/// parameter-list
3974/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003975/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003976///
3977/// parameter-list: [C99 6.7.5]
3978/// parameter-declaration
3979/// parameter-list ',' parameter-declaration
3980///
3981/// parameter-declaration: [C99 6.7.5]
3982/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003983/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003984/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003985/// declaration-specifiers abstract-declarator[opt]
3986/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003987/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003988/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3989///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003990void Parser::ParseParameterDeclarationClause(
3991 Declarator &D,
3992 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003993 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003994 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003995
Chris Lattnerf97409f2008-04-06 06:57:35 +00003996 while (1) {
3997 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00003998 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003999 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004000 }
Mike Stump1eb44332009-09-09 15:08:12 +00004001
Chris Lattnerf97409f2008-04-06 06:57:35 +00004002 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004003 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004004 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004005
4006 // Skip any Microsoft attributes before a param.
4007 if (getLang().Microsoft && Tok.is(tok::l_square))
4008 ParseMicrosoftAttributes(DS.getAttributes());
4009
4010 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004011
4012 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004013 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004014 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4015 // attributes lost? Should they even be allowed?
4016 // FIXME: If we can leave the attributes in the token stream somehow, we can
4017 // get rid of a parameter (attrs) and this statement. It might be too much
4018 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004019 DS.takeAttributesFrom(attrs);
4020
Chris Lattnere64c5492009-02-27 18:38:20 +00004021 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004022
Chris Lattnerf97409f2008-04-06 06:57:35 +00004023 // Parse the declarator. This is "PrototypeContext", because we must
4024 // accept either 'declarator' or 'abstract-declarator' here.
4025 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4026 ParseDeclarator(ParmDecl);
4027
4028 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004029 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Chris Lattnerf97409f2008-04-06 06:57:35 +00004031 // Remember this parsed parameter in ParamInfo.
4032 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004033
Douglas Gregor72b505b2008-12-16 21:30:33 +00004034 // DefArgToks is used when the parsing of default arguments needs
4035 // to be delayed.
4036 CachedTokens *DefArgToks = 0;
4037
Chris Lattnerf97409f2008-04-06 06:57:35 +00004038 // If no parameter was specified, verify that *something* was specified,
4039 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004040 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4041 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004042 // Completely missing, emit error.
4043 Diag(DSStart, diag::err_missing_param);
4044 } else {
4045 // Otherwise, we have something. Add it and let semantic analysis try
4046 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004047
Chris Lattnerf97409f2008-04-06 06:57:35 +00004048 // Inform the actions module about the parameter declarator, so it gets
4049 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004050 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004051
4052 // Parse the default argument, if any. We parse the default
4053 // arguments in all dialects; the semantic analysis in
4054 // ActOnParamDefaultArgument will reject the default argument in
4055 // C.
4056 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004057 SourceLocation EqualLoc = Tok.getLocation();
4058
Chris Lattner04421082008-04-08 04:40:51 +00004059 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004060 if (D.getContext() == Declarator::MemberContext) {
4061 // If we're inside a class definition, cache the tokens
4062 // corresponding to the default argument. We'll actually parse
4063 // them when we see the end of the class definition.
4064 // FIXME: Templates will require something similar.
4065 // FIXME: Can we use a smart pointer for Toks?
4066 DefArgToks = new CachedTokens;
4067
Mike Stump1eb44332009-09-09 15:08:12 +00004068 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004069 /*StopAtSemi=*/true,
4070 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004071 delete DefArgToks;
4072 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004073 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004074 } else {
4075 // Mark the end of the default argument so that we know when to
4076 // stop when we parse it later on.
4077 Token DefArgEnd;
4078 DefArgEnd.startToken();
4079 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4080 DefArgEnd.setLocation(Tok.getLocation());
4081 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004082 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004083 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004084 }
Chris Lattner04421082008-04-08 04:40:51 +00004085 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004086 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004087 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004088
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004089 // The argument isn't actually potentially evaluated unless it is
4090 // used.
4091 EnterExpressionEvaluationContext Eval(Actions,
4092 Sema::PotentiallyEvaluatedIfUsed);
4093
John McCall60d7b3a2010-08-24 06:29:42 +00004094 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004095 if (DefArgResult.isInvalid()) {
4096 Actions.ActOnParamDefaultArgumentError(Param);
4097 SkipUntil(tok::comma, tok::r_paren, true, true);
4098 } else {
4099 // Inform the actions module about the default argument
4100 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004101 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004102 }
Chris Lattner04421082008-04-08 04:40:51 +00004103 }
4104 }
Mike Stump1eb44332009-09-09 15:08:12 +00004105
4106 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4107 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004108 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004109 }
4110
4111 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004112 if (Tok.isNot(tok::comma)) {
4113 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004114 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4115
4116 if (!getLang().CPlusPlus) {
4117 // We have ellipsis without a preceding ',', which is ill-formed
4118 // in C. Complain and provide the fix.
4119 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004120 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004121 }
4122 }
4123
4124 break;
4125 }
Mike Stump1eb44332009-09-09 15:08:12 +00004126
Chris Lattnerf97409f2008-04-06 06:57:35 +00004127 // Consume the comma.
4128 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004129 }
Mike Stump1eb44332009-09-09 15:08:12 +00004130
Chris Lattner66d28652008-04-06 06:34:08 +00004131}
Chris Lattneref4715c2008-04-06 05:45:57 +00004132
Reid Spencer5f016e22007-07-11 17:01:13 +00004133/// [C90] direct-declarator '[' constant-expression[opt] ']'
4134/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4135/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4136/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4137/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4138void Parser::ParseBracketDeclarator(Declarator &D) {
4139 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004140
Chris Lattner378c7e42008-12-18 07:27:21 +00004141 // C array syntax has many features, but by-far the most common is [] and [4].
4142 // This code does a fast path to handle some of the most obvious cases.
4143 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004144 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004145 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004146 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004147
Chris Lattner378c7e42008-12-18 07:27:21 +00004148 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004149 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004150 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004151 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004152 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004153 return;
4154 } else if (Tok.getKind() == tok::numeric_constant &&
4155 GetLookAheadToken(1).is(tok::r_square)) {
4156 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004157 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004158 ConsumeToken();
4159
Sebastian Redlab197ba2009-02-09 18:23:29 +00004160 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004161 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004162 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004163
Chris Lattner378c7e42008-12-18 07:27:21 +00004164 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004165 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004166 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004167 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004168 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004169 return;
4170 }
Mike Stump1eb44332009-09-09 15:08:12 +00004171
Reid Spencer5f016e22007-07-11 17:01:13 +00004172 // If valid, this location is the position where we read the 'static' keyword.
4173 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004174 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004175 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004176
Reid Spencer5f016e22007-07-11 17:01:13 +00004177 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004178 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004179 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004180 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004181
Reid Spencer5f016e22007-07-11 17:01:13 +00004182 // If we haven't already read 'static', check to see if there is one after the
4183 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004184 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004185 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004186
Reid Spencer5f016e22007-07-11 17:01:13 +00004187 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4188 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004189 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004190
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004191 // Handle the case where we have '[*]' as the array size. However, a leading
4192 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4193 // the the token after the star is a ']'. Since stars in arrays are
4194 // infrequent, use of lookahead is not costly here.
4195 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004196 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004197
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004198 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004199 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004200 StaticLoc = SourceLocation(); // Drop the static.
4201 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004202 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004203 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004204 // Note, in C89, this production uses the constant-expr production instead
4205 // of assignment-expr. The only difference is that assignment-expr allows
4206 // things like '=' and '*='. Sema rejects these in C89 mode because they
4207 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004208
Douglas Gregore0762c92009-06-19 23:52:42 +00004209 // Parse the constant-expression or assignment-expression now (depending
4210 // on dialect).
4211 if (getLang().CPlusPlus)
4212 NumElements = ParseConstantExpression();
4213 else
4214 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004215 }
Mike Stump1eb44332009-09-09 15:08:12 +00004216
Reid Spencer5f016e22007-07-11 17:01:13 +00004217 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004218 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004219 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004220 // If the expression was invalid, skip it.
4221 SkipUntil(tok::r_square);
4222 return;
4223 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004224
4225 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4226
John McCall0b7e6782011-03-24 11:26:52 +00004227 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004228 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004229
Chris Lattner378c7e42008-12-18 07:27:21 +00004230 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004231 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004232 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004233 NumElements.release(),
4234 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004235 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004236}
4237
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004238/// [GNU] typeof-specifier:
4239/// typeof ( expressions )
4240/// typeof ( type-name )
4241/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004242///
4243void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004244 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004245 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004246 SourceLocation StartLoc = ConsumeToken();
4247
John McCallcfb708c2010-01-13 20:03:27 +00004248 const bool hasParens = Tok.is(tok::l_paren);
4249
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004250 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004251 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004252 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004253 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4254 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004255 if (hasParens)
4256 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004257
4258 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004259 // FIXME: Not accurate, the range gets one token more than it should.
4260 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004261 else
4262 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004263
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004264 if (isCastExpr) {
4265 if (!CastTy) {
4266 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004267 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004268 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004269
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004270 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004271 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004272 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4273 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004274 DiagID, CastTy))
4275 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004276 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004277 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004278
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004279 // If we get here, the operand to the typeof was an expresion.
4280 if (Operand.isInvalid()) {
4281 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004282 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004283 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004284
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004285 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004286 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004287 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4288 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004289 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004290 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004291}
Chris Lattner1b492422010-02-28 18:33:55 +00004292
4293
4294/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4295/// from TryAltiVecVectorToken.
4296bool Parser::TryAltiVecVectorTokenOutOfLine() {
4297 Token Next = NextToken();
4298 switch (Next.getKind()) {
4299 default: return false;
4300 case tok::kw_short:
4301 case tok::kw_long:
4302 case tok::kw_signed:
4303 case tok::kw_unsigned:
4304 case tok::kw_void:
4305 case tok::kw_char:
4306 case tok::kw_int:
4307 case tok::kw_float:
4308 case tok::kw_double:
4309 case tok::kw_bool:
4310 case tok::kw___pixel:
4311 Tok.setKind(tok::kw___vector);
4312 return true;
4313 case tok::identifier:
4314 if (Next.getIdentifierInfo() == Ident_pixel) {
4315 Tok.setKind(tok::kw___vector);
4316 return true;
4317 }
4318 return false;
4319 }
4320}
4321
4322bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4323 const char *&PrevSpec, unsigned &DiagID,
4324 bool &isInvalid) {
4325 if (Tok.getIdentifierInfo() == Ident_vector) {
4326 Token Next = NextToken();
4327 switch (Next.getKind()) {
4328 case tok::kw_short:
4329 case tok::kw_long:
4330 case tok::kw_signed:
4331 case tok::kw_unsigned:
4332 case tok::kw_void:
4333 case tok::kw_char:
4334 case tok::kw_int:
4335 case tok::kw_float:
4336 case tok::kw_double:
4337 case tok::kw_bool:
4338 case tok::kw___pixel:
4339 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4340 return true;
4341 case tok::identifier:
4342 if (Next.getIdentifierInfo() == Ident_pixel) {
4343 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4344 return true;
4345 }
4346 break;
4347 default:
4348 break;
4349 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004350 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004351 DS.isTypeAltiVecVector()) {
4352 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4353 return true;
4354 }
4355 return false;
4356}