blob: 26ac6193bd50a86f4ba03ec905c7dffa43bfd0b8 [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"
Larisse Voufo7c64ef02013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
34/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000047
Reid Spencer5f016e22007-07-11 17:01:13 +000048 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnereaaebc72009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor23c94db2010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000066}
67
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Sean Huntbbd37c62009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
92/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
96///
97/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000102///
Reid Spencer5f016e22007-07-11 17:01:13 +0000103/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000104/// token lookahead. Comment from gcc: "If they start with an identifier
105/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// start with that identifier; otherwise they are an expression list."
107///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000108/// GCC does not require the ',' between attribs in an attribute-list.
109///
Reid Spencer5f016e22007-07-11 17:01:13 +0000110/// At the moment, I am not doing 2 token lookahead. I am also unaware of
111/// any attributes that don't work (based on my limited testing). Most
112/// attributes are very simple in practice. Until we find a bug, I don't see
113/// a pressing need to implement the 2 token lookahead.
114
John McCall7f040a92010-12-24 02:08:15 +0000115void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000116 SourceLocation *endLoc,
117 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000118 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner04d66662007-10-09 17:33:22 +0000120 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 ConsumeToken();
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
123 "attribute")) {
124 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000125 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
128 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000129 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 }
131 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000132 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
133 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
136 ConsumeToken();
137 continue;
138 }
139 // we have an identifier or declaration specifier (const, int, etc.)
140 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
141 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000143 if (Tok.is(tok::l_paren)) {
144 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000145 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 LateParsedAttribute *LA =
147 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
148 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000149
Bill Wendlingad017fa2012-12-20 19:22:21 +0000150 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000151 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000152 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000153 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000155 // consume everything up to and including the matching right parens
156 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000158 Token Eof;
159 Eof.startToken();
160 Eof.setLocation(Tok.getLocation());
161 LA->Toks.push_back(Eof);
162 } else {
Michael Han6880f492012-10-03 01:56:22 +0000163 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000164 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 }
166 } else {
Aaron Ballman624421f2013-08-31 01:11:41 +0000167 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
168 AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 }
170 }
171 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000173 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
175 SkipUntil(tok::r_paren, false);
176 }
John McCall7f040a92010-12-24 02:08:15 +0000177 if (endLoc)
178 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000182/// \brief Determine whether the given attribute has all expression arguments.
183static bool attributeHasExprArgs(const IdentifierInfo &II) {
184 return llvm::StringSwitch<bool>(II.getName())
185#include "clang/Parse/AttrExprArgs.inc"
186 .Default(false);
187}
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000188
Richard Smith8edabd92013-09-03 18:01:40 +0000189IdentifierLoc *Parser::ParseIdentifierLoc() {
190 assert(Tok.is(tok::identifier) && "expected an identifier");
191 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
192 Tok.getLocation(),
193 Tok.getIdentifierInfo());
194 ConsumeToken();
195 return IL;
196}
197
Michael Han6880f492012-10-03 01:56:22 +0000198/// Parse the arguments to a parameterized GNU attribute or
199/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000200void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
201 SourceLocation AttrNameLoc,
202 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000203 SourceLocation *EndLoc,
204 IdentifierInfo *ScopeName,
205 SourceLocation ScopeLoc,
206 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000207
208 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
209
210 // Availability attributes have their own grammar.
211 if (AttrName->isStr("availability")) {
212 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
213 return;
214 }
215 // Thread safety attributes fit into the FIXME case above, so we
216 // just parse the arguments as a list of expressions
217 if (IsThreadSafetyAttribute(AttrName->getName())) {
218 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
219 return;
220 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000221 // Type safety attributes have their own grammar.
222 if (AttrName->isStr("type_tag_for_datatype")) {
223 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
224 return;
225 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000226
227 ConsumeParen(); // ignore the left paren loc for now
228
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000229 bool BuiltinType = false;
Aaron Ballman624421f2013-08-31 01:11:41 +0000230 ArgsVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000231
Joey Gouly37453b92013-03-08 09:42:32 +0000232 TypeResult T;
233 SourceRange TypeRange;
234 bool TypeParsed = false;
235
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000236 switch (Tok.getKind()) {
237 case tok::kw_char:
238 case tok::kw_wchar_t:
239 case tok::kw_char16_t:
240 case tok::kw_char32_t:
241 case tok::kw_bool:
242 case tok::kw_short:
243 case tok::kw_int:
244 case tok::kw_long:
245 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000246 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 case tok::kw_signed:
248 case tok::kw_unsigned:
249 case tok::kw_float:
250 case tok::kw_double:
251 case tok::kw_void:
252 case tok::kw_typeof:
253 // __attribute__(( vec_type_hint(char) ))
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000254 BuiltinType = true;
Joey Gouly37453b92013-03-08 09:42:32 +0000255 T = ParseTypeName(&TypeRange);
256 TypeParsed = true;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000257 break;
258
Aaron Ballman624421f2013-08-31 01:11:41 +0000259 case tok::identifier: {
Joey Gouly37453b92013-03-08 09:42:32 +0000260 if (AttrName->isStr("vec_type_hint")) {
261 T = ParseTypeName(&TypeRange);
262 TypeParsed = true;
263 break;
264 }
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000265 // If the attribute has all expression arguments, and not a "parameter",
266 // break out to handle it below.
267 if (attributeHasExprArgs(*AttrName))
268 break;
Aaron Ballman624421f2013-08-31 01:11:41 +0000269
Richard Smith8edabd92013-09-03 18:01:40 +0000270 ArgExprs.push_back(ParseIdentifierLoc());
Aaron Ballman624421f2013-08-31 01:11:41 +0000271 } break;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000272
273 default:
274 break;
275 }
276
Joey Gouly37453b92013-03-08 09:42:32 +0000277 bool isInvalid = false;
278 bool isParmType = false;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000279
Joey Gouly37453b92013-03-08 09:42:32 +0000280 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Aaron Ballman624421f2013-08-31 01:11:41 +0000281 (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000282 // Eat the comma.
Aaron Ballman624421f2013-08-31 01:11:41 +0000283 if (!ArgExprs.empty())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000284 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000285
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000286 // Parse the non-empty comma-separated list of expressions.
287 while (1) {
288 ExprResult ArgExpr(ParseAssignmentExpression());
289 if (ArgExpr.isInvalid()) {
290 SkipUntil(tok::r_paren);
291 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000292 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000293 ArgExprs.push_back(ArgExpr.release());
294 if (Tok.isNot(tok::comma))
295 break;
296 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000297 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000298 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000299 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
300 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
301 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000302 while (Tok.is(tok::identifier)) {
303 ConsumeToken();
304 if (Tok.is(tok::greater))
305 break;
306 if (Tok.is(tok::comma)) {
307 ConsumeToken();
308 continue;
309 }
310 }
311 if (Tok.isNot(tok::greater))
312 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000313 SkipUntil(tok::r_paren, false, true); // skip until ')'
314 }
Joey Gouly37453b92013-03-08 09:42:32 +0000315 } else if (AttrName->isStr("vec_type_hint")) {
316 if (T.get() && !T.isInvalid())
317 isParmType = true;
318 else {
319 if (Tok.is(tok::identifier))
320 ConsumeToken();
321 if (TypeParsed)
322 isInvalid = true;
323 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000324 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000325
326 SourceLocation RParen = Tok.getLocation();
Joey Gouly37453b92013-03-08 09:42:32 +0000327 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
328 !isInvalid) {
Michael Han45bed132012-10-04 16:42:52 +0000329 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Gouly37453b92013-03-08 09:42:32 +0000330 if (isParmType) {
Joey Gouly37453b92013-03-08 09:42:32 +0000331 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
Aaron Ballman624421f2013-08-31 01:11:41 +0000332 ScopeLoc, T.get(), Syntax);
Joey Gouly37453b92013-03-08 09:42:32 +0000333 } else {
334 AttributeList *attr = Attrs.addNew(
Aaron Ballman624421f2013-08-31 01:11:41 +0000335 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
336 ArgExprs.data(), ArgExprs.size(), Syntax);
Joey Gouly37453b92013-03-08 09:42:32 +0000337 if (BuiltinType &&
338 attr->getKind() == AttributeList::AT_IBOutletCollection)
339 Diag(Tok, diag::err_iboutletcollection_builtintype);
340 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000341 }
342}
343
Chad Rosier8decdee2012-06-26 22:30:43 +0000344/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000345/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000346void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000347 SourceLocation AttrNameLoc,
348 ParsedAttributes &Attrs)
349{
350 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000351 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000352 AttrName->getNameStart(), tok::r_paren))
353 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000354
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000355 ExprResult ArgExpr(ParseConstantExpression());
356 if (ArgExpr.isInvalid()) {
357 T.skipToEnd();
358 return;
359 }
Aaron Ballman624421f2013-08-31 01:11:41 +0000360 ArgsUnion ExprList = ArgExpr.take();
361 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
362 AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000363
364 T.consumeClose();
365}
366
Chad Rosier8decdee2012-06-26 22:30:43 +0000367/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000368/// arguments.
369bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
370 return llvm::StringSwitch<bool>(Ident->getName())
371 .Case("dllimport", true)
372 .Case("dllexport", true)
373 .Case("noreturn", true)
374 .Case("nothrow", true)
375 .Case("noinline", true)
376 .Case("naked", true)
377 .Case("appdomain", true)
378 .Case("process", true)
379 .Case("jitintrinsic", true)
380 .Case("noalias", true)
381 .Case("restrict", true)
382 .Case("novtable", true)
383 .Case("selectany", true)
384 .Case("thread", true)
Aaron Ballman3ce0de62013-05-04 16:58:37 +0000385 .Case("safebuffers", true )
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000386 .Default(false);
387}
388
Chad Rosier8decdee2012-06-26 22:30:43 +0000389/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000390/// parameters). Will return false if we properly handled the declspec, or
391/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000392void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000393 SourceLocation Loc,
394 ParsedAttributes &Attrs) {
395 // Try to handle the easy case first -- these declspecs all take a single
396 // parameter as their argument.
397 if (llvm::StringSwitch<bool>(Ident->getName())
398 .Case("uuid", true)
399 .Case("align", true)
400 .Case("allocate", true)
401 .Default(false)) {
402 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
403 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000404 // The deprecated declspec has an optional single argument, so we will
405 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000406 // not.
407 if (Tok.getKind() == tok::l_paren)
408 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
409 else
Aaron Ballman624421f2013-08-31 01:11:41 +0000410 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000411 } else if (Ident->getName() == "property") {
412 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000413 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000414 // must be named get or put.
John McCall76da55d2013-04-16 07:28:30 +0000415 if (Tok.isNot(tok::l_paren)) {
416 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
417 << Ident->getNameStart();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000418 return;
John McCall76da55d2013-04-16 07:28:30 +0000419 }
420 BalancedDelimiterTracker T(*this, tok::l_paren);
421 T.expectAndConsume(diag::err_expected_lparen_after,
422 Ident->getNameStart(), tok::r_paren);
423
424 enum AccessorKind {
425 AK_Invalid = -1,
426 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
427 };
428 IdentifierInfo *AccessorNames[] = { 0, 0 };
429 bool HasInvalidAccessor = false;
430
431 // Parse the accessor specifications.
432 while (true) {
433 // Stop if this doesn't look like an accessor spec.
434 if (!Tok.is(tok::identifier)) {
435 // If the user wrote a completely empty list, use a special diagnostic.
436 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
437 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
438 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
439 break;
440 }
441
442 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
443 break;
444 }
445
446 AccessorKind Kind;
447 SourceLocation KindLoc = Tok.getLocation();
448 StringRef KindStr = Tok.getIdentifierInfo()->getName();
449 if (KindStr == "get") {
450 Kind = AK_Get;
451 } else if (KindStr == "put") {
452 Kind = AK_Put;
453
454 // Recover from the common mistake of using 'set' instead of 'put'.
455 } else if (KindStr == "set") {
456 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
457 << FixItHint::CreateReplacement(KindLoc, "put");
458 Kind = AK_Put;
459
460 // Handle the mistake of forgetting the accessor kind by skipping
461 // this accessor.
462 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
463 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
464 ConsumeToken();
465 HasInvalidAccessor = true;
466 goto next_property_accessor;
467
468 // Otherwise, complain about the unknown accessor kind.
469 } else {
470 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
471 HasInvalidAccessor = true;
472 Kind = AK_Invalid;
473
474 // Try to keep parsing unless it doesn't look like an accessor spec.
475 if (!NextToken().is(tok::equal)) break;
476 }
477
478 // Consume the identifier.
479 ConsumeToken();
480
481 // Consume the '='.
482 if (Tok.is(tok::equal)) {
483 ConsumeToken();
484 } else {
485 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
486 << KindStr;
487 break;
488 }
489
490 // Expect the method name.
491 if (!Tok.is(tok::identifier)) {
492 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
493 break;
494 }
495
496 if (Kind == AK_Invalid) {
497 // Just drop invalid accessors.
498 } else if (AccessorNames[Kind] != NULL) {
499 // Complain about the repeated accessor, ignore it, and keep parsing.
500 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
501 } else {
502 AccessorNames[Kind] = Tok.getIdentifierInfo();
503 }
504 ConsumeToken();
505
506 next_property_accessor:
507 // Keep processing accessors until we run out.
508 if (Tok.is(tok::comma)) {
509 ConsumeAnyToken();
510 continue;
511
512 // If we run into the ')', stop without consuming it.
513 } else if (Tok.is(tok::r_paren)) {
514 break;
515 } else {
516 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
517 break;
518 }
519 }
520
521 // Only add the property attribute if it was well-formed.
522 if (!HasInvalidAccessor) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000523 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall76da55d2013-04-16 07:28:30 +0000524 AccessorNames[AK_Get], AccessorNames[AK_Put],
525 AttributeList::AS_Declspec);
526 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000527 T.skipToEnd();
528 } else {
529 // We don't recognize this as a valid declspec, but instead of creating the
530 // attribute and allowing sema to warn about it, we will warn here instead.
531 // This is because some attributes have multiple spellings, but we need to
532 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000533 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000534 // both locations.
535 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
536
537 // If there's an open paren, we should eat the open and close parens under
538 // the assumption that this unknown declspec has parameters.
539 BalancedDelimiterTracker T(*this, tok::l_paren);
540 if (!T.consumeOpen())
541 T.skipToEnd();
542 }
543}
544
Eli Friedmana23b4852009-06-08 07:21:15 +0000545/// [MS] decl-specifier:
546/// __declspec ( extended-decl-modifier-seq )
547///
548/// [MS] extended-decl-modifier-seq:
549/// extended-decl-modifier[opt]
550/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000551void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000552 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000553
Steve Narofff59e17e2008-12-24 20:59:21 +0000554 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000555 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000556 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000557 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000558 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000559
Chad Rosier8decdee2012-06-26 22:30:43 +0000560 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000561 // you can specify multiple attributes per declspec.
562 while (Tok.getKind() != tok::r_paren) {
563 // We expect either a well-known identifier or a generic string. Anything
564 // else is a malformed declspec.
565 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000566 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000567 Tok.getKind() != tok::kw_restrict) {
568 Diag(Tok, diag::err_ms_declspec_type);
569 T.skipToEnd();
570 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000571 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000572
573 IdentifierInfo *AttrName;
574 SourceLocation AttrNameLoc;
575 if (IsString) {
576 SmallString<8> StrBuffer;
577 bool Invalid = false;
578 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
579 if (Invalid) {
580 T.skipToEnd();
581 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000582 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000583 AttrName = PP.getIdentifierInfo(Str);
584 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000585 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000586 AttrName = Tok.getIdentifierInfo();
587 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000588 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000589
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000590 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000591 // If we have a generic string, we will allow it because there is no
592 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000593 // (for instance, SAL declspecs in older versions of MSVC).
594 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000595 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000596 // arguments and can be turned into an attribute directly.
Aaron Ballman624421f2013-08-31 01:11:41 +0000597 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
598 AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000599 else
600 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000601 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000602 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000603}
604
John McCall7f040a92010-12-24 02:08:15 +0000605void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000606 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000607 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000608 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000609 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballmanaa9df092013-05-22 23:25:32 +0000610 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
611 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000612 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
613 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000614 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
615 AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000616 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000617}
618
John McCall7f040a92010-12-24 02:08:15 +0000619void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000620 // Treat these like attributes
621 while (Tok.is(tok::kw___pascal)) {
622 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
623 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000624 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
625 AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000626 }
John McCall7f040a92010-12-24 02:08:15 +0000627}
628
Peter Collingbournef315fa82011-02-14 01:42:53 +0000629void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
630 // Treat these like attributes
631 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000632 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000633 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000634 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
635 AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000636 }
637}
638
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000639void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000640 // FIXME: The mapping from attribute spelling to semantics should be
641 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000642 SourceLocation Loc = Tok.getLocation();
643 switch(Tok.getKind()) {
644 // OpenCL qualifiers:
645 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000646 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000647 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000648 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000649 PP.getIdentifierInfo("address_space"), Loc, 0);
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___global:
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_global);
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___local:
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_local);
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___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000665 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000666 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000667 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
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___read_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_read_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___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000677 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000678 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000679 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000680 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000681
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000682 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000683 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000684 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000685 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000686 break;
687 default: break;
688 }
689}
690
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000691/// \brief Parse a version number.
692///
693/// version:
694/// simple-integer
695/// simple-integer ',' simple-integer
696/// simple-integer ',' simple-integer ',' simple-integer
697VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
698 Range = Tok.getLocation();
699
700 if (!Tok.is(tok::numeric_constant)) {
701 Diag(Tok, diag::err_expected_version);
702 SkipUntil(tok::comma, tok::r_paren, true, true, true);
703 return VersionTuple();
704 }
705
706 // Parse the major (and possibly minor and subminor) versions, which
707 // are stored in the numeric constant. We utilize a quirk of the
708 // lexer, which is that it handles something like 1.2.3 as a single
709 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000710 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000711 Buffer.resize(Tok.getLength()+1);
712 const char *ThisTokBegin = &Buffer[0];
713
714 // Get the spelling of the token, which eliminates trigraphs, etc.
715 bool Invalid = false;
716 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
717 if (Invalid)
718 return VersionTuple();
719
720 // Parse the major version.
721 unsigned AfterMajor = 0;
722 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000723 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000724 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
725 ++AfterMajor;
726 }
727
728 if (AfterMajor == 0) {
729 Diag(Tok, diag::err_expected_version);
730 SkipUntil(tok::comma, tok::r_paren, true, true, true);
731 return VersionTuple();
732 }
733
734 if (AfterMajor == ActualLength) {
735 ConsumeToken();
736
737 // We only had a single version component.
738 if (Major == 0) {
739 Diag(Tok, diag::err_zero_version);
740 return VersionTuple();
741 }
742
743 return VersionTuple(Major);
744 }
745
746 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
747 Diag(Tok, diag::err_expected_version);
748 SkipUntil(tok::comma, tok::r_paren, true, true, true);
749 return VersionTuple();
750 }
751
752 // Parse the minor version.
753 unsigned AfterMinor = AfterMajor + 1;
754 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000755 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000756 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
757 ++AfterMinor;
758 }
759
760 if (AfterMinor == ActualLength) {
761 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000762
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000763 // We had major.minor.
764 if (Major == 0 && Minor == 0) {
765 Diag(Tok, diag::err_zero_version);
766 return VersionTuple();
767 }
768
Chad Rosier8decdee2012-06-26 22:30:43 +0000769 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000770 }
771
772 // If what follows is not a '.', we have a problem.
773 if (ThisTokBegin[AfterMinor] != '.') {
774 Diag(Tok, diag::err_expected_version);
775 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000776 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000777 }
778
779 // Parse the subminor version.
780 unsigned AfterSubminor = AfterMinor + 1;
781 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000782 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000783 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
784 ++AfterSubminor;
785 }
786
787 if (AfterSubminor != ActualLength) {
788 Diag(Tok, diag::err_expected_version);
789 SkipUntil(tok::comma, tok::r_paren, true, true, true);
790 return VersionTuple();
791 }
792 ConsumeToken();
793 return VersionTuple(Major, Minor, Subminor);
794}
795
796/// \brief Parse the contents of the "availability" attribute.
797///
798/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000799/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000800///
801/// platform:
802/// identifier
803///
804/// version-arg-list:
805/// version-arg
806/// version-arg ',' version-arg-list
807///
808/// version-arg:
809/// 'introduced' '=' version
810/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000811/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000812/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000813/// opt-message:
814/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000815void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
816 SourceLocation AvailabilityLoc,
817 ParsedAttributes &attrs,
818 SourceLocation *endLoc) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000819 enum { Introduced, Deprecated, Obsoleted, Unknown };
820 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000821 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000822
823 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000824 BalancedDelimiterTracker T(*this, tok::l_paren);
825 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000826 Diag(Tok, diag::err_expected_lparen);
827 return;
828 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000829
830 // Parse the platform name,
831 if (Tok.isNot(tok::identifier)) {
832 Diag(Tok, diag::err_availability_expected_platform);
833 SkipUntil(tok::r_paren);
834 return;
835 }
Richard Smith8edabd92013-09-03 18:01:40 +0000836 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000837
838 // Parse the ',' following the platform name.
839 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
840 return;
841
842 // If we haven't grabbed the pointers for the identifiers
843 // "introduced", "deprecated", and "obsoleted", do so now.
844 if (!Ident_introduced) {
845 Ident_introduced = PP.getIdentifierInfo("introduced");
846 Ident_deprecated = PP.getIdentifierInfo("deprecated");
847 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000848 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000849 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000850 }
851
852 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000853 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000854 do {
855 if (Tok.isNot(tok::identifier)) {
856 Diag(Tok, diag::err_availability_expected_change);
857 SkipUntil(tok::r_paren);
858 return;
859 }
860 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
861 SourceLocation KeywordLoc = ConsumeToken();
862
Douglas Gregorb53e4172011-03-26 03:35:55 +0000863 if (Keyword == Ident_unavailable) {
864 if (UnavailableLoc.isValid()) {
865 Diag(KeywordLoc, diag::err_availability_redundant)
866 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000867 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000868 UnavailableLoc = KeywordLoc;
869
870 if (Tok.isNot(tok::comma))
871 break;
872
873 ConsumeToken();
874 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000875 }
876
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000877 if (Tok.isNot(tok::equal)) {
878 Diag(Tok, diag::err_expected_equal_after)
879 << Keyword;
880 SkipUntil(tok::r_paren);
881 return;
882 }
883 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000884 if (Keyword == Ident_message) {
885 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000886 Diag(Tok, diag::err_expected_string_literal)
887 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000888 SkipUntil(tok::r_paren);
889 return;
890 }
891 MessageExpr = ParseStringLiteralExpression();
892 break;
893 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000894
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000895 SourceRange VersionRange;
896 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000897
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000898 if (Version.empty()) {
899 SkipUntil(tok::r_paren);
900 return;
901 }
902
903 unsigned Index;
904 if (Keyword == Ident_introduced)
905 Index = Introduced;
906 else if (Keyword == Ident_deprecated)
907 Index = Deprecated;
908 else if (Keyword == Ident_obsoleted)
909 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000910 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000911 Index = Unknown;
912
913 if (Index < Unknown) {
914 if (!Changes[Index].KeywordLoc.isInvalid()) {
915 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000916 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000917 << SourceRange(Changes[Index].KeywordLoc,
918 Changes[Index].VersionRange.getEnd());
919 }
920
921 Changes[Index].KeywordLoc = KeywordLoc;
922 Changes[Index].Version = Version;
923 Changes[Index].VersionRange = VersionRange;
924 } else {
925 Diag(KeywordLoc, diag::err_availability_unknown_change)
926 << Keyword << VersionRange;
927 }
928
929 if (Tok.isNot(tok::comma))
930 break;
931
932 ConsumeToken();
933 } while (true);
934
935 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000936 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000937 return;
938
939 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000940 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000941
Douglas Gregorb53e4172011-03-26 03:35:55 +0000942 // The 'unavailable' availability cannot be combined with any other
943 // availability changes. Make sure that hasn't happened.
944 if (UnavailableLoc.isValid()) {
945 bool Complained = false;
946 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
947 if (Changes[Index].KeywordLoc.isValid()) {
948 if (!Complained) {
949 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
950 << SourceRange(Changes[Index].KeywordLoc,
951 Changes[Index].VersionRange.getEnd());
952 Complained = true;
953 }
954
955 // Clear out the availability.
956 Changes[Index] = AvailabilityChange();
957 }
958 }
959 }
960
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000961 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000962 attrs.addNew(&Availability,
963 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000964 0, AvailabilityLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +0000965 Platform,
John McCall0b7e6782011-03-24 11:26:52 +0000966 Changes[Introduced],
967 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000968 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000969 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000970 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000971}
972
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000973
Bill Wendlingad017fa2012-12-20 19:22:21 +0000974// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000975// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
976
977void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
978
979void Parser::LateParsedClass::ParseLexedAttributes() {
980 Self->ParseLexedAttributes(*Class);
981}
982
983void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000984 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000985}
986
987/// Wrapper class which calls ParseLexedAttribute, after setting up the
988/// scope appropriately.
989void Parser::ParseLexedAttributes(ParsingClass &Class) {
990 // Deal with templates
991 // FIXME: Test cases to make sure this does the right thing for templates.
992 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
993 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
994 HasTemplateScope);
995 if (HasTemplateScope)
996 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
997
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000998 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000999 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001000 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001001 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1002 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1003
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001004 // Enter the scope of nested classes
1005 if (!AlreadyHasClassScope)
1006 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1007 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +00001008 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001009 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1010 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1011 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001012 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001013
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001014 if (!AlreadyHasClassScope)
1015 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1016 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001017}
1018
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001019
1020/// \brief Parse all attributes in LAs, and attach them to Decl D.
1021void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1022 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +00001023 assert(LAs.parseSoon() &&
1024 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001025 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +00001026 if (D)
1027 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001028 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +00001029 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001030 }
1031 LAs.clear();
1032}
1033
1034
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001035/// \brief Finish parsing an attribute for which parsing was delayed.
1036/// This will be called at the end of parsing a class declaration
1037/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +00001038/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001039/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001040void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1041 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001042 // Save the current token position.
1043 SourceLocation OrigLoc = Tok.getLocation();
1044
1045 // Append the current token at the end of the new token stream so that it
1046 // doesn't get lost.
1047 LA.Toks.push_back(Tok);
1048 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1049 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00001050 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001051
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001052 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +00001053 // FIXME: Do not warn on C++11 attributes, once we start supporting
1054 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001055 Diag(Tok, diag::warn_attribute_on_function_definition)
1056 << LA.AttrName.getName();
1057 }
1058
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001059 ParsedAttributes Attrs(AttrFactory);
1060 SourceLocation endLoc;
1061
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001062 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001063 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001064 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1065 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001066
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001067 // Allow 'this' within late-parsed attributes.
Richard Smithcafeb942013-06-07 02:33:37 +00001068 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1069 ND && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001070
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001071 if (LA.Decls.size() == 1) {
1072 // If the Decl is templatized, add template parameters to scope.
1073 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1074 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1075 if (HasTemplateScope)
1076 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001077
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001078 // If the Decl is on a function, add function parameters to the scope.
1079 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1080 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1081 if (HasFunScope)
1082 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001083
Michael Han6880f492012-10-03 01:56:22 +00001084 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001085 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001086
1087 if (HasFunScope) {
1088 Actions.ActOnExitFunctionContext();
1089 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1090 }
1091 if (HasTemplateScope) {
1092 TempScope.Exit();
1093 }
1094 } else {
1095 // If there are multiple decls, then the decl cannot be within the
1096 // function scope.
Michael Han6880f492012-10-03 01:56:22 +00001097 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001098 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001099 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +00001100 } else {
1101 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001102 }
1103
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001104 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1105 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1106 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001107
1108 if (Tok.getLocation() != OrigLoc) {
1109 // Due to a parsing error, we either went over the cached tokens or
1110 // there are still cached tokens left, so we skip the leftover tokens.
1111 // Since this is an uncommon situation that should be avoided, use the
1112 // expensive isBeforeInTranslationUnit call.
1113 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1114 OrigLoc))
1115 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001116 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001117 }
1118}
1119
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001120/// \brief Wrapper around a case statement checking if AttrName is
1121/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001122bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001123 return llvm::StringSwitch<bool>(AttrName)
1124 .Case("guarded_by", true)
1125 .Case("guarded_var", true)
1126 .Case("pt_guarded_by", true)
1127 .Case("pt_guarded_var", true)
1128 .Case("lockable", true)
1129 .Case("scoped_lockable", true)
1130 .Case("no_thread_safety_analysis", true)
1131 .Case("acquired_after", true)
1132 .Case("acquired_before", true)
1133 .Case("exclusive_lock_function", true)
1134 .Case("shared_lock_function", true)
1135 .Case("exclusive_trylock_function", true)
1136 .Case("shared_trylock_function", true)
1137 .Case("unlock_function", true)
1138 .Case("lock_returned", true)
1139 .Case("locks_excluded", true)
1140 .Case("exclusive_locks_required", true)
1141 .Case("shared_locks_required", true)
1142 .Default(false);
1143}
1144
1145/// \brief Parse the contents of thread safety attributes. These
1146/// should always be parsed as an expression list.
1147///
1148/// We need to special case the parsing due to the fact that if the first token
1149/// of the first argument is an identifier, the main parse loop will store
1150/// that token as a "parameter" and the rest of
1151/// the arguments will be added to a list of "arguments". However,
1152/// subsequent tokens in the first argument are lost. We instead parse each
1153/// argument as an expression and add all arguments to the list of "arguments".
1154/// In future, we will take advantage of this special case to also
1155/// deal with some argument scoping issues here (for example, referring to a
1156/// function parameter in the attribute on that function).
1157void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1158 SourceLocation AttrNameLoc,
1159 ParsedAttributes &Attrs,
1160 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001161 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001162
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001163 BalancedDelimiterTracker T(*this, tok::l_paren);
1164 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001165
Aaron Ballman624421f2013-08-31 01:11:41 +00001166 ArgsVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001167 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001168
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001169 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001170 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001171 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001172 ExprResult ArgExpr(ParseAssignmentExpression());
1173 if (ArgExpr.isInvalid()) {
1174 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001175 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001176 break;
1177 } else {
1178 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001179 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001180 if (Tok.isNot(tok::comma))
1181 break;
1182 ConsumeToken(); // Eat the comma, move to the next argument
1183 }
1184 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001185 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001186 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1187 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001188 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001189 if (EndLoc)
1190 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001191}
1192
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001193void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1194 SourceLocation AttrNameLoc,
1195 ParsedAttributes &Attrs,
1196 SourceLocation *EndLoc) {
1197 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1198
1199 BalancedDelimiterTracker T(*this, tok::l_paren);
1200 T.consumeOpen();
1201
1202 if (Tok.isNot(tok::identifier)) {
1203 Diag(Tok, diag::err_expected_ident);
1204 T.skipToEnd();
1205 return;
1206 }
Richard Smith8edabd92013-09-03 18:01:40 +00001207 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001208
1209 if (Tok.isNot(tok::comma)) {
1210 Diag(Tok, diag::err_expected_comma);
1211 T.skipToEnd();
1212 return;
1213 }
1214 ConsumeToken();
1215
1216 SourceRange MatchingCTypeRange;
1217 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1218 if (MatchingCType.isInvalid()) {
1219 T.skipToEnd();
1220 return;
1221 }
1222
1223 bool LayoutCompatible = false;
1224 bool MustBeNull = false;
1225 while (Tok.is(tok::comma)) {
1226 ConsumeToken();
1227 if (Tok.isNot(tok::identifier)) {
1228 Diag(Tok, diag::err_expected_ident);
1229 T.skipToEnd();
1230 return;
1231 }
1232 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1233 if (Flag->isStr("layout_compatible"))
1234 LayoutCompatible = true;
1235 else if (Flag->isStr("must_be_null"))
1236 MustBeNull = true;
1237 else {
1238 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1239 T.skipToEnd();
1240 return;
1241 }
1242 ConsumeToken(); // consume flag
1243 }
1244
1245 if (!T.consumeClose()) {
1246 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +00001247 ArgumentKind, MatchingCType.release(),
1248 LayoutCompatible, MustBeNull,
1249 AttributeList::AS_GNU);
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001250 }
1251
1252 if (EndLoc)
1253 *EndLoc = T.getCloseLocation();
1254}
1255
Richard Smith6ee326a2012-04-10 01:32:12 +00001256/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1257/// of a C++11 attribute-specifier in a location where an attribute is not
1258/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1259/// situation.
1260///
1261/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1262/// this doesn't appear to actually be an attribute-specifier, and the caller
1263/// should try to parse it.
1264bool Parser::DiagnoseProhibitedCXX11Attribute() {
1265 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1266
1267 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1268 case CAK_NotAttributeSpecifier:
1269 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1270 return false;
1271
1272 case CAK_InvalidAttributeSpecifier:
1273 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1274 return false;
1275
1276 case CAK_AttributeSpecifier:
1277 // Parse and discard the attributes.
1278 SourceLocation BeginLoc = ConsumeBracket();
1279 ConsumeBracket();
1280 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1281 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1282 SourceLocation EndLoc = ConsumeBracket();
1283 Diag(BeginLoc, diag::err_attributes_not_allowed)
1284 << SourceRange(BeginLoc, EndLoc);
1285 return true;
1286 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001287 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001288}
1289
Richard Smith975d52c2013-02-20 01:17:14 +00001290/// \brief We have found the opening square brackets of a C++11
1291/// attribute-specifier in a location where an attribute is not permitted, but
1292/// we know where the attributes ought to be written. Parse them anyway, and
1293/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001294void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1295 SourceLocation CorrectLocation) {
1296 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1297 Tok.is(tok::kw_alignas));
1298
1299 // Consume the attributes.
1300 SourceLocation Loc = Tok.getLocation();
1301 ParseCXX11Attributes(Attrs);
1302 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1303
1304 Diag(Loc, diag::err_attributes_not_allowed)
1305 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1306 << FixItHint::CreateRemoval(AttrRange);
1307}
1308
John McCall7f040a92010-12-24 02:08:15 +00001309void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1310 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1311 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001312}
1313
Michael Hanf64231e2012-11-06 19:34:54 +00001314void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1315 AttributeList *AttrList = attrs.getList();
1316 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001317 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001318 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001319 << AttrList->getName();
1320 AttrList->setInvalid();
1321 }
1322 AttrList = AttrList->getNext();
1323 }
1324}
1325
Reid Spencer5f016e22007-07-11 17:01:13 +00001326/// ParseDeclaration - Parse a full 'declaration', which consists of
1327/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001328/// 'Context' should be a Declarator::TheContext value. This returns the
1329/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001330///
1331/// declaration: [C99 6.7]
1332/// block-declaration ->
1333/// simple-declaration
1334/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001335/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001336/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001337/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001338/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001339/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001340/// others... [FIXME]
1341///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001342Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1343 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001344 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001345 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001346 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001347 // Must temporarily exit the objective-c container scope for
1348 // parsing c none objective-c decls.
1349 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001350
John McCalld226f652010-08-21 09:40:31 +00001351 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001352 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001353 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001354 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001355 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001356 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001357 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001358 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001359 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001360 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001361 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001362 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001363 SourceLocation InlineLoc = ConsumeToken();
1364 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1365 break;
1366 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001367 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001368 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001369 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001370 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001371 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001372 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001373 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001374 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001375 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001376 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001377 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001378 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001379 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001380 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001381 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001382 default:
John McCall7f040a92010-12-24 02:08:15 +00001383 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001384 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001385
Chris Lattner682bf922009-03-29 16:50:03 +00001386 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001387 // single decl, convert it now. Alias declarations can also declare a type;
1388 // include that too if it is present.
1389 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001390}
1391
1392/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1393/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001394/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1395/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001396///[C90/C++]init-declarator-list ';' [TODO]
1397/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001398///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001399/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001400/// attribute-specifier-seq[opt] type-specifier-seq declarator
1401///
Chris Lattnercd147752009-03-29 17:27:48 +00001402/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001403/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001404///
1405/// If FRI is non-null, we might be parsing a for-range-declaration instead
1406/// of a simple-declaration. If we find that we are, we also parse the
1407/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001408Parser::DeclGroupPtrTy
1409Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1410 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001411 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001412 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001414 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001415
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001416 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001417 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001418
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1420 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001421 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001422 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001423 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001424 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001425 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001426 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001427 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001428 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001430
Richard Smith68ea3ae2013-02-22 09:06:26 +00001431 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001432 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001433}
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Richard Smith0706df42011-10-19 21:33:05 +00001435/// Returns true if this might be the start of a declarator, or a common typo
1436/// for a declarator.
1437bool Parser::MightBeDeclarator(unsigned Context) {
1438 switch (Tok.getKind()) {
1439 case tok::annot_cxxscope:
1440 case tok::annot_template_id:
1441 case tok::caret:
1442 case tok::code_completion:
1443 case tok::coloncolon:
1444 case tok::ellipsis:
1445 case tok::kw___attribute:
1446 case tok::kw_operator:
1447 case tok::l_paren:
1448 case tok::star:
1449 return true;
1450
1451 case tok::amp:
1452 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001453 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001454
Richard Smith1c94c162012-01-09 22:31:44 +00001455 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001456 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001457 NextToken().is(tok::l_square);
1458
1459 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001460 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001461
Richard Smith0706df42011-10-19 21:33:05 +00001462 case tok::identifier:
1463 switch (NextToken().getKind()) {
1464 case tok::code_completion:
1465 case tok::coloncolon:
1466 case tok::comma:
1467 case tok::equal:
1468 case tok::equalequal: // Might be a typo for '='.
1469 case tok::kw_alignas:
1470 case tok::kw_asm:
1471 case tok::kw___attribute:
1472 case tok::l_brace:
1473 case tok::l_paren:
1474 case tok::l_square:
1475 case tok::less:
1476 case tok::r_brace:
1477 case tok::r_paren:
1478 case tok::r_square:
1479 case tok::semi:
1480 return true;
1481
1482 case tok::colon:
1483 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001484 // and in block scope it's probably a label. Inside a class definition,
1485 // this is a bit-field.
1486 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001487 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001488
1489 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001490 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001491
1492 default:
1493 return false;
1494 }
1495
1496 default:
1497 return false;
1498 }
1499}
1500
Richard Smith994d73f2012-04-11 20:59:20 +00001501/// Skip until we reach something which seems like a sensible place to pick
1502/// up parsing after a malformed declaration. This will sometimes stop sooner
1503/// than SkipUntil(tok::r_brace) would, but will never stop later.
1504void Parser::SkipMalformedDecl() {
1505 while (true) {
1506 switch (Tok.getKind()) {
1507 case tok::l_brace:
1508 // Skip until matching }, then stop. We've probably skipped over
1509 // a malformed class or function definition or similar.
1510 ConsumeBrace();
1511 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1512 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1513 // This declaration isn't over yet. Keep skipping.
1514 continue;
1515 }
1516 if (Tok.is(tok::semi))
1517 ConsumeToken();
1518 return;
1519
1520 case tok::l_square:
1521 ConsumeBracket();
1522 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1523 continue;
1524
1525 case tok::l_paren:
1526 ConsumeParen();
1527 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1528 continue;
1529
1530 case tok::r_brace:
1531 return;
1532
1533 case tok::semi:
1534 ConsumeToken();
1535 return;
1536
1537 case tok::kw_inline:
1538 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001539 // a good place to pick back up parsing, except in an Objective-C
1540 // @interface context.
1541 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1542 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001543 return;
1544 break;
1545
1546 case tok::kw_namespace:
1547 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001548 // place to pick back up parsing, except in an Objective-C
1549 // @interface context.
1550 if (Tok.isAtStartOfLine() &&
1551 (!ParsingInObjCContainer || CurParsedObjCImpl))
1552 return;
1553 break;
1554
1555 case tok::at:
1556 // @end is very much like } in Objective-C contexts.
1557 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1558 ParsingInObjCContainer)
1559 return;
1560 break;
1561
1562 case tok::minus:
1563 case tok::plus:
1564 // - and + probably start new method declarations in Objective-C contexts.
1565 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001566 return;
1567 break;
1568
1569 case tok::eof:
1570 return;
1571
1572 default:
1573 break;
1574 }
1575
1576 ConsumeAnyToken();
1577 }
1578}
1579
John McCalld8ac0572009-11-03 19:26:08 +00001580/// ParseDeclGroup - Having concluded that this is either a function
1581/// definition or a group of object declarations, actually parse the
1582/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001583Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1584 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001585 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001586 SourceLocation *DeclEnd,
1587 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001588 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001589 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001590 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001591
John McCalld8ac0572009-11-03 19:26:08 +00001592 // Bail out if the first declarator didn't seem well-formed.
1593 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001594 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001595 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001598 // Save late-parsed attributes for now; they need to be parsed in the
1599 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001600 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1601 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001602 if (D.isFunctionDeclarator())
1603 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1604
Chris Lattnerc82daef2010-07-11 22:24:20 +00001605 // Check to see if we have a function *definition* which must have a body.
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001606 if (D.isFunctionDeclarator() &&
Chris Lattnerc82daef2010-07-11 22:24:20 +00001607 // Look at the next token to make sure that this isn't a function
1608 // declaration. We have to check this because __attribute__ might be the
1609 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001610 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001611
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001612 if (AllowFunctionDefinitions) {
1613 if (isStartOfFunctionDefinition(D)) {
1614 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1615 Diag(Tok, diag::err_function_declared_typedef);
John McCalld8ac0572009-11-03 19:26:08 +00001616
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001617 // Recover by treating the 'typedef' as spurious.
1618 DS.ClearStorageClassSpecs();
1619 }
1620
1621 Decl *TheDecl =
1622 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1623 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld8ac0572009-11-03 19:26:08 +00001624 }
1625
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001626 if (isDeclarationSpecifier()) {
1627 // If there is an invalid declaration specifier right after the function
1628 // prototype, then we must be in a missing semicolon case where this isn't
1629 // actually a body. Just fall through into the code that handles it as a
1630 // prototype, and let the top-level code handle the erroneous declspec
1631 // where it would otherwise expect a comma or semicolon.
1632 } else {
1633 Diag(Tok, diag::err_expected_fn_body);
1634 SkipUntil(tok::semi);
1635 return DeclGroupPtrTy();
1636 }
John McCalld8ac0572009-11-03 19:26:08 +00001637 } else {
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001638 if (Tok.is(tok::l_brace)) {
1639 Diag(Tok, diag::err_function_definition_not_allowed);
1640 SkipUntil(tok::r_brace, true, true);
1641 }
John McCalld8ac0572009-11-03 19:26:08 +00001642 }
1643 }
1644
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001645 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001646 return DeclGroupPtrTy();
1647
1648 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1649 // must parse and analyze the for-range-initializer before the declaration is
1650 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001651 //
1652 // Handle the Objective-C for-in loop variable similarly, although we
1653 // don't need to parse the container in advance.
1654 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1655 bool IsForRangeLoop = false;
1656 if (Tok.is(tok::colon)) {
1657 IsForRangeLoop = true;
1658 FRI->ColonLoc = ConsumeToken();
1659 if (Tok.is(tok::l_brace))
1660 FRI->RangeExpr = ParseBraceInitializer();
1661 else
1662 FRI->RangeExpr = ParseExpression();
1663 }
1664
Richard Smithad762fc2011-04-14 22:09:26 +00001665 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001666 if (IsForRangeLoop)
1667 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001668 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001669 D.complete(ThisDecl);
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001670 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001671 }
1672
Chris Lattner5f9e2722011-07-23 10:55:15 +00001673 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001674 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001675 if (LateParsedAttrs.size() > 0)
1676 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001677 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001678 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001679 DeclsInGroup.push_back(FirstDecl);
1680
Richard Smith0706df42011-10-19 21:33:05 +00001681 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001682
John McCalld8ac0572009-11-03 19:26:08 +00001683 // If we don't have a comma, it is either the end of the list (a ';') or an
1684 // error, bail out.
1685 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001686 SourceLocation CommaLoc = ConsumeToken();
1687
1688 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1689 // This comma was followed by a line-break and something which can't be
1690 // the start of a declarator. The comma was probably a typo for a
1691 // semicolon.
1692 Diag(CommaLoc, diag::err_expected_semi_declaration)
1693 << FixItHint::CreateReplacement(CommaLoc, ";");
1694 ExpectSemi = false;
1695 break;
1696 }
John McCalld8ac0572009-11-03 19:26:08 +00001697
1698 // Parse the next declarator.
1699 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001700 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001701
1702 // Accept attributes in an init-declarator. In the first declarator in a
1703 // declaration, these would be part of the declspec. In subsequent
1704 // declarators, they become part of the declarator itself, so that they
1705 // don't apply to declarators after *this* one. Examples:
1706 // short __attribute__((common)) var; -> declspec
1707 // short var __attribute__((common)); -> declarator
1708 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001709 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001710
1711 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001712 if (!D.isInvalidType()) {
1713 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1714 D.complete(ThisDecl);
1715 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001716 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001717 }
John McCalld8ac0572009-11-03 19:26:08 +00001718 }
1719
1720 if (DeclEnd)
1721 *DeclEnd = Tok.getLocation();
1722
Richard Smith0706df42011-10-19 21:33:05 +00001723 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001724 ExpectAndConsumeSemi(Context == Declarator::FileContext
1725 ? diag::err_invalid_token_after_toplevel_declarator
1726 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001727 // Okay, there was no semicolon and one was expected. If we see a
1728 // declaration specifier, just assume it was missing and continue parsing.
1729 // Otherwise things are very confused and we skip to recover.
1730 if (!isDeclarationSpecifier()) {
1731 SkipUntil(tok::r_brace, true, true);
1732 if (Tok.is(tok::semi))
1733 ConsumeToken();
1734 }
John McCalld8ac0572009-11-03 19:26:08 +00001735 }
1736
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001737 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001738}
1739
Richard Smithad762fc2011-04-14 22:09:26 +00001740/// Parse an optional simple-asm-expr and attributes, and attach them to a
1741/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001742bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001743 // If a simple-asm-expr is present, parse it.
1744 if (Tok.is(tok::kw_asm)) {
1745 SourceLocation Loc;
1746 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1747 if (AsmLabel.isInvalid()) {
1748 SkipUntil(tok::semi, true, true);
1749 return true;
1750 }
1751
1752 D.setAsmLabel(AsmLabel.release());
1753 D.SetRangeEnd(Loc);
1754 }
1755
1756 MaybeParseGNUAttributes(D);
1757 return false;
1758}
1759
Douglas Gregor1426e532009-05-12 21:31:51 +00001760/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1761/// declarator'. This method parses the remainder of the declaration
1762/// (including any attributes or initializer, among other things) and
1763/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001764///
Reid Spencer5f016e22007-07-11 17:01:13 +00001765/// init-declarator: [C99 6.7]
1766/// declarator
1767/// declarator '=' initializer
1768/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1769/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001770/// [C++] declarator initializer[opt]
1771///
1772/// [C++] initializer:
1773/// [C++] '=' initializer-clause
1774/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001775/// [C++0x] '=' 'default' [TODO]
1776/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001777/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001778///
1779/// According to the standard grammar, =default and =delete are function
1780/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001781///
John McCalld226f652010-08-21 09:40:31 +00001782Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001783 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001784 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001785 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Richard Smithad762fc2011-04-14 22:09:26 +00001787 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1788}
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Richard Smithad762fc2011-04-14 22:09:26 +00001790Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1791 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001792 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001793 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001794 switch (TemplateInfo.Kind) {
1795 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001796 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001797 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001798
Douglas Gregord5a423b2009-09-25 18:43:00 +00001799 case ParsedTemplateInfo::Template:
Larisse Voufoef4579c2013-08-06 01:03:05 +00001800 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001801 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001802 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001803 D);
Larisse Voufo06935f32013-08-06 03:43:07 +00001804 if (Tok.is(tok::semi) &&
1805 Actions.HandleVariableRedeclaration(ThisDecl, D.getCXXScopeSpec())) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00001806 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001807 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001808 }
Larisse Voufo25218132013-08-06 07:33:00 +00001809 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufoef4579c2013-08-06 01:03:05 +00001810 // Re-direct this decl to refer to the templated decl so that we can
1811 // initialize it.
1812 ThisDecl = VT->getTemplatedDecl();
1813 break;
1814 }
1815 case ParsedTemplateInfo::ExplicitInstantiation: {
1816 if (Tok.is(tok::semi)) {
1817 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1818 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1819 if (ThisRes.isInvalid()) {
1820 SkipUntil(tok::semi, true, true);
1821 return 0;
1822 }
1823 ThisDecl = ThisRes.get();
1824 } else {
1825 // FIXME: This check should be for a variable template instantiation only.
1826
1827 // Check that this is a valid instantiation
1828 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1829 // If the declarator-id is not a template-id, issue a diagnostic and
1830 // recover by ignoring the 'template' keyword.
1831 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1832 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1833 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1834 } else {
1835 SourceLocation LAngleLoc =
1836 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1837 Diag(D.getIdentifierLoc(),
1838 diag::err_explicit_instantiation_with_definition)
1839 << SourceRange(TemplateInfo.TemplateLoc)
1840 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1841
1842 // Recover as if it were an explicit specialization.
1843 TemplateParameterLists FakedParamLists;
1844 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1845 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1846 LAngleLoc));
1847
1848 ThisDecl =
1849 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1850 }
1851 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00001852 break;
1853 }
1854 }
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Richard Smitha2c36462013-04-26 16:15:35 +00001856 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith34b41d92011-02-20 03:19:35 +00001857
Douglas Gregor1426e532009-05-12 21:31:51 +00001858 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001859 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001860 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001861 ConsumeToken();
Larisse Voufoef4579c2013-08-06 01:03:05 +00001862
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001863 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001864 if (D.isFunctionDeclarator())
1865 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1866 << 1 /* delete */;
1867 else
1868 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001869 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001870 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001871 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1872 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001873 else
1874 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001875 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001876 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001877 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001878 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001879 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001880
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001881 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001882 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001883 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001884 cutOffParsing();
1885 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001886 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001887
John McCall60d7b3a2010-08-24 06:29:42 +00001888 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001889
David Blaikie4e4d0842012-03-11 07:00:24 +00001890 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001891 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001892 ExitScope();
1893 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001894
Douglas Gregor1426e532009-05-12 21:31:51 +00001895 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001896 SkipUntil(tok::comma, true, true);
1897 Actions.ActOnInitializerError(ThisDecl);
1898 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001899 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1900 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001901 }
1902 } else if (Tok.is(tok::l_paren)) {
1903 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001904 BalancedDelimiterTracker T(*this, tok::l_paren);
1905 T.consumeOpen();
1906
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001907 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001908 CommaLocsTy CommaLocs;
1909
David Blaikie4e4d0842012-03-11 07:00:24 +00001910 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001911 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001912 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001913 }
1914
Douglas Gregor1426e532009-05-12 21:31:51 +00001915 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001916 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001917 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001918
David Blaikie4e4d0842012-03-11 07:00:24 +00001919 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001920 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001921 ExitScope();
1922 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001923 } else {
1924 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001925 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001926
1927 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1928 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001929
David Blaikie4e4d0842012-03-11 07:00:24 +00001930 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001931 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001932 ExitScope();
1933 }
1934
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001935 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1936 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001937 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001938 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1939 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001940 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001941 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001942 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001943 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001944 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1945
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001946 if (D.getCXXScopeSpec().isSet()) {
1947 EnterScope(0);
1948 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1949 }
1950
1951 ExprResult Init(ParseBraceInitializer());
1952
1953 if (D.getCXXScopeSpec().isSet()) {
1954 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1955 ExitScope();
1956 }
1957
1958 if (Init.isInvalid()) {
1959 Actions.ActOnInitializerError(ThisDecl);
1960 } else
1961 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1962 /*DirectInit=*/true, TypeContainsAuto);
1963
Douglas Gregor1426e532009-05-12 21:31:51 +00001964 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001965 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001966 }
1967
Richard Smith483b9f32011-02-21 20:05:19 +00001968 Actions.FinalizeDeclaration(ThisDecl);
1969
Douglas Gregor1426e532009-05-12 21:31:51 +00001970 return ThisDecl;
1971}
1972
Reid Spencer5f016e22007-07-11 17:01:13 +00001973/// ParseSpecifierQualifierList
1974/// specifier-qualifier-list:
1975/// type-specifier specifier-qualifier-list[opt]
1976/// type-qualifier specifier-qualifier-list[opt]
1977/// [GNU] attributes specifier-qualifier-list[opt]
1978///
Richard Smith69730c12012-03-12 07:56:15 +00001979void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1980 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001981 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1982 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001983 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001984 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Reid Spencer5f016e22007-07-11 17:01:13 +00001986 // Validate declspec for type-name.
1987 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001988 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1989 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001990 Diag(Tok, diag::err_expected_type);
1991 DS.SetTypeSpecError();
1992 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1993 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001995 if (!DS.hasTypeSpecifier())
1996 DS.SetTypeSpecError();
1997 }
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 // Issue diagnostic and remove storage class if present.
2000 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
2001 if (DS.getStorageClassSpecLoc().isValid())
2002 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2003 else
Richard Smithec642442013-04-12 22:46:28 +00002004 Diag(DS.getThreadStorageClassSpecLoc(),
2005 diag::err_typename_invalid_storageclass);
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 DS.ClearStorageClassSpecs();
2007 }
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Reid Spencer5f016e22007-07-11 17:01:13 +00002009 // Issue diagnostic and remove function specfier if present.
2010 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00002011 if (DS.isInlineSpecified())
2012 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2013 if (DS.isVirtualSpecified())
2014 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2015 if (DS.isExplicitSpecified())
2016 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 DS.ClearFunctionSpecs();
2018 }
Richard Smith69730c12012-03-12 07:56:15 +00002019
2020 // Issue diagnostic and remove constexpr specfier if present.
2021 if (DS.isConstexprSpecified()) {
2022 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2023 DS.ClearConstexprSpec();
2024 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002025}
2026
Chris Lattnerc199ab32009-04-12 20:42:31 +00002027/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2028/// specified token is valid after the identifier in a declarator which
2029/// immediately follows the declspec. For example, these things are valid:
2030///
2031/// int x [ 4]; // direct-declarator
2032/// int x ( int y); // direct-declarator
2033/// int(int x ) // direct-declarator
2034/// int x ; // simple-declaration
2035/// int x = 17; // init-declarator-list
2036/// int x , y; // init-declarator-list
2037/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002038/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00002039/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00002040///
2041/// This is not, because 'x' does not immediately follow the declspec (though
2042/// ')' happens to be valid anyway).
2043/// int (x)
2044///
2045static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2046 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2047 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002048 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00002049}
2050
Chris Lattnere40c2952009-04-14 21:34:55 +00002051
2052/// ParseImplicitInt - This method is called when we have an non-typename
2053/// identifier in a declspec (which normally terminates the decl spec) when
2054/// the declspec has no type specifier. In this case, the declspec is either
2055/// malformed or is "implicit int" (in K&R and C89).
2056///
2057/// This method handles diagnosing this prettily and returns false if the
2058/// declspec is done being processed. If it recovers and thinks there may be
2059/// other pieces of declspec after it, it returns true.
2060///
Chris Lattnerf4382f52009-04-14 22:17:06 +00002061bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002062 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00002063 AccessSpecifier AS, DeclSpecContext DSC,
2064 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002065 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002066
Chris Lattnere40c2952009-04-14 21:34:55 +00002067 SourceLocation Loc = Tok.getLocation();
2068 // If we see an identifier that is not a type name, we normally would
2069 // parse it as the identifer being declared. However, when a typename
2070 // is typo'd or the definition is not included, this will incorrectly
2071 // parse the typename as the identifier name and fall over misparsing
2072 // later parts of the diagnostic.
2073 //
2074 // As such, we try to do some look-ahead in cases where this would
2075 // otherwise be an "implicit-int" case to see if this is invalid. For
2076 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2077 // an identifier with implicit int, we'd get a parse error because the
2078 // next token is obviously invalid for a type. Parse these as a case
2079 // with an invalid type specifier.
2080 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Chris Lattnere40c2952009-04-14 21:34:55 +00002082 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00002083 // error, do lookahead to try to do better recovery. This never applies
2084 // within a type specifier. Outside of C++, we allow this even if the
2085 // language doesn't "officially" support implicit int -- we support
Richard Smith58eb3702013-04-30 22:43:51 +00002086 // implicit int as an extension in C99 and C11.
Richard Smitha971d242012-05-09 20:55:26 +00002087 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith58eb3702013-04-30 22:43:51 +00002088 !getLangOpts().CPlusPlus &&
Richard Smith69730c12012-03-12 07:56:15 +00002089 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002090 // If this token is valid for implicit int, e.g. "static x = 4", then
2091 // we just avoid eating the identifier, so it will be parsed as the
2092 // identifier in the declarator.
2093 return false;
2094 }
Mike Stump1eb44332009-09-09 15:08:12 +00002095
Richard Smith827adaf2012-05-15 21:01:51 +00002096 if (getLangOpts().CPlusPlus &&
2097 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2098 // Don't require a type specifier if we have the 'auto' storage class
2099 // specifier in C++98 -- we'll promote it to a type specifier.
2100 return false;
2101 }
2102
Chris Lattnere40c2952009-04-14 21:34:55 +00002103 // Otherwise, if we don't consume this token, we are going to emit an
2104 // error anyway. Try to recover from various common problems. Check
2105 // to see if this was a reference to a tag name without a tag specified.
2106 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00002107 //
2108 // C++ doesn't need this, and isTagName doesn't take SS.
2109 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002110 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002111 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Douglas Gregor23c94db2010-07-02 17:43:08 +00002113 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002114 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002115 case DeclSpec::TST_enum:
2116 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2117 case DeclSpec::TST_union:
2118 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2119 case DeclSpec::TST_struct:
2120 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00002121 case DeclSpec::TST_interface:
2122 TagName="__interface"; FixitTagName = "__interface ";
2123 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002124 case DeclSpec::TST_class:
2125 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00002126 }
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Chris Lattnerf4382f52009-04-14 22:17:06 +00002128 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002129 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2130 LookupResult R(Actions, TokenName, SourceLocation(),
2131 Sema::LookupOrdinaryName);
2132
Chris Lattnerf4382f52009-04-14 22:17:06 +00002133 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002134 << TokenName << TagName << getLangOpts().CPlusPlus
2135 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2136
2137 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2138 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2139 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00002140 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002141 << TokenName << TagName;
2142 }
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Chris Lattnerf4382f52009-04-14 22:17:06 +00002144 // Parse this as a tag as if the missing tag were present.
2145 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00002146 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002147 else
Richard Smith69730c12012-03-12 07:56:15 +00002148 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002149 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002150 return true;
2151 }
Chris Lattnere40c2952009-04-14 21:34:55 +00002152 }
Mike Stump1eb44332009-09-09 15:08:12 +00002153
Richard Smith8f0a7e72012-05-15 21:29:55 +00002154 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002155 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00002156 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2157 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002158 // Look ahead to the next token to try to figure out what this declaration
2159 // was supposed to be.
2160 switch (NextToken().getKind()) {
2161 case tok::comma:
2162 case tok::equal:
2163 case tok::kw_asm:
2164 case tok::l_brace:
2165 case tok::l_square:
2166 case tok::semi:
2167 // This looks like a variable declaration. The type is probably missing.
2168 // We're done parsing decl-specifiers.
2169 return false;
2170
2171 case tok::l_paren: {
2172 // static x(4); // 'x' is not a type
2173 // x(int n); // 'x' is not a type
2174 // x (*p)[]; // 'x' is a type
2175 //
2176 // Since we're in an error case (or the rare 'implicit int in C++' MS
2177 // extension), we can afford to perform a tentative parse to determine
2178 // which case we're in.
2179 TentativeParsingAction PA(*this);
2180 ConsumeToken();
2181 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2182 PA.Revert();
2183 if (TPR == TPResult::False())
2184 return false;
2185 // The identifier is followed by a parenthesized declarator.
2186 // It's supposed to be a type.
2187 break;
2188 }
2189
2190 default:
2191 // This is probably supposed to be a type. This includes cases like:
2192 // int f(itn);
2193 // struct S { unsinged : 4; };
2194 break;
2195 }
2196 }
2197
Chad Rosier8decdee2012-06-26 22:30:43 +00002198 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002199 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002200 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002201 IdentifierInfo *II = Tok.getIdentifierInfo();
2202 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002203 // The action emitted a diagnostic, so we don't have to.
2204 if (T) {
2205 // The action has suggested that the type T could be used. Set that as
2206 // the type in the declaration specifiers, consume the would-be type
2207 // name token, and we're done.
2208 const char *PrevSpec;
2209 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002210 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002211 DS.SetRangeEnd(Tok.getLocation());
2212 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002213 // There may be other declaration specifiers after this.
2214 return true;
2215 } else if (II != Tok.getIdentifierInfo()) {
2216 // If no type was suggested, the correction is to a keyword
2217 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002218 // There may be other declaration specifiers after this.
2219 return true;
2220 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002221
Douglas Gregora786fdb2009-10-13 23:27:22 +00002222 // Fall through; the action had no suggestion for us.
2223 } else {
2224 // The action did not emit a diagnostic, so emit one now.
2225 SourceRange R;
2226 if (SS) R = SS->getRange();
2227 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2228 }
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Douglas Gregora786fdb2009-10-13 23:27:22 +00002230 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002231 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002232 DS.SetRangeEnd(Tok.getLocation());
2233 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002234
Chris Lattnere40c2952009-04-14 21:34:55 +00002235 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2236 // avoid rippling error messages on subsequent uses of the same type,
2237 // could be useful if #include was forgotten.
2238 return false;
2239}
2240
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002241/// \brief Determine the declaration specifier context from the declarator
2242/// context.
2243///
2244/// \param Context the declarator context, which is one of the
2245/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002246Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002247Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2248 if (Context == Declarator::MemberContext)
2249 return DSC_class;
2250 if (Context == Declarator::FileContext)
2251 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002252 if (Context == Declarator::TrailingReturnContext)
2253 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002254 return DSC_normal;
2255}
2256
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002257/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2258///
2259/// FIXME: Simply returns an alignof() expression if the argument is a
2260/// type. Ideally, the type should be propagated directly into Sema.
2261///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002262/// [C11] type-id
2263/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002264/// [C++0x] type-id ...[opt]
2265/// [C++0x] assignment-expression ...[opt]
2266ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2267 SourceLocation &EllipsisLoc) {
2268 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002269 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002270 SourceLocation TypeLoc = Tok.getLocation();
2271 ParsedType Ty = ParseTypeName().get();
2272 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002273 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2274 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002275 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002276 ER = ParseConstantExpression();
2277
Richard Smith80ad52f2013-01-02 11:42:31 +00002278 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002279 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002280
2281 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002282}
2283
2284/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2285/// attribute to Attrs.
2286///
2287/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002288/// [C11] '_Alignas' '(' type-id ')'
2289/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002290/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2291/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002292void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002293 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002294 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2295 "Not an alignment-specifier!");
2296
Richard Smith33f04a22013-01-29 01:48:07 +00002297 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2298 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002299
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002300 BalancedDelimiterTracker T(*this, tok::l_paren);
2301 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002302 return;
2303
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002304 SourceLocation EllipsisLoc;
2305 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002306 if (ArgExpr.isInvalid()) {
2307 SkipUntil(tok::r_paren);
2308 return;
2309 }
2310
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002311 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002312 if (EndLoc)
2313 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002314
Aaron Ballman624421f2013-08-31 01:11:41 +00002315 ArgsVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002316 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman624421f2013-08-31 01:11:41 +00002317 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2318 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002319}
2320
Reid Spencer5f016e22007-07-11 17:01:13 +00002321/// ParseDeclarationSpecifiers
2322/// declaration-specifiers: [C99 6.7]
2323/// storage-class-specifier declaration-specifiers[opt]
2324/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002325/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002326/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002327/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002328/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002329///
2330/// storage-class-specifier: [C99 6.7.1]
2331/// 'typedef'
2332/// 'extern'
2333/// 'static'
2334/// 'auto'
2335/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002336/// [C++] 'mutable'
Richard Smithec642442013-04-12 22:46:28 +00002337/// [C++11] 'thread_local'
2338/// [C11] '_Thread_local'
Reid Spencer5f016e22007-07-11 17:01:13 +00002339/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002340/// function-specifier: [C99 6.7.4]
2341/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002342/// [C++] 'virtual'
2343/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002344/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002345/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002346/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002347
Reid Spencer5f016e22007-07-11 17:01:13 +00002348///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002349void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002350 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002351 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002352 DeclSpecContext DSContext,
2353 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002354 if (DS.getSourceRange().isInvalid()) {
2355 DS.SetRangeStart(Tok.getLocation());
2356 DS.SetRangeEnd(Tok.getLocation());
2357 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002358
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002359 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002360 bool AttrsLastTime = false;
2361 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002363 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002364 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002365 unsigned DiagID = 0;
2366
Reid Spencer5f016e22007-07-11 17:01:13 +00002367 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002368
Reid Spencer5f016e22007-07-11 17:01:13 +00002369 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002370 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002371 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002372 if (!AttrsLastTime)
2373 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002374 else {
2375 // Reject C++11 attributes that appertain to decl specifiers as
2376 // we don't support any C++11 attributes that appertain to decl
2377 // specifiers. This also conforms to what g++ 4.8 is doing.
2378 ProhibitCXX11Attributes(attrs);
2379
Sean Hunt2edf0a22012-06-23 05:07:58 +00002380 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002381 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002382
Reid Spencer5f016e22007-07-11 17:01:13 +00002383 // If this is not a declaration specifier token, we're done reading decl
2384 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002385 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002386 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Sean Hunt2edf0a22012-06-23 05:07:58 +00002388 case tok::l_square:
2389 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002390 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002391 goto DoneWithDeclSpec;
2392
2393 ProhibitAttributes(attrs);
2394 // FIXME: It would be good to recover by accepting the attributes,
2395 // but attempting to do that now would cause serious
2396 // madness in terms of diagnostics.
2397 attrs.clear();
2398 attrs.Range = SourceRange();
2399
2400 ParseCXX11Attributes(attrs);
2401 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002402 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002403
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002404 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002405 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002406 if (DS.hasTypeSpecifier()) {
2407 bool AllowNonIdentifiers
2408 = (getCurScope()->getFlags() & (Scope::ControlScope |
2409 Scope::BlockScope |
2410 Scope::TemplateParamScope |
2411 Scope::FunctionPrototypeScope |
2412 Scope::AtCatchScope)) == 0;
2413 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002414 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002415 (DSContext == DSC_class && DS.isFriendSpecified());
2416
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002417 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002418 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002419 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002420 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002421 }
2422
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002423 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2424 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2425 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002426 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002427 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002428 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002429 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002430 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002431 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002432
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002433 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002434 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002435 }
2436
Chris Lattner5e02c472009-01-05 00:07:25 +00002437 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002438 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman5a428202013-08-15 23:59:20 +00002439 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002440 if (!DS.hasTypeSpecifier())
2441 DS.SetTypeSpecError();
2442 goto DoneWithDeclSpec;
2443 }
John McCall2e0a7152010-03-01 18:20:46 +00002444 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2445 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002446 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002447
2448 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002449 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002450 goto DoneWithDeclSpec;
2451
John McCallaa87d332009-12-12 11:40:51 +00002452 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002453 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2454 Tok.getAnnotationRange(),
2455 SS);
John McCallaa87d332009-12-12 11:40:51 +00002456
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002457 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002458 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002459 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002460 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002461 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002462 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002463
2464 // C++ [class.qual]p2:
2465 // In a lookup in which the constructor is an acceptable lookup
2466 // result and the nested-name-specifier nominates a class C:
2467 //
2468 // - if the name specified after the
2469 // nested-name-specifier, when looked up in C, is the
2470 // injected-class-name of C (Clause 9), or
2471 //
2472 // - if the name specified after the nested-name-specifier
2473 // is the same as the identifier or the
2474 // simple-template-id's template-name in the last
2475 // component of the nested-name-specifier,
2476 //
2477 // the name is instead considered to name the constructor of
2478 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002479 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002480 // Thus, if the template-name is actually the constructor
2481 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002482 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002483 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002484 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002485 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002486 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002487 if (isConstructorDeclarator()) {
2488 // The user meant this to be an out-of-line constructor
2489 // definition, but template arguments are not allowed
2490 // there. Just allow this as a constructor; we'll
2491 // complain about it later.
2492 goto DoneWithDeclSpec;
2493 }
2494
2495 // The user meant this to name a type, but it actually names
2496 // a constructor with some extraneous template
2497 // arguments. Complain, then parse it as a type as the user
2498 // intended.
2499 Diag(TemplateId->TemplateNameLoc,
2500 diag::err_out_of_line_template_id_names_constructor)
2501 << TemplateId->Name;
2502 }
2503
John McCallaa87d332009-12-12 11:40:51 +00002504 DS.getTypeSpecScope() = SS;
2505 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002506 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002507 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002508 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002509 continue;
2510 }
2511
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002512 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002513 DS.getTypeSpecScope() = SS;
2514 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002515 if (Tok.getAnnotationValue()) {
2516 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002517 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002518 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002519 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002520 if (isInvalid)
2521 break;
John McCallb3d87482010-08-24 05:47:05 +00002522 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002523 else
2524 DS.SetTypeSpecError();
2525 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2526 ConsumeToken(); // The typename
2527 }
2528
Douglas Gregor9135c722009-03-25 15:40:00 +00002529 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002530 goto DoneWithDeclSpec;
2531
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002532 // If we're in a context where the identifier could be a class name,
2533 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002534 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002535 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002536 &SS)) {
2537 if (isConstructorDeclarator())
2538 goto DoneWithDeclSpec;
2539
2540 // As noted in C++ [class.qual]p2 (cited above), when the name
2541 // of the class is qualified in a context where it could name
2542 // a constructor, its a constructor name. However, we've
2543 // looked at the declarator, and the user probably meant this
2544 // to be a type. Complain that it isn't supposed to be treated
2545 // as a type, then proceed to parse it as a type.
2546 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2547 << Next.getIdentifierInfo();
2548 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002549
John McCallb3d87482010-08-24 05:47:05 +00002550 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2551 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002552 getCurScope(), &SS,
2553 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002554 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002555 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002556
Chris Lattnerf4382f52009-04-14 22:17:06 +00002557 // If the referenced identifier is not a type, then this declspec is
2558 // erroneous: We already checked about that it has no type specifier, and
2559 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002560 // typename.
David Blaikie7247c882013-05-15 07:37:26 +00002561 if (!TypeRep) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002562 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002563 ParsedAttributesWithRange Attrs(AttrFactory);
2564 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2565 if (!Attrs.empty()) {
2566 AttrsLastTime = true;
2567 attrs.takeAllFrom(Attrs);
2568 }
2569 continue;
2570 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002571 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002572 }
Mike Stump1eb44332009-09-09 15:08:12 +00002573
John McCallaa87d332009-12-12 11:40:51 +00002574 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002575 ConsumeToken(); // The C++ scope.
2576
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002578 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002579 if (isInvalid)
2580 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002582 DS.SetRangeEnd(Tok.getLocation());
2583 ConsumeToken(); // The typename.
2584
2585 continue;
2586 }
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Chris Lattner80d0c892009-01-21 19:48:37 +00002588 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002589 if (Tok.getAnnotationValue()) {
2590 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002591 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002592 DiagID, T);
2593 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002594 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002595
Chris Lattner5c5db552010-04-05 18:18:31 +00002596 if (isInvalid)
2597 break;
2598
Chris Lattner80d0c892009-01-21 19:48:37 +00002599 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2600 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Chris Lattner80d0c892009-01-21 19:48:37 +00002602 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2603 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002604 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002605 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002606 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002607
Chris Lattner80d0c892009-01-21 19:48:37 +00002608 continue;
2609 }
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Douglas Gregorbfad9152011-04-28 15:48:45 +00002611 case tok::kw___is_signed:
2612 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2613 // typically treats it as a trait. If we see __is_signed as it appears
2614 // in libstdc++, e.g.,
2615 //
2616 // static const bool __is_signed;
2617 //
2618 // then treat __is_signed as an identifier rather than as a keyword.
2619 if (DS.getTypeSpecType() == TST_bool &&
2620 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2621 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2622 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2623 Tok.setKind(tok::identifier);
2624 }
2625
2626 // We're done with the declaration-specifiers.
2627 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002628
Chris Lattner3bd934a2008-07-26 01:18:38 +00002629 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002630 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002631 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002632 // In C++, check to see if this is a scope specifier like foo::bar::, if
2633 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002634 if (getLangOpts().CPlusPlus) {
Eli Friedman5a428202013-08-15 23:59:20 +00002635 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002636 if (!DS.hasTypeSpecifier())
2637 DS.SetTypeSpecError();
2638 goto DoneWithDeclSpec;
2639 }
2640 if (!Tok.is(tok::identifier))
2641 continue;
2642 }
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Chris Lattner3bd934a2008-07-26 01:18:38 +00002644 // This identifier can only be a typedef name if we haven't already seen
2645 // a type-specifier. Without this check we misparse:
2646 // typedef int X; struct Y { short X; }; as 'short int'.
2647 if (DS.hasTypeSpecifier())
2648 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002649
John Thompson82287d12010-02-05 00:12:22 +00002650 // Check for need to substitute AltiVec keyword tokens.
2651 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2652 break;
2653
Richard Smithf63eee72012-05-09 18:56:43 +00002654 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2655 // allow the use of a typedef name as a type specifier.
2656 if (DS.isTypeAltiVecVector())
2657 goto DoneWithDeclSpec;
2658
John McCallb3d87482010-08-24 05:47:05 +00002659 ParsedType TypeRep =
2660 Actions.getTypeName(*Tok.getIdentifierInfo(),
2661 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002662
Chris Lattnerc199ab32009-04-12 20:42:31 +00002663 // If this is not a typedef name, don't parse it as part of the declspec,
2664 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002665 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002666 ParsedAttributesWithRange Attrs(AttrFactory);
2667 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2668 if (!Attrs.empty()) {
2669 AttrsLastTime = true;
2670 attrs.takeAllFrom(Attrs);
2671 }
2672 continue;
2673 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002674 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002675 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002676
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002677 // If we're in a context where the identifier could be a class name,
2678 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002679 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002680 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002681 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002682 goto DoneWithDeclSpec;
2683
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002684 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002685 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002686 if (isInvalid)
2687 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002688
Chris Lattner3bd934a2008-07-26 01:18:38 +00002689 DS.SetRangeEnd(Tok.getLocation());
2690 ConsumeToken(); // The identifier
2691
2692 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2693 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002694 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002695 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002696 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002697
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002698 // Need to support trailing type qualifiers (e.g. "id<p> const").
2699 // If a type specifier follows, it will be diagnosed elsewhere.
2700 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002701 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002702
2703 // type-name
2704 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002705 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002706 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002707 // This template-id does not refer to a type name, so we're
2708 // done with the type-specifiers.
2709 goto DoneWithDeclSpec;
2710 }
2711
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002712 // If we're in a context where the template-id could be a
2713 // constructor name or specialization, check whether this is a
2714 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002715 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002716 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002717 isConstructorDeclarator())
2718 goto DoneWithDeclSpec;
2719
Douglas Gregor39a8de12009-02-25 19:37:18 +00002720 // Turn the template-id annotation token into a type annotation
2721 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002722 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002723 continue;
2724 }
2725
Reid Spencer5f016e22007-07-11 17:01:13 +00002726 // GNU attributes support.
2727 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002728 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002730
2731 // Microsoft declspec support.
2732 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002733 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002734 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Steve Naroff239f0732008-12-25 14:16:32 +00002736 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002737 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002738 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002739 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002740 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002741 // FIXME: This does not work correctly if it is set to be a declspec
2742 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman624421f2013-08-31 01:11:41 +00002743 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2744 AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002745 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002746 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002747
Aaron Ballmanaa9df092013-05-22 23:25:32 +00002748 case tok::kw___sptr:
2749 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00002750 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002751 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002752 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002753 case tok::kw___cdecl:
2754 case tok::kw___stdcall:
2755 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002756 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002757 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002758 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002759 continue;
2760
Dawn Perchik52fc3142010-09-03 01:29:35 +00002761 // Borland single token adornments.
2762 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002763 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002764 continue;
2765
Peter Collingbournef315fa82011-02-14 01:42:53 +00002766 // OpenCL single token adornments.
2767 case tok::kw___kernel:
2768 ParseOpenCLAttributes(DS.getAttributes());
2769 continue;
2770
Reid Spencer5f016e22007-07-11 17:01:13 +00002771 // storage-class-specifier
2772 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002773 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2774 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002775 break;
2776 case tok::kw_extern:
Richard Smithec642442013-04-12 22:46:28 +00002777 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002778 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002779 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2780 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002781 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002782 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002783 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2784 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002785 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002786 case tok::kw_static:
Richard Smithec642442013-04-12 22:46:28 +00002787 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002788 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002789 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2790 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002791 break;
2792 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002793 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002794 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002795 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2796 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002797 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002798 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002799 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002800 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002801 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2802 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002803 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002804 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2805 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002806 break;
2807 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002808 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2809 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002811 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002812 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2813 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002814 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002815 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00002816 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2817 PrevSpec, DiagID);
2818 break;
2819 case tok::kw_thread_local:
2820 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2821 PrevSpec, DiagID);
2822 break;
2823 case tok::kw__Thread_local:
2824 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2825 Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002826 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Reid Spencer5f016e22007-07-11 17:01:13 +00002828 // function-specifier
2829 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002830 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002831 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002832 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002833 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002834 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002835 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002836 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002837 break;
Richard Smithde03c152013-01-17 22:16:11 +00002838 case tok::kw__Noreturn:
2839 if (!getLangOpts().C11)
2840 Diag(Loc, diag::ext_c11_noreturn);
2841 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2842 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002843
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002844 // alignment-specifier
2845 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002846 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002847 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002848 ParseAlignmentSpecifier(DS.getAttributes());
2849 continue;
2850
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002851 // friend
2852 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002853 if (DSContext == DSC_class)
2854 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2855 else {
2856 PrevSpec = ""; // not actually used by the diagnostic
2857 DiagID = diag::err_friend_invalid_in_context;
2858 isInvalid = true;
2859 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002860 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002861
Douglas Gregor8d267c52011-09-09 02:06:17 +00002862 // Modules
2863 case tok::kw___module_private__:
2864 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2865 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002866
Sebastian Redl2ac67232009-11-05 15:47:02 +00002867 // constexpr
2868 case tok::kw_constexpr:
2869 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2870 break;
2871
Chris Lattner80d0c892009-01-21 19:48:37 +00002872 // type-specifier
2873 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002874 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2875 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002876 break;
2877 case tok::kw_long:
2878 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002879 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2880 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002881 else
John McCallfec54012009-08-03 20:12:06 +00002882 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2883 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002884 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002885 case tok::kw___int64:
2886 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2887 DiagID);
2888 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002889 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002890 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2891 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002892 break;
2893 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002894 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2895 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002896 break;
2897 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002898 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2899 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002900 break;
2901 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002902 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2903 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002904 break;
2905 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2907 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002908 break;
2909 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002910 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2911 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002912 break;
2913 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002914 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2915 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002916 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002917 case tok::kw___int128:
2918 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2919 DiagID);
2920 break;
2921 case tok::kw_half:
2922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2923 DiagID);
2924 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002925 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002926 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2927 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002928 break;
2929 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002930 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2931 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002932 break;
2933 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002934 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2935 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002936 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002937 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002938 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2939 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002940 break;
2941 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2943 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002944 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002945 case tok::kw_bool:
2946 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002947 if (Tok.is(tok::kw_bool) &&
2948 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2949 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2950 PrevSpec = ""; // Not used by the diagnostic.
2951 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002952 // For better error recovery.
2953 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002954 isInvalid = true;
2955 } else {
2956 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2957 DiagID);
2958 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002959 break;
2960 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2962 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002963 break;
2964 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2966 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002967 break;
2968 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2970 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002971 break;
John Thompson82287d12010-02-05 00:12:22 +00002972 case tok::kw___vector:
2973 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2974 break;
2975 case tok::kw___pixel:
2976 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2977 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002978 case tok::kw_image1d_t:
2979 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2980 PrevSpec, DiagID);
2981 break;
2982 case tok::kw_image1d_array_t:
2983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2984 PrevSpec, DiagID);
2985 break;
2986 case tok::kw_image1d_buffer_t:
2987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2988 PrevSpec, DiagID);
2989 break;
2990 case tok::kw_image2d_t:
2991 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2992 PrevSpec, DiagID);
2993 break;
2994 case tok::kw_image2d_array_t:
2995 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2996 PrevSpec, DiagID);
2997 break;
2998 case tok::kw_image3d_t:
2999 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3000 PrevSpec, DiagID);
3001 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00003002 case tok::kw_sampler_t:
3003 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3004 PrevSpec, DiagID);
3005 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00003006 case tok::kw_event_t:
3007 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3008 PrevSpec, DiagID);
3009 break;
John McCalla5fc4722011-04-09 22:50:59 +00003010 case tok::kw___unknown_anytype:
3011 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3012 PrevSpec, DiagID);
3013 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003014
3015 // class-specifier:
3016 case tok::kw_class:
3017 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003018 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00003019 case tok::kw_union: {
3020 tok::TokenKind Kind = Tok.getKind();
3021 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00003022
3023 // These are attributes following class specifiers.
3024 // To produce better diagnostic, we parse them when
3025 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003026 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00003027 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00003028 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003029
3030 // If there are attributes following class specifier,
3031 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003032 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00003033 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00003034 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003035 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003036 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00003037 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003038
3039 // enum-specifier:
3040 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00003041 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00003042 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00003043 continue;
3044
3045 // cv-qualifier:
3046 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003047 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003048 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003049 break;
3050 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003051 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003052 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003053 break;
3054 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003055 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003056 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003057 break;
3058
Douglas Gregord57959a2009-03-27 23:10:48 +00003059 // C++ typename-specifier:
3060 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00003061 if (TryAnnotateTypeOrScopeToken()) {
3062 DS.SetTypeSpecError();
3063 goto DoneWithDeclSpec;
3064 }
3065 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00003066 continue;
3067 break;
3068
Chris Lattner80d0c892009-01-21 19:48:37 +00003069 // GNU typeof support.
3070 case tok::kw_typeof:
3071 ParseTypeofSpecifier(DS);
3072 continue;
3073
David Blaikie42d6d0c2011-12-04 05:04:18 +00003074 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00003075 ParseDecltypeSpecifier(DS);
3076 continue;
3077
Sean Huntdb5d44b2011-05-19 05:37:45 +00003078 case tok::kw___underlying_type:
3079 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00003080 continue;
3081
3082 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003083 // C11 6.7.2.4/4:
3084 // If the _Atomic keyword is immediately followed by a left parenthesis,
3085 // it is interpreted as a type specifier (with a type name), not as a
3086 // type qualifier.
3087 if (NextToken().is(tok::l_paren)) {
3088 ParseAtomicSpecifier(DS);
3089 continue;
3090 }
3091 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3092 getLangOpts());
3093 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00003094
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003095 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003096 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003097 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003098 goto DoneWithDeclSpec;
3099 case tok::kw___private:
3100 case tok::kw___global:
3101 case tok::kw___local:
3102 case tok::kw___constant:
3103 case tok::kw___read_only:
3104 case tok::kw___write_only:
3105 case tok::kw___read_write:
3106 ParseOpenCLQualifiers(DS);
3107 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00003108
Steve Naroffd3ded1f2008-06-05 00:02:44 +00003109 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00003110 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00003111 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3112 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00003113 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00003114 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Douglas Gregor46f936e2010-11-19 17:10:50 +00003116 if (!ParseObjCProtocolQualifiers(DS))
3117 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3118 << FixItHint::CreateInsertion(Loc, "id")
3119 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00003120
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00003121 // Need to support trailing type qualifiers (e.g. "id<p> const").
3122 // If a type specifier follows, it will be diagnosed elsewhere.
3123 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00003124 }
John McCallfec54012009-08-03 20:12:06 +00003125 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00003126 if (isInvalid) {
3127 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00003128 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00003129
Douglas Gregorae2fb142010-08-23 14:34:43 +00003130 if (DiagID == diag::ext_duplicate_declspec)
3131 Diag(Tok, DiagID)
3132 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3133 else
3134 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003135 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00003136
Chris Lattner81c018d2008-03-13 06:29:04 +00003137 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003138 if (DiagID != diag::err_bool_redeclaration)
3139 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00003140
3141 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003142 }
3143}
Douglas Gregoradcac882008-12-01 23:54:00 +00003144
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003145/// ParseStructDeclaration - Parse a struct declaration without the terminating
3146/// semicolon.
3147///
Reid Spencer5f016e22007-07-11 17:01:13 +00003148/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003149/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003150/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003151/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003152/// struct-declarator-list:
3153/// struct-declarator
3154/// struct-declarator-list ',' struct-declarator
3155/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3156/// struct-declarator:
3157/// declarator
3158/// [GNU] declarator attributes[opt]
3159/// declarator[opt] ':' constant-expression
3160/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3161///
Chris Lattnere1359422008-04-10 06:46:29 +00003162void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003163ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003164
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003165 if (Tok.is(tok::kw___extension__)) {
3166 // __extension__ silences extension warnings in the subexpression.
3167 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003168 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003169 return ParseStructDeclaration(DS, Fields);
3170 }
Mike Stump1eb44332009-09-09 15:08:12 +00003171
Steve Naroff28a7ca82007-08-20 22:28:22 +00003172 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003173 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003174
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003175 // If there are no declarators, this is a free-standing declaration
3176 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003177 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003178 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3179 DS);
3180 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003181 return;
3182 }
3183
3184 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003185 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003186 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003187 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003188 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003189 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003190
Bill Wendlingad017fa2012-12-20 19:22:21 +00003191 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003192 if (!FirstDeclarator)
3193 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003194
Steve Naroff28a7ca82007-08-20 22:28:22 +00003195 /// struct-declarator: declarator
3196 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003197 if (Tok.isNot(tok::colon)) {
3198 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3199 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003200 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003201 }
Mike Stump1eb44332009-09-09 15:08:12 +00003202
Chris Lattner04d66662007-10-09 17:33:22 +00003203 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003204 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003205 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003206 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003207 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003208 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003209 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003210 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003211
Steve Naroff28a7ca82007-08-20 22:28:22 +00003212 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003213 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003214
John McCallbdd563e2009-11-03 02:38:08 +00003215 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003216 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003217
Steve Naroff28a7ca82007-08-20 22:28:22 +00003218 // If we don't have a comma, it is either the end of the list (a ';')
3219 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003220 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003221 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003222
Steve Naroff28a7ca82007-08-20 22:28:22 +00003223 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003224 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003225
John McCallbdd563e2009-11-03 02:38:08 +00003226 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003227 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003228}
3229
3230/// ParseStructUnionBody
3231/// struct-contents:
3232/// struct-declaration-list
3233/// [EXT] empty
3234/// [GNU] "struct-declaration-list" without terminatoring ';'
3235/// struct-declaration-list:
3236/// struct-declaration
3237/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003238/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003239///
Reid Spencer5f016e22007-07-11 17:01:13 +00003240void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003241 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003242 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3243 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003244 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003246 BalancedDelimiterTracker T(*this, tok::l_brace);
3247 if (T.consumeOpen())
3248 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003249
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003250 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003251 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003252
Chris Lattner5f9e2722011-07-23 10:55:15 +00003253 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003254
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003256 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003257 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003258
Reid Spencer5f016e22007-07-11 17:01:13 +00003259 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003260 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003261 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003262 continue;
3263 }
Chris Lattnere1359422008-04-10 06:46:29 +00003264
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003265 // Parse _Static_assert declaration.
3266 if (Tok.is(tok::kw__Static_assert)) {
3267 SourceLocation DeclEnd;
3268 ParseStaticAssertDeclaration(DeclEnd);
3269 continue;
3270 }
3271
Argyrios Kyrtzidisbd957452013-04-18 01:42:35 +00003272 if (Tok.is(tok::annot_pragma_pack)) {
3273 HandlePragmaPack();
3274 continue;
3275 }
3276
3277 if (Tok.is(tok::annot_pragma_align)) {
3278 HandlePragmaAlign();
3279 continue;
3280 }
3281
John McCallbdd563e2009-11-03 02:38:08 +00003282 if (!Tok.is(tok::at)) {
3283 struct CFieldCallback : FieldCallback {
3284 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003285 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003286 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003287
John McCalld226f652010-08-21 09:40:31 +00003288 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003289 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003290 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3291
Eli Friedmandcdff462012-08-08 23:53:27 +00003292 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003293 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003294 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003295 FD.D.getDeclSpec().getSourceRange().getBegin(),
3296 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003297 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003298 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003299 }
John McCallbdd563e2009-11-03 02:38:08 +00003300 } Callback(*this, TagDecl, FieldDecls);
3301
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003302 // Parse all the comma separated declarators.
3303 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003304 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003305 } else { // Handle @defs
3306 ConsumeToken();
3307 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3308 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003309 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003310 continue;
3311 }
3312 ConsumeToken();
3313 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3314 if (!Tok.is(tok::identifier)) {
3315 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003316 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003317 continue;
3318 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003319 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003320 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003321 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003322 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3323 ConsumeToken();
3324 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003325 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003326
Chris Lattner04d66662007-10-09 17:33:22 +00003327 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003328 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003329 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003330 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003331 break;
3332 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003333 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3334 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003335 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003336 // If we stopped at a ';', eat it.
3337 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003338 }
3339 }
Mike Stump1eb44332009-09-09 15:08:12 +00003340
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003341 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003342
John McCall0b7e6782011-03-24 11:26:52 +00003343 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003344 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003345 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003346
Douglas Gregor23c94db2010-07-02 17:43:08 +00003347 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003348 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003349 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003350 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003351 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003352 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3353 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003354}
3355
Reid Spencer5f016e22007-07-11 17:01:13 +00003356/// ParseEnumSpecifier
3357/// enum-specifier: [C99 6.7.2.2]
3358/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003359///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003360/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3361/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003362/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3363/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003364/// 'enum' identifier
3365/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003366///
Richard Smith1af83c42012-03-23 03:33:32 +00003367/// [C++11] enum-head '{' enumerator-list[opt] '}'
3368/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003369///
Richard Smith1af83c42012-03-23 03:33:32 +00003370/// enum-head: [C++11]
3371/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3372/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3373/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003374///
Richard Smith1af83c42012-03-23 03:33:32 +00003375/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003376/// 'enum'
3377/// 'enum' 'class'
3378/// 'enum' 'struct'
3379///
Richard Smith1af83c42012-03-23 03:33:32 +00003380/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003381/// ':' type-specifier-seq
3382///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003383/// [C++] elaborated-type-specifier:
3384/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3385///
Chris Lattner4c97d762009-04-12 21:49:30 +00003386void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003387 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003388 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003389 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003390 if (Tok.is(tok::code_completion)) {
3391 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003392 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003393 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003394 }
John McCall57c13002011-07-06 05:58:41 +00003395
Sean Hunt2edf0a22012-06-23 05:07:58 +00003396 // If attributes exist after tag, parse them.
3397 ParsedAttributesWithRange attrs(AttrFactory);
3398 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003399 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003400
3401 // If declspecs exist after tag, parse them.
3402 while (Tok.is(tok::kw___declspec))
3403 ParseMicrosoftDeclSpec(attrs);
3404
Richard Smithbdad7a22012-01-10 01:33:14 +00003405 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003406 bool IsScopedUsingClassTag = false;
3407
John McCall1e12b3d2012-06-23 22:30:04 +00003408 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieued5a2922013-04-23 02:47:36 +00003409 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3410 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3411 : diag::ext_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003412 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003413 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003414
Bill Wendlingad017fa2012-12-20 19:22:21 +00003415 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003416 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003417 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003418
3419 // They are allowed afterwards, though.
3420 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003421 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003422 while (Tok.is(tok::kw___declspec))
3423 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003424 }
Richard Smith1af83c42012-03-23 03:33:32 +00003425
John McCall13489672012-05-07 06:16:58 +00003426 // C++11 [temp.explicit]p12:
3427 // The usual access controls do not apply to names used to specify
3428 // explicit instantiations.
3429 // We extend this to also cover explicit specializations. Note that
3430 // we don't suppress if this turns out to be an elaborated type
3431 // specifier.
3432 bool shouldDelayDiagsInTag =
3433 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3434 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3435 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003436
Richard Smith7796eb52012-03-12 08:56:40 +00003437 // Enum definitions should not be parsed in a trailing-return-type.
3438 bool AllowDeclaration = DSC != DSC_trailing;
3439
3440 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003441 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003442 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003443
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003444 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003445 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003446 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3447 // if a fixed underlying type is allowed.
3448 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003449
3450 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003451 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003452 return;
3453
3454 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003455 Diag(Tok, diag::err_expected_ident);
3456 if (Tok.isNot(tok::l_brace)) {
3457 // Has no name and is not a definition.
3458 // Skip the rest of this declarator, up until the comma or semicolon.
3459 SkipUntil(tok::comma, true);
3460 return;
3461 }
3462 }
3463 }
Mike Stump1eb44332009-09-09 15:08:12 +00003464
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003465 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003466 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003467 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003468 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003470 // Skip the rest of this declarator, up until the comma or semicolon.
3471 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003473 }
Mike Stump1eb44332009-09-09 15:08:12 +00003474
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003475 // If an identifier is present, consume and remember it.
3476 IdentifierInfo *Name = 0;
3477 SourceLocation NameLoc;
3478 if (Tok.is(tok::identifier)) {
3479 Name = Tok.getIdentifierInfo();
3480 NameLoc = ConsumeToken();
3481 }
Mike Stump1eb44332009-09-09 15:08:12 +00003482
Richard Smithbdad7a22012-01-10 01:33:14 +00003483 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003484 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3485 // declaration of a scoped enumeration.
3486 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003487 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003488 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003489 }
3490
John McCall13489672012-05-07 06:16:58 +00003491 // Okay, end the suppression area. We'll decide whether to emit the
3492 // diagnostics in a second.
3493 if (shouldDelayDiagsInTag)
3494 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003495
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003496 TypeResult BaseType;
3497
Douglas Gregora61b3e72010-12-01 17:42:47 +00003498 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003499 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003500 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003501 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003502 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003503 // If we're in class scope, this can either be an enum declaration with
3504 // an underlying type, or a declaration of a bitfield member. We try to
3505 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003506 // (integer literal, sizeof); if it's still ambiguous, we then consider
3507 // anything that's a simple-type-specifier followed by '(' as an
3508 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003509 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003510 EnterExpressionEvaluationContext Unevaluated(Actions,
3511 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003512 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003513 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003514 // bit-field. This is the common case.
3515 if (TPR == TPResult::True())
3516 PossibleBitfield = true;
3517 // If the next token starts a type-specifier-seq, it may be either a
3518 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003519 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003520 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003521 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003522 GetLookAheadToken(2).getKind() == tok::semi) {
3523 // Consume the ':'.
3524 ConsumeToken();
3525 } else {
3526 // We have the start of a type-specifier-seq, so we have to perform
3527 // tentative parsing to determine whether we have an expression or a
3528 // type.
3529 TentativeParsingAction TPA(*this);
3530
3531 // Consume the ':'.
3532 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003533
3534 // If we see a type specifier followed by an open-brace, we have an
3535 // ambiguity between an underlying type and a C++11 braced
3536 // function-style cast. Resolve this by always treating it as an
3537 // underlying type.
3538 // FIXME: The standard is not entirely clear on how to disambiguate in
3539 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003540 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003541 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003542 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003543 // We'll parse this as a bitfield later.
3544 PossibleBitfield = true;
3545 TPA.Revert();
3546 } else {
3547 // We have a type-specifier-seq.
3548 TPA.Commit();
3549 }
3550 }
3551 } else {
3552 // Consume the ':'.
3553 ConsumeToken();
3554 }
3555
3556 if (!PossibleBitfield) {
3557 SourceRange Range;
3558 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003559
Richard Smith80ad52f2013-01-02 11:42:31 +00003560 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003561 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003562 } else if (!getLangOpts().ObjC2) {
3563 if (getLangOpts().CPlusPlus)
3564 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3565 else
3566 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3567 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003568 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003569 }
3570
Richard Smithbdad7a22012-01-10 01:33:14 +00003571 // There are four options here. If we have 'friend enum foo;' then this is a
3572 // friend declaration, and cannot have an accompanying definition. If we have
3573 // 'enum foo;', then this is a forward declaration. If we have
3574 // 'enum foo {...' then this is a definition. Otherwise we have something
3575 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003576 //
3577 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3578 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3579 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3580 //
John McCallf312b1e2010-08-26 23:41:50 +00003581 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003582 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003583 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003584 } else if (Tok.is(tok::l_brace)) {
3585 if (DS.isFriendSpecified()) {
3586 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3587 << SourceRange(DS.getFriendSpecLoc());
3588 ConsumeBrace();
3589 SkipUntil(tok::r_brace);
3590 TUK = Sema::TUK_Friend;
3591 } else {
3592 TUK = Sema::TUK_Definition;
3593 }
Richard Smithc9f35172012-06-25 21:37:02 +00003594 } else if (DSC != DSC_type_specifier &&
3595 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003596 (Tok.isAtStartOfLine() &&
3597 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003598 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3599 if (Tok.isNot(tok::semi)) {
3600 // A semicolon was missing after this declaration. Diagnose and recover.
3601 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3602 "enum");
3603 PP.EnterToken(Tok);
3604 Tok.setKind(tok::semi);
3605 }
John McCall13489672012-05-07 06:16:58 +00003606 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003607 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003608 }
3609
3610 // If this is an elaborated type specifier, and we delayed
3611 // diagnostics before, just merge them into the current pool.
3612 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3613 diagsFromTag.redelay();
3614 }
Richard Smith1af83c42012-03-23 03:33:32 +00003615
3616 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003617 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003618 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003619 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003620 // Skip the rest of this declarator, up until the comma or semicolon.
3621 Diag(Tok, diag::err_enum_template);
3622 SkipUntil(tok::comma, true);
3623 return;
3624 }
3625
3626 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3627 // Enumerations can't be explicitly instantiated.
3628 DS.SetTypeSpecError();
3629 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3630 return;
3631 }
3632
3633 assert(TemplateInfo.TemplateParams && "no template parameters");
3634 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3635 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003636 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003637
Sean Hunt2edf0a22012-06-23 05:07:58 +00003638 if (TUK == Sema::TUK_Reference)
3639 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003640
Douglas Gregorb9075602011-02-22 02:55:24 +00003641 if (!Name && TUK != Sema::TUK_Definition) {
3642 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003643
Douglas Gregorb9075602011-02-22 02:55:24 +00003644 // Skip the rest of this declarator, up until the comma or semicolon.
3645 SkipUntil(tok::comma, true);
3646 return;
3647 }
Richard Smith1af83c42012-03-23 03:33:32 +00003648
Douglas Gregor402abb52009-05-28 23:31:59 +00003649 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003650 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003651 const char *PrevSpec = 0;
3652 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003653 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003654 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003655 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003656 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003657 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003658
Douglas Gregor48c89f42010-04-24 16:38:41 +00003659 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003660 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003661 // dependent tag.
3662 if (!Name) {
3663 DS.SetTypeSpecError();
3664 Diag(Tok, diag::err_expected_type_name_after_typename);
3665 return;
3666 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003667
Douglas Gregor23c94db2010-07-02 17:43:08 +00003668 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003669 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003670 NameLoc);
3671 if (Type.isInvalid()) {
3672 DS.SetTypeSpecError();
3673 return;
3674 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003675
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003676 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3677 NameLoc.isValid() ? NameLoc : StartLoc,
3678 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003679 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003680
Douglas Gregor48c89f42010-04-24 16:38:41 +00003681 return;
3682 }
Mike Stump1eb44332009-09-09 15:08:12 +00003683
John McCalld226f652010-08-21 09:40:31 +00003684 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003685 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003686 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003687 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003688 ConsumeBrace();
3689 SkipUntil(tok::r_brace);
3690 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003691
Douglas Gregor48c89f42010-04-24 16:38:41 +00003692 DS.SetTypeSpecError();
3693 return;
3694 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003695
Richard Smithc9f35172012-06-25 21:37:02 +00003696 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003697 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003698
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003699 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3700 NameLoc.isValid() ? NameLoc : StartLoc,
3701 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003702 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003703}
3704
3705/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3706/// enumerator-list:
3707/// enumerator
3708/// enumerator-list ',' enumerator
3709/// enumerator:
3710/// enumeration-constant
3711/// enumeration-constant '=' constant-expression
3712/// enumeration-constant:
3713/// identifier
3714///
John McCalld226f652010-08-21 09:40:31 +00003715void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003716 // Enter the scope of the enum body and start the definition.
3717 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003718 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003719
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003720 BalancedDelimiterTracker T(*this, tok::l_brace);
3721 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003722
Chris Lattner7946dd32007-08-27 17:24:30 +00003723 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003724 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003725 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003726
Chris Lattner5f9e2722011-07-23 10:55:15 +00003727 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003728
John McCalld226f652010-08-21 09:40:31 +00003729 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003730
Reid Spencer5f016e22007-07-11 17:01:13 +00003731 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003732 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003733 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3734 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003735
John McCall5b629aa2010-10-22 23:36:17 +00003736 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003737 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003738 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003739 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003740 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003741
Reid Spencer5f016e22007-07-11 17:01:13 +00003742 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003743 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003744 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003745
Chris Lattner04d66662007-10-09 17:33:22 +00003746 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003747 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003748 AssignedVal = ParseConstantExpression();
3749 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003750 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 }
Mike Stump1eb44332009-09-09 15:08:12 +00003752
Reid Spencer5f016e22007-07-11 17:01:13 +00003753 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003754 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3755 LastEnumConstDecl,
3756 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003757 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003758 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003759 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003760
Reid Spencer5f016e22007-07-11 17:01:13 +00003761 EnumConstantDecls.push_back(EnumConstDecl);
3762 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003763
Douglas Gregor751f6922010-09-07 14:51:08 +00003764 if (Tok.is(tok::identifier)) {
3765 // We're missing a comma between enumerators.
3766 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003767 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003768 << FixItHint::CreateInsertion(Loc, ", ");
3769 continue;
3770 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003771
Chris Lattner04d66662007-10-09 17:33:22 +00003772 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003773 break;
3774 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003775
Richard Smith7fe62082011-10-15 05:09:34 +00003776 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003777 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003778 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3779 diag::ext_enumerator_list_comma_cxx :
3780 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003781 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003782 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003783 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3784 << FixItHint::CreateRemoval(CommaLoc);
3785 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003786 }
Mike Stump1eb44332009-09-09 15:08:12 +00003787
Reid Spencer5f016e22007-07-11 17:01:13 +00003788 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003789 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003790
Reid Spencer5f016e22007-07-11 17:01:13 +00003791 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003792 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003793 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003794
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003795 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenko9ff2b422013-04-27 20:23:52 +00003796 EnumDecl, EnumConstantDecls,
3797 getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003798 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003799
Douglas Gregor72de6672009-01-08 20:45:30 +00003800 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003801 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3802 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003803
3804 // The next token must be valid after an enum definition. If not, a ';'
3805 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003806 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3807 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003808 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3809 // Push this token back into the preprocessor and change our current token
3810 // to ';' so that the rest of the code recovers as though there were an
3811 // ';' after the definition.
3812 PP.EnterToken(Tok);
3813 Tok.setKind(tok::semi);
3814 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003815}
3816
3817/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003818/// start of a type-qualifier-list.
3819bool Parser::isTypeQualifier() const {
3820 switch (Tok.getKind()) {
3821 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003822
3823 // type-qualifier only in OpenCL
3824 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003825 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003826
Steve Naroff5f8aa692008-02-11 23:15:56 +00003827 // type-qualifier
3828 case tok::kw_const:
3829 case tok::kw_volatile:
3830 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003831 case tok::kw___private:
3832 case tok::kw___local:
3833 case tok::kw___global:
3834 case tok::kw___constant:
3835 case tok::kw___read_only:
3836 case tok::kw___read_write:
3837 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003838 return true;
3839 }
3840}
3841
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003842/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3843/// is definitely a type-specifier. Return false if it isn't part of a type
3844/// specifier or if we're not sure.
3845bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3846 switch (Tok.getKind()) {
3847 default: return false;
3848 // type-specifiers
3849 case tok::kw_short:
3850 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003851 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003852 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003853 case tok::kw_signed:
3854 case tok::kw_unsigned:
3855 case tok::kw__Complex:
3856 case tok::kw__Imaginary:
3857 case tok::kw_void:
3858 case tok::kw_char:
3859 case tok::kw_wchar_t:
3860 case tok::kw_char16_t:
3861 case tok::kw_char32_t:
3862 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003863 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003864 case tok::kw_float:
3865 case tok::kw_double:
3866 case tok::kw_bool:
3867 case tok::kw__Bool:
3868 case tok::kw__Decimal32:
3869 case tok::kw__Decimal64:
3870 case tok::kw__Decimal128:
3871 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003872
Guy Benyeib13621d2012-12-18 14:38:23 +00003873 // OpenCL specific types:
3874 case tok::kw_image1d_t:
3875 case tok::kw_image1d_array_t:
3876 case tok::kw_image1d_buffer_t:
3877 case tok::kw_image2d_t:
3878 case tok::kw_image2d_array_t:
3879 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003880 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003881 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003882
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003883 // struct-or-union-specifier (C99) or class-specifier (C++)
3884 case tok::kw_class:
3885 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003886 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003887 case tok::kw_union:
3888 // enum-specifier
3889 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003890
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003891 // typedef-name
3892 case tok::annot_typename:
3893 return true;
3894 }
3895}
3896
Steve Naroff5f8aa692008-02-11 23:15:56 +00003897/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003898/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003899bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003900 switch (Tok.getKind()) {
3901 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003902
Chris Lattner166a8fc2009-01-04 23:41:41 +00003903 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003904 if (TryAltiVecVectorToken())
3905 return true;
3906 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003907 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003908 // Annotate typenames and C++ scope specifiers. If we get one, just
3909 // recurse to handle whatever we get.
3910 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003911 return true;
3912 if (Tok.is(tok::identifier))
3913 return false;
3914 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003915
Chris Lattner166a8fc2009-01-04 23:41:41 +00003916 case tok::coloncolon: // ::foo::bar
3917 if (NextToken().is(tok::kw_new) || // ::new
3918 NextToken().is(tok::kw_delete)) // ::delete
3919 return false;
3920
Chris Lattner166a8fc2009-01-04 23:41:41 +00003921 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003922 return true;
3923 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Reid Spencer5f016e22007-07-11 17:01:13 +00003925 // GNU attributes support.
3926 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003927 // GNU typeof support.
3928 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003929
Reid Spencer5f016e22007-07-11 17:01:13 +00003930 // type-specifiers
3931 case tok::kw_short:
3932 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003933 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003934 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003935 case tok::kw_signed:
3936 case tok::kw_unsigned:
3937 case tok::kw__Complex:
3938 case tok::kw__Imaginary:
3939 case tok::kw_void:
3940 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003941 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003942 case tok::kw_char16_t:
3943 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003944 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003945 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003946 case tok::kw_float:
3947 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003948 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003949 case tok::kw__Bool:
3950 case tok::kw__Decimal32:
3951 case tok::kw__Decimal64:
3952 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003953 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003954
Guy Benyeib13621d2012-12-18 14:38:23 +00003955 // OpenCL specific types:
3956 case tok::kw_image1d_t:
3957 case tok::kw_image1d_array_t:
3958 case tok::kw_image1d_buffer_t:
3959 case tok::kw_image2d_t:
3960 case tok::kw_image2d_array_t:
3961 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003962 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003963 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003964
Chris Lattner99dc9142008-04-13 18:59:07 +00003965 // struct-or-union-specifier (C99) or class-specifier (C++)
3966 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003967 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003968 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003969 case tok::kw_union:
3970 // enum-specifier
3971 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003972
Reid Spencer5f016e22007-07-11 17:01:13 +00003973 // type-qualifier
3974 case tok::kw_const:
3975 case tok::kw_volatile:
3976 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003977
John McCallb8a8de32012-11-14 00:49:39 +00003978 // Debugger support.
3979 case tok::kw___unknown_anytype:
3980
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003981 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003982 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003983 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003984
Chris Lattner7c186be2008-10-20 00:25:30 +00003985 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3986 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003987 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003988
Steve Naroff239f0732008-12-25 14:16:32 +00003989 case tok::kw___cdecl:
3990 case tok::kw___stdcall:
3991 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003992 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003993 case tok::kw___w64:
3994 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003995 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003996 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003997 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003998
3999 case tok::kw___private:
4000 case tok::kw___local:
4001 case tok::kw___global:
4002 case tok::kw___constant:
4003 case tok::kw___read_only:
4004 case tok::kw___read_write:
4005 case tok::kw___write_only:
4006
Eli Friedman290eeb02009-06-08 23:27:34 +00004007 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004008
4009 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004010 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00004011
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004012 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004013 case tok::kw__Atomic:
4014 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004015 }
4016}
4017
4018/// isDeclarationSpecifier() - Return true if the current token is part of a
4019/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00004020///
4021/// \param DisambiguatingWithExpression True to indicate that the purpose of
4022/// this check is to disambiguate between an expression and a declaration.
4023bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004024 switch (Tok.getKind()) {
4025 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004026
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004027 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004028 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004029
Chris Lattner166a8fc2009-01-04 23:41:41 +00004030 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00004031 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00004032 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00004033 return false;
John Thompson82287d12010-02-05 00:12:22 +00004034 if (TryAltiVecVectorToken())
4035 return true;
4036 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004037 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00004038 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004039 // Annotate typenames and C++ scope specifiers. If we get one, just
4040 // recurse to handle whatever we get.
4041 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004042 return true;
4043 if (Tok.is(tok::identifier))
4044 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004045
Douglas Gregor9497a732010-09-16 01:51:54 +00004046 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00004047 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00004048 // expression is permitted, then this is probably a class message send
4049 // missing the initial '['. In this case, we won't consider this to be
4050 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00004051 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00004052 isStartOfObjCClassMessageMissingOpenBracket())
4053 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004054
John McCall9ba61662010-02-26 08:45:28 +00004055 return isDeclarationSpecifier();
4056
Chris Lattner166a8fc2009-01-04 23:41:41 +00004057 case tok::coloncolon: // ::foo::bar
4058 if (NextToken().is(tok::kw_new) || // ::new
4059 NextToken().is(tok::kw_delete)) // ::delete
4060 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004061
Chris Lattner166a8fc2009-01-04 23:41:41 +00004062 // Annotate typenames and C++ scope specifiers. If we get one, just
4063 // recurse to handle whatever we get.
4064 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004065 return true;
4066 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004067
Reid Spencer5f016e22007-07-11 17:01:13 +00004068 // storage-class-specifier
4069 case tok::kw_typedef:
4070 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00004071 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00004072 case tok::kw_static:
4073 case tok::kw_auto:
4074 case tok::kw_register:
4075 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00004076 case tok::kw_thread_local:
4077 case tok::kw__Thread_local:
Mike Stump1eb44332009-09-09 15:08:12 +00004078
Douglas Gregor8d267c52011-09-09 02:06:17 +00004079 // Modules
4080 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00004081
John McCallb8a8de32012-11-14 00:49:39 +00004082 // Debugger support
4083 case tok::kw___unknown_anytype:
4084
Reid Spencer5f016e22007-07-11 17:01:13 +00004085 // type-specifiers
4086 case tok::kw_short:
4087 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004088 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004089 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004090 case tok::kw_signed:
4091 case tok::kw_unsigned:
4092 case tok::kw__Complex:
4093 case tok::kw__Imaginary:
4094 case tok::kw_void:
4095 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004096 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004097 case tok::kw_char16_t:
4098 case tok::kw_char32_t:
4099
Reid Spencer5f016e22007-07-11 17:01:13 +00004100 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004101 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004102 case tok::kw_float:
4103 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004104 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004105 case tok::kw__Bool:
4106 case tok::kw__Decimal32:
4107 case tok::kw__Decimal64:
4108 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004109 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004110
Guy Benyeib13621d2012-12-18 14:38:23 +00004111 // OpenCL specific types:
4112 case tok::kw_image1d_t:
4113 case tok::kw_image1d_array_t:
4114 case tok::kw_image1d_buffer_t:
4115 case tok::kw_image2d_t:
4116 case tok::kw_image2d_array_t:
4117 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00004118 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00004119 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00004120
Chris Lattner99dc9142008-04-13 18:59:07 +00004121 // struct-or-union-specifier (C99) or class-specifier (C++)
4122 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004123 case tok::kw_struct:
4124 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00004125 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004126 // enum-specifier
4127 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004128
Reid Spencer5f016e22007-07-11 17:01:13 +00004129 // type-qualifier
4130 case tok::kw_const:
4131 case tok::kw_volatile:
4132 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004133
Reid Spencer5f016e22007-07-11 17:01:13 +00004134 // function-specifier
4135 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00004136 case tok::kw_virtual:
4137 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00004138 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004139
Richard Smith4cd81c52013-01-29 09:02:09 +00004140 // alignment-specifier
4141 case tok::kw__Alignas:
4142
Richard Smith53aec2a2012-10-25 00:00:53 +00004143 // friend keyword.
4144 case tok::kw_friend:
4145
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00004146 // static_assert-declaration
4147 case tok::kw__Static_assert:
4148
Chris Lattner1ef08762007-08-09 17:01:07 +00004149 // GNU typeof support.
4150 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004151
Chris Lattner1ef08762007-08-09 17:01:07 +00004152 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004153 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00004154
Richard Smith53aec2a2012-10-25 00:00:53 +00004155 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004156 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00004157 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00004158
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004159 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004160 case tok::kw__Atomic:
4161 return true;
4162
Chris Lattnerf3948c42008-07-26 03:38:44 +00004163 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4164 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004165 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004166
Douglas Gregord9d75e52011-04-27 05:41:15 +00004167 // typedef-name
4168 case tok::annot_typename:
4169 return !DisambiguatingWithExpression ||
4170 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00004171
Steve Naroff47f52092009-01-06 19:34:12 +00004172 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004173 case tok::kw___cdecl:
4174 case tok::kw___stdcall:
4175 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004176 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004177 case tok::kw___w64:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004178 case tok::kw___sptr:
4179 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004180 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004181 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004182 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004183 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004184 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004185
4186 case tok::kw___private:
4187 case tok::kw___local:
4188 case tok::kw___global:
4189 case tok::kw___constant:
4190 case tok::kw___read_only:
4191 case tok::kw___read_write:
4192 case tok::kw___write_only:
4193
Eli Friedman290eeb02009-06-08 23:27:34 +00004194 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004195 }
4196}
4197
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004198bool Parser::isConstructorDeclarator() {
4199 TentativeParsingAction TPA(*this);
4200
4201 // Parse the C++ scope specifier.
4202 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004203 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004204 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004205 TPA.Revert();
4206 return false;
4207 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004208
4209 // Parse the constructor name.
4210 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4211 // We already know that we have a constructor name; just consume
4212 // the token.
4213 ConsumeToken();
4214 } else {
4215 TPA.Revert();
4216 return false;
4217 }
4218
Richard Smith22592862012-03-27 23:05:05 +00004219 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004220 if (Tok.isNot(tok::l_paren)) {
4221 TPA.Revert();
4222 return false;
4223 }
4224 ConsumeParen();
4225
Richard Smith22592862012-03-27 23:05:05 +00004226 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4227 // that we have a constructor.
4228 if (Tok.is(tok::r_paren) ||
4229 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004230 TPA.Revert();
4231 return true;
4232 }
4233
4234 // If we need to, enter the specified scope.
4235 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004236 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004237 DeclScopeObj.EnterDeclaratorScope();
4238
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004239 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004240 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004241 MaybeParseMicrosoftAttributes(Attrs);
4242
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004243 // Check whether the next token(s) are part of a declaration
4244 // specifier, in which case we have the start of a parameter and,
4245 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004246 bool IsConstructor = false;
4247 if (isDeclarationSpecifier())
4248 IsConstructor = true;
4249 else if (Tok.is(tok::identifier) ||
4250 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4251 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4252 // This might be a parenthesized member name, but is more likely to
4253 // be a constructor declaration with an invalid argument type. Keep
4254 // looking.
4255 if (Tok.is(tok::annot_cxxscope))
4256 ConsumeToken();
4257 ConsumeToken();
4258
4259 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004260 // which must have one of the following syntactic forms (see the
4261 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004262 switch (Tok.getKind()) {
4263 case tok::l_paren:
4264 // C(X ( int));
4265 case tok::l_square:
4266 // C(X [ 5]);
4267 // C(X [ [attribute]]);
4268 case tok::coloncolon:
4269 // C(X :: Y);
4270 // C(X :: *p);
4271 case tok::r_paren:
4272 // C(X )
4273 // Assume this isn't a constructor, rather than assuming it's a
4274 // constructor with an unnamed parameter of an ill-formed type.
4275 break;
4276
4277 default:
4278 IsConstructor = true;
4279 break;
4280 }
4281 }
4282
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004283 TPA.Revert();
4284 return IsConstructor;
4285}
Reid Spencer5f016e22007-07-11 17:01:13 +00004286
4287/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004288/// type-qualifier-list: [C99 6.7.5]
4289/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004290/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004291/// [ only if VendorAttributesAllowed=true ]
4292/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004293/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004294/// [ only if VendorAttributesAllowed=true ]
4295/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004296/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004297/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004298///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004299void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4300 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004301 bool CXX11AttributesAllowed,
4302 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004303 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004304 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004305 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004306 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004307 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004308 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004309
4310 SourceLocation EndLoc;
4311
Reid Spencer5f016e22007-07-11 17:01:13 +00004312 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004313 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004314 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004315 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004316 SourceLocation Loc = Tok.getLocation();
4317
4318 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004319 case tok::code_completion:
4320 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004321 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004322
Reid Spencer5f016e22007-07-11 17:01:13 +00004323 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004324 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004325 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004326 break;
4327 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004328 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004329 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004330 break;
4331 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004332 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004333 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004334 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004335 case tok::kw__Atomic:
4336 if (!AtomicAllowed)
4337 goto DoneWithTypeQuals;
4338 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4339 getLangOpts());
4340 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004341
4342 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004343 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004344 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004345 goto DoneWithTypeQuals;
4346 case tok::kw___private:
4347 case tok::kw___global:
4348 case tok::kw___local:
4349 case tok::kw___constant:
4350 case tok::kw___read_only:
4351 case tok::kw___write_only:
4352 case tok::kw___read_write:
4353 ParseOpenCLQualifiers(DS);
4354 break;
4355
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004356 case tok::kw___sptr:
4357 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004358 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004359 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004360 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004361 case tok::kw___cdecl:
4362 case tok::kw___stdcall:
4363 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004364 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004365 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004366 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004367 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004368 continue;
4369 }
4370 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004371 case tok::kw___pascal:
4372 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004373 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004374 continue;
4375 }
4376 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004377 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004378 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004379 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004380 continue; // do *not* consume the next token!
4381 }
4382 // otherwise, FALL THROUGH!
4383 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004384 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004385 // If this is not a type-qualifier token, we're done reading type
4386 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004387 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004388 if (EndLoc.isValid())
4389 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004390 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004391 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004392
Reid Spencer5f016e22007-07-11 17:01:13 +00004393 // If the specifier combination wasn't legal, issue a diagnostic.
4394 if (isInvalid) {
4395 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004396 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004397 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004398 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004399 }
4400}
4401
4402
4403/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4404///
4405void Parser::ParseDeclarator(Declarator &D) {
4406 /// This implements the 'declarator' production in the C grammar, then checks
4407 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004408 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004409}
4410
Richard Smith9988f282012-03-29 01:16:42 +00004411static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4412 if (Kind == tok::star || Kind == tok::caret)
4413 return true;
4414
4415 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4416 if (!Lang.CPlusPlus)
4417 return false;
4418
4419 return Kind == tok::amp || Kind == tok::ampamp;
4420}
4421
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004422/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4423/// is parsed by the function passed to it. Pass null, and the direct-declarator
4424/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004425/// ptr-operator production.
4426///
Richard Smith0706df42011-10-19 21:33:05 +00004427/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004428/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4429/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004430///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004431/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4432/// [C] pointer[opt] direct-declarator
4433/// [C++] direct-declarator
4434/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004435///
4436/// pointer: [C99 6.7.5]
4437/// '*' type-qualifier-list[opt]
4438/// '*' type-qualifier-list[opt] pointer
4439///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004440/// ptr-operator:
4441/// '*' cv-qualifier-seq[opt]
4442/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004443/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004444/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004445/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004446/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004447void Parser::ParseDeclaratorInternal(Declarator &D,
4448 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004449 if (Diags.hasAllExtensionsSilenced())
4450 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004451
Sebastian Redlf30208a2009-01-24 21:16:55 +00004452 // C++ member pointers start with a '::' or a nested-name.
4453 // Member pointers get special handling, since there's no place for the
4454 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004455 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004456 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4457 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004458 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4459 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004460 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004461 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004462
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004463 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004464 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004465 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004466 if (D.mayHaveIdentifier())
4467 D.getCXXScopeSpec() = SS;
4468 else
4469 AnnotateScopeToken(SS, true);
4470
Sebastian Redlf30208a2009-01-24 21:16:55 +00004471 if (DirectDeclParser)
4472 (this->*DirectDeclParser)(D);
4473 return;
4474 }
4475
4476 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004477 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004478 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004479 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004480 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004481
4482 // Recurse to parse whatever is left.
4483 ParseDeclaratorInternal(D, DirectDeclParser);
4484
4485 // Sema will have to catch (syntactically invalid) pointers into global
4486 // scope. It has to catch pointers into namespace scope anyway.
4487 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004488 Loc),
4489 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004490 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004491 return;
4492 }
4493 }
4494
4495 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004496 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004497 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004498 if (DirectDeclParser)
4499 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004500 return;
4501 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004502
Sebastian Redl05532f22009-03-15 22:02:01 +00004503 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4504 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004505 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004506 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004507
Chris Lattner9af55002009-03-27 04:18:06 +00004508 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004509 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004510 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004511
Richard Smith6ee326a2012-04-10 01:32:12 +00004512 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004513 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004514 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004515
Reid Spencer5f016e22007-07-11 17:01:13 +00004516 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004517 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004518 if (Kind == tok::star)
4519 // Remember that we parsed a pointer type, and remember the type-quals.
4520 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004521 DS.getConstSpecLoc(),
4522 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004523 DS.getRestrictSpecLoc()),
4524 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004525 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004526 else
4527 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004528 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004529 Loc),
4530 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004531 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004532 } else {
4533 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004534 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004535
Sebastian Redl743de1f2009-03-23 00:00:23 +00004536 // Complain about rvalue references in C++03, but then go on and build
4537 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004538 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004539 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004540 diag::warn_cxx98_compat_rvalue_reference :
4541 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004542
Richard Smith6ee326a2012-04-10 01:32:12 +00004543 // GNU-style and C++11 attributes are allowed here, as is restrict.
4544 ParseTypeQualifierListOpt(DS);
4545 D.ExtendWithDeclSpec(DS);
4546
Reid Spencer5f016e22007-07-11 17:01:13 +00004547 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4548 // cv-qualifiers are introduced through the use of a typedef or of a
4549 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004550 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4551 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4552 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004553 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004554 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4555 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004556 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004557 // 'restrict' is permitted as an extension.
4558 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4559 Diag(DS.getAtomicSpecLoc(),
4560 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004561 }
4562
4563 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004564 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004565
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004566 if (D.getNumTypeObjects() > 0) {
4567 // C++ [dcl.ref]p4: There shall be no references to references.
4568 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4569 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004570 if (const IdentifierInfo *II = D.getIdentifier())
4571 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4572 << II;
4573 else
4574 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4575 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004576
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004577 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004578 // can go ahead and build the (technically ill-formed)
4579 // declarator: reference collapsing will take care of it.
4580 }
4581 }
4582
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004583 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004584 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004585 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004586 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004587 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004588 }
4589}
4590
Richard Smith9988f282012-03-29 01:16:42 +00004591static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4592 SourceLocation EllipsisLoc) {
4593 if (EllipsisLoc.isValid()) {
4594 FixItHint Insertion;
4595 if (!D.getEllipsisLoc().isValid()) {
4596 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4597 D.setEllipsisLoc(EllipsisLoc);
4598 }
4599 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4600 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4601 }
4602}
4603
Reid Spencer5f016e22007-07-11 17:01:13 +00004604/// ParseDirectDeclarator
4605/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004606/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004607/// '(' declarator ')'
4608/// [GNU] '(' attributes declarator ')'
4609/// [C90] direct-declarator '[' constant-expression[opt] ']'
4610/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4611/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4612/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4613/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004614/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4615/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004616/// direct-declarator '(' parameter-type-list ')'
4617/// direct-declarator '(' identifier-list[opt] ')'
4618/// [GNU] direct-declarator '(' parameter-forward-declarations
4619/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004620/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4621/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004622/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4623/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4624/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004625/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004626/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004627///
4628/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004629/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004630/// '::'[opt] nested-name-specifier[opt] type-name
4631///
4632/// id-expression: [C++ 5.1]
4633/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004634/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004635///
4636/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004637/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004638/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004639/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004640/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004641/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004642///
Richard Smith5d8388c2012-03-27 01:42:32 +00004643/// Note, any additional constructs added here may need corresponding changes
4644/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004645void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004646 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004647
David Blaikie4e4d0842012-03-11 07:00:24 +00004648 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004649 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004650 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004651 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4652 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004653 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004654 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004655 }
4656
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004657 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004658 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004659 // Change the declaration context for name lookup, until this function
4660 // is exited (and the declarator has been parsed).
4661 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004662 }
4663
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004664 // C++0x [dcl.fct]p14:
4665 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004666 // of a parameter-declaration-clause without a preceding comma. In
4667 // this case, the ellipsis is parsed as part of the
4668 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004669 // parameter pack that has not been expanded; otherwise, it is parsed
4670 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004671 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004672 !((D.getContext() == Declarator::PrototypeContext ||
4673 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004674 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004675 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004676 !Actions.containsUnexpandedParameterPacks(D))) {
4677 SourceLocation EllipsisLoc = ConsumeToken();
4678 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4679 // The ellipsis was put in the wrong place. Recover, and explain to
4680 // the user what they should have done.
4681 ParseDeclarator(D);
4682 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4683 return;
4684 } else
4685 D.setEllipsisLoc(EllipsisLoc);
4686
4687 // The ellipsis can't be followed by a parenthesized declarator. We
4688 // check for that in ParseParenDeclarator, after we have disambiguated
4689 // the l_paren token.
4690 }
4691
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004692 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4693 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4694 // We found something that indicates the start of an unqualified-id.
4695 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004696 bool AllowConstructorName;
4697 if (D.getDeclSpec().hasTypeSpecifier())
4698 AllowConstructorName = false;
4699 else if (D.getCXXScopeSpec().isSet())
4700 AllowConstructorName =
4701 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004702 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004703 else
4704 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4705
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004706 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004707 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4708 /*EnteringContext=*/true,
4709 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004710 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004711 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004712 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004713 D.getName()) ||
4714 // Once we're past the identifier, if the scope was bad, mark the
4715 // whole declarator bad.
4716 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004717 D.SetIdentifier(0, Tok.getLocation());
4718 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004719 } else {
4720 // Parsed the unqualified-id; update range information and move along.
4721 if (D.getSourceRange().getBegin().isInvalid())
4722 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4723 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004724 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004725 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004726 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004727 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004728 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004729 "There's a C++-specific check for tok::identifier above");
4730 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4731 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4732 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004733 goto PastIdentifier;
Richard Smitha38253c2013-07-11 05:10:21 +00004734 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
4735 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4736 << FixItHint::CreateRemoval(Tok.getLocation());
4737 D.SetIdentifier(0, Tok.getLocation());
4738 ConsumeToken();
4739 goto PastIdentifier;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004740 }
Richard Smith9988f282012-03-29 01:16:42 +00004741
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004742 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004743 // direct-declarator: '(' declarator ')'
4744 // direct-declarator: '(' attributes declarator ')'
4745 // Example: 'char (*X)' or 'int (*XX)(void)'
4746 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004747
4748 // If the declarator was parenthesized, we entered the declarator
4749 // scope when parsing the parenthesized declarator, then exited
4750 // the scope already. Re-enter the scope, if we need to.
4751 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004752 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004753 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004754 if (!D.isInvalidType() &&
4755 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004756 // Change the declaration context for name lookup, until this function
4757 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004758 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004759 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004760 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004761 // This could be something simple like "int" (in which case the declarator
4762 // portion is empty), if an abstract-declarator is allowed.
4763 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004764
4765 // The grammar for abstract-pack-declarator does not allow grouping parens.
4766 // FIXME: Revisit this once core issue 1488 is resolved.
4767 if (D.hasEllipsis() && D.hasGroupingParens())
4768 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4769 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004770 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004771 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004772 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004773 if (D.getContext() == Declarator::MemberContext)
4774 Diag(Tok, diag::err_expected_member_name_or_semi)
4775 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004776 else if (getLangOpts().CPlusPlus) {
4777 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4778 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4779 else
4780 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4781 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004782 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004783 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004784 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004785 }
Mike Stump1eb44332009-09-09 15:08:12 +00004786
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004787 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004788 assert(D.isPastIdentifier() &&
4789 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004790
Richard Smith6ee326a2012-04-10 01:32:12 +00004791 // Don't parse attributes unless we have parsed an unparenthesized name.
4792 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004793 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004794
Reid Spencer5f016e22007-07-11 17:01:13 +00004795 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004796 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004797 // Enter function-declaration scope, limiting any declarators to the
4798 // function prototype scope, including parameter declarators.
4799 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004800 Scope::FunctionPrototypeScope|Scope::DeclScope|
4801 (D.isFunctionDeclaratorAFunctionDeclaration()
4802 ? Scope::FunctionDeclarationScope : 0));
4803
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004804 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4805 // In such a case, check if we actually have a function declarator; if it
4806 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004807 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004808 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4809 // The name of the declarator, if any, is tentatively declared within
4810 // a possible direct initializer.
4811 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4812 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4813 TentativelyDeclaredIdentifiers.pop_back();
4814 if (!IsFunctionDecl)
4815 break;
4816 }
John McCall0b7e6782011-03-24 11:26:52 +00004817 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004818 BalancedDelimiterTracker T(*this, tok::l_paren);
4819 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004820 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004821 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004822 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004823 ParseBracketDeclarator(D);
4824 } else {
4825 break;
4826 }
4827 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004828}
Reid Spencer5f016e22007-07-11 17:01:13 +00004829
Chris Lattneref4715c2008-04-06 05:45:57 +00004830/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4831/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004832/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004833/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4834///
4835/// direct-declarator:
4836/// '(' declarator ')'
4837/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004838/// direct-declarator '(' parameter-type-list ')'
4839/// direct-declarator '(' identifier-list[opt] ')'
4840/// [GNU] direct-declarator '(' parameter-forward-declarations
4841/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004842///
4843void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004844 BalancedDelimiterTracker T(*this, tok::l_paren);
4845 T.consumeOpen();
4846
Chris Lattneref4715c2008-04-06 05:45:57 +00004847 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004848
Chris Lattner7399ee02008-10-20 02:05:46 +00004849 // Eat any attributes before we look at whether this is a grouping or function
4850 // declarator paren. If this is a grouping paren, the attribute applies to
4851 // the type being built up, for example:
4852 // int (__attribute__(()) *x)(long y)
4853 // If this ends up not being a grouping paren, the attribute applies to the
4854 // first argument, for example:
4855 // int (__attribute__(()) int x)
4856 // In either case, we need to eat any attributes to be able to determine what
4857 // sort of paren this is.
4858 //
John McCall0b7e6782011-03-24 11:26:52 +00004859 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004860 bool RequiresArg = false;
4861 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004862 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004863
Chris Lattner7399ee02008-10-20 02:05:46 +00004864 // We require that the argument list (if this is a non-grouping paren) be
4865 // present even if the attribute list was empty.
4866 RequiresArg = true;
4867 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004868
Steve Naroff239f0732008-12-25 14:16:32 +00004869 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004870 ParseMicrosoftTypeAttributes(attrs);
4871
Dawn Perchik52fc3142010-09-03 01:29:35 +00004872 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004873 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004874 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004875
Chris Lattneref4715c2008-04-06 05:45:57 +00004876 // If we haven't past the identifier yet (or where the identifier would be
4877 // stored, if this is an abstract declarator), then this is probably just
4878 // grouping parens. However, if this could be an abstract-declarator, then
4879 // this could also be the start of function arguments (consider 'void()').
4880 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004881
Chris Lattneref4715c2008-04-06 05:45:57 +00004882 if (!D.mayOmitIdentifier()) {
4883 // If this can't be an abstract-declarator, this *must* be a grouping
4884 // paren, because we haven't seen the identifier yet.
4885 isGrouping = true;
4886 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004887 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4888 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004889 isDeclarationSpecifier() || // 'int(int)' is a function.
4890 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004891 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4892 // considered to be a type, not a K&R identifier-list.
4893 isGrouping = false;
4894 } else {
4895 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4896 isGrouping = true;
4897 }
Mike Stump1eb44332009-09-09 15:08:12 +00004898
Chris Lattneref4715c2008-04-06 05:45:57 +00004899 // If this is a grouping paren, handle:
4900 // direct-declarator: '(' declarator ')'
4901 // direct-declarator: '(' attributes declarator ')'
4902 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004903 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4904 D.setEllipsisLoc(SourceLocation());
4905
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004906 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004907 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004908 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004909 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004910 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004911 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004912 T.getCloseLocation()),
4913 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004914
4915 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004916
4917 // An ellipsis cannot be placed outside parentheses.
4918 if (EllipsisLoc.isValid())
4919 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4920
Chris Lattneref4715c2008-04-06 05:45:57 +00004921 return;
4922 }
Mike Stump1eb44332009-09-09 15:08:12 +00004923
Chris Lattneref4715c2008-04-06 05:45:57 +00004924 // Okay, if this wasn't a grouping paren, it must be the start of a function
4925 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004926 // identifier (and remember where it would have been), then call into
4927 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004928 D.SetIdentifier(0, Tok.getLocation());
4929
David Blaikie42d6d0c2011-12-04 05:04:18 +00004930 // Enter function-declaration scope, limiting any declarators to the
4931 // function prototype scope, including parameter declarators.
4932 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004933 Scope::FunctionPrototypeScope | Scope::DeclScope |
4934 (D.isFunctionDeclaratorAFunctionDeclaration()
4935 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004936 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004937 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004938}
4939
4940/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4941/// declarator D up to a paren, which indicates that we are parsing function
4942/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004943///
Richard Smith6ee326a2012-04-10 01:32:12 +00004944/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4945/// immediately after the open paren - they should be considered to be the
4946/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004947///
Richard Smith6ee326a2012-04-10 01:32:12 +00004948/// If RequiresArg is true, then the first argument of the function is required
4949/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004950///
Richard Smith6ee326a2012-04-10 01:32:12 +00004951/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4952/// (C++11) ref-qualifier[opt], exception-specification[opt],
4953/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4954///
4955/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004956/// dynamic-exception-specification
4957/// noexcept-specification
4958///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004959void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004960 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004961 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004962 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004963 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004964 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004965 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004966 // lparen is already consumed!
4967 assert(D.isPastIdentifier() && "Should not call before identifier!");
4968
4969 // This should be true when the function has typed arguments.
4970 // Otherwise, it is treated as a K&R-style function.
4971 bool HasProto = false;
4972 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004973 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004974 // Remember where we see an ellipsis, if any.
4975 SourceLocation EllipsisLoc;
4976
4977 DeclSpec DS(AttrFactory);
4978 bool RefQualifierIsLValueRef = true;
4979 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004980 SourceLocation ConstQualifierLoc;
4981 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004982 ExceptionSpecificationType ESpecType = EST_None;
4983 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004984 SmallVector<ParsedType, 2> DynamicExceptions;
4985 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004986 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004987 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004988 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004989
James Molloy16f1f712012-02-29 10:24:19 +00004990 Actions.ActOnStartFunctionDeclarator();
Manuel Klimek152b4e42013-08-22 12:12:24 +00004991
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004992 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4993 EndLoc is the end location for the function declarator.
4994 They differ for trailing return types. */
4995 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004996 SourceLocation LParenLoc, RParenLoc;
4997 LParenLoc = Tracker.getOpenLocation();
4998 StartLoc = LParenLoc;
4999
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005000 if (isFunctionDeclaratorIdentifierList()) {
5001 if (RequiresArg)
5002 Diag(Tok, diag::err_argument_required_after_attribute);
5003
5004 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5005
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005006 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005007 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005008 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005009 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005010 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005011 if (Tok.isNot(tok::r_paren))
Manuel Klimek152b4e42013-08-22 12:12:24 +00005012 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005013 else if (RequiresArg)
5014 Diag(Tok, diag::err_argument_required_after_attribute);
5015
David Blaikie4e4d0842012-03-11 07:00:24 +00005016 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005017
5018 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005019 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005020 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005021 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005022 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005023
David Blaikie4e4d0842012-03-11 07:00:24 +00005024 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005025 // FIXME: Accept these components in any order, and produce fixits to
5026 // correct the order if the user gets it wrong. Ideally we should deal
5027 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005028
5029 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005030 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5031 /*CXX11AttributesAllowed*/ false,
5032 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00005033 if (!DS.getSourceRange().getEnd().isInvalid()) {
5034 EndLoc = DS.getSourceRange().getEnd();
5035 ConstQualifierLoc = DS.getConstSpecLoc();
5036 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5037 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005038
5039 // Parse ref-qualifier[opt].
5040 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00005041 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00005042 diag::warn_cxx98_compat_ref_qualifier :
5043 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00005044
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005045 RefQualifierIsLValueRef = Tok.is(tok::amp);
5046 RefQualifierLoc = ConsumeToken();
5047 EndLoc = RefQualifierLoc;
5048 }
5049
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005050 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00005051 // If a declaration declares a member function or member function
5052 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005053 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00005054 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005055 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00005056 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00005057 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00005058 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00005059 (D.getContext() == Declarator::MemberContext
5060 ? !D.getDeclSpec().isFriendSpecified()
5061 : D.getContext() == Declarator::FileContext &&
5062 D.getCXXScopeSpec().isValid() &&
5063 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005064 Sema::CXXThisScopeRAII ThisScope(Actions,
5065 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00005066 DS.getTypeQualifiers() |
Richard Smith84046262013-04-21 01:08:50 +00005067 (D.getDeclSpec().isConstexprSpecified() &&
5068 !getLangOpts().CPlusPlus1y
Richard Smith7b19cb12013-01-14 01:55:13 +00005069 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005070 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00005071
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005072 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00005073 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00005074 DynamicExceptions,
5075 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00005076 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005077 if (ESpecType != EST_None)
5078 EndLoc = ESpecRange.getEnd();
5079
Richard Smith6ee326a2012-04-10 01:32:12 +00005080 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5081 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005082 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00005083
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005084 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005085 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00005086 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00005087 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005088 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5089 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005090 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00005091 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00005092 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005093 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005094 }
5095 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005096 }
5097
5098 // Remember that we parsed a function type, and remember the attributes.
5099 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005100 IsAmbiguous,
5101 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005102 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005103 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005104 DS.getTypeQualifiers(),
5105 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00005106 RefQualifierLoc, ConstQualifierLoc,
5107 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00005108 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005109 ESpecType, ESpecRange.getBegin(),
5110 DynamicExceptions.data(),
5111 DynamicExceptionRanges.data(),
5112 DynamicExceptions.size(),
5113 NoexceptExpr.isUsable() ?
5114 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005115 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005116 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00005117 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00005118
5119 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005120}
5121
5122/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5123/// identifier list form for a K&R-style function: void foo(a,b,c)
5124///
5125/// Note that identifier-lists are only allowed for normal declarators, not for
5126/// abstract-declarators.
5127bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00005128 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005129 && Tok.is(tok::identifier)
5130 && !TryAltiVecVectorToken()
5131 // K&R identifier lists can't have typedefs as identifiers, per C99
5132 // 6.7.5.3p11.
5133 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5134 // Identifier lists follow a really simple grammar: the identifiers can
5135 // be followed *only* by a ", identifier" or ")". However, K&R
5136 // identifier lists are really rare in the brave new modern world, and
5137 // it is very common for someone to typo a type in a non-K&R style
5138 // list. If we are presented with something like: "void foo(intptr x,
5139 // float y)", we don't want to start parsing the function declarator as
5140 // though it is a K&R style declarator just because intptr is an
5141 // invalid type.
5142 //
5143 // To handle this, we check to see if the token after the first
5144 // identifier is a "," or ")". Only then do we parse it as an
5145 // identifier list.
5146 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5147}
5148
5149/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5150/// we found a K&R-style identifier list instead of a typed parameter list.
5151///
5152/// After returning, ParamInfo will hold the parsed parameters.
5153///
5154/// identifier-list: [C99 6.7.5]
5155/// identifier
5156/// identifier-list ',' identifier
5157///
5158void Parser::ParseFunctionDeclaratorIdentifierList(
5159 Declarator &D,
Craig Topper6b9240e2013-07-05 19:34:19 +00005160 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005161 // If there was no identifier specified for the declarator, either we are in
5162 // an abstract-declarator, or we are in a parameter declarator which was found
5163 // to be abstract. In abstract-declarators, identifier lists are not valid:
5164 // diagnose this.
5165 if (!D.getIdentifier())
5166 Diag(Tok, diag::ext_ident_list_in_param);
5167
5168 // Maintain an efficient lookup of params we have seen so far.
5169 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5170
5171 while (1) {
5172 // If this isn't an identifier, report the error and skip until ')'.
5173 if (Tok.isNot(tok::identifier)) {
5174 Diag(Tok, diag::err_expected_ident);
5175 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5176 // Forget we parsed anything.
5177 ParamInfo.clear();
5178 return;
5179 }
5180
5181 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5182
5183 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5184 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5185 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5186
5187 // Verify that the argument identifier has not already been mentioned.
5188 if (!ParamsSoFar.insert(ParmII)) {
5189 Diag(Tok, diag::err_param_redefinition) << ParmII;
5190 } else {
5191 // Remember this identifier in ParamInfo.
5192 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5193 Tok.getLocation(),
5194 0));
5195 }
5196
5197 // Eat the identifier.
5198 ConsumeToken();
5199
5200 // The list continues if we see a comma.
5201 if (Tok.isNot(tok::comma))
5202 break;
5203 ConsumeToken();
5204 }
5205}
5206
5207/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5208/// after the opening parenthesis. This function will not parse a K&R-style
5209/// identifier list.
5210///
Richard Smith6ce48a72012-04-11 04:01:28 +00005211/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5212/// caller parsed those arguments immediately after the open paren - they should
5213/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005214///
5215/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5216/// be the location of the ellipsis, if any was parsed.
5217///
Reid Spencer5f016e22007-07-11 17:01:13 +00005218/// parameter-type-list: [C99 6.7.5]
5219/// parameter-list
5220/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005221/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005222///
5223/// parameter-list: [C99 6.7.5]
5224/// parameter-declaration
5225/// parameter-list ',' parameter-declaration
5226///
5227/// parameter-declaration: [C99 6.7.5]
5228/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005229/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005230/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005231/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005232/// declaration-specifiers abstract-declarator[opt]
5233/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005234/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005235/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005236/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005237///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005238void Parser::ParseParameterDeclarationClause(
5239 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005240 ParsedAttributes &FirstArgAttrs,
Craig Topper6b9240e2013-07-05 19:34:19 +00005241 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005242 SourceLocation &EllipsisLoc) {
Manuel Klimek152b4e42013-08-22 12:12:24 +00005243
Chris Lattnerf97409f2008-04-06 06:57:35 +00005244 while (1) {
5245 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005246 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5247 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005248 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005249 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005250 }
Mike Stump1eb44332009-09-09 15:08:12 +00005251
Chris Lattnerf97409f2008-04-06 06:57:35 +00005252 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005253 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005254 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005255
Richard Smith6ce48a72012-04-11 04:01:28 +00005256 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005257 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005258
John McCall7f040a92010-12-24 02:08:15 +00005259 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005260 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005261
5262 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005263
5264 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005265 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005266 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005267 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5268 // too much hassle.
5269 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005270
Chris Lattnere64c5492009-02-27 18:38:20 +00005271 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005272
Manuel Klimek152b4e42013-08-22 12:12:24 +00005273 // Parse the declarator. This is "PrototypeContext", because we must
5274 // accept either 'declarator' or 'abstract-declarator' here.
5275 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5276 ParseDeclarator(ParmDecl);
Chris Lattnerf97409f2008-04-06 06:57:35 +00005277
5278 // Parse GNU attributes, if present.
Manuel Klimek152b4e42013-08-22 12:12:24 +00005279 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005280
Chris Lattnerf97409f2008-04-06 06:57:35 +00005281 // Remember this parsed parameter in ParamInfo.
Manuel Klimek152b4e42013-08-22 12:12:24 +00005282 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005283
Douglas Gregor72b505b2008-12-16 21:30:33 +00005284 // DefArgToks is used when the parsing of default arguments needs
5285 // to be delayed.
5286 CachedTokens *DefArgToks = 0;
5287
Chris Lattnerf97409f2008-04-06 06:57:35 +00005288 // If no parameter was specified, verify that *something* was specified,
5289 // otherwise we have a missing type and identifier.
Manuel Klimek152b4e42013-08-22 12:12:24 +00005290 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5291 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005292 // Completely missing, emit error.
5293 Diag(DSStart, diag::err_missing_param);
5294 } else {
5295 // Otherwise, we have something. Add it and let semantic analysis try
5296 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005297
Chris Lattnerf97409f2008-04-06 06:57:35 +00005298 // Inform the actions module about the parameter declarator, so it gets
5299 // added to the current scope.
Manuel Klimek152b4e42013-08-22 12:12:24 +00005300 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
5301
Chris Lattner04421082008-04-08 04:40:51 +00005302 // Parse the default argument, if any. We parse the default
5303 // arguments in all dialects; the semantic analysis in
5304 // ActOnParamDefaultArgument will reject the default argument in
5305 // C.
5306 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005307 SourceLocation EqualLoc = Tok.getLocation();
5308
Chris Lattner04421082008-04-08 04:40:51 +00005309 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005310 if (D.getContext() == Declarator::MemberContext) {
5311 // If we're inside a class definition, cache the tokens
5312 // corresponding to the default argument. We'll actually parse
5313 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005314 // FIXME: Can we use a smart pointer for Toks?
5315 DefArgToks = new CachedTokens;
5316
Mike Stump1eb44332009-09-09 15:08:12 +00005317 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005318 /*StopAtSemi=*/true,
5319 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005320 delete DefArgToks;
5321 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005322 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005323 } else {
5324 // Mark the end of the default argument so that we know when to
5325 // stop when we parse it later on.
5326 Token DefArgEnd;
5327 DefArgEnd.startToken();
5328 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5329 DefArgEnd.setLocation(Tok.getLocation());
5330 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005331 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005332 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005333 }
Chris Lattner04421082008-04-08 04:40:51 +00005334 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005335 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005336 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005337
Chad Rosier8decdee2012-06-26 22:30:43 +00005338 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005339 // used.
5340 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005341 Sema::PotentiallyEvaluatedIfUsed,
5342 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005343
Sebastian Redl84407ba2012-03-14 15:54:00 +00005344 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005345 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005346 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005347 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005348 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005349 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005350 if (DefArgResult.isInvalid()) {
5351 Actions.ActOnParamDefaultArgumentError(Param);
5352 SkipUntil(tok::comma, tok::r_paren, true, true);
5353 } else {
5354 // Inform the actions module about the default argument
5355 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005356 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005357 }
Chris Lattner04421082008-04-08 04:40:51 +00005358 }
5359 }
Mike Stump1eb44332009-09-09 15:08:12 +00005360
5361 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Manuel Klimek152b4e42013-08-22 12:12:24 +00005362 ParmDecl.getIdentifierLoc(), Param,
5363 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005364 }
5365
5366 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005367 if (Tok.isNot(tok::comma)) {
5368 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005369 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005370
David Blaikie4e4d0842012-03-11 07:00:24 +00005371 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005372 // We have ellipsis without a preceding ',', which is ill-formed
5373 // in C. Complain and provide the fix.
5374 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005375 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005376 }
5377 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005378
Douglas Gregored5d6512009-09-22 21:41:40 +00005379 break;
5380 }
Mike Stump1eb44332009-09-09 15:08:12 +00005381
Chris Lattnerf97409f2008-04-06 06:57:35 +00005382 // Consume the comma.
5383 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005384 }
Mike Stump1eb44332009-09-09 15:08:12 +00005385
Chris Lattner66d28652008-04-06 06:34:08 +00005386}
Chris Lattneref4715c2008-04-06 05:45:57 +00005387
Reid Spencer5f016e22007-07-11 17:01:13 +00005388/// [C90] direct-declarator '[' constant-expression[opt] ']'
5389/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5390/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5391/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5392/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005393/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5394/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005395void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005396 if (CheckProhibitedCXX11Attribute())
5397 return;
5398
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005399 BalancedDelimiterTracker T(*this, tok::l_square);
5400 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005401
Chris Lattner378c7e42008-12-18 07:27:21 +00005402 // C array syntax has many features, but by-far the most common is [] and [4].
5403 // This code does a fast path to handle some of the most obvious cases.
5404 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005405 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005406 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005407 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005408
Chris Lattner378c7e42008-12-18 07:27:21 +00005409 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005410 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005411 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005412 T.getOpenLocation(),
5413 T.getCloseLocation()),
5414 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005415 return;
5416 } else if (Tok.getKind() == tok::numeric_constant &&
5417 GetLookAheadToken(1).is(tok::r_square)) {
5418 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005419 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005420 ConsumeToken();
5421
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005422 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005423 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005424 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005425
Chris Lattner378c7e42008-12-18 07:27:21 +00005426 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005427 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005428 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005429 T.getOpenLocation(),
5430 T.getCloseLocation()),
5431 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005432 return;
5433 }
Mike Stump1eb44332009-09-09 15:08:12 +00005434
Reid Spencer5f016e22007-07-11 17:01:13 +00005435 // If valid, this location is the position where we read the 'static' keyword.
5436 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005437 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005438 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005439
Reid Spencer5f016e22007-07-11 17:01:13 +00005440 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005441 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005442 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005443 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005444
Reid Spencer5f016e22007-07-11 17:01:13 +00005445 // If we haven't already read 'static', check to see if there is one after the
5446 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005447 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005448 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005449
Reid Spencer5f016e22007-07-11 17:01:13 +00005450 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5451 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005452 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005453
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005454 // Handle the case where we have '[*]' as the array size. However, a leading
5455 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005456 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005457 // infrequent, use of lookahead is not costly here.
5458 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005459 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005460
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005461 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005462 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005463 StaticLoc = SourceLocation(); // Drop the static.
5464 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005465 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005466 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005467 // Note, in C89, this production uses the constant-expr production instead
5468 // of assignment-expr. The only difference is that assignment-expr allows
5469 // things like '=' and '*='. Sema rejects these in C89 mode because they
5470 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005471
Douglas Gregore0762c92009-06-19 23:52:42 +00005472 // Parse the constant-expression or assignment-expression now (depending
5473 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005474 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005475 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005476 } else {
5477 EnterExpressionEvaluationContext Unevaluated(Actions,
5478 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005479 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005480 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005481 }
Mike Stump1eb44332009-09-09 15:08:12 +00005482
Reid Spencer5f016e22007-07-11 17:01:13 +00005483 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005484 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005485 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005486 // If the expression was invalid, skip it.
5487 SkipUntil(tok::r_square);
5488 return;
5489 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005490
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005491 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005492
John McCall0b7e6782011-03-24 11:26:52 +00005493 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005494 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005495
Chris Lattner378c7e42008-12-18 07:27:21 +00005496 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005497 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005498 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005499 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005500 T.getOpenLocation(),
5501 T.getCloseLocation()),
5502 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005503}
5504
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005505/// [GNU] typeof-specifier:
5506/// typeof ( expressions )
5507/// typeof ( type-name )
5508/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005509///
5510void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005511 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005512 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005513 SourceLocation StartLoc = ConsumeToken();
5514
John McCallcfb708c2010-01-13 20:03:27 +00005515 const bool hasParens = Tok.is(tok::l_paren);
5516
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005517 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5518 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005519
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005520 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005521 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005522 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005523 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5524 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005525 if (hasParens)
5526 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005527
5528 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005529 // FIXME: Not accurate, the range gets one token more than it should.
5530 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005531 else
5532 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005533
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005534 if (isCastExpr) {
5535 if (!CastTy) {
5536 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005537 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005538 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005539
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005540 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005541 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005542 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5543 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005544 DiagID, CastTy))
5545 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005546 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005547 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005548
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005549 // If we get here, the operand to the typeof was an expresion.
5550 if (Operand.isInvalid()) {
5551 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005552 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005553 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005554
Eli Friedman71b8fb52012-01-21 01:01:51 +00005555 // We might need to transform the operand if it is potentially evaluated.
5556 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5557 if (Operand.isInvalid()) {
5558 DS.SetTypeSpecError();
5559 return;
5560 }
5561
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005562 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005563 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005564 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5565 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005566 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005567 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005568}
Chris Lattner1b492422010-02-28 18:33:55 +00005569
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005570/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005571/// _Atomic ( type-name )
5572///
5573void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005574 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5575 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005576
5577 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005578 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005579 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005580 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005581
5582 TypeResult Result = ParseTypeName();
5583 if (Result.isInvalid()) {
5584 SkipUntil(tok::r_paren);
5585 return;
5586 }
5587
5588 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005589 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005590
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005591 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005592 return;
5593
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005594 DS.setTypeofParensRange(T.getRange());
5595 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005596
5597 const char *PrevSpec = 0;
5598 unsigned DiagID;
5599 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5600 DiagID, Result.release()))
5601 Diag(StartLoc, DiagID) << PrevSpec;
5602}
5603
Chris Lattner1b492422010-02-28 18:33:55 +00005604
5605/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5606/// from TryAltiVecVectorToken.
5607bool Parser::TryAltiVecVectorTokenOutOfLine() {
5608 Token Next = NextToken();
5609 switch (Next.getKind()) {
5610 default: return false;
5611 case tok::kw_short:
5612 case tok::kw_long:
5613 case tok::kw_signed:
5614 case tok::kw_unsigned:
5615 case tok::kw_void:
5616 case tok::kw_char:
5617 case tok::kw_int:
5618 case tok::kw_float:
5619 case tok::kw_double:
5620 case tok::kw_bool:
5621 case tok::kw___pixel:
5622 Tok.setKind(tok::kw___vector);
5623 return true;
5624 case tok::identifier:
5625 if (Next.getIdentifierInfo() == Ident_pixel) {
5626 Tok.setKind(tok::kw___vector);
5627 return true;
5628 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005629 if (Next.getIdentifierInfo() == Ident_bool) {
5630 Tok.setKind(tok::kw___vector);
5631 return true;
5632 }
Chris Lattner1b492422010-02-28 18:33:55 +00005633 return false;
5634 }
5635}
5636
5637bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5638 const char *&PrevSpec, unsigned &DiagID,
5639 bool &isInvalid) {
5640 if (Tok.getIdentifierInfo() == Ident_vector) {
5641 Token Next = NextToken();
5642 switch (Next.getKind()) {
5643 case tok::kw_short:
5644 case tok::kw_long:
5645 case tok::kw_signed:
5646 case tok::kw_unsigned:
5647 case tok::kw_void:
5648 case tok::kw_char:
5649 case tok::kw_int:
5650 case tok::kw_float:
5651 case tok::kw_double:
5652 case tok::kw_bool:
5653 case tok::kw___pixel:
5654 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5655 return true;
5656 case tok::identifier:
5657 if (Next.getIdentifierInfo() == Ident_pixel) {
5658 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5659 return true;
5660 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005661 if (Next.getIdentifierInfo() == Ident_bool) {
5662 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5663 return true;
5664 }
Chris Lattner1b492422010-02-28 18:33:55 +00005665 break;
5666 default:
5667 break;
5668 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005669 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005670 DS.isTypeAltiVecVector()) {
5671 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5672 return true;
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005673 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5674 DS.isTypeAltiVecVector()) {
5675 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5676 return true;
Chris Lattner1b492422010-02-28 18:33:55 +00005677 }
5678 return false;
5679}