blob: 03e4879db68826cce890ed0711836aad364f4976 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000018#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000020#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000026#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.7: Declarations.
31//===----------------------------------------------------------------------===//
32
33/// ParseTypeName
34/// type-name: [C99 6.7.6]
35/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000036///
37/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000038TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000039 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000040 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000041 Decl **OwnedType,
42 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000043 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000044 if (DSC == DSC_normal)
45 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000048 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000049 if (Attrs)
50 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000051 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000052 if (OwnedType)
53 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000054
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000056 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000057 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000058 if (Range)
59 *Range = DeclaratorInfo.getSourceRange();
60
Chris Lattnereaaebc72009-04-25 08:06:05 +000061 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000062 return true;
63
Douglas Gregor23c94db2010-07-02 17:43:08 +000064 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000065}
66
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000067
68/// isAttributeLateParsed - Return true if the attribute has arguments that
69/// require late parsing.
70static bool isAttributeLateParsed(const IdentifierInfo &II) {
71 return llvm::StringSwitch<bool>(II.getName())
72#include "clang/Parse/AttrLateParsed.inc"
73 .Default(false);
74}
75
Sean Huntbbd37c62009-11-21 08:43:09 +000076/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000077///
78/// [GNU] attributes:
79/// attribute
80/// attributes attribute
81///
82/// [GNU] attribute:
83/// '__attribute__' '(' '(' attribute-list ')' ')'
84///
85/// [GNU] attribute-list:
86/// attrib
87/// attribute_list ',' attrib
88///
89/// [GNU] attrib:
90/// empty
91/// attrib-name
92/// attrib-name '(' identifier ')'
93/// attrib-name '(' identifier ',' nonempty-expr-list ')'
94/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
95///
96/// [GNU] attrib-name:
97/// identifier
98/// typespec
99/// typequal
100/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000103/// token lookahead. Comment from gcc: "If they start with an identifier
104/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// start with that identifier; otherwise they are an expression list."
106///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000107/// GCC does not require the ',' between attribs in an attribute-list.
108///
Reid Spencer5f016e22007-07-11 17:01:13 +0000109/// At the moment, I am not doing 2 token lookahead. I am also unaware of
110/// any attributes that don't work (based on my limited testing). Most
111/// attributes are very simple in practice. Until we find a bug, I don't see
112/// a pressing need to implement the 2 token lookahead.
113
John McCall7f040a92010-12-24 02:08:15 +0000114void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000115 SourceLocation *endLoc,
116 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000117 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 ConsumeToken();
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
122 "attribute")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000124 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 }
126 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000128 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
130 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000131 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
132 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
135 ConsumeToken();
136 continue;
137 }
138 // we have an identifier or declaration specifier (const, int, etc.)
139 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
140 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000142 if (Tok.is(tok::l_paren)) {
143 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000144 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000145 LateParsedAttribute *LA =
146 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
147 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148
Bill Wendlingad017fa2012-12-20 19:22:21 +0000149 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000150 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000151 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000152 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000154 // consume everything up to and including the matching right parens
155 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000157 Token Eof;
158 Eof.startToken();
159 Eof.setLocation(Tok.getLocation());
160 LA->Toks.push_back(Eof);
161 } else {
Michael Han6880f492012-10-03 01:56:22 +0000162 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000163 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
165 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000166 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000167 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
169 }
170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000172 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000173 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
174 SkipUntil(tok::r_paren, false);
175 }
John McCall7f040a92010-12-24 02:08:15 +0000176 if (endLoc)
177 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000181/// \brief Determine whether the given attribute has all expression arguments.
182static bool attributeHasExprArgs(const IdentifierInfo &II) {
183 return llvm::StringSwitch<bool>(II.getName())
184#include "clang/Parse/AttrExprArgs.inc"
185 .Default(false);
186}
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000187
Michael Han6880f492012-10-03 01:56:22 +0000188/// Parse the arguments to a parameterized GNU attribute or
189/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000190void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
191 SourceLocation AttrNameLoc,
192 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000193 SourceLocation *EndLoc,
194 IdentifierInfo *ScopeName,
195 SourceLocation ScopeLoc,
196 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000197
198 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
199
200 // Availability attributes have their own grammar.
201 if (AttrName->isStr("availability")) {
202 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
205 // Thread safety attributes fit into the FIXME case above, so we
206 // just parse the arguments as a list of expressions
207 if (IsThreadSafetyAttribute(AttrName->getName())) {
208 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
209 return;
210 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000211 // Type safety attributes have their own grammar.
212 if (AttrName->isStr("type_tag_for_datatype")) {
213 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
214 return;
215 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000216
217 ConsumeParen(); // ignore the left paren loc for now
218
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000219 IdentifierInfo *ParmName = 0;
220 SourceLocation ParmLoc;
221 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000222
Joey Gouly37453b92013-03-08 09:42:32 +0000223 TypeResult T;
224 SourceRange TypeRange;
225 bool TypeParsed = false;
226
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000227 switch (Tok.getKind()) {
228 case tok::kw_char:
229 case tok::kw_wchar_t:
230 case tok::kw_char16_t:
231 case tok::kw_char32_t:
232 case tok::kw_bool:
233 case tok::kw_short:
234 case tok::kw_int:
235 case tok::kw_long:
236 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000237 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000238 case tok::kw_signed:
239 case tok::kw_unsigned:
240 case tok::kw_float:
241 case tok::kw_double:
242 case tok::kw_void:
243 case tok::kw_typeof:
244 // __attribute__(( vec_type_hint(char) ))
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000245 BuiltinType = true;
Joey Gouly37453b92013-03-08 09:42:32 +0000246 T = ParseTypeName(&TypeRange);
247 TypeParsed = true;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000248 break;
249
250 case tok::identifier:
Joey Gouly37453b92013-03-08 09:42:32 +0000251 if (AttrName->isStr("vec_type_hint")) {
252 T = ParseTypeName(&TypeRange);
253 TypeParsed = true;
254 break;
255 }
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000256 // If the attribute has all expression arguments, and not a "parameter",
257 // break out to handle it below.
258 if (attributeHasExprArgs(*AttrName))
259 break;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000260 ParmName = Tok.getIdentifierInfo();
261 ParmLoc = ConsumeToken();
262 break;
263
264 default:
265 break;
266 }
267
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000268 ExprVector ArgExprs;
Joey Gouly37453b92013-03-08 09:42:32 +0000269 bool isInvalid = false;
270 bool isParmType = false;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000271
Joey Gouly37453b92013-03-08 09:42:32 +0000272 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000273 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
274 // Eat the comma.
275 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000276 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000277
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000278 // Parse the non-empty comma-separated list of expressions.
279 while (1) {
280 ExprResult ArgExpr(ParseAssignmentExpression());
281 if (ArgExpr.isInvalid()) {
282 SkipUntil(tok::r_paren);
283 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000284 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000285 ArgExprs.push_back(ArgExpr.release());
286 if (Tok.isNot(tok::comma))
287 break;
288 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000289 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000290 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000291 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
292 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
293 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000294 while (Tok.is(tok::identifier)) {
295 ConsumeToken();
296 if (Tok.is(tok::greater))
297 break;
298 if (Tok.is(tok::comma)) {
299 ConsumeToken();
300 continue;
301 }
302 }
303 if (Tok.isNot(tok::greater))
304 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000305 SkipUntil(tok::r_paren, false, true); // skip until ')'
306 }
Joey Gouly37453b92013-03-08 09:42:32 +0000307 } else if (AttrName->isStr("vec_type_hint")) {
308 if (T.get() && !T.isInvalid())
309 isParmType = true;
310 else {
311 if (Tok.is(tok::identifier))
312 ConsumeToken();
313 if (TypeParsed)
314 isInvalid = true;
315 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000316 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000317
318 SourceLocation RParen = Tok.getLocation();
Joey Gouly37453b92013-03-08 09:42:32 +0000319 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
320 !isInvalid) {
Michael Han45bed132012-10-04 16:42:52 +0000321 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Gouly37453b92013-03-08 09:42:32 +0000322 if (isParmType) {
Joey Gouly37453b92013-03-08 09:42:32 +0000323 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
324 ScopeLoc, ParmName, ParmLoc, T.get(), Syntax);
325 } else {
326 AttributeList *attr = Attrs.addNew(
327 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName,
328 ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax);
329 if (BuiltinType &&
330 attr->getKind() == AttributeList::AT_IBOutletCollection)
331 Diag(Tok, diag::err_iboutletcollection_builtintype);
332 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000333 }
334}
335
Chad Rosier8decdee2012-06-26 22:30:43 +0000336/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000337/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000338void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000339 SourceLocation AttrNameLoc,
340 ParsedAttributes &Attrs)
341{
342 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000343 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000344 AttrName->getNameStart(), tok::r_paren))
345 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000346
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000347 ExprResult ArgExpr(ParseConstantExpression());
348 if (ArgExpr.isInvalid()) {
349 T.skipToEnd();
350 return;
351 }
352 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000353 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000354 &ExprList, 1, AttributeList::AS_Declspec);
355
356 T.consumeClose();
357}
358
Chad Rosier8decdee2012-06-26 22:30:43 +0000359/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000360/// arguments.
361bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
362 return llvm::StringSwitch<bool>(Ident->getName())
363 .Case("dllimport", true)
364 .Case("dllexport", true)
365 .Case("noreturn", true)
366 .Case("nothrow", true)
367 .Case("noinline", true)
368 .Case("naked", true)
369 .Case("appdomain", true)
370 .Case("process", true)
371 .Case("jitintrinsic", true)
372 .Case("noalias", true)
373 .Case("restrict", true)
374 .Case("novtable", true)
375 .Case("selectany", true)
376 .Case("thread", true)
Aaron Ballman3ce0de62013-05-04 16:58:37 +0000377 .Case("safebuffers", true )
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000378 .Default(false);
379}
380
Chad Rosier8decdee2012-06-26 22:30:43 +0000381/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000382/// parameters). Will return false if we properly handled the declspec, or
383/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000384void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000385 SourceLocation Loc,
386 ParsedAttributes &Attrs) {
387 // Try to handle the easy case first -- these declspecs all take a single
388 // parameter as their argument.
389 if (llvm::StringSwitch<bool>(Ident->getName())
390 .Case("uuid", true)
391 .Case("align", true)
392 .Case("allocate", true)
393 .Default(false)) {
394 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
395 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000396 // The deprecated declspec has an optional single argument, so we will
397 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000398 // not.
399 if (Tok.getKind() == tok::l_paren)
400 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
401 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000402 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000403 AttributeList::AS_Declspec);
404 } else if (Ident->getName() == "property") {
405 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000406 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000407 // must be named get or put.
John McCall76da55d2013-04-16 07:28:30 +0000408 if (Tok.isNot(tok::l_paren)) {
409 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
410 << Ident->getNameStart();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000411 return;
John McCall76da55d2013-04-16 07:28:30 +0000412 }
413 BalancedDelimiterTracker T(*this, tok::l_paren);
414 T.expectAndConsume(diag::err_expected_lparen_after,
415 Ident->getNameStart(), tok::r_paren);
416
417 enum AccessorKind {
418 AK_Invalid = -1,
419 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
420 };
421 IdentifierInfo *AccessorNames[] = { 0, 0 };
422 bool HasInvalidAccessor = false;
423
424 // Parse the accessor specifications.
425 while (true) {
426 // Stop if this doesn't look like an accessor spec.
427 if (!Tok.is(tok::identifier)) {
428 // If the user wrote a completely empty list, use a special diagnostic.
429 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
430 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
431 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
432 break;
433 }
434
435 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
436 break;
437 }
438
439 AccessorKind Kind;
440 SourceLocation KindLoc = Tok.getLocation();
441 StringRef KindStr = Tok.getIdentifierInfo()->getName();
442 if (KindStr == "get") {
443 Kind = AK_Get;
444 } else if (KindStr == "put") {
445 Kind = AK_Put;
446
447 // Recover from the common mistake of using 'set' instead of 'put'.
448 } else if (KindStr == "set") {
449 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
450 << FixItHint::CreateReplacement(KindLoc, "put");
451 Kind = AK_Put;
452
453 // Handle the mistake of forgetting the accessor kind by skipping
454 // this accessor.
455 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
456 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
457 ConsumeToken();
458 HasInvalidAccessor = true;
459 goto next_property_accessor;
460
461 // Otherwise, complain about the unknown accessor kind.
462 } else {
463 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
464 HasInvalidAccessor = true;
465 Kind = AK_Invalid;
466
467 // Try to keep parsing unless it doesn't look like an accessor spec.
468 if (!NextToken().is(tok::equal)) break;
469 }
470
471 // Consume the identifier.
472 ConsumeToken();
473
474 // Consume the '='.
475 if (Tok.is(tok::equal)) {
476 ConsumeToken();
477 } else {
478 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
479 << KindStr;
480 break;
481 }
482
483 // Expect the method name.
484 if (!Tok.is(tok::identifier)) {
485 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
486 break;
487 }
488
489 if (Kind == AK_Invalid) {
490 // Just drop invalid accessors.
491 } else if (AccessorNames[Kind] != NULL) {
492 // Complain about the repeated accessor, ignore it, and keep parsing.
493 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
494 } else {
495 AccessorNames[Kind] = Tok.getIdentifierInfo();
496 }
497 ConsumeToken();
498
499 next_property_accessor:
500 // Keep processing accessors until we run out.
501 if (Tok.is(tok::comma)) {
502 ConsumeAnyToken();
503 continue;
504
505 // If we run into the ')', stop without consuming it.
506 } else if (Tok.is(tok::r_paren)) {
507 break;
508 } else {
509 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
510 break;
511 }
512 }
513
514 // Only add the property attribute if it was well-formed.
515 if (!HasInvalidAccessor) {
516 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0,
517 SourceLocation(),
518 AccessorNames[AK_Get], AccessorNames[AK_Put],
519 AttributeList::AS_Declspec);
520 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000521 T.skipToEnd();
522 } else {
523 // We don't recognize this as a valid declspec, but instead of creating the
524 // attribute and allowing sema to warn about it, we will warn here instead.
525 // This is because some attributes have multiple spellings, but we need to
526 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000527 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000528 // both locations.
529 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
530
531 // If there's an open paren, we should eat the open and close parens under
532 // the assumption that this unknown declspec has parameters.
533 BalancedDelimiterTracker T(*this, tok::l_paren);
534 if (!T.consumeOpen())
535 T.skipToEnd();
536 }
537}
538
Eli Friedmana23b4852009-06-08 07:21:15 +0000539/// [MS] decl-specifier:
540/// __declspec ( extended-decl-modifier-seq )
541///
542/// [MS] extended-decl-modifier-seq:
543/// extended-decl-modifier[opt]
544/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000545void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000546 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000547
Steve Narofff59e17e2008-12-24 20:59:21 +0000548 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000549 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000550 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000551 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000552 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000553
Chad Rosier8decdee2012-06-26 22:30:43 +0000554 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000555 // you can specify multiple attributes per declspec.
556 while (Tok.getKind() != tok::r_paren) {
557 // We expect either a well-known identifier or a generic string. Anything
558 // else is a malformed declspec.
559 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000560 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000561 Tok.getKind() != tok::kw_restrict) {
562 Diag(Tok, diag::err_ms_declspec_type);
563 T.skipToEnd();
564 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000565 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000566
567 IdentifierInfo *AttrName;
568 SourceLocation AttrNameLoc;
569 if (IsString) {
570 SmallString<8> StrBuffer;
571 bool Invalid = false;
572 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
573 if (Invalid) {
574 T.skipToEnd();
575 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000576 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000577 AttrName = PP.getIdentifierInfo(Str);
578 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000579 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000580 AttrName = Tok.getIdentifierInfo();
581 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000582 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000583
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000584 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000585 // If we have a generic string, we will allow it because there is no
586 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000587 // (for instance, SAL declspecs in older versions of MSVC).
588 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000589 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000590 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000591 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000592 0, 0, AttributeList::AS_Declspec);
593 else
594 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000595 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000596 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000597}
598
John McCall7f040a92010-12-24 02:08:15 +0000599void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000600 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000601 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000602 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000603 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballmanaa9df092013-05-22 23:25:32 +0000604 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
605 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000606 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
607 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000608 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000609 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000610 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000611}
612
John McCall7f040a92010-12-24 02:08:15 +0000613void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000614 // Treat these like attributes
615 while (Tok.is(tok::kw___pascal)) {
616 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
617 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000618 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000619 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000620 }
John McCall7f040a92010-12-24 02:08:15 +0000621}
622
Peter Collingbournef315fa82011-02-14 01:42:53 +0000623void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
624 // Treat these like attributes
625 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000626 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000627 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000628 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
629 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000630 }
631}
632
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000633void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000634 // FIXME: The mapping from attribute spelling to semantics should be
635 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000636 SourceLocation Loc = Tok.getLocation();
637 switch(Tok.getKind()) {
638 // OpenCL qualifiers:
639 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000640 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000641 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000642 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000643 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000644 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000645
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000646 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000647 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000648 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000649 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000650 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000651
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000652 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000653 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000654 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000655 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000656 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000657
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000658 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000659 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000660 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000661 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000662 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000663
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000664 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000665 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000666 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000667 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000668 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000669
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000670 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000671 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000672 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000673 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000674 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000675
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000676 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000677 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000678 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000679 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000680 break;
681 default: break;
682 }
683}
684
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000685/// \brief Parse a version number.
686///
687/// version:
688/// simple-integer
689/// simple-integer ',' simple-integer
690/// simple-integer ',' simple-integer ',' simple-integer
691VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
692 Range = Tok.getLocation();
693
694 if (!Tok.is(tok::numeric_constant)) {
695 Diag(Tok, diag::err_expected_version);
696 SkipUntil(tok::comma, tok::r_paren, true, true, true);
697 return VersionTuple();
698 }
699
700 // Parse the major (and possibly minor and subminor) versions, which
701 // are stored in the numeric constant. We utilize a quirk of the
702 // lexer, which is that it handles something like 1.2.3 as a single
703 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000704 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000705 Buffer.resize(Tok.getLength()+1);
706 const char *ThisTokBegin = &Buffer[0];
707
708 // Get the spelling of the token, which eliminates trigraphs, etc.
709 bool Invalid = false;
710 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
711 if (Invalid)
712 return VersionTuple();
713
714 // Parse the major version.
715 unsigned AfterMajor = 0;
716 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000717 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000718 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
719 ++AfterMajor;
720 }
721
722 if (AfterMajor == 0) {
723 Diag(Tok, diag::err_expected_version);
724 SkipUntil(tok::comma, tok::r_paren, true, true, true);
725 return VersionTuple();
726 }
727
728 if (AfterMajor == ActualLength) {
729 ConsumeToken();
730
731 // We only had a single version component.
732 if (Major == 0) {
733 Diag(Tok, diag::err_zero_version);
734 return VersionTuple();
735 }
736
737 return VersionTuple(Major);
738 }
739
740 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
741 Diag(Tok, diag::err_expected_version);
742 SkipUntil(tok::comma, tok::r_paren, true, true, true);
743 return VersionTuple();
744 }
745
746 // Parse the minor version.
747 unsigned AfterMinor = AfterMajor + 1;
748 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000749 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000750 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
751 ++AfterMinor;
752 }
753
754 if (AfterMinor == ActualLength) {
755 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000756
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000757 // We had major.minor.
758 if (Major == 0 && Minor == 0) {
759 Diag(Tok, diag::err_zero_version);
760 return VersionTuple();
761 }
762
Chad Rosier8decdee2012-06-26 22:30:43 +0000763 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000764 }
765
766 // If what follows is not a '.', we have a problem.
767 if (ThisTokBegin[AfterMinor] != '.') {
768 Diag(Tok, diag::err_expected_version);
769 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000770 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000771 }
772
773 // Parse the subminor version.
774 unsigned AfterSubminor = AfterMinor + 1;
775 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000776 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000777 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
778 ++AfterSubminor;
779 }
780
781 if (AfterSubminor != ActualLength) {
782 Diag(Tok, diag::err_expected_version);
783 SkipUntil(tok::comma, tok::r_paren, true, true, true);
784 return VersionTuple();
785 }
786 ConsumeToken();
787 return VersionTuple(Major, Minor, Subminor);
788}
789
790/// \brief Parse the contents of the "availability" attribute.
791///
792/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000793/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000794///
795/// platform:
796/// identifier
797///
798/// version-arg-list:
799/// version-arg
800/// version-arg ',' version-arg-list
801///
802/// version-arg:
803/// 'introduced' '=' version
804/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000805/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000806/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000807/// opt-message:
808/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000809void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
810 SourceLocation AvailabilityLoc,
811 ParsedAttributes &attrs,
812 SourceLocation *endLoc) {
813 SourceLocation PlatformLoc;
814 IdentifierInfo *Platform = 0;
815
816 enum { Introduced, Deprecated, Obsoleted, Unknown };
817 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000818 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000819
820 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000821 BalancedDelimiterTracker T(*this, tok::l_paren);
822 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000823 Diag(Tok, diag::err_expected_lparen);
824 return;
825 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000826
827 // Parse the platform name,
828 if (Tok.isNot(tok::identifier)) {
829 Diag(Tok, diag::err_availability_expected_platform);
830 SkipUntil(tok::r_paren);
831 return;
832 }
833 Platform = Tok.getIdentifierInfo();
834 PlatformLoc = ConsumeToken();
835
836 // Parse the ',' following the platform name.
837 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
838 return;
839
840 // If we haven't grabbed the pointers for the identifiers
841 // "introduced", "deprecated", and "obsoleted", do so now.
842 if (!Ident_introduced) {
843 Ident_introduced = PP.getIdentifierInfo("introduced");
844 Ident_deprecated = PP.getIdentifierInfo("deprecated");
845 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000846 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000847 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000848 }
849
850 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000851 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000852 do {
853 if (Tok.isNot(tok::identifier)) {
854 Diag(Tok, diag::err_availability_expected_change);
855 SkipUntil(tok::r_paren);
856 return;
857 }
858 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
859 SourceLocation KeywordLoc = ConsumeToken();
860
Douglas Gregorb53e4172011-03-26 03:35:55 +0000861 if (Keyword == Ident_unavailable) {
862 if (UnavailableLoc.isValid()) {
863 Diag(KeywordLoc, diag::err_availability_redundant)
864 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000865 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000866 UnavailableLoc = KeywordLoc;
867
868 if (Tok.isNot(tok::comma))
869 break;
870
871 ConsumeToken();
872 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000873 }
874
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000875 if (Tok.isNot(tok::equal)) {
876 Diag(Tok, diag::err_expected_equal_after)
877 << Keyword;
878 SkipUntil(tok::r_paren);
879 return;
880 }
881 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000882 if (Keyword == Ident_message) {
883 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000884 Diag(Tok, diag::err_expected_string_literal)
885 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000886 SkipUntil(tok::r_paren);
887 return;
888 }
889 MessageExpr = ParseStringLiteralExpression();
890 break;
891 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000892
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000893 SourceRange VersionRange;
894 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000895
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000896 if (Version.empty()) {
897 SkipUntil(tok::r_paren);
898 return;
899 }
900
901 unsigned Index;
902 if (Keyword == Ident_introduced)
903 Index = Introduced;
904 else if (Keyword == Ident_deprecated)
905 Index = Deprecated;
906 else if (Keyword == Ident_obsoleted)
907 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000908 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000909 Index = Unknown;
910
911 if (Index < Unknown) {
912 if (!Changes[Index].KeywordLoc.isInvalid()) {
913 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000914 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000915 << SourceRange(Changes[Index].KeywordLoc,
916 Changes[Index].VersionRange.getEnd());
917 }
918
919 Changes[Index].KeywordLoc = KeywordLoc;
920 Changes[Index].Version = Version;
921 Changes[Index].VersionRange = VersionRange;
922 } else {
923 Diag(KeywordLoc, diag::err_availability_unknown_change)
924 << Keyword << VersionRange;
925 }
926
927 if (Tok.isNot(tok::comma))
928 break;
929
930 ConsumeToken();
931 } while (true);
932
933 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000934 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000935 return;
936
937 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000938 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000939
Douglas Gregorb53e4172011-03-26 03:35:55 +0000940 // The 'unavailable' availability cannot be combined with any other
941 // availability changes. Make sure that hasn't happened.
942 if (UnavailableLoc.isValid()) {
943 bool Complained = false;
944 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
945 if (Changes[Index].KeywordLoc.isValid()) {
946 if (!Complained) {
947 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
948 << SourceRange(Changes[Index].KeywordLoc,
949 Changes[Index].VersionRange.getEnd());
950 Complained = true;
951 }
952
953 // Clear out the availability.
954 Changes[Index] = AvailabilityChange();
955 }
956 }
957 }
958
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000959 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000960 attrs.addNew(&Availability,
961 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000962 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000963 Platform, PlatformLoc,
964 Changes[Introduced],
965 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000966 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000967 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000968 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000969}
970
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000971
Bill Wendlingad017fa2012-12-20 19:22:21 +0000972// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000973// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
974
975void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
976
977void Parser::LateParsedClass::ParseLexedAttributes() {
978 Self->ParseLexedAttributes(*Class);
979}
980
981void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000982 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000983}
984
985/// Wrapper class which calls ParseLexedAttribute, after setting up the
986/// scope appropriately.
987void Parser::ParseLexedAttributes(ParsingClass &Class) {
988 // Deal with templates
989 // FIXME: Test cases to make sure this does the right thing for templates.
990 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
991 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
992 HasTemplateScope);
993 if (HasTemplateScope)
994 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
995
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000996 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000997 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000998 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000999 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1000 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1001
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001002 // Enter the scope of nested classes
1003 if (!AlreadyHasClassScope)
1004 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1005 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +00001006 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001007 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1008 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1009 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001010 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001011
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001012 if (!AlreadyHasClassScope)
1013 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1014 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001015}
1016
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001017
1018/// \brief Parse all attributes in LAs, and attach them to Decl D.
1019void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1020 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +00001021 assert(LAs.parseSoon() &&
1022 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001023 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +00001024 if (D)
1025 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001026 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +00001027 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001028 }
1029 LAs.clear();
1030}
1031
1032
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001033/// \brief Finish parsing an attribute for which parsing was delayed.
1034/// This will be called at the end of parsing a class declaration
1035/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +00001036/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001037/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001038void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1039 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001040 // Save the current token position.
1041 SourceLocation OrigLoc = Tok.getLocation();
1042
1043 // Append the current token at the end of the new token stream so that it
1044 // doesn't get lost.
1045 LA.Toks.push_back(Tok);
1046 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1047 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00001048 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001049
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001050 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +00001051 // FIXME: Do not warn on C++11 attributes, once we start supporting
1052 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001053 Diag(Tok, diag::warn_attribute_on_function_definition)
1054 << LA.AttrName.getName();
1055 }
1056
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001057 ParsedAttributes Attrs(AttrFactory);
1058 SourceLocation endLoc;
1059
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001060 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001061 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001062 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1063 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001064
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001065 // Allow 'this' within late-parsed attributes.
Richard Smithcafeb942013-06-07 02:33:37 +00001066 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1067 ND && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001068
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001069 if (LA.Decls.size() == 1) {
1070 // If the Decl is templatized, add template parameters to scope.
1071 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1072 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1073 if (HasTemplateScope)
1074 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001075
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001076 // If the Decl is on a function, add function parameters to the scope.
1077 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1078 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1079 if (HasFunScope)
1080 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001081
Michael Han6880f492012-10-03 01:56:22 +00001082 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001083 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001084
1085 if (HasFunScope) {
1086 Actions.ActOnExitFunctionContext();
1087 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1088 }
1089 if (HasTemplateScope) {
1090 TempScope.Exit();
1091 }
1092 } else {
1093 // If there are multiple decls, then the decl cannot be within the
1094 // function scope.
Michael Han6880f492012-10-03 01:56:22 +00001095 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001096 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001097 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +00001098 } else {
1099 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001100 }
1101
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001102 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1103 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1104 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001105
1106 if (Tok.getLocation() != OrigLoc) {
1107 // Due to a parsing error, we either went over the cached tokens or
1108 // there are still cached tokens left, so we skip the leftover tokens.
1109 // Since this is an uncommon situation that should be avoided, use the
1110 // expensive isBeforeInTranslationUnit call.
1111 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1112 OrigLoc))
1113 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001114 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001115 }
1116}
1117
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001118/// \brief Wrapper around a case statement checking if AttrName is
1119/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001120bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001121 return llvm::StringSwitch<bool>(AttrName)
1122 .Case("guarded_by", true)
1123 .Case("guarded_var", true)
1124 .Case("pt_guarded_by", true)
1125 .Case("pt_guarded_var", true)
1126 .Case("lockable", true)
1127 .Case("scoped_lockable", true)
1128 .Case("no_thread_safety_analysis", true)
1129 .Case("acquired_after", true)
1130 .Case("acquired_before", true)
1131 .Case("exclusive_lock_function", true)
1132 .Case("shared_lock_function", true)
1133 .Case("exclusive_trylock_function", true)
1134 .Case("shared_trylock_function", true)
1135 .Case("unlock_function", true)
1136 .Case("lock_returned", true)
1137 .Case("locks_excluded", true)
1138 .Case("exclusive_locks_required", true)
1139 .Case("shared_locks_required", true)
1140 .Default(false);
1141}
1142
1143/// \brief Parse the contents of thread safety attributes. These
1144/// should always be parsed as an expression list.
1145///
1146/// We need to special case the parsing due to the fact that if the first token
1147/// of the first argument is an identifier, the main parse loop will store
1148/// that token as a "parameter" and the rest of
1149/// the arguments will be added to a list of "arguments". However,
1150/// subsequent tokens in the first argument are lost. We instead parse each
1151/// argument as an expression and add all arguments to the list of "arguments".
1152/// In future, we will take advantage of this special case to also
1153/// deal with some argument scoping issues here (for example, referring to a
1154/// function parameter in the attribute on that function).
1155void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1156 SourceLocation AttrNameLoc,
1157 ParsedAttributes &Attrs,
1158 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001159 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001160
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001161 BalancedDelimiterTracker T(*this, tok::l_paren);
1162 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001163
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001164 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001165 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001166
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001167 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001168 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001169 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001170 ExprResult ArgExpr(ParseAssignmentExpression());
1171 if (ArgExpr.isInvalid()) {
1172 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001173 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001174 break;
1175 } else {
1176 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001177 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001178 if (Tok.isNot(tok::comma))
1179 break;
1180 ConsumeToken(); // Eat the comma, move to the next argument
1181 }
1182 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001183 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001184 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001185 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001186 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001187 if (EndLoc)
1188 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001189}
1190
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001191void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1192 SourceLocation AttrNameLoc,
1193 ParsedAttributes &Attrs,
1194 SourceLocation *EndLoc) {
1195 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1196
1197 BalancedDelimiterTracker T(*this, tok::l_paren);
1198 T.consumeOpen();
1199
1200 if (Tok.isNot(tok::identifier)) {
1201 Diag(Tok, diag::err_expected_ident);
1202 T.skipToEnd();
1203 return;
1204 }
1205 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1206 SourceLocation ArgumentKindLoc = ConsumeToken();
1207
1208 if (Tok.isNot(tok::comma)) {
1209 Diag(Tok, diag::err_expected_comma);
1210 T.skipToEnd();
1211 return;
1212 }
1213 ConsumeToken();
1214
1215 SourceRange MatchingCTypeRange;
1216 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1217 if (MatchingCType.isInvalid()) {
1218 T.skipToEnd();
1219 return;
1220 }
1221
1222 bool LayoutCompatible = false;
1223 bool MustBeNull = false;
1224 while (Tok.is(tok::comma)) {
1225 ConsumeToken();
1226 if (Tok.isNot(tok::identifier)) {
1227 Diag(Tok, diag::err_expected_ident);
1228 T.skipToEnd();
1229 return;
1230 }
1231 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1232 if (Flag->isStr("layout_compatible"))
1233 LayoutCompatible = true;
1234 else if (Flag->isStr("must_be_null"))
1235 MustBeNull = true;
1236 else {
1237 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1238 T.skipToEnd();
1239 return;
1240 }
1241 ConsumeToken(); // consume flag
1242 }
1243
1244 if (!T.consumeClose()) {
1245 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1246 ArgumentKind, ArgumentKindLoc,
1247 MatchingCType.release(), LayoutCompatible,
1248 MustBeNull, AttributeList::AS_GNU);
1249 }
1250
1251 if (EndLoc)
1252 *EndLoc = T.getCloseLocation();
1253}
1254
Richard Smith6ee326a2012-04-10 01:32:12 +00001255/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1256/// of a C++11 attribute-specifier in a location where an attribute is not
1257/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1258/// situation.
1259///
1260/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1261/// this doesn't appear to actually be an attribute-specifier, and the caller
1262/// should try to parse it.
1263bool Parser::DiagnoseProhibitedCXX11Attribute() {
1264 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1265
1266 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1267 case CAK_NotAttributeSpecifier:
1268 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1269 return false;
1270
1271 case CAK_InvalidAttributeSpecifier:
1272 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1273 return false;
1274
1275 case CAK_AttributeSpecifier:
1276 // Parse and discard the attributes.
1277 SourceLocation BeginLoc = ConsumeBracket();
1278 ConsumeBracket();
1279 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1280 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1281 SourceLocation EndLoc = ConsumeBracket();
1282 Diag(BeginLoc, diag::err_attributes_not_allowed)
1283 << SourceRange(BeginLoc, EndLoc);
1284 return true;
1285 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001286 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001287}
1288
Richard Smith975d52c2013-02-20 01:17:14 +00001289/// \brief We have found the opening square brackets of a C++11
1290/// attribute-specifier in a location where an attribute is not permitted, but
1291/// we know where the attributes ought to be written. Parse them anyway, and
1292/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001293void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1294 SourceLocation CorrectLocation) {
1295 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1296 Tok.is(tok::kw_alignas));
1297
1298 // Consume the attributes.
1299 SourceLocation Loc = Tok.getLocation();
1300 ParseCXX11Attributes(Attrs);
1301 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1302
1303 Diag(Loc, diag::err_attributes_not_allowed)
1304 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1305 << FixItHint::CreateRemoval(AttrRange);
1306}
1307
John McCall7f040a92010-12-24 02:08:15 +00001308void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1309 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1310 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001311}
1312
Michael Hanf64231e2012-11-06 19:34:54 +00001313void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1314 AttributeList *AttrList = attrs.getList();
1315 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001316 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001317 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001318 << AttrList->getName();
1319 AttrList->setInvalid();
1320 }
1321 AttrList = AttrList->getNext();
1322 }
1323}
1324
Reid Spencer5f016e22007-07-11 17:01:13 +00001325/// ParseDeclaration - Parse a full 'declaration', which consists of
1326/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001327/// 'Context' should be a Declarator::TheContext value. This returns the
1328/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001329///
1330/// declaration: [C99 6.7]
1331/// block-declaration ->
1332/// simple-declaration
1333/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001334/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001335/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001336/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001337/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001338/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001339/// others... [FIXME]
1340///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001341Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1342 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001343 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001344 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001345 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001346 // Must temporarily exit the objective-c container scope for
1347 // parsing c none objective-c decls.
1348 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001349
John McCalld226f652010-08-21 09:40:31 +00001350 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001351 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001352 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001353 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001354 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001355 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001356 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001357 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001358 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001359 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001360 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001361 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001362 SourceLocation InlineLoc = ConsumeToken();
1363 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1364 break;
1365 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001366 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001367 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001368 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001369 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001370 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001371 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001372 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001373 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001374 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001375 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001376 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001377 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001378 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001379 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001380 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001381 default:
John McCall7f040a92010-12-24 02:08:15 +00001382 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001383 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001384
Chris Lattner682bf922009-03-29 16:50:03 +00001385 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001386 // single decl, convert it now. Alias declarations can also declare a type;
1387 // include that too if it is present.
1388 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001389}
1390
1391/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1392/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001393/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1394/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001395///[C90/C++]init-declarator-list ';' [TODO]
1396/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001397///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001398/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001399/// attribute-specifier-seq[opt] type-specifier-seq declarator
1400///
Chris Lattnercd147752009-03-29 17:27:48 +00001401/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001402/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001403///
1404/// If FRI is non-null, we might be parsing a for-range-declaration instead
1405/// of a simple-declaration. If we find that we are, we also parse the
1406/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001407Parser::DeclGroupPtrTy
1408Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1409 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001410 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001411 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001413 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001414
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001415 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001416 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001417
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1419 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001420 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001421 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001422 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001423 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001424 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001425 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001426 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001427 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001429
Richard Smith68ea3ae2013-02-22 09:06:26 +00001430 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001431 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001432}
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Richard Smith0706df42011-10-19 21:33:05 +00001434/// Returns true if this might be the start of a declarator, or a common typo
1435/// for a declarator.
1436bool Parser::MightBeDeclarator(unsigned Context) {
1437 switch (Tok.getKind()) {
1438 case tok::annot_cxxscope:
1439 case tok::annot_template_id:
1440 case tok::caret:
1441 case tok::code_completion:
1442 case tok::coloncolon:
1443 case tok::ellipsis:
1444 case tok::kw___attribute:
1445 case tok::kw_operator:
1446 case tok::l_paren:
1447 case tok::star:
1448 return true;
1449
1450 case tok::amp:
1451 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001452 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001453
Richard Smith1c94c162012-01-09 22:31:44 +00001454 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001455 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001456 NextToken().is(tok::l_square);
1457
1458 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001459 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001460
Richard Smith0706df42011-10-19 21:33:05 +00001461 case tok::identifier:
1462 switch (NextToken().getKind()) {
1463 case tok::code_completion:
1464 case tok::coloncolon:
1465 case tok::comma:
1466 case tok::equal:
1467 case tok::equalequal: // Might be a typo for '='.
1468 case tok::kw_alignas:
1469 case tok::kw_asm:
1470 case tok::kw___attribute:
1471 case tok::l_brace:
1472 case tok::l_paren:
1473 case tok::l_square:
1474 case tok::less:
1475 case tok::r_brace:
1476 case tok::r_paren:
1477 case tok::r_square:
1478 case tok::semi:
1479 return true;
1480
1481 case tok::colon:
1482 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001483 // and in block scope it's probably a label. Inside a class definition,
1484 // this is a bit-field.
1485 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001486 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001487
1488 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001489 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001490
1491 default:
1492 return false;
1493 }
1494
1495 default:
1496 return false;
1497 }
1498}
1499
Richard Smith994d73f2012-04-11 20:59:20 +00001500/// Skip until we reach something which seems like a sensible place to pick
1501/// up parsing after a malformed declaration. This will sometimes stop sooner
1502/// than SkipUntil(tok::r_brace) would, but will never stop later.
1503void Parser::SkipMalformedDecl() {
1504 while (true) {
1505 switch (Tok.getKind()) {
1506 case tok::l_brace:
1507 // Skip until matching }, then stop. We've probably skipped over
1508 // a malformed class or function definition or similar.
1509 ConsumeBrace();
1510 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1511 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1512 // This declaration isn't over yet. Keep skipping.
1513 continue;
1514 }
1515 if (Tok.is(tok::semi))
1516 ConsumeToken();
1517 return;
1518
1519 case tok::l_square:
1520 ConsumeBracket();
1521 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1522 continue;
1523
1524 case tok::l_paren:
1525 ConsumeParen();
1526 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1527 continue;
1528
1529 case tok::r_brace:
1530 return;
1531
1532 case tok::semi:
1533 ConsumeToken();
1534 return;
1535
1536 case tok::kw_inline:
1537 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001538 // a good place to pick back up parsing, except in an Objective-C
1539 // @interface context.
1540 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1541 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001542 return;
1543 break;
1544
1545 case tok::kw_namespace:
1546 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001547 // place to pick back up parsing, except in an Objective-C
1548 // @interface context.
1549 if (Tok.isAtStartOfLine() &&
1550 (!ParsingInObjCContainer || CurParsedObjCImpl))
1551 return;
1552 break;
1553
1554 case tok::at:
1555 // @end is very much like } in Objective-C contexts.
1556 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1557 ParsingInObjCContainer)
1558 return;
1559 break;
1560
1561 case tok::minus:
1562 case tok::plus:
1563 // - and + probably start new method declarations in Objective-C contexts.
1564 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001565 return;
1566 break;
1567
1568 case tok::eof:
1569 return;
1570
1571 default:
1572 break;
1573 }
1574
1575 ConsumeAnyToken();
1576 }
1577}
1578
John McCalld8ac0572009-11-03 19:26:08 +00001579/// ParseDeclGroup - Having concluded that this is either a function
1580/// definition or a group of object declarations, actually parse the
1581/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001582Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1583 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001584 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001585 SourceLocation *DeclEnd,
1586 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001587 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001588 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001589 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001590
John McCalld8ac0572009-11-03 19:26:08 +00001591 // Bail out if the first declarator didn't seem well-formed.
1592 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001593 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001594 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001595 }
Mike Stump1eb44332009-09-09 15:08:12 +00001596
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001597 // Save late-parsed attributes for now; they need to be parsed in the
1598 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001599 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1600 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001601 if (D.isFunctionDeclarator())
1602 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1603
Chris Lattnerc82daef2010-07-11 22:24:20 +00001604 // Check to see if we have a function *definition* which must have a body.
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001605 if (D.isFunctionDeclarator() &&
Chris Lattnerc82daef2010-07-11 22:24:20 +00001606 // Look at the next token to make sure that this isn't a function
1607 // declaration. We have to check this because __attribute__ might be the
1608 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001609 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001610
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001611 if (AllowFunctionDefinitions) {
1612 if (isStartOfFunctionDefinition(D)) {
1613 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1614 Diag(Tok, diag::err_function_declared_typedef);
John McCalld8ac0572009-11-03 19:26:08 +00001615
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001616 // Recover by treating the 'typedef' as spurious.
1617 DS.ClearStorageClassSpecs();
1618 }
1619
1620 Decl *TheDecl =
1621 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1622 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld8ac0572009-11-03 19:26:08 +00001623 }
1624
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001625 if (isDeclarationSpecifier()) {
1626 // If there is an invalid declaration specifier right after the function
1627 // prototype, then we must be in a missing semicolon case where this isn't
1628 // actually a body. Just fall through into the code that handles it as a
1629 // prototype, and let the top-level code handle the erroneous declspec
1630 // where it would otherwise expect a comma or semicolon.
1631 } else {
1632 Diag(Tok, diag::err_expected_fn_body);
1633 SkipUntil(tok::semi);
1634 return DeclGroupPtrTy();
1635 }
John McCalld8ac0572009-11-03 19:26:08 +00001636 } else {
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001637 if (Tok.is(tok::l_brace)) {
1638 Diag(Tok, diag::err_function_definition_not_allowed);
1639 SkipUntil(tok::r_brace, true, true);
1640 }
John McCalld8ac0572009-11-03 19:26:08 +00001641 }
1642 }
1643
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001644 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001645 return DeclGroupPtrTy();
1646
1647 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1648 // must parse and analyze the for-range-initializer before the declaration is
1649 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001650 //
1651 // Handle the Objective-C for-in loop variable similarly, although we
1652 // don't need to parse the container in advance.
1653 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1654 bool IsForRangeLoop = false;
1655 if (Tok.is(tok::colon)) {
1656 IsForRangeLoop = true;
1657 FRI->ColonLoc = ConsumeToken();
1658 if (Tok.is(tok::l_brace))
1659 FRI->RangeExpr = ParseBraceInitializer();
1660 else
1661 FRI->RangeExpr = ParseExpression();
1662 }
1663
Richard Smithad762fc2011-04-14 22:09:26 +00001664 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001665 if (IsForRangeLoop)
1666 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001667 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001668 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001669 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1670 }
1671
Chris Lattner5f9e2722011-07-23 10:55:15 +00001672 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001673 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001674 if (LateParsedAttrs.size() > 0)
1675 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001676 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001677 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001678 DeclsInGroup.push_back(FirstDecl);
1679
Richard Smith0706df42011-10-19 21:33:05 +00001680 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001681
John McCalld8ac0572009-11-03 19:26:08 +00001682 // If we don't have a comma, it is either the end of the list (a ';') or an
1683 // error, bail out.
1684 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001685 SourceLocation CommaLoc = ConsumeToken();
1686
1687 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1688 // This comma was followed by a line-break and something which can't be
1689 // the start of a declarator. The comma was probably a typo for a
1690 // semicolon.
1691 Diag(CommaLoc, diag::err_expected_semi_declaration)
1692 << FixItHint::CreateReplacement(CommaLoc, ";");
1693 ExpectSemi = false;
1694 break;
1695 }
John McCalld8ac0572009-11-03 19:26:08 +00001696
1697 // Parse the next declarator.
1698 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001699 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001700
1701 // Accept attributes in an init-declarator. In the first declarator in a
1702 // declaration, these would be part of the declspec. In subsequent
1703 // declarators, they become part of the declarator itself, so that they
1704 // don't apply to declarators after *this* one. Examples:
1705 // short __attribute__((common)) var; -> declspec
1706 // short var __attribute__((common)); -> declarator
1707 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001708 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001709
1710 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001711 if (!D.isInvalidType()) {
1712 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1713 D.complete(ThisDecl);
1714 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001715 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001716 }
John McCalld8ac0572009-11-03 19:26:08 +00001717 }
1718
1719 if (DeclEnd)
1720 *DeclEnd = Tok.getLocation();
1721
Richard Smith0706df42011-10-19 21:33:05 +00001722 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001723 ExpectAndConsumeSemi(Context == Declarator::FileContext
1724 ? diag::err_invalid_token_after_toplevel_declarator
1725 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001726 // Okay, there was no semicolon and one was expected. If we see a
1727 // declaration specifier, just assume it was missing and continue parsing.
1728 // Otherwise things are very confused and we skip to recover.
1729 if (!isDeclarationSpecifier()) {
1730 SkipUntil(tok::r_brace, true, true);
1731 if (Tok.is(tok::semi))
1732 ConsumeToken();
1733 }
John McCalld8ac0572009-11-03 19:26:08 +00001734 }
1735
Douglas Gregor23c94db2010-07-02 17:43:08 +00001736 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001737 DeclsInGroup.data(),
1738 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001739}
1740
Richard Smithad762fc2011-04-14 22:09:26 +00001741/// Parse an optional simple-asm-expr and attributes, and attach them to a
1742/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001743bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001744 // If a simple-asm-expr is present, parse it.
1745 if (Tok.is(tok::kw_asm)) {
1746 SourceLocation Loc;
1747 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1748 if (AsmLabel.isInvalid()) {
1749 SkipUntil(tok::semi, true, true);
1750 return true;
1751 }
1752
1753 D.setAsmLabel(AsmLabel.release());
1754 D.SetRangeEnd(Loc);
1755 }
1756
1757 MaybeParseGNUAttributes(D);
1758 return false;
1759}
1760
Douglas Gregor1426e532009-05-12 21:31:51 +00001761/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1762/// declarator'. This method parses the remainder of the declaration
1763/// (including any attributes or initializer, among other things) and
1764/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001765///
Reid Spencer5f016e22007-07-11 17:01:13 +00001766/// init-declarator: [C99 6.7]
1767/// declarator
1768/// declarator '=' initializer
1769/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1770/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001771/// [C++] declarator initializer[opt]
1772///
1773/// [C++] initializer:
1774/// [C++] '=' initializer-clause
1775/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001776/// [C++0x] '=' 'default' [TODO]
1777/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001778/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001779///
1780/// According to the standard grammar, =default and =delete are function
1781/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001782///
John McCalld226f652010-08-21 09:40:31 +00001783Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001784 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001785 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001786 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Richard Smithad762fc2011-04-14 22:09:26 +00001788 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1789}
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Richard Smithad762fc2011-04-14 22:09:26 +00001791Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1792 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001793 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001794 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001795 switch (TemplateInfo.Kind) {
1796 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001797 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001798 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001799
Douglas Gregord5a423b2009-09-25 18:43:00 +00001800 case ParsedTemplateInfo::Template:
1801 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001802 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001803 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001804 D);
1805 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001806
Douglas Gregord5a423b2009-09-25 18:43:00 +00001807 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001808 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001809 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001810 TemplateInfo.ExternLoc,
1811 TemplateInfo.TemplateLoc,
1812 D);
1813 if (ThisRes.isInvalid()) {
1814 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001815 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001816 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001817
Douglas Gregord5a423b2009-09-25 18:43:00 +00001818 ThisDecl = ThisRes.get();
1819 break;
1820 }
1821 }
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Richard Smitha2c36462013-04-26 16:15:35 +00001823 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith34b41d92011-02-20 03:19:35 +00001824
Douglas Gregor1426e532009-05-12 21:31:51 +00001825 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001826 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001827 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001828 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001829 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001830 if (D.isFunctionDeclarator())
1831 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1832 << 1 /* delete */;
1833 else
1834 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001835 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001836 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001837 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1838 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001839 else
1840 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001841 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001842 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001843 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001844 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001845 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001846
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001847 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001848 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001849 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001850 cutOffParsing();
1851 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001852 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001853
John McCall60d7b3a2010-08-24 06:29:42 +00001854 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001855
David Blaikie4e4d0842012-03-11 07:00:24 +00001856 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001857 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001858 ExitScope();
1859 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001860
Douglas Gregor1426e532009-05-12 21:31:51 +00001861 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001862 SkipUntil(tok::comma, true, true);
1863 Actions.ActOnInitializerError(ThisDecl);
1864 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001865 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1866 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001867 }
1868 } else if (Tok.is(tok::l_paren)) {
1869 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001870 BalancedDelimiterTracker T(*this, tok::l_paren);
1871 T.consumeOpen();
1872
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001873 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001874 CommaLocsTy CommaLocs;
1875
David Blaikie4e4d0842012-03-11 07:00:24 +00001876 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001877 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001878 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001879 }
1880
Douglas Gregor1426e532009-05-12 21:31:51 +00001881 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001882 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001883 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001884
David Blaikie4e4d0842012-03-11 07:00:24 +00001885 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001886 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001887 ExitScope();
1888 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001889 } else {
1890 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001891 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001892
1893 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1894 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001895
David Blaikie4e4d0842012-03-11 07:00:24 +00001896 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001897 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001898 ExitScope();
1899 }
1900
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001901 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1902 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001903 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001904 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1905 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001906 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001907 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001908 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001909 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001910 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1911
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001912 if (D.getCXXScopeSpec().isSet()) {
1913 EnterScope(0);
1914 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1915 }
1916
1917 ExprResult Init(ParseBraceInitializer());
1918
1919 if (D.getCXXScopeSpec().isSet()) {
1920 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1921 ExitScope();
1922 }
1923
1924 if (Init.isInvalid()) {
1925 Actions.ActOnInitializerError(ThisDecl);
1926 } else
1927 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1928 /*DirectInit=*/true, TypeContainsAuto);
1929
Douglas Gregor1426e532009-05-12 21:31:51 +00001930 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001931 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001932 }
1933
Richard Smith483b9f32011-02-21 20:05:19 +00001934 Actions.FinalizeDeclaration(ThisDecl);
1935
Douglas Gregor1426e532009-05-12 21:31:51 +00001936 return ThisDecl;
1937}
1938
Reid Spencer5f016e22007-07-11 17:01:13 +00001939/// ParseSpecifierQualifierList
1940/// specifier-qualifier-list:
1941/// type-specifier specifier-qualifier-list[opt]
1942/// type-qualifier specifier-qualifier-list[opt]
1943/// [GNU] attributes specifier-qualifier-list[opt]
1944///
Richard Smith69730c12012-03-12 07:56:15 +00001945void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1946 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001947 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1948 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001949 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001950 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 // Validate declspec for type-name.
1953 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001954 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1955 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001956 Diag(Tok, diag::err_expected_type);
1957 DS.SetTypeSpecError();
1958 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1959 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001961 if (!DS.hasTypeSpecifier())
1962 DS.SetTypeSpecError();
1963 }
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Reid Spencer5f016e22007-07-11 17:01:13 +00001965 // Issue diagnostic and remove storage class if present.
1966 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1967 if (DS.getStorageClassSpecLoc().isValid())
1968 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1969 else
Richard Smithec642442013-04-12 22:46:28 +00001970 Diag(DS.getThreadStorageClassSpecLoc(),
1971 diag::err_typename_invalid_storageclass);
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 DS.ClearStorageClassSpecs();
1973 }
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 // Issue diagnostic and remove function specfier if present.
1976 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001977 if (DS.isInlineSpecified())
1978 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1979 if (DS.isVirtualSpecified())
1980 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1981 if (DS.isExplicitSpecified())
1982 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001983 DS.ClearFunctionSpecs();
1984 }
Richard Smith69730c12012-03-12 07:56:15 +00001985
1986 // Issue diagnostic and remove constexpr specfier if present.
1987 if (DS.isConstexprSpecified()) {
1988 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1989 DS.ClearConstexprSpec();
1990 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001991}
1992
Chris Lattnerc199ab32009-04-12 20:42:31 +00001993/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1994/// specified token is valid after the identifier in a declarator which
1995/// immediately follows the declspec. For example, these things are valid:
1996///
1997/// int x [ 4]; // direct-declarator
1998/// int x ( int y); // direct-declarator
1999/// int(int x ) // direct-declarator
2000/// int x ; // simple-declaration
2001/// int x = 17; // init-declarator-list
2002/// int x , y; // init-declarator-list
2003/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002004/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00002005/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00002006///
2007/// This is not, because 'x' does not immediately follow the declspec (though
2008/// ')' happens to be valid anyway).
2009/// int (x)
2010///
2011static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2012 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2013 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002014 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00002015}
2016
Chris Lattnere40c2952009-04-14 21:34:55 +00002017
2018/// ParseImplicitInt - This method is called when we have an non-typename
2019/// identifier in a declspec (which normally terminates the decl spec) when
2020/// the declspec has no type specifier. In this case, the declspec is either
2021/// malformed or is "implicit int" (in K&R and C89).
2022///
2023/// This method handles diagnosing this prettily and returns false if the
2024/// declspec is done being processed. If it recovers and thinks there may be
2025/// other pieces of declspec after it, it returns true.
2026///
Chris Lattnerf4382f52009-04-14 22:17:06 +00002027bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002028 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00002029 AccessSpecifier AS, DeclSpecContext DSC,
2030 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002031 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Chris Lattnere40c2952009-04-14 21:34:55 +00002033 SourceLocation Loc = Tok.getLocation();
2034 // If we see an identifier that is not a type name, we normally would
2035 // parse it as the identifer being declared. However, when a typename
2036 // is typo'd or the definition is not included, this will incorrectly
2037 // parse the typename as the identifier name and fall over misparsing
2038 // later parts of the diagnostic.
2039 //
2040 // As such, we try to do some look-ahead in cases where this would
2041 // otherwise be an "implicit-int" case to see if this is invalid. For
2042 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2043 // an identifier with implicit int, we'd get a parse error because the
2044 // next token is obviously invalid for a type. Parse these as a case
2045 // with an invalid type specifier.
2046 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Chris Lattnere40c2952009-04-14 21:34:55 +00002048 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00002049 // error, do lookahead to try to do better recovery. This never applies
2050 // within a type specifier. Outside of C++, we allow this even if the
2051 // language doesn't "officially" support implicit int -- we support
Richard Smith58eb3702013-04-30 22:43:51 +00002052 // implicit int as an extension in C99 and C11.
Richard Smitha971d242012-05-09 20:55:26 +00002053 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith58eb3702013-04-30 22:43:51 +00002054 !getLangOpts().CPlusPlus &&
Richard Smith69730c12012-03-12 07:56:15 +00002055 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002056 // If this token is valid for implicit int, e.g. "static x = 4", then
2057 // we just avoid eating the identifier, so it will be parsed as the
2058 // identifier in the declarator.
2059 return false;
2060 }
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Richard Smith827adaf2012-05-15 21:01:51 +00002062 if (getLangOpts().CPlusPlus &&
2063 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2064 // Don't require a type specifier if we have the 'auto' storage class
2065 // specifier in C++98 -- we'll promote it to a type specifier.
2066 return false;
2067 }
2068
Chris Lattnere40c2952009-04-14 21:34:55 +00002069 // Otherwise, if we don't consume this token, we are going to emit an
2070 // error anyway. Try to recover from various common problems. Check
2071 // to see if this was a reference to a tag name without a tag specified.
2072 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00002073 //
2074 // C++ doesn't need this, and isTagName doesn't take SS.
2075 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002076 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002077 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Douglas Gregor23c94db2010-07-02 17:43:08 +00002079 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002080 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002081 case DeclSpec::TST_enum:
2082 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2083 case DeclSpec::TST_union:
2084 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2085 case DeclSpec::TST_struct:
2086 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00002087 case DeclSpec::TST_interface:
2088 TagName="__interface"; FixitTagName = "__interface ";
2089 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002090 case DeclSpec::TST_class:
2091 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00002092 }
Mike Stump1eb44332009-09-09 15:08:12 +00002093
Chris Lattnerf4382f52009-04-14 22:17:06 +00002094 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002095 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2096 LookupResult R(Actions, TokenName, SourceLocation(),
2097 Sema::LookupOrdinaryName);
2098
Chris Lattnerf4382f52009-04-14 22:17:06 +00002099 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002100 << TokenName << TagName << getLangOpts().CPlusPlus
2101 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2102
2103 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2104 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2105 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00002106 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002107 << TokenName << TagName;
2108 }
Mike Stump1eb44332009-09-09 15:08:12 +00002109
Chris Lattnerf4382f52009-04-14 22:17:06 +00002110 // Parse this as a tag as if the missing tag were present.
2111 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00002112 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002113 else
Richard Smith69730c12012-03-12 07:56:15 +00002114 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002115 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002116 return true;
2117 }
Chris Lattnere40c2952009-04-14 21:34:55 +00002118 }
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Richard Smith8f0a7e72012-05-15 21:29:55 +00002120 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002121 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00002122 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2123 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002124 // Look ahead to the next token to try to figure out what this declaration
2125 // was supposed to be.
2126 switch (NextToken().getKind()) {
2127 case tok::comma:
2128 case tok::equal:
2129 case tok::kw_asm:
2130 case tok::l_brace:
2131 case tok::l_square:
2132 case tok::semi:
2133 // This looks like a variable declaration. The type is probably missing.
2134 // We're done parsing decl-specifiers.
2135 return false;
2136
2137 case tok::l_paren: {
2138 // static x(4); // 'x' is not a type
2139 // x(int n); // 'x' is not a type
2140 // x (*p)[]; // 'x' is a type
2141 //
2142 // Since we're in an error case (or the rare 'implicit int in C++' MS
2143 // extension), we can afford to perform a tentative parse to determine
2144 // which case we're in.
2145 TentativeParsingAction PA(*this);
2146 ConsumeToken();
2147 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2148 PA.Revert();
2149 if (TPR == TPResult::False())
2150 return false;
2151 // The identifier is followed by a parenthesized declarator.
2152 // It's supposed to be a type.
2153 break;
2154 }
2155
2156 default:
2157 // This is probably supposed to be a type. This includes cases like:
2158 // int f(itn);
2159 // struct S { unsinged : 4; };
2160 break;
2161 }
2162 }
2163
Chad Rosier8decdee2012-06-26 22:30:43 +00002164 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002165 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002166 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002167 IdentifierInfo *II = Tok.getIdentifierInfo();
2168 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002169 // The action emitted a diagnostic, so we don't have to.
2170 if (T) {
2171 // The action has suggested that the type T could be used. Set that as
2172 // the type in the declaration specifiers, consume the would-be type
2173 // name token, and we're done.
2174 const char *PrevSpec;
2175 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002176 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002177 DS.SetRangeEnd(Tok.getLocation());
2178 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002179 // There may be other declaration specifiers after this.
2180 return true;
2181 } else if (II != Tok.getIdentifierInfo()) {
2182 // If no type was suggested, the correction is to a keyword
2183 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002184 // There may be other declaration specifiers after this.
2185 return true;
2186 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002187
Douglas Gregora786fdb2009-10-13 23:27:22 +00002188 // Fall through; the action had no suggestion for us.
2189 } else {
2190 // The action did not emit a diagnostic, so emit one now.
2191 SourceRange R;
2192 if (SS) R = SS->getRange();
2193 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2194 }
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Douglas Gregora786fdb2009-10-13 23:27:22 +00002196 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002197 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002198 DS.SetRangeEnd(Tok.getLocation());
2199 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Chris Lattnere40c2952009-04-14 21:34:55 +00002201 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2202 // avoid rippling error messages on subsequent uses of the same type,
2203 // could be useful if #include was forgotten.
2204 return false;
2205}
2206
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002207/// \brief Determine the declaration specifier context from the declarator
2208/// context.
2209///
2210/// \param Context the declarator context, which is one of the
2211/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002212Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002213Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2214 if (Context == Declarator::MemberContext)
2215 return DSC_class;
2216 if (Context == Declarator::FileContext)
2217 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002218 if (Context == Declarator::TrailingReturnContext)
2219 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002220 return DSC_normal;
2221}
2222
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002223/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2224///
2225/// FIXME: Simply returns an alignof() expression if the argument is a
2226/// type. Ideally, the type should be propagated directly into Sema.
2227///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002228/// [C11] type-id
2229/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002230/// [C++0x] type-id ...[opt]
2231/// [C++0x] assignment-expression ...[opt]
2232ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2233 SourceLocation &EllipsisLoc) {
2234 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002235 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002236 SourceLocation TypeLoc = Tok.getLocation();
2237 ParsedType Ty = ParseTypeName().get();
2238 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002239 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2240 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002241 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002242 ER = ParseConstantExpression();
2243
Richard Smith80ad52f2013-01-02 11:42:31 +00002244 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002245 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002246
2247 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002248}
2249
2250/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2251/// attribute to Attrs.
2252///
2253/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002254/// [C11] '_Alignas' '(' type-id ')'
2255/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002256/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2257/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002258void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002259 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002260 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2261 "Not an alignment-specifier!");
2262
Richard Smith33f04a22013-01-29 01:48:07 +00002263 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2264 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002265
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002266 BalancedDelimiterTracker T(*this, tok::l_paren);
2267 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002268 return;
2269
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002270 SourceLocation EllipsisLoc;
2271 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002272 if (ArgExpr.isInvalid()) {
2273 SkipUntil(tok::r_paren);
2274 return;
2275 }
2276
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002277 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002278 if (EndLoc)
2279 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002280
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002281 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002282 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002283 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smithf6565a92013-02-22 08:32:16 +00002284 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002285}
2286
Reid Spencer5f016e22007-07-11 17:01:13 +00002287/// ParseDeclarationSpecifiers
2288/// declaration-specifiers: [C99 6.7]
2289/// storage-class-specifier declaration-specifiers[opt]
2290/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002291/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002292/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002293/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002294/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002295///
2296/// storage-class-specifier: [C99 6.7.1]
2297/// 'typedef'
2298/// 'extern'
2299/// 'static'
2300/// 'auto'
2301/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002302/// [C++] 'mutable'
Richard Smithec642442013-04-12 22:46:28 +00002303/// [C++11] 'thread_local'
2304/// [C11] '_Thread_local'
Reid Spencer5f016e22007-07-11 17:01:13 +00002305/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002306/// function-specifier: [C99 6.7.4]
2307/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002308/// [C++] 'virtual'
2309/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002310/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002311/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002312/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002313
Reid Spencer5f016e22007-07-11 17:01:13 +00002314///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002315void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002316 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002317 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002318 DeclSpecContext DSContext,
2319 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002320 if (DS.getSourceRange().isInvalid()) {
2321 DS.SetRangeStart(Tok.getLocation());
2322 DS.SetRangeEnd(Tok.getLocation());
2323 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002324
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002325 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002326 bool AttrsLastTime = false;
2327 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002329 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002331 unsigned DiagID = 0;
2332
Reid Spencer5f016e22007-07-11 17:01:13 +00002333 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002334
Reid Spencer5f016e22007-07-11 17:01:13 +00002335 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002336 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002337 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002338 if (!AttrsLastTime)
2339 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002340 else {
2341 // Reject C++11 attributes that appertain to decl specifiers as
2342 // we don't support any C++11 attributes that appertain to decl
2343 // specifiers. This also conforms to what g++ 4.8 is doing.
2344 ProhibitCXX11Attributes(attrs);
2345
Sean Hunt2edf0a22012-06-23 05:07:58 +00002346 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002347 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002348
Reid Spencer5f016e22007-07-11 17:01:13 +00002349 // If this is not a declaration specifier token, we're done reading decl
2350 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002351 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002352 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Sean Hunt2edf0a22012-06-23 05:07:58 +00002354 case tok::l_square:
2355 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002356 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002357 goto DoneWithDeclSpec;
2358
2359 ProhibitAttributes(attrs);
2360 // FIXME: It would be good to recover by accepting the attributes,
2361 // but attempting to do that now would cause serious
2362 // madness in terms of diagnostics.
2363 attrs.clear();
2364 attrs.Range = SourceRange();
2365
2366 ParseCXX11Attributes(attrs);
2367 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002368 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002369
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002370 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002371 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002372 if (DS.hasTypeSpecifier()) {
2373 bool AllowNonIdentifiers
2374 = (getCurScope()->getFlags() & (Scope::ControlScope |
2375 Scope::BlockScope |
2376 Scope::TemplateParamScope |
2377 Scope::FunctionPrototypeScope |
2378 Scope::AtCatchScope)) == 0;
2379 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002380 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002381 (DSContext == DSC_class && DS.isFriendSpecified());
2382
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002383 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002384 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002385 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002386 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002387 }
2388
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002389 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2390 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2391 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002392 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002393 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002394 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002395 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002396 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002397 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002398
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002399 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002400 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002401 }
2402
Chris Lattner5e02c472009-01-05 00:07:25 +00002403 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002404 // C++ scope specifier. Annotate and loop, or bail out on error.
2405 if (TryAnnotateCXXScopeToken(true)) {
2406 if (!DS.hasTypeSpecifier())
2407 DS.SetTypeSpecError();
2408 goto DoneWithDeclSpec;
2409 }
John McCall2e0a7152010-03-01 18:20:46 +00002410 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2411 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002412 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002413
2414 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002415 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002416 goto DoneWithDeclSpec;
2417
John McCallaa87d332009-12-12 11:40:51 +00002418 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002419 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2420 Tok.getAnnotationRange(),
2421 SS);
John McCallaa87d332009-12-12 11:40:51 +00002422
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002423 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002424 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002425 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002426 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002427 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002428 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002429
2430 // C++ [class.qual]p2:
2431 // In a lookup in which the constructor is an acceptable lookup
2432 // result and the nested-name-specifier nominates a class C:
2433 //
2434 // - if the name specified after the
2435 // nested-name-specifier, when looked up in C, is the
2436 // injected-class-name of C (Clause 9), or
2437 //
2438 // - if the name specified after the nested-name-specifier
2439 // is the same as the identifier or the
2440 // simple-template-id's template-name in the last
2441 // component of the nested-name-specifier,
2442 //
2443 // the name is instead considered to name the constructor of
2444 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002445 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002446 // Thus, if the template-name is actually the constructor
2447 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002448 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002449 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002450 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002451 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002452 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002453 if (isConstructorDeclarator()) {
2454 // The user meant this to be an out-of-line constructor
2455 // definition, but template arguments are not allowed
2456 // there. Just allow this as a constructor; we'll
2457 // complain about it later.
2458 goto DoneWithDeclSpec;
2459 }
2460
2461 // The user meant this to name a type, but it actually names
2462 // a constructor with some extraneous template
2463 // arguments. Complain, then parse it as a type as the user
2464 // intended.
2465 Diag(TemplateId->TemplateNameLoc,
2466 diag::err_out_of_line_template_id_names_constructor)
2467 << TemplateId->Name;
2468 }
2469
John McCallaa87d332009-12-12 11:40:51 +00002470 DS.getTypeSpecScope() = SS;
2471 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002472 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002473 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002474 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002475 continue;
2476 }
2477
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002478 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002479 DS.getTypeSpecScope() = SS;
2480 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002481 if (Tok.getAnnotationValue()) {
2482 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002483 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002484 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002485 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002486 if (isInvalid)
2487 break;
John McCallb3d87482010-08-24 05:47:05 +00002488 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002489 else
2490 DS.SetTypeSpecError();
2491 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2492 ConsumeToken(); // The typename
2493 }
2494
Douglas Gregor9135c722009-03-25 15:40:00 +00002495 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002496 goto DoneWithDeclSpec;
2497
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002498 // If we're in a context where the identifier could be a class name,
2499 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002500 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002501 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002502 &SS)) {
2503 if (isConstructorDeclarator())
2504 goto DoneWithDeclSpec;
2505
2506 // As noted in C++ [class.qual]p2 (cited above), when the name
2507 // of the class is qualified in a context where it could name
2508 // a constructor, its a constructor name. However, we've
2509 // looked at the declarator, and the user probably meant this
2510 // to be a type. Complain that it isn't supposed to be treated
2511 // as a type, then proceed to parse it as a type.
2512 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2513 << Next.getIdentifierInfo();
2514 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002515
John McCallb3d87482010-08-24 05:47:05 +00002516 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2517 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002518 getCurScope(), &SS,
2519 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002520 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002521 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002522
Chris Lattnerf4382f52009-04-14 22:17:06 +00002523 // If the referenced identifier is not a type, then this declspec is
2524 // erroneous: We already checked about that it has no type specifier, and
2525 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002526 // typename.
David Blaikie7247c882013-05-15 07:37:26 +00002527 if (!TypeRep) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002528 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002529 ParsedAttributesWithRange Attrs(AttrFactory);
2530 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2531 if (!Attrs.empty()) {
2532 AttrsLastTime = true;
2533 attrs.takeAllFrom(Attrs);
2534 }
2535 continue;
2536 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002537 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002538 }
Mike Stump1eb44332009-09-09 15:08:12 +00002539
John McCallaa87d332009-12-12 11:40:51 +00002540 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002541 ConsumeToken(); // The C++ scope.
2542
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002544 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002545 if (isInvalid)
2546 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002548 DS.SetRangeEnd(Tok.getLocation());
2549 ConsumeToken(); // The typename.
2550
2551 continue;
2552 }
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Chris Lattner80d0c892009-01-21 19:48:37 +00002554 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002555 if (Tok.getAnnotationValue()) {
2556 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002558 DiagID, T);
2559 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002560 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002561
Chris Lattner5c5db552010-04-05 18:18:31 +00002562 if (isInvalid)
2563 break;
2564
Chris Lattner80d0c892009-01-21 19:48:37 +00002565 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2566 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002567
Chris Lattner80d0c892009-01-21 19:48:37 +00002568 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2569 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002570 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002571 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002572 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002573
Chris Lattner80d0c892009-01-21 19:48:37 +00002574 continue;
2575 }
Mike Stump1eb44332009-09-09 15:08:12 +00002576
Douglas Gregorbfad9152011-04-28 15:48:45 +00002577 case tok::kw___is_signed:
2578 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2579 // typically treats it as a trait. If we see __is_signed as it appears
2580 // in libstdc++, e.g.,
2581 //
2582 // static const bool __is_signed;
2583 //
2584 // then treat __is_signed as an identifier rather than as a keyword.
2585 if (DS.getTypeSpecType() == TST_bool &&
2586 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2587 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2588 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2589 Tok.setKind(tok::identifier);
2590 }
2591
2592 // We're done with the declaration-specifiers.
2593 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002594
Chris Lattner3bd934a2008-07-26 01:18:38 +00002595 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002596 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002597 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002598 // In C++, check to see if this is a scope specifier like foo::bar::, if
2599 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002600 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002601 if (TryAnnotateCXXScopeToken(true)) {
2602 if (!DS.hasTypeSpecifier())
2603 DS.SetTypeSpecError();
2604 goto DoneWithDeclSpec;
2605 }
2606 if (!Tok.is(tok::identifier))
2607 continue;
2608 }
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Chris Lattner3bd934a2008-07-26 01:18:38 +00002610 // This identifier can only be a typedef name if we haven't already seen
2611 // a type-specifier. Without this check we misparse:
2612 // typedef int X; struct Y { short X; }; as 'short int'.
2613 if (DS.hasTypeSpecifier())
2614 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002615
John Thompson82287d12010-02-05 00:12:22 +00002616 // Check for need to substitute AltiVec keyword tokens.
2617 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2618 break;
2619
Richard Smithf63eee72012-05-09 18:56:43 +00002620 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2621 // allow the use of a typedef name as a type specifier.
2622 if (DS.isTypeAltiVecVector())
2623 goto DoneWithDeclSpec;
2624
John McCallb3d87482010-08-24 05:47:05 +00002625 ParsedType TypeRep =
2626 Actions.getTypeName(*Tok.getIdentifierInfo(),
2627 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002628
Chris Lattnerc199ab32009-04-12 20:42:31 +00002629 // If this is not a typedef name, don't parse it as part of the declspec,
2630 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002631 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002632 ParsedAttributesWithRange Attrs(AttrFactory);
2633 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2634 if (!Attrs.empty()) {
2635 AttrsLastTime = true;
2636 attrs.takeAllFrom(Attrs);
2637 }
2638 continue;
2639 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002640 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002641 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002642
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002643 // If we're in a context where the identifier could be a class name,
2644 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002645 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002646 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002647 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002648 goto DoneWithDeclSpec;
2649
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002650 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002651 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002652 if (isInvalid)
2653 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002654
Chris Lattner3bd934a2008-07-26 01:18:38 +00002655 DS.SetRangeEnd(Tok.getLocation());
2656 ConsumeToken(); // The identifier
2657
2658 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2659 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002660 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002661 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002662 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002663
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002664 // Need to support trailing type qualifiers (e.g. "id<p> const").
2665 // If a type specifier follows, it will be diagnosed elsewhere.
2666 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002667 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002668
2669 // type-name
2670 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002671 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002672 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002673 // This template-id does not refer to a type name, so we're
2674 // done with the type-specifiers.
2675 goto DoneWithDeclSpec;
2676 }
2677
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002678 // If we're in a context where the template-id could be a
2679 // constructor name or specialization, check whether this is a
2680 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002681 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002682 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002683 isConstructorDeclarator())
2684 goto DoneWithDeclSpec;
2685
Douglas Gregor39a8de12009-02-25 19:37:18 +00002686 // Turn the template-id annotation token into a type annotation
2687 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002688 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002689 continue;
2690 }
2691
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 // GNU attributes support.
2693 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002694 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002696
2697 // Microsoft declspec support.
2698 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002699 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002700 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Steve Naroff239f0732008-12-25 14:16:32 +00002702 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002703 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002704 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002705 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002706 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002707 // FIXME: This does not work correctly if it is set to be a declspec
2708 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002709 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002710 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002711 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002712 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002713
Aaron Ballmanaa9df092013-05-22 23:25:32 +00002714 case tok::kw___sptr:
2715 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00002716 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002717 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002718 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002719 case tok::kw___cdecl:
2720 case tok::kw___stdcall:
2721 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002722 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002723 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002724 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002725 continue;
2726
Dawn Perchik52fc3142010-09-03 01:29:35 +00002727 // Borland single token adornments.
2728 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002729 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002730 continue;
2731
Peter Collingbournef315fa82011-02-14 01:42:53 +00002732 // OpenCL single token adornments.
2733 case tok::kw___kernel:
2734 ParseOpenCLAttributes(DS.getAttributes());
2735 continue;
2736
Reid Spencer5f016e22007-07-11 17:01:13 +00002737 // storage-class-specifier
2738 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002739 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2740 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002741 break;
2742 case tok::kw_extern:
Richard Smithec642442013-04-12 22:46:28 +00002743 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002744 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002745 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2746 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002747 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002748 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002749 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2750 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002751 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002752 case tok::kw_static:
Richard Smithec642442013-04-12 22:46:28 +00002753 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002754 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002755 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2756 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002757 break;
2758 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002759 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002760 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002761 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2762 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002763 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002764 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002765 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002766 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2768 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002769 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002770 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2771 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002772 break;
2773 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002774 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2775 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002776 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002777 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002778 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2779 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002780 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002781 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00002782 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2783 PrevSpec, DiagID);
2784 break;
2785 case tok::kw_thread_local:
2786 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2787 PrevSpec, DiagID);
2788 break;
2789 case tok::kw__Thread_local:
2790 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2791 Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002792 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002793
Reid Spencer5f016e22007-07-11 17:01:13 +00002794 // function-specifier
2795 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002796 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002798 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002799 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002800 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002801 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002802 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002803 break;
Richard Smithde03c152013-01-17 22:16:11 +00002804 case tok::kw__Noreturn:
2805 if (!getLangOpts().C11)
2806 Diag(Loc, diag::ext_c11_noreturn);
2807 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2808 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002809
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002810 // alignment-specifier
2811 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002812 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002813 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002814 ParseAlignmentSpecifier(DS.getAttributes());
2815 continue;
2816
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002817 // friend
2818 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002819 if (DSContext == DSC_class)
2820 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2821 else {
2822 PrevSpec = ""; // not actually used by the diagnostic
2823 DiagID = diag::err_friend_invalid_in_context;
2824 isInvalid = true;
2825 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002826 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Douglas Gregor8d267c52011-09-09 02:06:17 +00002828 // Modules
2829 case tok::kw___module_private__:
2830 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2831 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002832
Sebastian Redl2ac67232009-11-05 15:47:02 +00002833 // constexpr
2834 case tok::kw_constexpr:
2835 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2836 break;
2837
Chris Lattner80d0c892009-01-21 19:48:37 +00002838 // type-specifier
2839 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002840 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2841 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002842 break;
2843 case tok::kw_long:
2844 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002845 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2846 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002847 else
John McCallfec54012009-08-03 20:12:06 +00002848 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2849 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002850 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002851 case tok::kw___int64:
2852 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2853 DiagID);
2854 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002855 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002856 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2857 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002858 break;
2859 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002860 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2861 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002862 break;
2863 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002864 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2865 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002866 break;
2867 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002868 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2869 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002870 break;
2871 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002872 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2873 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002874 break;
2875 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002876 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2877 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002878 break;
2879 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002880 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2881 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002882 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002883 case tok::kw___int128:
2884 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2885 DiagID);
2886 break;
2887 case tok::kw_half:
2888 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2889 DiagID);
2890 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002891 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002892 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2893 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002894 break;
2895 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002896 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2897 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002898 break;
2899 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2901 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002902 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002903 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002904 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2905 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002906 break;
2907 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2909 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002910 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002911 case tok::kw_bool:
2912 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002913 if (Tok.is(tok::kw_bool) &&
2914 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2915 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2916 PrevSpec = ""; // Not used by the diagnostic.
2917 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002918 // For better error recovery.
2919 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002920 isInvalid = true;
2921 } else {
2922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2923 DiagID);
2924 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002925 break;
2926 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002927 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2928 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002929 break;
2930 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002931 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2932 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002933 break;
2934 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002935 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2936 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002937 break;
John Thompson82287d12010-02-05 00:12:22 +00002938 case tok::kw___vector:
2939 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2940 break;
2941 case tok::kw___pixel:
2942 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2943 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002944 case tok::kw_image1d_t:
2945 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2946 PrevSpec, DiagID);
2947 break;
2948 case tok::kw_image1d_array_t:
2949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2950 PrevSpec, DiagID);
2951 break;
2952 case tok::kw_image1d_buffer_t:
2953 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2954 PrevSpec, DiagID);
2955 break;
2956 case tok::kw_image2d_t:
2957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2958 PrevSpec, DiagID);
2959 break;
2960 case tok::kw_image2d_array_t:
2961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2962 PrevSpec, DiagID);
2963 break;
2964 case tok::kw_image3d_t:
2965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2966 PrevSpec, DiagID);
2967 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002968 case tok::kw_sampler_t:
2969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2970 PrevSpec, DiagID);
2971 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002972 case tok::kw_event_t:
2973 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2974 PrevSpec, DiagID);
2975 break;
John McCalla5fc4722011-04-09 22:50:59 +00002976 case tok::kw___unknown_anytype:
2977 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2978 PrevSpec, DiagID);
2979 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002980
2981 // class-specifier:
2982 case tok::kw_class:
2983 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002984 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002985 case tok::kw_union: {
2986 tok::TokenKind Kind = Tok.getKind();
2987 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002988
2989 // These are attributes following class specifiers.
2990 // To produce better diagnostic, we parse them when
2991 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002992 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002993 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002994 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002995
2996 // If there are attributes following class specifier,
2997 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002998 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002999 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00003000 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003001 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003002 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00003003 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003004
3005 // enum-specifier:
3006 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00003007 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00003008 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00003009 continue;
3010
3011 // cv-qualifier:
3012 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003013 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003014 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003015 break;
3016 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003017 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003018 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003019 break;
3020 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003021 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003022 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003023 break;
3024
Douglas Gregord57959a2009-03-27 23:10:48 +00003025 // C++ typename-specifier:
3026 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00003027 if (TryAnnotateTypeOrScopeToken()) {
3028 DS.SetTypeSpecError();
3029 goto DoneWithDeclSpec;
3030 }
3031 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00003032 continue;
3033 break;
3034
Chris Lattner80d0c892009-01-21 19:48:37 +00003035 // GNU typeof support.
3036 case tok::kw_typeof:
3037 ParseTypeofSpecifier(DS);
3038 continue;
3039
David Blaikie42d6d0c2011-12-04 05:04:18 +00003040 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00003041 ParseDecltypeSpecifier(DS);
3042 continue;
3043
Sean Huntdb5d44b2011-05-19 05:37:45 +00003044 case tok::kw___underlying_type:
3045 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00003046 continue;
3047
3048 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003049 // C11 6.7.2.4/4:
3050 // If the _Atomic keyword is immediately followed by a left parenthesis,
3051 // it is interpreted as a type specifier (with a type name), not as a
3052 // type qualifier.
3053 if (NextToken().is(tok::l_paren)) {
3054 ParseAtomicSpecifier(DS);
3055 continue;
3056 }
3057 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3058 getLangOpts());
3059 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00003060
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003061 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003062 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003063 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003064 goto DoneWithDeclSpec;
3065 case tok::kw___private:
3066 case tok::kw___global:
3067 case tok::kw___local:
3068 case tok::kw___constant:
3069 case tok::kw___read_only:
3070 case tok::kw___write_only:
3071 case tok::kw___read_write:
3072 ParseOpenCLQualifiers(DS);
3073 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00003074
Steve Naroffd3ded1f2008-06-05 00:02:44 +00003075 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00003076 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00003077 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3078 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00003079 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00003080 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00003081
Douglas Gregor46f936e2010-11-19 17:10:50 +00003082 if (!ParseObjCProtocolQualifiers(DS))
3083 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3084 << FixItHint::CreateInsertion(Loc, "id")
3085 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00003086
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00003087 // Need to support trailing type qualifiers (e.g. "id<p> const").
3088 // If a type specifier follows, it will be diagnosed elsewhere.
3089 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00003090 }
John McCallfec54012009-08-03 20:12:06 +00003091 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00003092 if (isInvalid) {
3093 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00003094 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00003095
Douglas Gregorae2fb142010-08-23 14:34:43 +00003096 if (DiagID == diag::ext_duplicate_declspec)
3097 Diag(Tok, DiagID)
3098 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3099 else
3100 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003101 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00003102
Chris Lattner81c018d2008-03-13 06:29:04 +00003103 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003104 if (DiagID != diag::err_bool_redeclaration)
3105 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00003106
3107 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003108 }
3109}
Douglas Gregoradcac882008-12-01 23:54:00 +00003110
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003111/// ParseStructDeclaration - Parse a struct declaration without the terminating
3112/// semicolon.
3113///
Reid Spencer5f016e22007-07-11 17:01:13 +00003114/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003115/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003116/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003117/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003118/// struct-declarator-list:
3119/// struct-declarator
3120/// struct-declarator-list ',' struct-declarator
3121/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3122/// struct-declarator:
3123/// declarator
3124/// [GNU] declarator attributes[opt]
3125/// declarator[opt] ':' constant-expression
3126/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3127///
Chris Lattnere1359422008-04-10 06:46:29 +00003128void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003129ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003130
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003131 if (Tok.is(tok::kw___extension__)) {
3132 // __extension__ silences extension warnings in the subexpression.
3133 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003134 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003135 return ParseStructDeclaration(DS, Fields);
3136 }
Mike Stump1eb44332009-09-09 15:08:12 +00003137
Steve Naroff28a7ca82007-08-20 22:28:22 +00003138 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003139 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003140
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003141 // If there are no declarators, this is a free-standing declaration
3142 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003143 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003144 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3145 DS);
3146 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003147 return;
3148 }
3149
3150 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003151 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003152 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003153 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003154 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003155 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003156
Bill Wendlingad017fa2012-12-20 19:22:21 +00003157 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003158 if (!FirstDeclarator)
3159 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003160
Steve Naroff28a7ca82007-08-20 22:28:22 +00003161 /// struct-declarator: declarator
3162 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003163 if (Tok.isNot(tok::colon)) {
3164 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3165 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003166 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003167 }
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Chris Lattner04d66662007-10-09 17:33:22 +00003169 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003170 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003171 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003172 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003173 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003174 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003175 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003176 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003177
Steve Naroff28a7ca82007-08-20 22:28:22 +00003178 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003179 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003180
John McCallbdd563e2009-11-03 02:38:08 +00003181 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003182 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003183
Steve Naroff28a7ca82007-08-20 22:28:22 +00003184 // If we don't have a comma, it is either the end of the list (a ';')
3185 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003186 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003187 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003188
Steve Naroff28a7ca82007-08-20 22:28:22 +00003189 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003190 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003191
John McCallbdd563e2009-11-03 02:38:08 +00003192 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003193 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003194}
3195
3196/// ParseStructUnionBody
3197/// struct-contents:
3198/// struct-declaration-list
3199/// [EXT] empty
3200/// [GNU] "struct-declaration-list" without terminatoring ';'
3201/// struct-declaration-list:
3202/// struct-declaration
3203/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003204/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003205///
Reid Spencer5f016e22007-07-11 17:01:13 +00003206void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003207 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003208 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3209 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003210 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003211
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003212 BalancedDelimiterTracker T(*this, tok::l_brace);
3213 if (T.consumeOpen())
3214 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003215
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003216 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003217 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003218
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003219 // Empty structs are an extension in C (C99 6.7.2.1p7).
3220 if (Tok.is(tok::r_brace)) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003221 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3222 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3223 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003224
Chris Lattner5f9e2722011-07-23 10:55:15 +00003225 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003226
Reid Spencer5f016e22007-07-11 17:01:13 +00003227 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003228 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003229 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003230
Reid Spencer5f016e22007-07-11 17:01:13 +00003231 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003232 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003233 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 continue;
3235 }
Chris Lattnere1359422008-04-10 06:46:29 +00003236
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003237 // Parse _Static_assert declaration.
3238 if (Tok.is(tok::kw__Static_assert)) {
3239 SourceLocation DeclEnd;
3240 ParseStaticAssertDeclaration(DeclEnd);
3241 continue;
3242 }
3243
Argyrios Kyrtzidisbd957452013-04-18 01:42:35 +00003244 if (Tok.is(tok::annot_pragma_pack)) {
3245 HandlePragmaPack();
3246 continue;
3247 }
3248
3249 if (Tok.is(tok::annot_pragma_align)) {
3250 HandlePragmaAlign();
3251 continue;
3252 }
3253
John McCallbdd563e2009-11-03 02:38:08 +00003254 if (!Tok.is(tok::at)) {
3255 struct CFieldCallback : FieldCallback {
3256 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003257 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003258 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003259
John McCalld226f652010-08-21 09:40:31 +00003260 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003261 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003262 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3263
Eli Friedmandcdff462012-08-08 23:53:27 +00003264 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003265 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003266 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003267 FD.D.getDeclSpec().getSourceRange().getBegin(),
3268 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003269 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003270 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003271 }
John McCallbdd563e2009-11-03 02:38:08 +00003272 } Callback(*this, TagDecl, FieldDecls);
3273
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003274 // Parse all the comma separated declarators.
3275 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003276 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003277 } else { // Handle @defs
3278 ConsumeToken();
3279 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3280 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003281 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003282 continue;
3283 }
3284 ConsumeToken();
3285 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3286 if (!Tok.is(tok::identifier)) {
3287 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003288 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003289 continue;
3290 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003291 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003292 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003293 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003294 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3295 ConsumeToken();
3296 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003297 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003298
Chris Lattner04d66662007-10-09 17:33:22 +00003299 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003301 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003302 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 break;
3304 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003305 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3306 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003307 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003308 // If we stopped at a ';', eat it.
3309 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 }
3311 }
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003313 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003314
John McCall0b7e6782011-03-24 11:26:52 +00003315 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003316 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003317 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003318
Douglas Gregor23c94db2010-07-02 17:43:08 +00003319 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003320 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003321 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003322 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003323 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003324 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3325 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003326}
3327
Reid Spencer5f016e22007-07-11 17:01:13 +00003328/// ParseEnumSpecifier
3329/// enum-specifier: [C99 6.7.2.2]
3330/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003331///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003332/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3333/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003334/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3335/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003336/// 'enum' identifier
3337/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003338///
Richard Smith1af83c42012-03-23 03:33:32 +00003339/// [C++11] enum-head '{' enumerator-list[opt] '}'
3340/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003341///
Richard Smith1af83c42012-03-23 03:33:32 +00003342/// enum-head: [C++11]
3343/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3344/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3345/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003346///
Richard Smith1af83c42012-03-23 03:33:32 +00003347/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003348/// 'enum'
3349/// 'enum' 'class'
3350/// 'enum' 'struct'
3351///
Richard Smith1af83c42012-03-23 03:33:32 +00003352/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003353/// ':' type-specifier-seq
3354///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003355/// [C++] elaborated-type-specifier:
3356/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3357///
Chris Lattner4c97d762009-04-12 21:49:30 +00003358void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003359 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003360 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003361 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003362 if (Tok.is(tok::code_completion)) {
3363 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003364 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003365 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003366 }
John McCall57c13002011-07-06 05:58:41 +00003367
Sean Hunt2edf0a22012-06-23 05:07:58 +00003368 // If attributes exist after tag, parse them.
3369 ParsedAttributesWithRange attrs(AttrFactory);
3370 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003371 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003372
3373 // If declspecs exist after tag, parse them.
3374 while (Tok.is(tok::kw___declspec))
3375 ParseMicrosoftDeclSpec(attrs);
3376
Richard Smithbdad7a22012-01-10 01:33:14 +00003377 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003378 bool IsScopedUsingClassTag = false;
3379
John McCall1e12b3d2012-06-23 22:30:04 +00003380 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieued5a2922013-04-23 02:47:36 +00003381 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3382 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3383 : diag::ext_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003384 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003385 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003386
Bill Wendlingad017fa2012-12-20 19:22:21 +00003387 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003388 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003389 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003390
3391 // They are allowed afterwards, though.
3392 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003393 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003394 while (Tok.is(tok::kw___declspec))
3395 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003396 }
Richard Smith1af83c42012-03-23 03:33:32 +00003397
John McCall13489672012-05-07 06:16:58 +00003398 // C++11 [temp.explicit]p12:
3399 // The usual access controls do not apply to names used to specify
3400 // explicit instantiations.
3401 // We extend this to also cover explicit specializations. Note that
3402 // we don't suppress if this turns out to be an elaborated type
3403 // specifier.
3404 bool shouldDelayDiagsInTag =
3405 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3406 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3407 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003408
Richard Smith7796eb52012-03-12 08:56:40 +00003409 // Enum definitions should not be parsed in a trailing-return-type.
3410 bool AllowDeclaration = DSC != DSC_trailing;
3411
3412 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003413 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003414 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003415
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003416 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003417 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003418 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3419 // if a fixed underlying type is allowed.
3420 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003421
3422 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003423 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003424 return;
3425
3426 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003427 Diag(Tok, diag::err_expected_ident);
3428 if (Tok.isNot(tok::l_brace)) {
3429 // Has no name and is not a definition.
3430 // Skip the rest of this declarator, up until the comma or semicolon.
3431 SkipUntil(tok::comma, true);
3432 return;
3433 }
3434 }
3435 }
Mike Stump1eb44332009-09-09 15:08:12 +00003436
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003437 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003438 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003439 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003440 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003441
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003442 // Skip the rest of this declarator, up until the comma or semicolon.
3443 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003445 }
Mike Stump1eb44332009-09-09 15:08:12 +00003446
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003447 // If an identifier is present, consume and remember it.
3448 IdentifierInfo *Name = 0;
3449 SourceLocation NameLoc;
3450 if (Tok.is(tok::identifier)) {
3451 Name = Tok.getIdentifierInfo();
3452 NameLoc = ConsumeToken();
3453 }
Mike Stump1eb44332009-09-09 15:08:12 +00003454
Richard Smithbdad7a22012-01-10 01:33:14 +00003455 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003456 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3457 // declaration of a scoped enumeration.
3458 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003459 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003460 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003461 }
3462
John McCall13489672012-05-07 06:16:58 +00003463 // Okay, end the suppression area. We'll decide whether to emit the
3464 // diagnostics in a second.
3465 if (shouldDelayDiagsInTag)
3466 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003467
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003468 TypeResult BaseType;
3469
Douglas Gregora61b3e72010-12-01 17:42:47 +00003470 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003471 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003472 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003473 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003474 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003475 // If we're in class scope, this can either be an enum declaration with
3476 // an underlying type, or a declaration of a bitfield member. We try to
3477 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003478 // (integer literal, sizeof); if it's still ambiguous, we then consider
3479 // anything that's a simple-type-specifier followed by '(' as an
3480 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003481 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003482 EnterExpressionEvaluationContext Unevaluated(Actions,
3483 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003484 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003485 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003486 // bit-field. This is the common case.
3487 if (TPR == TPResult::True())
3488 PossibleBitfield = true;
3489 // If the next token starts a type-specifier-seq, it may be either a
3490 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003491 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003492 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003493 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003494 GetLookAheadToken(2).getKind() == tok::semi) {
3495 // Consume the ':'.
3496 ConsumeToken();
3497 } else {
3498 // We have the start of a type-specifier-seq, so we have to perform
3499 // tentative parsing to determine whether we have an expression or a
3500 // type.
3501 TentativeParsingAction TPA(*this);
3502
3503 // Consume the ':'.
3504 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003505
3506 // If we see a type specifier followed by an open-brace, we have an
3507 // ambiguity between an underlying type and a C++11 braced
3508 // function-style cast. Resolve this by always treating it as an
3509 // underlying type.
3510 // FIXME: The standard is not entirely clear on how to disambiguate in
3511 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003512 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003513 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003514 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003515 // We'll parse this as a bitfield later.
3516 PossibleBitfield = true;
3517 TPA.Revert();
3518 } else {
3519 // We have a type-specifier-seq.
3520 TPA.Commit();
3521 }
3522 }
3523 } else {
3524 // Consume the ':'.
3525 ConsumeToken();
3526 }
3527
3528 if (!PossibleBitfield) {
3529 SourceRange Range;
3530 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003531
Richard Smith80ad52f2013-01-02 11:42:31 +00003532 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003533 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003534 } else if (!getLangOpts().ObjC2) {
3535 if (getLangOpts().CPlusPlus)
3536 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3537 else
3538 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3539 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003540 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003541 }
3542
Richard Smithbdad7a22012-01-10 01:33:14 +00003543 // There are four options here. If we have 'friend enum foo;' then this is a
3544 // friend declaration, and cannot have an accompanying definition. If we have
3545 // 'enum foo;', then this is a forward declaration. If we have
3546 // 'enum foo {...' then this is a definition. Otherwise we have something
3547 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003548 //
3549 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3550 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3551 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3552 //
John McCallf312b1e2010-08-26 23:41:50 +00003553 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003554 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003555 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003556 } else if (Tok.is(tok::l_brace)) {
3557 if (DS.isFriendSpecified()) {
3558 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3559 << SourceRange(DS.getFriendSpecLoc());
3560 ConsumeBrace();
3561 SkipUntil(tok::r_brace);
3562 TUK = Sema::TUK_Friend;
3563 } else {
3564 TUK = Sema::TUK_Definition;
3565 }
Richard Smithc9f35172012-06-25 21:37:02 +00003566 } else if (DSC != DSC_type_specifier &&
3567 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003568 (Tok.isAtStartOfLine() &&
3569 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003570 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3571 if (Tok.isNot(tok::semi)) {
3572 // A semicolon was missing after this declaration. Diagnose and recover.
3573 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3574 "enum");
3575 PP.EnterToken(Tok);
3576 Tok.setKind(tok::semi);
3577 }
John McCall13489672012-05-07 06:16:58 +00003578 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003579 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003580 }
3581
3582 // If this is an elaborated type specifier, and we delayed
3583 // diagnostics before, just merge them into the current pool.
3584 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3585 diagsFromTag.redelay();
3586 }
Richard Smith1af83c42012-03-23 03:33:32 +00003587
3588 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003589 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003590 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003591 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003592 // Skip the rest of this declarator, up until the comma or semicolon.
3593 Diag(Tok, diag::err_enum_template);
3594 SkipUntil(tok::comma, true);
3595 return;
3596 }
3597
3598 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3599 // Enumerations can't be explicitly instantiated.
3600 DS.SetTypeSpecError();
3601 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3602 return;
3603 }
3604
3605 assert(TemplateInfo.TemplateParams && "no template parameters");
3606 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3607 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003608 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003609
Sean Hunt2edf0a22012-06-23 05:07:58 +00003610 if (TUK == Sema::TUK_Reference)
3611 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003612
Douglas Gregorb9075602011-02-22 02:55:24 +00003613 if (!Name && TUK != Sema::TUK_Definition) {
3614 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003615
Douglas Gregorb9075602011-02-22 02:55:24 +00003616 // Skip the rest of this declarator, up until the comma or semicolon.
3617 SkipUntil(tok::comma, true);
3618 return;
3619 }
Richard Smith1af83c42012-03-23 03:33:32 +00003620
Douglas Gregor402abb52009-05-28 23:31:59 +00003621 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003622 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003623 const char *PrevSpec = 0;
3624 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003625 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003626 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003627 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003628 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003629 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003630
Douglas Gregor48c89f42010-04-24 16:38:41 +00003631 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003632 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003633 // dependent tag.
3634 if (!Name) {
3635 DS.SetTypeSpecError();
3636 Diag(Tok, diag::err_expected_type_name_after_typename);
3637 return;
3638 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003639
Douglas Gregor23c94db2010-07-02 17:43:08 +00003640 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003641 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003642 NameLoc);
3643 if (Type.isInvalid()) {
3644 DS.SetTypeSpecError();
3645 return;
3646 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003647
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003648 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3649 NameLoc.isValid() ? NameLoc : StartLoc,
3650 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003651 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003652
Douglas Gregor48c89f42010-04-24 16:38:41 +00003653 return;
3654 }
Mike Stump1eb44332009-09-09 15:08:12 +00003655
John McCalld226f652010-08-21 09:40:31 +00003656 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003657 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003658 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003659 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003660 ConsumeBrace();
3661 SkipUntil(tok::r_brace);
3662 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003663
Douglas Gregor48c89f42010-04-24 16:38:41 +00003664 DS.SetTypeSpecError();
3665 return;
3666 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003667
Richard Smithc9f35172012-06-25 21:37:02 +00003668 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003669 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003670
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003671 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3672 NameLoc.isValid() ? NameLoc : StartLoc,
3673 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003674 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003675}
3676
3677/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3678/// enumerator-list:
3679/// enumerator
3680/// enumerator-list ',' enumerator
3681/// enumerator:
3682/// enumeration-constant
3683/// enumeration-constant '=' constant-expression
3684/// enumeration-constant:
3685/// identifier
3686///
John McCalld226f652010-08-21 09:40:31 +00003687void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003688 // Enter the scope of the enum body and start the definition.
3689 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003690 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003691
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003692 BalancedDelimiterTracker T(*this, tok::l_brace);
3693 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003694
Chris Lattner7946dd32007-08-27 17:24:30 +00003695 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003696 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003697 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003698
Chris Lattner5f9e2722011-07-23 10:55:15 +00003699 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003700
John McCalld226f652010-08-21 09:40:31 +00003701 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003702
Reid Spencer5f016e22007-07-11 17:01:13 +00003703 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003704 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003705 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3706 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003707
John McCall5b629aa2010-10-22 23:36:17 +00003708 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003709 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003710 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003711 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003712 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003713
Reid Spencer5f016e22007-07-11 17:01:13 +00003714 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003715 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003716 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003717
Chris Lattner04d66662007-10-09 17:33:22 +00003718 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003719 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003720 AssignedVal = ParseConstantExpression();
3721 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003722 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003723 }
Mike Stump1eb44332009-09-09 15:08:12 +00003724
Reid Spencer5f016e22007-07-11 17:01:13 +00003725 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003726 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3727 LastEnumConstDecl,
3728 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003729 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003730 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003731 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003732
Reid Spencer5f016e22007-07-11 17:01:13 +00003733 EnumConstantDecls.push_back(EnumConstDecl);
3734 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003735
Douglas Gregor751f6922010-09-07 14:51:08 +00003736 if (Tok.is(tok::identifier)) {
3737 // We're missing a comma between enumerators.
3738 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003739 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003740 << FixItHint::CreateInsertion(Loc, ", ");
3741 continue;
3742 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003743
Chris Lattner04d66662007-10-09 17:33:22 +00003744 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003745 break;
3746 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003747
Richard Smith7fe62082011-10-15 05:09:34 +00003748 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003749 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003750 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3751 diag::ext_enumerator_list_comma_cxx :
3752 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003753 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003754 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003755 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3756 << FixItHint::CreateRemoval(CommaLoc);
3757 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003758 }
Mike Stump1eb44332009-09-09 15:08:12 +00003759
Reid Spencer5f016e22007-07-11 17:01:13 +00003760 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003761 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003762
Reid Spencer5f016e22007-07-11 17:01:13 +00003763 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003764 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003765 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003766
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003767 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenko9ff2b422013-04-27 20:23:52 +00003768 EnumDecl, EnumConstantDecls,
3769 getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003770 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003771
Douglas Gregor72de6672009-01-08 20:45:30 +00003772 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003773 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3774 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003775
3776 // The next token must be valid after an enum definition. If not, a ';'
3777 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003778 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3779 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003780 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3781 // Push this token back into the preprocessor and change our current token
3782 // to ';' so that the rest of the code recovers as though there were an
3783 // ';' after the definition.
3784 PP.EnterToken(Tok);
3785 Tok.setKind(tok::semi);
3786 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003787}
3788
3789/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003790/// start of a type-qualifier-list.
3791bool Parser::isTypeQualifier() const {
3792 switch (Tok.getKind()) {
3793 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003794
3795 // type-qualifier only in OpenCL
3796 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003797 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003798
Steve Naroff5f8aa692008-02-11 23:15:56 +00003799 // type-qualifier
3800 case tok::kw_const:
3801 case tok::kw_volatile:
3802 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003803 case tok::kw___private:
3804 case tok::kw___local:
3805 case tok::kw___global:
3806 case tok::kw___constant:
3807 case tok::kw___read_only:
3808 case tok::kw___read_write:
3809 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003810 return true;
3811 }
3812}
3813
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003814/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3815/// is definitely a type-specifier. Return false if it isn't part of a type
3816/// specifier or if we're not sure.
3817bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3818 switch (Tok.getKind()) {
3819 default: return false;
3820 // type-specifiers
3821 case tok::kw_short:
3822 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003823 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003824 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003825 case tok::kw_signed:
3826 case tok::kw_unsigned:
3827 case tok::kw__Complex:
3828 case tok::kw__Imaginary:
3829 case tok::kw_void:
3830 case tok::kw_char:
3831 case tok::kw_wchar_t:
3832 case tok::kw_char16_t:
3833 case tok::kw_char32_t:
3834 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003835 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003836 case tok::kw_float:
3837 case tok::kw_double:
3838 case tok::kw_bool:
3839 case tok::kw__Bool:
3840 case tok::kw__Decimal32:
3841 case tok::kw__Decimal64:
3842 case tok::kw__Decimal128:
3843 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003844
Guy Benyeib13621d2012-12-18 14:38:23 +00003845 // OpenCL specific types:
3846 case tok::kw_image1d_t:
3847 case tok::kw_image1d_array_t:
3848 case tok::kw_image1d_buffer_t:
3849 case tok::kw_image2d_t:
3850 case tok::kw_image2d_array_t:
3851 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003852 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003853 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003854
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003855 // struct-or-union-specifier (C99) or class-specifier (C++)
3856 case tok::kw_class:
3857 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003858 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003859 case tok::kw_union:
3860 // enum-specifier
3861 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003862
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003863 // typedef-name
3864 case tok::annot_typename:
3865 return true;
3866 }
3867}
3868
Steve Naroff5f8aa692008-02-11 23:15:56 +00003869/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003870/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003871bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003872 switch (Tok.getKind()) {
3873 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003874
Chris Lattner166a8fc2009-01-04 23:41:41 +00003875 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003876 if (TryAltiVecVectorToken())
3877 return true;
3878 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003879 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003880 // Annotate typenames and C++ scope specifiers. If we get one, just
3881 // recurse to handle whatever we get.
3882 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003883 return true;
3884 if (Tok.is(tok::identifier))
3885 return false;
3886 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003887
Chris Lattner166a8fc2009-01-04 23:41:41 +00003888 case tok::coloncolon: // ::foo::bar
3889 if (NextToken().is(tok::kw_new) || // ::new
3890 NextToken().is(tok::kw_delete)) // ::delete
3891 return false;
3892
Chris Lattner166a8fc2009-01-04 23:41:41 +00003893 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003894 return true;
3895 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003896
Reid Spencer5f016e22007-07-11 17:01:13 +00003897 // GNU attributes support.
3898 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003899 // GNU typeof support.
3900 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003901
Reid Spencer5f016e22007-07-11 17:01:13 +00003902 // type-specifiers
3903 case tok::kw_short:
3904 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003905 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003906 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003907 case tok::kw_signed:
3908 case tok::kw_unsigned:
3909 case tok::kw__Complex:
3910 case tok::kw__Imaginary:
3911 case tok::kw_void:
3912 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003913 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003914 case tok::kw_char16_t:
3915 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003916 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003917 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003918 case tok::kw_float:
3919 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003920 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003921 case tok::kw__Bool:
3922 case tok::kw__Decimal32:
3923 case tok::kw__Decimal64:
3924 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003925 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003926
Guy Benyeib13621d2012-12-18 14:38:23 +00003927 // OpenCL specific types:
3928 case tok::kw_image1d_t:
3929 case tok::kw_image1d_array_t:
3930 case tok::kw_image1d_buffer_t:
3931 case tok::kw_image2d_t:
3932 case tok::kw_image2d_array_t:
3933 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003934 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003935 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003936
Chris Lattner99dc9142008-04-13 18:59:07 +00003937 // struct-or-union-specifier (C99) or class-specifier (C++)
3938 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003939 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003940 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003941 case tok::kw_union:
3942 // enum-specifier
3943 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003944
Reid Spencer5f016e22007-07-11 17:01:13 +00003945 // type-qualifier
3946 case tok::kw_const:
3947 case tok::kw_volatile:
3948 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003949
John McCallb8a8de32012-11-14 00:49:39 +00003950 // Debugger support.
3951 case tok::kw___unknown_anytype:
3952
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003953 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003954 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003955 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003956
Chris Lattner7c186be2008-10-20 00:25:30 +00003957 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3958 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003959 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003960
Steve Naroff239f0732008-12-25 14:16:32 +00003961 case tok::kw___cdecl:
3962 case tok::kw___stdcall:
3963 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003964 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003965 case tok::kw___w64:
3966 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003967 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003968 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003969 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003970
3971 case tok::kw___private:
3972 case tok::kw___local:
3973 case tok::kw___global:
3974 case tok::kw___constant:
3975 case tok::kw___read_only:
3976 case tok::kw___read_write:
3977 case tok::kw___write_only:
3978
Eli Friedman290eeb02009-06-08 23:27:34 +00003979 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003980
3981 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003982 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003983
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003984 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00003985 case tok::kw__Atomic:
3986 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003987 }
3988}
3989
3990/// isDeclarationSpecifier() - Return true if the current token is part of a
3991/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003992///
3993/// \param DisambiguatingWithExpression True to indicate that the purpose of
3994/// this check is to disambiguate between an expression and a declaration.
3995bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003996 switch (Tok.getKind()) {
3997 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003998
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003999 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004000 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004001
Chris Lattner166a8fc2009-01-04 23:41:41 +00004002 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00004003 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00004004 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00004005 return false;
John Thompson82287d12010-02-05 00:12:22 +00004006 if (TryAltiVecVectorToken())
4007 return true;
4008 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004009 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00004010 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004011 // Annotate typenames and C++ scope specifiers. If we get one, just
4012 // recurse to handle whatever we get.
4013 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004014 return true;
4015 if (Tok.is(tok::identifier))
4016 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004017
Douglas Gregor9497a732010-09-16 01:51:54 +00004018 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00004019 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00004020 // expression is permitted, then this is probably a class message send
4021 // missing the initial '['. In this case, we won't consider this to be
4022 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00004023 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00004024 isStartOfObjCClassMessageMissingOpenBracket())
4025 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004026
John McCall9ba61662010-02-26 08:45:28 +00004027 return isDeclarationSpecifier();
4028
Chris Lattner166a8fc2009-01-04 23:41:41 +00004029 case tok::coloncolon: // ::foo::bar
4030 if (NextToken().is(tok::kw_new) || // ::new
4031 NextToken().is(tok::kw_delete)) // ::delete
4032 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004033
Chris Lattner166a8fc2009-01-04 23:41:41 +00004034 // Annotate typenames and C++ scope specifiers. If we get one, just
4035 // recurse to handle whatever we get.
4036 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004037 return true;
4038 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004039
Reid Spencer5f016e22007-07-11 17:01:13 +00004040 // storage-class-specifier
4041 case tok::kw_typedef:
4042 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00004043 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00004044 case tok::kw_static:
4045 case tok::kw_auto:
4046 case tok::kw_register:
4047 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00004048 case tok::kw_thread_local:
4049 case tok::kw__Thread_local:
Mike Stump1eb44332009-09-09 15:08:12 +00004050
Douglas Gregor8d267c52011-09-09 02:06:17 +00004051 // Modules
4052 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00004053
John McCallb8a8de32012-11-14 00:49:39 +00004054 // Debugger support
4055 case tok::kw___unknown_anytype:
4056
Reid Spencer5f016e22007-07-11 17:01:13 +00004057 // type-specifiers
4058 case tok::kw_short:
4059 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004060 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004061 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004062 case tok::kw_signed:
4063 case tok::kw_unsigned:
4064 case tok::kw__Complex:
4065 case tok::kw__Imaginary:
4066 case tok::kw_void:
4067 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004068 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004069 case tok::kw_char16_t:
4070 case tok::kw_char32_t:
4071
Reid Spencer5f016e22007-07-11 17:01:13 +00004072 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004073 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004074 case tok::kw_float:
4075 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004076 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004077 case tok::kw__Bool:
4078 case tok::kw__Decimal32:
4079 case tok::kw__Decimal64:
4080 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004081 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004082
Guy Benyeib13621d2012-12-18 14:38:23 +00004083 // OpenCL specific types:
4084 case tok::kw_image1d_t:
4085 case tok::kw_image1d_array_t:
4086 case tok::kw_image1d_buffer_t:
4087 case tok::kw_image2d_t:
4088 case tok::kw_image2d_array_t:
4089 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00004090 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00004091 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00004092
Chris Lattner99dc9142008-04-13 18:59:07 +00004093 // struct-or-union-specifier (C99) or class-specifier (C++)
4094 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004095 case tok::kw_struct:
4096 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00004097 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004098 // enum-specifier
4099 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004100
Reid Spencer5f016e22007-07-11 17:01:13 +00004101 // type-qualifier
4102 case tok::kw_const:
4103 case tok::kw_volatile:
4104 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004105
Reid Spencer5f016e22007-07-11 17:01:13 +00004106 // function-specifier
4107 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00004108 case tok::kw_virtual:
4109 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00004110 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004111
Richard Smith4cd81c52013-01-29 09:02:09 +00004112 // alignment-specifier
4113 case tok::kw__Alignas:
4114
Richard Smith53aec2a2012-10-25 00:00:53 +00004115 // friend keyword.
4116 case tok::kw_friend:
4117
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00004118 // static_assert-declaration
4119 case tok::kw__Static_assert:
4120
Chris Lattner1ef08762007-08-09 17:01:07 +00004121 // GNU typeof support.
4122 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004123
Chris Lattner1ef08762007-08-09 17:01:07 +00004124 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004125 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00004126
Richard Smith53aec2a2012-10-25 00:00:53 +00004127 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004128 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00004129 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00004130
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004131 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004132 case tok::kw__Atomic:
4133 return true;
4134
Chris Lattnerf3948c42008-07-26 03:38:44 +00004135 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4136 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004137 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004138
Douglas Gregord9d75e52011-04-27 05:41:15 +00004139 // typedef-name
4140 case tok::annot_typename:
4141 return !DisambiguatingWithExpression ||
4142 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00004143
Steve Naroff47f52092009-01-06 19:34:12 +00004144 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004145 case tok::kw___cdecl:
4146 case tok::kw___stdcall:
4147 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004148 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004149 case tok::kw___w64:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004150 case tok::kw___sptr:
4151 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004152 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004153 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004154 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004155 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004156 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004157
4158 case tok::kw___private:
4159 case tok::kw___local:
4160 case tok::kw___global:
4161 case tok::kw___constant:
4162 case tok::kw___read_only:
4163 case tok::kw___read_write:
4164 case tok::kw___write_only:
4165
Eli Friedman290eeb02009-06-08 23:27:34 +00004166 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004167 }
4168}
4169
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004170bool Parser::isConstructorDeclarator() {
4171 TentativeParsingAction TPA(*this);
4172
4173 // Parse the C++ scope specifier.
4174 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004175 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004176 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004177 TPA.Revert();
4178 return false;
4179 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004180
4181 // Parse the constructor name.
4182 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4183 // We already know that we have a constructor name; just consume
4184 // the token.
4185 ConsumeToken();
4186 } else {
4187 TPA.Revert();
4188 return false;
4189 }
4190
Richard Smith22592862012-03-27 23:05:05 +00004191 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004192 if (Tok.isNot(tok::l_paren)) {
4193 TPA.Revert();
4194 return false;
4195 }
4196 ConsumeParen();
4197
Richard Smith22592862012-03-27 23:05:05 +00004198 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4199 // that we have a constructor.
4200 if (Tok.is(tok::r_paren) ||
4201 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004202 TPA.Revert();
4203 return true;
4204 }
4205
4206 // If we need to, enter the specified scope.
4207 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004208 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004209 DeclScopeObj.EnterDeclaratorScope();
4210
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004211 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004212 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004213 MaybeParseMicrosoftAttributes(Attrs);
4214
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004215 // Check whether the next token(s) are part of a declaration
4216 // specifier, in which case we have the start of a parameter and,
4217 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004218 bool IsConstructor = false;
4219 if (isDeclarationSpecifier())
4220 IsConstructor = true;
4221 else if (Tok.is(tok::identifier) ||
4222 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4223 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4224 // This might be a parenthesized member name, but is more likely to
4225 // be a constructor declaration with an invalid argument type. Keep
4226 // looking.
4227 if (Tok.is(tok::annot_cxxscope))
4228 ConsumeToken();
4229 ConsumeToken();
4230
4231 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004232 // which must have one of the following syntactic forms (see the
4233 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004234 switch (Tok.getKind()) {
4235 case tok::l_paren:
4236 // C(X ( int));
4237 case tok::l_square:
4238 // C(X [ 5]);
4239 // C(X [ [attribute]]);
4240 case tok::coloncolon:
4241 // C(X :: Y);
4242 // C(X :: *p);
4243 case tok::r_paren:
4244 // C(X )
4245 // Assume this isn't a constructor, rather than assuming it's a
4246 // constructor with an unnamed parameter of an ill-formed type.
4247 break;
4248
4249 default:
4250 IsConstructor = true;
4251 break;
4252 }
4253 }
4254
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004255 TPA.Revert();
4256 return IsConstructor;
4257}
Reid Spencer5f016e22007-07-11 17:01:13 +00004258
4259/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004260/// type-qualifier-list: [C99 6.7.5]
4261/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004262/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004263/// [ only if VendorAttributesAllowed=true ]
4264/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004265/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004266/// [ only if VendorAttributesAllowed=true ]
4267/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004268/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004269/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004270///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004271void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4272 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004273 bool CXX11AttributesAllowed,
4274 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004275 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004276 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004277 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004278 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004279 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004280 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004281
4282 SourceLocation EndLoc;
4283
Reid Spencer5f016e22007-07-11 17:01:13 +00004284 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004285 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004286 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004287 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004288 SourceLocation Loc = Tok.getLocation();
4289
4290 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004291 case tok::code_completion:
4292 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004293 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004294
Reid Spencer5f016e22007-07-11 17:01:13 +00004295 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004296 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004297 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004298 break;
4299 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004300 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004301 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004302 break;
4303 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004304 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004305 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004306 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004307 case tok::kw__Atomic:
4308 if (!AtomicAllowed)
4309 goto DoneWithTypeQuals;
4310 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4311 getLangOpts());
4312 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004313
4314 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004315 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004316 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004317 goto DoneWithTypeQuals;
4318 case tok::kw___private:
4319 case tok::kw___global:
4320 case tok::kw___local:
4321 case tok::kw___constant:
4322 case tok::kw___read_only:
4323 case tok::kw___write_only:
4324 case tok::kw___read_write:
4325 ParseOpenCLQualifiers(DS);
4326 break;
4327
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004328 case tok::kw___sptr:
4329 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004330 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004331 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004332 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004333 case tok::kw___cdecl:
4334 case tok::kw___stdcall:
4335 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004336 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004337 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004338 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004339 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004340 continue;
4341 }
4342 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004343 case tok::kw___pascal:
4344 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004345 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004346 continue;
4347 }
4348 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004349 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004350 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004351 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004352 continue; // do *not* consume the next token!
4353 }
4354 // otherwise, FALL THROUGH!
4355 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004356 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004357 // If this is not a type-qualifier token, we're done reading type
4358 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004359 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004360 if (EndLoc.isValid())
4361 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004362 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004363 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004364
Reid Spencer5f016e22007-07-11 17:01:13 +00004365 // If the specifier combination wasn't legal, issue a diagnostic.
4366 if (isInvalid) {
4367 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004368 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004369 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004370 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004371 }
4372}
4373
4374
4375/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4376///
4377void Parser::ParseDeclarator(Declarator &D) {
4378 /// This implements the 'declarator' production in the C grammar, then checks
4379 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004380 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004381}
4382
Richard Smith9988f282012-03-29 01:16:42 +00004383static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4384 if (Kind == tok::star || Kind == tok::caret)
4385 return true;
4386
4387 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4388 if (!Lang.CPlusPlus)
4389 return false;
4390
4391 return Kind == tok::amp || Kind == tok::ampamp;
4392}
4393
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004394/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4395/// is parsed by the function passed to it. Pass null, and the direct-declarator
4396/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004397/// ptr-operator production.
4398///
Richard Smith0706df42011-10-19 21:33:05 +00004399/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004400/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4401/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004402///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004403/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4404/// [C] pointer[opt] direct-declarator
4405/// [C++] direct-declarator
4406/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004407///
4408/// pointer: [C99 6.7.5]
4409/// '*' type-qualifier-list[opt]
4410/// '*' type-qualifier-list[opt] pointer
4411///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004412/// ptr-operator:
4413/// '*' cv-qualifier-seq[opt]
4414/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004415/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004416/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004417/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004418/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004419void Parser::ParseDeclaratorInternal(Declarator &D,
4420 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004421 if (Diags.hasAllExtensionsSilenced())
4422 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004423
Sebastian Redlf30208a2009-01-24 21:16:55 +00004424 // C++ member pointers start with a '::' or a nested-name.
4425 // Member pointers get special handling, since there's no place for the
4426 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004427 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004428 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4429 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004430 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4431 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004432 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004433 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004434
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004435 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004436 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004437 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004438 if (D.mayHaveIdentifier())
4439 D.getCXXScopeSpec() = SS;
4440 else
4441 AnnotateScopeToken(SS, true);
4442
Sebastian Redlf30208a2009-01-24 21:16:55 +00004443 if (DirectDeclParser)
4444 (this->*DirectDeclParser)(D);
4445 return;
4446 }
4447
4448 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004449 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004450 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004451 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004452 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004453
4454 // Recurse to parse whatever is left.
4455 ParseDeclaratorInternal(D, DirectDeclParser);
4456
4457 // Sema will have to catch (syntactically invalid) pointers into global
4458 // scope. It has to catch pointers into namespace scope anyway.
4459 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004460 Loc),
4461 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004462 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004463 return;
4464 }
4465 }
4466
4467 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004468 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004469 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004470 if (DirectDeclParser)
4471 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004472 return;
4473 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004474
Sebastian Redl05532f22009-03-15 22:02:01 +00004475 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4476 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004477 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004478 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004479
Chris Lattner9af55002009-03-27 04:18:06 +00004480 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004481 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004482 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004483
Richard Smith6ee326a2012-04-10 01:32:12 +00004484 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004485 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004486 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004487
Reid Spencer5f016e22007-07-11 17:01:13 +00004488 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004489 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004490 if (Kind == tok::star)
4491 // Remember that we parsed a pointer type, and remember the type-quals.
4492 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004493 DS.getConstSpecLoc(),
4494 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004495 DS.getRestrictSpecLoc()),
4496 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004497 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004498 else
4499 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004500 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004501 Loc),
4502 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004503 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004504 } else {
4505 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004506 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004507
Sebastian Redl743de1f2009-03-23 00:00:23 +00004508 // Complain about rvalue references in C++03, but then go on and build
4509 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004510 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004511 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004512 diag::warn_cxx98_compat_rvalue_reference :
4513 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004514
Richard Smith6ee326a2012-04-10 01:32:12 +00004515 // GNU-style and C++11 attributes are allowed here, as is restrict.
4516 ParseTypeQualifierListOpt(DS);
4517 D.ExtendWithDeclSpec(DS);
4518
Reid Spencer5f016e22007-07-11 17:01:13 +00004519 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4520 // cv-qualifiers are introduced through the use of a typedef or of a
4521 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004522 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4523 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4524 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004525 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004526 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4527 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004528 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004529 // 'restrict' is permitted as an extension.
4530 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4531 Diag(DS.getAtomicSpecLoc(),
4532 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004533 }
4534
4535 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004536 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004537
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004538 if (D.getNumTypeObjects() > 0) {
4539 // C++ [dcl.ref]p4: There shall be no references to references.
4540 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4541 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004542 if (const IdentifierInfo *II = D.getIdentifier())
4543 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4544 << II;
4545 else
4546 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4547 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004548
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004549 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004550 // can go ahead and build the (technically ill-formed)
4551 // declarator: reference collapsing will take care of it.
4552 }
4553 }
4554
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004555 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004556 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004557 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004558 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004559 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004560 }
4561}
4562
Richard Smith9988f282012-03-29 01:16:42 +00004563static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4564 SourceLocation EllipsisLoc) {
4565 if (EllipsisLoc.isValid()) {
4566 FixItHint Insertion;
4567 if (!D.getEllipsisLoc().isValid()) {
4568 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4569 D.setEllipsisLoc(EllipsisLoc);
4570 }
4571 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4572 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4573 }
4574}
4575
Reid Spencer5f016e22007-07-11 17:01:13 +00004576/// ParseDirectDeclarator
4577/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004578/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004579/// '(' declarator ')'
4580/// [GNU] '(' attributes declarator ')'
4581/// [C90] direct-declarator '[' constant-expression[opt] ']'
4582/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4583/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4584/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4585/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004586/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4587/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004588/// direct-declarator '(' parameter-type-list ')'
4589/// direct-declarator '(' identifier-list[opt] ')'
4590/// [GNU] direct-declarator '(' parameter-forward-declarations
4591/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004592/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4593/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004594/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4595/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4596/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004597/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004598/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004599///
4600/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004601/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004602/// '::'[opt] nested-name-specifier[opt] type-name
4603///
4604/// id-expression: [C++ 5.1]
4605/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004606/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004607///
4608/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004609/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004610/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004611/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004612/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004613/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004614///
Richard Smith5d8388c2012-03-27 01:42:32 +00004615/// Note, any additional constructs added here may need corresponding changes
4616/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004617void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004618 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004619
David Blaikie4e4d0842012-03-11 07:00:24 +00004620 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004621 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004622 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004623 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4624 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004625 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004626 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004627 }
4628
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004629 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004630 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004631 // Change the declaration context for name lookup, until this function
4632 // is exited (and the declarator has been parsed).
4633 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004634 }
4635
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004636 // C++0x [dcl.fct]p14:
4637 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004638 // of a parameter-declaration-clause without a preceding comma. In
4639 // this case, the ellipsis is parsed as part of the
4640 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004641 // parameter pack that has not been expanded; otherwise, it is parsed
4642 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004643 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004644 !((D.getContext() == Declarator::PrototypeContext ||
4645 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004646 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004647 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004648 !Actions.containsUnexpandedParameterPacks(D))) {
4649 SourceLocation EllipsisLoc = ConsumeToken();
4650 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4651 // The ellipsis was put in the wrong place. Recover, and explain to
4652 // the user what they should have done.
4653 ParseDeclarator(D);
4654 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4655 return;
4656 } else
4657 D.setEllipsisLoc(EllipsisLoc);
4658
4659 // The ellipsis can't be followed by a parenthesized declarator. We
4660 // check for that in ParseParenDeclarator, after we have disambiguated
4661 // the l_paren token.
4662 }
4663
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004664 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4665 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4666 // We found something that indicates the start of an unqualified-id.
4667 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004668 bool AllowConstructorName;
4669 if (D.getDeclSpec().hasTypeSpecifier())
4670 AllowConstructorName = false;
4671 else if (D.getCXXScopeSpec().isSet())
4672 AllowConstructorName =
4673 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004674 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004675 else
4676 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4677
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004678 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004679 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4680 /*EnteringContext=*/true,
4681 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004682 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004683 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004684 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004685 D.getName()) ||
4686 // Once we're past the identifier, if the scope was bad, mark the
4687 // whole declarator bad.
4688 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004689 D.SetIdentifier(0, Tok.getLocation());
4690 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004691 } else {
4692 // Parsed the unqualified-id; update range information and move along.
4693 if (D.getSourceRange().getBegin().isInvalid())
4694 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4695 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004696 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004697 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004698 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004699 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004700 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004701 "There's a C++-specific check for tok::identifier above");
4702 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4703 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4704 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004705 goto PastIdentifier;
4706 }
Richard Smith9988f282012-03-29 01:16:42 +00004707
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004708 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004709 // direct-declarator: '(' declarator ')'
4710 // direct-declarator: '(' attributes declarator ')'
4711 // Example: 'char (*X)' or 'int (*XX)(void)'
4712 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004713
4714 // If the declarator was parenthesized, we entered the declarator
4715 // scope when parsing the parenthesized declarator, then exited
4716 // the scope already. Re-enter the scope, if we need to.
4717 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004718 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004719 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004720 if (!D.isInvalidType() &&
4721 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004722 // Change the declaration context for name lookup, until this function
4723 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004724 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004725 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004726 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004727 // This could be something simple like "int" (in which case the declarator
4728 // portion is empty), if an abstract-declarator is allowed.
4729 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004730
4731 // The grammar for abstract-pack-declarator does not allow grouping parens.
4732 // FIXME: Revisit this once core issue 1488 is resolved.
4733 if (D.hasEllipsis() && D.hasGroupingParens())
4734 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4735 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004736 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004737 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004738 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004739 if (D.getContext() == Declarator::MemberContext)
4740 Diag(Tok, diag::err_expected_member_name_or_semi)
4741 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004742 else if (getLangOpts().CPlusPlus) {
4743 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4744 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4745 else
4746 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4747 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004748 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004749 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004750 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004751 }
Mike Stump1eb44332009-09-09 15:08:12 +00004752
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004753 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004754 assert(D.isPastIdentifier() &&
4755 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004756
Richard Smith6ee326a2012-04-10 01:32:12 +00004757 // Don't parse attributes unless we have parsed an unparenthesized name.
4758 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004759 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004760
Reid Spencer5f016e22007-07-11 17:01:13 +00004761 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004762 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004763 // Enter function-declaration scope, limiting any declarators to the
4764 // function prototype scope, including parameter declarators.
4765 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004766 Scope::FunctionPrototypeScope|Scope::DeclScope|
4767 (D.isFunctionDeclaratorAFunctionDeclaration()
4768 ? Scope::FunctionDeclarationScope : 0));
4769
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004770 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4771 // In such a case, check if we actually have a function declarator; if it
4772 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004773 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004774 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4775 // The name of the declarator, if any, is tentatively declared within
4776 // a possible direct initializer.
4777 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4778 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4779 TentativelyDeclaredIdentifiers.pop_back();
4780 if (!IsFunctionDecl)
4781 break;
4782 }
John McCall0b7e6782011-03-24 11:26:52 +00004783 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004784 BalancedDelimiterTracker T(*this, tok::l_paren);
4785 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004786 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004787 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004788 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004789 ParseBracketDeclarator(D);
4790 } else {
4791 break;
4792 }
4793 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004794}
Reid Spencer5f016e22007-07-11 17:01:13 +00004795
Chris Lattneref4715c2008-04-06 05:45:57 +00004796/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4797/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004798/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004799/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4800///
4801/// direct-declarator:
4802/// '(' declarator ')'
4803/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004804/// direct-declarator '(' parameter-type-list ')'
4805/// direct-declarator '(' identifier-list[opt] ')'
4806/// [GNU] direct-declarator '(' parameter-forward-declarations
4807/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004808///
4809void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004810 BalancedDelimiterTracker T(*this, tok::l_paren);
4811 T.consumeOpen();
4812
Chris Lattneref4715c2008-04-06 05:45:57 +00004813 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004814
Chris Lattner7399ee02008-10-20 02:05:46 +00004815 // Eat any attributes before we look at whether this is a grouping or function
4816 // declarator paren. If this is a grouping paren, the attribute applies to
4817 // the type being built up, for example:
4818 // int (__attribute__(()) *x)(long y)
4819 // If this ends up not being a grouping paren, the attribute applies to the
4820 // first argument, for example:
4821 // int (__attribute__(()) int x)
4822 // In either case, we need to eat any attributes to be able to determine what
4823 // sort of paren this is.
4824 //
John McCall0b7e6782011-03-24 11:26:52 +00004825 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004826 bool RequiresArg = false;
4827 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004828 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004829
Chris Lattner7399ee02008-10-20 02:05:46 +00004830 // We require that the argument list (if this is a non-grouping paren) be
4831 // present even if the attribute list was empty.
4832 RequiresArg = true;
4833 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004834
Steve Naroff239f0732008-12-25 14:16:32 +00004835 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004836 ParseMicrosoftTypeAttributes(attrs);
4837
Dawn Perchik52fc3142010-09-03 01:29:35 +00004838 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004839 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004840 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004841
Chris Lattneref4715c2008-04-06 05:45:57 +00004842 // If we haven't past the identifier yet (or where the identifier would be
4843 // stored, if this is an abstract declarator), then this is probably just
4844 // grouping parens. However, if this could be an abstract-declarator, then
4845 // this could also be the start of function arguments (consider 'void()').
4846 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004847
Chris Lattneref4715c2008-04-06 05:45:57 +00004848 if (!D.mayOmitIdentifier()) {
4849 // If this can't be an abstract-declarator, this *must* be a grouping
4850 // paren, because we haven't seen the identifier yet.
4851 isGrouping = true;
4852 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004853 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4854 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004855 isDeclarationSpecifier() || // 'int(int)' is a function.
4856 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004857 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4858 // considered to be a type, not a K&R identifier-list.
4859 isGrouping = false;
4860 } else {
4861 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4862 isGrouping = true;
4863 }
Mike Stump1eb44332009-09-09 15:08:12 +00004864
Chris Lattneref4715c2008-04-06 05:45:57 +00004865 // If this is a grouping paren, handle:
4866 // direct-declarator: '(' declarator ')'
4867 // direct-declarator: '(' attributes declarator ')'
4868 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004869 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4870 D.setEllipsisLoc(SourceLocation());
4871
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004872 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004873 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004874 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004875 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004876 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004877 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004878 T.getCloseLocation()),
4879 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004880
4881 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004882
4883 // An ellipsis cannot be placed outside parentheses.
4884 if (EllipsisLoc.isValid())
4885 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4886
Chris Lattneref4715c2008-04-06 05:45:57 +00004887 return;
4888 }
Mike Stump1eb44332009-09-09 15:08:12 +00004889
Chris Lattneref4715c2008-04-06 05:45:57 +00004890 // Okay, if this wasn't a grouping paren, it must be the start of a function
4891 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004892 // identifier (and remember where it would have been), then call into
4893 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004894 D.SetIdentifier(0, Tok.getLocation());
4895
David Blaikie42d6d0c2011-12-04 05:04:18 +00004896 // Enter function-declaration scope, limiting any declarators to the
4897 // function prototype scope, including parameter declarators.
4898 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004899 Scope::FunctionPrototypeScope | Scope::DeclScope |
4900 (D.isFunctionDeclaratorAFunctionDeclaration()
4901 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004902 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004903 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004904}
4905
4906/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4907/// declarator D up to a paren, which indicates that we are parsing function
4908/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004909///
Richard Smith6ee326a2012-04-10 01:32:12 +00004910/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4911/// immediately after the open paren - they should be considered to be the
4912/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004913///
Richard Smith6ee326a2012-04-10 01:32:12 +00004914/// If RequiresArg is true, then the first argument of the function is required
4915/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004916///
Richard Smith6ee326a2012-04-10 01:32:12 +00004917/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4918/// (C++11) ref-qualifier[opt], exception-specification[opt],
4919/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4920///
4921/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004922/// dynamic-exception-specification
4923/// noexcept-specification
4924///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004925void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004926 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004927 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004928 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004929 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004930 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004931 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004932 // lparen is already consumed!
4933 assert(D.isPastIdentifier() && "Should not call before identifier!");
4934
4935 // This should be true when the function has typed arguments.
4936 // Otherwise, it is treated as a K&R-style function.
4937 bool HasProto = false;
4938 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004939 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004940 // Remember where we see an ellipsis, if any.
4941 SourceLocation EllipsisLoc;
4942
4943 DeclSpec DS(AttrFactory);
4944 bool RefQualifierIsLValueRef = true;
4945 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004946 SourceLocation ConstQualifierLoc;
4947 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004948 ExceptionSpecificationType ESpecType = EST_None;
4949 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004950 SmallVector<ParsedType, 2> DynamicExceptions;
4951 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004952 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004953 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004954 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004955
James Molloy16f1f712012-02-29 10:24:19 +00004956 Actions.ActOnStartFunctionDeclarator();
4957
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004958 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4959 EndLoc is the end location for the function declarator.
4960 They differ for trailing return types. */
4961 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004962 SourceLocation LParenLoc, RParenLoc;
4963 LParenLoc = Tracker.getOpenLocation();
4964 StartLoc = LParenLoc;
4965
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004966 if (isFunctionDeclaratorIdentifierList()) {
4967 if (RequiresArg)
4968 Diag(Tok, diag::err_argument_required_after_attribute);
4969
4970 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4971
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004972 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004973 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004974 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004975 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004976 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004977 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004978 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004979 else if (RequiresArg)
4980 Diag(Tok, diag::err_argument_required_after_attribute);
4981
David Blaikie4e4d0842012-03-11 07:00:24 +00004982 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004983
4984 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004985 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004986 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004987 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004988 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004989
David Blaikie4e4d0842012-03-11 07:00:24 +00004990 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004991 // FIXME: Accept these components in any order, and produce fixits to
4992 // correct the order if the user gets it wrong. Ideally we should deal
4993 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004994
4995 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004996 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4997 /*CXX11AttributesAllowed*/ false,
4998 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00004999 if (!DS.getSourceRange().getEnd().isInvalid()) {
5000 EndLoc = DS.getSourceRange().getEnd();
5001 ConstQualifierLoc = DS.getConstSpecLoc();
5002 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5003 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005004
5005 // Parse ref-qualifier[opt].
5006 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00005007 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00005008 diag::warn_cxx98_compat_ref_qualifier :
5009 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00005010
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005011 RefQualifierIsLValueRef = Tok.is(tok::amp);
5012 RefQualifierLoc = ConsumeToken();
5013 EndLoc = RefQualifierLoc;
5014 }
5015
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005016 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00005017 // If a declaration declares a member function or member function
5018 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005019 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00005020 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005021 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00005022 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00005023 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00005024 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00005025 (D.getContext() == Declarator::MemberContext
5026 ? !D.getDeclSpec().isFriendSpecified()
5027 : D.getContext() == Declarator::FileContext &&
5028 D.getCXXScopeSpec().isValid() &&
5029 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005030 Sema::CXXThisScopeRAII ThisScope(Actions,
5031 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00005032 DS.getTypeQualifiers() |
Richard Smith84046262013-04-21 01:08:50 +00005033 (D.getDeclSpec().isConstexprSpecified() &&
5034 !getLangOpts().CPlusPlus1y
Richard Smith7b19cb12013-01-14 01:55:13 +00005035 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005036 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00005037
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005038 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00005039 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00005040 DynamicExceptions,
5041 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00005042 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005043 if (ESpecType != EST_None)
5044 EndLoc = ESpecRange.getEnd();
5045
Richard Smith6ee326a2012-04-10 01:32:12 +00005046 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5047 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005048 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00005049
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005050 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005051 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00005052 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00005053 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005054 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5055 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005056 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00005057 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00005058 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005059 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005060 }
5061 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005062 }
5063
5064 // Remember that we parsed a function type, and remember the attributes.
5065 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005066 IsAmbiguous,
5067 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005068 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005069 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005070 DS.getTypeQualifiers(),
5071 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00005072 RefQualifierLoc, ConstQualifierLoc,
5073 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00005074 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005075 ESpecType, ESpecRange.getBegin(),
5076 DynamicExceptions.data(),
5077 DynamicExceptionRanges.data(),
5078 DynamicExceptions.size(),
5079 NoexceptExpr.isUsable() ?
5080 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005081 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005082 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00005083 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00005084
5085 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005086}
5087
5088/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5089/// identifier list form for a K&R-style function: void foo(a,b,c)
5090///
5091/// Note that identifier-lists are only allowed for normal declarators, not for
5092/// abstract-declarators.
5093bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00005094 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005095 && Tok.is(tok::identifier)
5096 && !TryAltiVecVectorToken()
5097 // K&R identifier lists can't have typedefs as identifiers, per C99
5098 // 6.7.5.3p11.
5099 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5100 // Identifier lists follow a really simple grammar: the identifiers can
5101 // be followed *only* by a ", identifier" or ")". However, K&R
5102 // identifier lists are really rare in the brave new modern world, and
5103 // it is very common for someone to typo a type in a non-K&R style
5104 // list. If we are presented with something like: "void foo(intptr x,
5105 // float y)", we don't want to start parsing the function declarator as
5106 // though it is a K&R style declarator just because intptr is an
5107 // invalid type.
5108 //
5109 // To handle this, we check to see if the token after the first
5110 // identifier is a "," or ")". Only then do we parse it as an
5111 // identifier list.
5112 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5113}
5114
5115/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5116/// we found a K&R-style identifier list instead of a typed parameter list.
5117///
5118/// After returning, ParamInfo will hold the parsed parameters.
5119///
5120/// identifier-list: [C99 6.7.5]
5121/// identifier
5122/// identifier-list ',' identifier
5123///
5124void Parser::ParseFunctionDeclaratorIdentifierList(
5125 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005126 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005127 // If there was no identifier specified for the declarator, either we are in
5128 // an abstract-declarator, or we are in a parameter declarator which was found
5129 // to be abstract. In abstract-declarators, identifier lists are not valid:
5130 // diagnose this.
5131 if (!D.getIdentifier())
5132 Diag(Tok, diag::ext_ident_list_in_param);
5133
5134 // Maintain an efficient lookup of params we have seen so far.
5135 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5136
5137 while (1) {
5138 // If this isn't an identifier, report the error and skip until ')'.
5139 if (Tok.isNot(tok::identifier)) {
5140 Diag(Tok, diag::err_expected_ident);
5141 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5142 // Forget we parsed anything.
5143 ParamInfo.clear();
5144 return;
5145 }
5146
5147 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5148
5149 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5150 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5151 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5152
5153 // Verify that the argument identifier has not already been mentioned.
5154 if (!ParamsSoFar.insert(ParmII)) {
5155 Diag(Tok, diag::err_param_redefinition) << ParmII;
5156 } else {
5157 // Remember this identifier in ParamInfo.
5158 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5159 Tok.getLocation(),
5160 0));
5161 }
5162
5163 // Eat the identifier.
5164 ConsumeToken();
5165
5166 // The list continues if we see a comma.
5167 if (Tok.isNot(tok::comma))
5168 break;
5169 ConsumeToken();
5170 }
5171}
5172
5173/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5174/// after the opening parenthesis. This function will not parse a K&R-style
5175/// identifier list.
5176///
Richard Smith6ce48a72012-04-11 04:01:28 +00005177/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5178/// caller parsed those arguments immediately after the open paren - they should
5179/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005180///
5181/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5182/// be the location of the ellipsis, if any was parsed.
5183///
Reid Spencer5f016e22007-07-11 17:01:13 +00005184/// parameter-type-list: [C99 6.7.5]
5185/// parameter-list
5186/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005187/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005188///
5189/// parameter-list: [C99 6.7.5]
5190/// parameter-declaration
5191/// parameter-list ',' parameter-declaration
5192///
5193/// parameter-declaration: [C99 6.7.5]
5194/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005195/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005196/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005197/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005198/// declaration-specifiers abstract-declarator[opt]
5199/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005200/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005201/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005202/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005203///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005204void Parser::ParseParameterDeclarationClause(
5205 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005206 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005207 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005208 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005209
Chris Lattnerf97409f2008-04-06 06:57:35 +00005210 while (1) {
5211 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005212 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5213 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005214 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005215 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005216 }
Mike Stump1eb44332009-09-09 15:08:12 +00005217
Chris Lattnerf97409f2008-04-06 06:57:35 +00005218 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005219 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005220 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005221
Richard Smith6ce48a72012-04-11 04:01:28 +00005222 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005223 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005224
John McCall7f040a92010-12-24 02:08:15 +00005225 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005226 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005227
5228 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005229
5230 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005231 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005232 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005233 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5234 // too much hassle.
5235 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005236
Chris Lattnere64c5492009-02-27 18:38:20 +00005237 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005238
Chris Lattnerf97409f2008-04-06 06:57:35 +00005239 // Parse the declarator. This is "PrototypeContext", because we must
5240 // accept either 'declarator' or 'abstract-declarator' here.
5241 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5242 ParseDeclarator(ParmDecl);
5243
5244 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005245 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005246
Chris Lattnerf97409f2008-04-06 06:57:35 +00005247 // Remember this parsed parameter in ParamInfo.
5248 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005249
Douglas Gregor72b505b2008-12-16 21:30:33 +00005250 // DefArgToks is used when the parsing of default arguments needs
5251 // to be delayed.
5252 CachedTokens *DefArgToks = 0;
5253
Chris Lattnerf97409f2008-04-06 06:57:35 +00005254 // If no parameter was specified, verify that *something* was specified,
5255 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005256 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5257 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005258 // Completely missing, emit error.
5259 Diag(DSStart, diag::err_missing_param);
5260 } else {
5261 // Otherwise, we have something. Add it and let semantic analysis try
5262 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005263
Chris Lattnerf97409f2008-04-06 06:57:35 +00005264 // Inform the actions module about the parameter declarator, so it gets
5265 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005266 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005267
5268 // Parse the default argument, if any. We parse the default
5269 // arguments in all dialects; the semantic analysis in
5270 // ActOnParamDefaultArgument will reject the default argument in
5271 // C.
5272 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005273 SourceLocation EqualLoc = Tok.getLocation();
5274
Chris Lattner04421082008-04-08 04:40:51 +00005275 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005276 if (D.getContext() == Declarator::MemberContext) {
5277 // If we're inside a class definition, cache the tokens
5278 // corresponding to the default argument. We'll actually parse
5279 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005280 // FIXME: Can we use a smart pointer for Toks?
5281 DefArgToks = new CachedTokens;
5282
Mike Stump1eb44332009-09-09 15:08:12 +00005283 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005284 /*StopAtSemi=*/true,
5285 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005286 delete DefArgToks;
5287 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005288 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005289 } else {
5290 // Mark the end of the default argument so that we know when to
5291 // stop when we parse it later on.
5292 Token DefArgEnd;
5293 DefArgEnd.startToken();
5294 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5295 DefArgEnd.setLocation(Tok.getLocation());
5296 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005297 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005298 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005299 }
Chris Lattner04421082008-04-08 04:40:51 +00005300 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005301 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005302 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005303
Chad Rosier8decdee2012-06-26 22:30:43 +00005304 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005305 // used.
5306 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005307 Sema::PotentiallyEvaluatedIfUsed,
5308 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005309
Sebastian Redl84407ba2012-03-14 15:54:00 +00005310 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005311 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005312 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005313 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005314 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005315 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005316 if (DefArgResult.isInvalid()) {
5317 Actions.ActOnParamDefaultArgumentError(Param);
5318 SkipUntil(tok::comma, tok::r_paren, true, true);
5319 } else {
5320 // Inform the actions module about the default argument
5321 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005322 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005323 }
Chris Lattner04421082008-04-08 04:40:51 +00005324 }
5325 }
Mike Stump1eb44332009-09-09 15:08:12 +00005326
5327 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5328 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005329 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005330 }
5331
5332 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005333 if (Tok.isNot(tok::comma)) {
5334 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005335 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005336
David Blaikie4e4d0842012-03-11 07:00:24 +00005337 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005338 // We have ellipsis without a preceding ',', which is ill-formed
5339 // in C. Complain and provide the fix.
5340 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005341 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005342 }
5343 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005344
Douglas Gregored5d6512009-09-22 21:41:40 +00005345 break;
5346 }
Mike Stump1eb44332009-09-09 15:08:12 +00005347
Chris Lattnerf97409f2008-04-06 06:57:35 +00005348 // Consume the comma.
5349 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005350 }
Mike Stump1eb44332009-09-09 15:08:12 +00005351
Chris Lattner66d28652008-04-06 06:34:08 +00005352}
Chris Lattneref4715c2008-04-06 05:45:57 +00005353
Reid Spencer5f016e22007-07-11 17:01:13 +00005354/// [C90] direct-declarator '[' constant-expression[opt] ']'
5355/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5356/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5357/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5358/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005359/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5360/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005361void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005362 if (CheckProhibitedCXX11Attribute())
5363 return;
5364
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005365 BalancedDelimiterTracker T(*this, tok::l_square);
5366 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005367
Chris Lattner378c7e42008-12-18 07:27:21 +00005368 // C array syntax has many features, but by-far the most common is [] and [4].
5369 // This code does a fast path to handle some of the most obvious cases.
5370 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005371 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005372 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005373 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005374
Chris Lattner378c7e42008-12-18 07:27:21 +00005375 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005376 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005377 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005378 T.getOpenLocation(),
5379 T.getCloseLocation()),
5380 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005381 return;
5382 } else if (Tok.getKind() == tok::numeric_constant &&
5383 GetLookAheadToken(1).is(tok::r_square)) {
5384 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005385 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005386 ConsumeToken();
5387
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005388 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005389 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005390 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005391
Chris Lattner378c7e42008-12-18 07:27:21 +00005392 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005393 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005394 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005395 T.getOpenLocation(),
5396 T.getCloseLocation()),
5397 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005398 return;
5399 }
Mike Stump1eb44332009-09-09 15:08:12 +00005400
Reid Spencer5f016e22007-07-11 17:01:13 +00005401 // If valid, this location is the position where we read the 'static' keyword.
5402 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005403 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005404 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005405
Reid Spencer5f016e22007-07-11 17:01:13 +00005406 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005407 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005408 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005409 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005410
Reid Spencer5f016e22007-07-11 17:01:13 +00005411 // If we haven't already read 'static', check to see if there is one after the
5412 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005413 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005414 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005415
Reid Spencer5f016e22007-07-11 17:01:13 +00005416 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5417 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005418 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005419
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005420 // Handle the case where we have '[*]' as the array size. However, a leading
5421 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005422 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005423 // infrequent, use of lookahead is not costly here.
5424 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005425 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005426
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005427 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005428 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005429 StaticLoc = SourceLocation(); // Drop the static.
5430 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005431 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005432 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005433 // Note, in C89, this production uses the constant-expr production instead
5434 // of assignment-expr. The only difference is that assignment-expr allows
5435 // things like '=' and '*='. Sema rejects these in C89 mode because they
5436 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005437
Douglas Gregore0762c92009-06-19 23:52:42 +00005438 // Parse the constant-expression or assignment-expression now (depending
5439 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005440 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005441 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005442 } else {
5443 EnterExpressionEvaluationContext Unevaluated(Actions,
5444 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005445 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005446 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005447 }
Mike Stump1eb44332009-09-09 15:08:12 +00005448
Reid Spencer5f016e22007-07-11 17:01:13 +00005449 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005450 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005451 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005452 // If the expression was invalid, skip it.
5453 SkipUntil(tok::r_square);
5454 return;
5455 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005456
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005457 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005458
John McCall0b7e6782011-03-24 11:26:52 +00005459 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005460 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005461
Chris Lattner378c7e42008-12-18 07:27:21 +00005462 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005463 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005464 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005465 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005466 T.getOpenLocation(),
5467 T.getCloseLocation()),
5468 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005469}
5470
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005471/// [GNU] typeof-specifier:
5472/// typeof ( expressions )
5473/// typeof ( type-name )
5474/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005475///
5476void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005477 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005478 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005479 SourceLocation StartLoc = ConsumeToken();
5480
John McCallcfb708c2010-01-13 20:03:27 +00005481 const bool hasParens = Tok.is(tok::l_paren);
5482
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005483 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5484 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005485
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005486 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005487 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005488 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005489 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5490 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005491 if (hasParens)
5492 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005493
5494 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005495 // FIXME: Not accurate, the range gets one token more than it should.
5496 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005497 else
5498 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005499
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005500 if (isCastExpr) {
5501 if (!CastTy) {
5502 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005503 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005504 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005505
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005506 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005507 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005508 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5509 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005510 DiagID, CastTy))
5511 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005512 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005513 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005514
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005515 // If we get here, the operand to the typeof was an expresion.
5516 if (Operand.isInvalid()) {
5517 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005518 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005519 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005520
Eli Friedman71b8fb52012-01-21 01:01:51 +00005521 // We might need to transform the operand if it is potentially evaluated.
5522 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5523 if (Operand.isInvalid()) {
5524 DS.SetTypeSpecError();
5525 return;
5526 }
5527
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005528 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005529 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005530 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5531 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005532 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005533 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005534}
Chris Lattner1b492422010-02-28 18:33:55 +00005535
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005536/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005537/// _Atomic ( type-name )
5538///
5539void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005540 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5541 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005542
5543 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005544 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005545 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005546 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005547
5548 TypeResult Result = ParseTypeName();
5549 if (Result.isInvalid()) {
5550 SkipUntil(tok::r_paren);
5551 return;
5552 }
5553
5554 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005555 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005556
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005557 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005558 return;
5559
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005560 DS.setTypeofParensRange(T.getRange());
5561 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005562
5563 const char *PrevSpec = 0;
5564 unsigned DiagID;
5565 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5566 DiagID, Result.release()))
5567 Diag(StartLoc, DiagID) << PrevSpec;
5568}
5569
Chris Lattner1b492422010-02-28 18:33:55 +00005570
5571/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5572/// from TryAltiVecVectorToken.
5573bool Parser::TryAltiVecVectorTokenOutOfLine() {
5574 Token Next = NextToken();
5575 switch (Next.getKind()) {
5576 default: return false;
5577 case tok::kw_short:
5578 case tok::kw_long:
5579 case tok::kw_signed:
5580 case tok::kw_unsigned:
5581 case tok::kw_void:
5582 case tok::kw_char:
5583 case tok::kw_int:
5584 case tok::kw_float:
5585 case tok::kw_double:
5586 case tok::kw_bool:
5587 case tok::kw___pixel:
5588 Tok.setKind(tok::kw___vector);
5589 return true;
5590 case tok::identifier:
5591 if (Next.getIdentifierInfo() == Ident_pixel) {
5592 Tok.setKind(tok::kw___vector);
5593 return true;
5594 }
5595 return false;
5596 }
5597}
5598
5599bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5600 const char *&PrevSpec, unsigned &DiagID,
5601 bool &isInvalid) {
5602 if (Tok.getIdentifierInfo() == Ident_vector) {
5603 Token Next = NextToken();
5604 switch (Next.getKind()) {
5605 case tok::kw_short:
5606 case tok::kw_long:
5607 case tok::kw_signed:
5608 case tok::kw_unsigned:
5609 case tok::kw_void:
5610 case tok::kw_char:
5611 case tok::kw_int:
5612 case tok::kw_float:
5613 case tok::kw_double:
5614 case tok::kw_bool:
5615 case tok::kw___pixel:
5616 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5617 return true;
5618 case tok::identifier:
5619 if (Next.getIdentifierInfo() == Ident_pixel) {
5620 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5621 return true;
5622 }
5623 break;
5624 default:
5625 break;
5626 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005627 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005628 DS.isTypeAltiVecVector()) {
5629 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5630 return true;
5631 }
5632 return false;
5633}