blob: 602c853d70c9582ab2ca1d4ec18e086f55878aa4 [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///
Richard Smith4c6c4112013-09-03 18:57:36 +0000103/// Whether an attribute takes an 'identifier' is determined by the
104/// attrib-name. GCC's behavior here is not worth imitating:
Reid Spencer5f016e22007-07-11 17:01:13 +0000105///
Richard Smith4c6c4112013-09-03 18:57:36 +0000106/// * In C mode, if the attribute argument list starts with an identifier
107/// followed by a ',' or an ')', and the identifier doesn't resolve to
108/// a type, it is parsed as an identifier. If the attribute actually
109/// wanted an expression, it's out of luck (but it turns out that no
110/// attributes work that way, because C constant expressions are very
111/// limited).
112/// * In C++ mode, if the attribute argument list starts with an identifier,
113/// and the attribute *wants* an identifier, it is parsed as an identifier.
114/// At block scope, any additional tokens between the identifier and the
115/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000116///
Richard Smith4c6c4112013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall7f040a92010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
120 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000121 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner04d66662007-10-09 17:33:22 +0000123 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 ConsumeToken();
125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
126 "attribute")) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000127 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000128 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000131 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000132 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 }
134 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000135 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
136 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000137 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
139 ConsumeToken();
140 continue;
141 }
142 // we have an identifier or declaration specifier (const, int, etc.)
143 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
144 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 if (Tok.is(tok::l_paren)) {
147 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000149 LateParsedAttribute *LA =
150 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
151 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000152
Bill Wendlingad017fa2012-12-20 19:22:21 +0000153 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000154 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000155 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000156 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000158 // consume everything up to and including the matching right parens
159 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000161 Token Eof;
162 Eof.startToken();
163 Eof.setLocation(Tok.getLocation());
164 LA->Toks.push_back(Eof);
165 } else {
Michael Han6880f492012-10-03 01:56:22 +0000166 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000167 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
169 } else {
Aaron Ballman624421f2013-08-31 01:11:41 +0000170 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
171 AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 }
173 }
174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Alexey Bataev8fe24752013-11-18 08:17:37 +0000175 SkipUntil(tok::r_paren, StopAtSemi);
Sean Huntbbd37c62009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Richard Smithd92aa2d2013-10-24 01:07:54 +0000177 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Alexey Bataev8fe24752013-11-18 08:17:37 +0000178 SkipUntil(tok::r_paren, StopAtSemi);
John McCall7f040a92010-12-24 02:08:15 +0000179 if (endLoc)
180 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
Aaron Ballman9feedb82013-11-04 12:55:56 +0000184/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
185static StringRef normalizeAttrName(StringRef Name) {
Richard Smithd92aa2d2013-10-24 01:07:54 +0000186 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
187 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman9feedb82013-11-04 12:55:56 +0000188 return Name;
189}
190
191/// \brief Determine whether the given attribute has an identifier argument.
192static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
193 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Richard Smithd92aa2d2013-10-24 01:07:54 +0000194#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000195 .Default(false);
196}
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000197
Aaron Ballman9feedb82013-11-04 12:55:56 +0000198/// \brief Determine whether the given attribute parses a type argument.
199static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
200 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
201#include "clang/Parse/AttrTypeArg.inc"
202 .Default(false);
203}
204
Richard Smith8edabd92013-09-03 18:01:40 +0000205IdentifierLoc *Parser::ParseIdentifierLoc() {
206 assert(Tok.is(tok::identifier) && "expected an identifier");
207 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
208 Tok.getLocation(),
209 Tok.getIdentifierInfo());
210 ConsumeToken();
211 return IL;
212}
213
Richard Smithd386fef2013-10-31 01:56:18 +0000214void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
215 SourceLocation AttrNameLoc,
216 ParsedAttributes &Attrs,
217 SourceLocation *EndLoc) {
218 BalancedDelimiterTracker Parens(*this, tok::l_paren);
219 Parens.consumeOpen();
220
221 TypeResult T;
222 if (Tok.isNot(tok::r_paren))
223 T = ParseTypeName();
224
225 if (Parens.consumeClose())
226 return;
227
228 if (T.isInvalid())
229 return;
230
231 if (T.isUsable())
232 Attrs.addNewTypeAttr(&AttrName,
233 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
234 AttrNameLoc, T.get(), AttributeList::AS_GNU);
235 else
236 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
237 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
238}
239
Michael Han6880f492012-10-03 01:56:22 +0000240/// Parse the arguments to a parameterized GNU attribute or
241/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000242void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
243 SourceLocation AttrNameLoc,
244 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000245 SourceLocation *EndLoc,
246 IdentifierInfo *ScopeName,
247 SourceLocation ScopeLoc,
248 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000249
250 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
251
Richard Smithd92aa2d2013-10-24 01:07:54 +0000252 AttributeList::Kind AttrKind =
Richard Smithd386fef2013-10-31 01:56:18 +0000253 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smithd92aa2d2013-10-24 01:07:54 +0000254
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000255 // Availability attributes have their own grammar.
Richard Smithd386fef2013-10-31 01:56:18 +0000256 // FIXME: All these cases fail to pass in the syntax and scope, and might be
257 // written as C++11 gnu:: attributes.
Richard Smithd92aa2d2013-10-24 01:07:54 +0000258 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000259 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
260 return;
261 }
Richard Smithd386fef2013-10-31 01:56:18 +0000262 // Thread safety attributes are parsed in an unevaluated context.
263 // FIXME: Share the bulk of the parsing code here and just pull out
264 // the unevaluated context.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000265 if (IsThreadSafetyAttribute(AttrName->getName())) {
266 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
267 return;
268 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000269 // Type safety attributes have their own grammar.
Richard Smithd92aa2d2013-10-24 01:07:54 +0000270 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000271 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
272 return;
273 }
Aaron Ballman9feedb82013-11-04 12:55:56 +0000274 // Some attributes expect solely a type parameter.
275 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithd386fef2013-10-31 01:56:18 +0000276 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
277 return;
278 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000279
Richard Smithd92aa2d2013-10-24 01:07:54 +0000280 // Ignore the left paren location for now.
281 ConsumeParen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000282
Aaron Ballman624421f2013-08-31 01:11:41 +0000283 ArgsVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000284
Richard Smithd386fef2013-10-31 01:56:18 +0000285 if (Tok.is(tok::identifier)) {
Richard Smithd92aa2d2013-10-24 01:07:54 +0000286 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithd386fef2013-10-31 01:56:18 +0000287 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smithd92aa2d2013-10-24 01:07:54 +0000288
289 // If we don't know how to parse this attribute, but this is the only
290 // token in this argument, assume it's meant to be an identifier.
291 if (AttrKind == AttributeList::UnknownAttribute) {
292 const Token &Next = NextToken();
Richard Smithd386fef2013-10-31 01:56:18 +0000293 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smithd92aa2d2013-10-24 01:07:54 +0000294 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000295
Richard Smithd386fef2013-10-31 01:56:18 +0000296 if (IsIdentifierArg)
297 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000298 }
299
Richard Smithd386fef2013-10-31 01:56:18 +0000300 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000301 // Eat the comma.
Aaron Ballman624421f2013-08-31 01:11:41 +0000302 if (!ArgExprs.empty())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000303 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000304
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000305 // Parse the non-empty comma-separated list of expressions.
306 while (1) {
307 ExprResult ArgExpr(ParseAssignmentExpression());
308 if (ArgExpr.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000309 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000310 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000311 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000312 ArgExprs.push_back(ArgExpr.release());
313 if (Tok.isNot(tok::comma))
314 break;
315 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000316 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000317 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000318
319 SourceLocation RParen = Tok.getLocation();
Richard Smithd386fef2013-10-31 01:56:18 +0000320 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000321 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithd386fef2013-10-31 01:56:18 +0000322 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
323 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000324 }
325}
326
Chad Rosier8decdee2012-06-26 22:30:43 +0000327/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000328/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000329void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000330 SourceLocation AttrNameLoc,
331 ParsedAttributes &Attrs)
332{
333 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000334 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000335 AttrName->getNameStart(), tok::r_paren))
336 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000337
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000338 ExprResult ArgExpr(ParseConstantExpression());
339 if (ArgExpr.isInvalid()) {
340 T.skipToEnd();
341 return;
342 }
Aaron Ballman624421f2013-08-31 01:11:41 +0000343 ArgsUnion ExprList = ArgExpr.take();
344 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
345 AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000346
347 T.consumeClose();
348}
349
Chad Rosier8decdee2012-06-26 22:30:43 +0000350/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000351/// arguments.
352bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
353 return llvm::StringSwitch<bool>(Ident->getName())
354 .Case("dllimport", true)
355 .Case("dllexport", true)
356 .Case("noreturn", true)
357 .Case("nothrow", true)
358 .Case("noinline", true)
359 .Case("naked", true)
360 .Case("appdomain", true)
361 .Case("process", true)
362 .Case("jitintrinsic", true)
363 .Case("noalias", true)
364 .Case("restrict", true)
365 .Case("novtable", true)
366 .Case("selectany", true)
367 .Case("thread", true)
Aaron Ballman3ce0de62013-05-04 16:58:37 +0000368 .Case("safebuffers", true )
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000369 .Default(false);
370}
371
Chad Rosier8decdee2012-06-26 22:30:43 +0000372/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000373/// parameters). Will return false if we properly handled the declspec, or
374/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000375void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000376 SourceLocation Loc,
377 ParsedAttributes &Attrs) {
378 // Try to handle the easy case first -- these declspecs all take a single
379 // parameter as their argument.
380 if (llvm::StringSwitch<bool>(Ident->getName())
381 .Case("uuid", true)
382 .Case("align", true)
383 .Case("allocate", true)
384 .Default(false)) {
385 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
386 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000387 // The deprecated declspec has an optional single argument, so we will
388 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000389 // not.
390 if (Tok.getKind() == tok::l_paren)
391 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
392 else
Aaron Ballman624421f2013-08-31 01:11:41 +0000393 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000394 } else if (Ident->getName() == "property") {
395 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000396 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000397 // must be named get or put.
John McCall76da55d2013-04-16 07:28:30 +0000398 if (Tok.isNot(tok::l_paren)) {
399 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
400 << Ident->getNameStart();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000401 return;
John McCall76da55d2013-04-16 07:28:30 +0000402 }
403 BalancedDelimiterTracker T(*this, tok::l_paren);
404 T.expectAndConsume(diag::err_expected_lparen_after,
405 Ident->getNameStart(), tok::r_paren);
406
407 enum AccessorKind {
408 AK_Invalid = -1,
409 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
410 };
411 IdentifierInfo *AccessorNames[] = { 0, 0 };
412 bool HasInvalidAccessor = false;
413
414 // Parse the accessor specifications.
415 while (true) {
416 // Stop if this doesn't look like an accessor spec.
417 if (!Tok.is(tok::identifier)) {
418 // If the user wrote a completely empty list, use a special diagnostic.
419 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
420 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
421 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
422 break;
423 }
424
425 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
426 break;
427 }
428
429 AccessorKind Kind;
430 SourceLocation KindLoc = Tok.getLocation();
431 StringRef KindStr = Tok.getIdentifierInfo()->getName();
432 if (KindStr == "get") {
433 Kind = AK_Get;
434 } else if (KindStr == "put") {
435 Kind = AK_Put;
436
437 // Recover from the common mistake of using 'set' instead of 'put'.
438 } else if (KindStr == "set") {
439 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
440 << FixItHint::CreateReplacement(KindLoc, "put");
441 Kind = AK_Put;
442
443 // Handle the mistake of forgetting the accessor kind by skipping
444 // this accessor.
445 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
446 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
447 ConsumeToken();
448 HasInvalidAccessor = true;
449 goto next_property_accessor;
450
451 // Otherwise, complain about the unknown accessor kind.
452 } else {
453 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
454 HasInvalidAccessor = true;
455 Kind = AK_Invalid;
456
457 // Try to keep parsing unless it doesn't look like an accessor spec.
458 if (!NextToken().is(tok::equal)) break;
459 }
460
461 // Consume the identifier.
462 ConsumeToken();
463
464 // Consume the '='.
465 if (Tok.is(tok::equal)) {
466 ConsumeToken();
467 } else {
468 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
469 << KindStr;
470 break;
471 }
472
473 // Expect the method name.
474 if (!Tok.is(tok::identifier)) {
475 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
476 break;
477 }
478
479 if (Kind == AK_Invalid) {
480 // Just drop invalid accessors.
481 } else if (AccessorNames[Kind] != NULL) {
482 // Complain about the repeated accessor, ignore it, and keep parsing.
483 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
484 } else {
485 AccessorNames[Kind] = Tok.getIdentifierInfo();
486 }
487 ConsumeToken();
488
489 next_property_accessor:
490 // Keep processing accessors until we run out.
491 if (Tok.is(tok::comma)) {
492 ConsumeAnyToken();
493 continue;
494
495 // If we run into the ')', stop without consuming it.
496 } else if (Tok.is(tok::r_paren)) {
497 break;
498 } else {
499 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
500 break;
501 }
502 }
503
504 // Only add the property attribute if it was well-formed.
505 if (!HasInvalidAccessor) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000506 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall76da55d2013-04-16 07:28:30 +0000507 AccessorNames[AK_Get], AccessorNames[AK_Put],
508 AttributeList::AS_Declspec);
509 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000510 T.skipToEnd();
511 } else {
512 // We don't recognize this as a valid declspec, but instead of creating the
513 // attribute and allowing sema to warn about it, we will warn here instead.
514 // This is because some attributes have multiple spellings, but we need to
515 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000516 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000517 // both locations.
518 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
519
520 // If there's an open paren, we should eat the open and close parens under
521 // the assumption that this unknown declspec has parameters.
522 BalancedDelimiterTracker T(*this, tok::l_paren);
523 if (!T.consumeOpen())
524 T.skipToEnd();
525 }
526}
527
Eli Friedmana23b4852009-06-08 07:21:15 +0000528/// [MS] decl-specifier:
529/// __declspec ( extended-decl-modifier-seq )
530///
531/// [MS] extended-decl-modifier-seq:
532/// extended-decl-modifier[opt]
533/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000534void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000535 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000536
Steve Narofff59e17e2008-12-24 20:59:21 +0000537 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000538 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000539 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000540 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000541 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000542
Chad Rosier8decdee2012-06-26 22:30:43 +0000543 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000544 // you can specify multiple attributes per declspec.
545 while (Tok.getKind() != tok::r_paren) {
546 // We expect either a well-known identifier or a generic string. Anything
547 // else is a malformed declspec.
548 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000549 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000550 Tok.getKind() != tok::kw_restrict) {
551 Diag(Tok, diag::err_ms_declspec_type);
552 T.skipToEnd();
553 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000554 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000555
556 IdentifierInfo *AttrName;
557 SourceLocation AttrNameLoc;
558 if (IsString) {
559 SmallString<8> StrBuffer;
560 bool Invalid = false;
561 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
562 if (Invalid) {
563 T.skipToEnd();
564 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000565 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000566 AttrName = PP.getIdentifierInfo(Str);
567 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000568 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000569 AttrName = Tok.getIdentifierInfo();
570 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000571 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000572
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000573 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000574 // If we have a generic string, we will allow it because there is no
575 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000576 // (for instance, SAL declspecs in older versions of MSVC).
577 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000578 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000579 // arguments and can be turned into an attribute directly.
Aaron Ballman624421f2013-08-31 01:11:41 +0000580 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
581 AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000582 else
583 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000584 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000585 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000586}
587
John McCall7f040a92010-12-24 02:08:15 +0000588void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000589 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000590 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000591 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000592 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballmanaa9df092013-05-22 23:25:32 +0000593 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
594 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000595 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
596 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000597 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
598 AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000599 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000600}
601
John McCall7f040a92010-12-24 02:08:15 +0000602void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000603 // Treat these like attributes
604 while (Tok.is(tok::kw___pascal)) {
605 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
606 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000607 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
608 AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000609 }
John McCall7f040a92010-12-24 02:08:15 +0000610}
611
Peter Collingbournef315fa82011-02-14 01:42:53 +0000612void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
613 // Treat these like attributes
614 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000615 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000616 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000617 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
618 AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000619 }
620}
621
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000622void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000623 // FIXME: The mapping from attribute spelling to semantics should be
624 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000625 SourceLocation Loc = Tok.getLocation();
626 switch(Tok.getKind()) {
627 // OpenCL qualifiers:
628 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000629 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000630 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000631 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000632 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000633 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000634
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000635 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000636 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000637 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000638 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000639 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000640
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000641 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000642 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000643 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000644 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000645 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000646
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000647 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000648 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000649 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000650 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000651 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000652
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000653 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000654 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000655 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000656 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000657 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000658
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000659 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000660 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000661 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000662 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000663 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000664
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000665 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000666 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000667 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000668 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000669 break;
670 default: break;
671 }
672}
673
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000674/// \brief Parse a version number.
675///
676/// version:
677/// simple-integer
678/// simple-integer ',' simple-integer
679/// simple-integer ',' simple-integer ',' simple-integer
680VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
681 Range = Tok.getLocation();
682
683 if (!Tok.is(tok::numeric_constant)) {
684 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000685 SkipUntil(tok::comma, tok::r_paren,
686 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000687 return VersionTuple();
688 }
689
690 // Parse the major (and possibly minor and subminor) versions, which
691 // are stored in the numeric constant. We utilize a quirk of the
692 // lexer, which is that it handles something like 1.2.3 as a single
693 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000694 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000695 Buffer.resize(Tok.getLength()+1);
696 const char *ThisTokBegin = &Buffer[0];
697
698 // Get the spelling of the token, which eliminates trigraphs, etc.
699 bool Invalid = false;
700 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
701 if (Invalid)
702 return VersionTuple();
703
704 // Parse the major version.
705 unsigned AfterMajor = 0;
706 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000707 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000708 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
709 ++AfterMajor;
710 }
711
712 if (AfterMajor == 0) {
713 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000714 SkipUntil(tok::comma, tok::r_paren,
715 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000716 return VersionTuple();
717 }
718
719 if (AfterMajor == ActualLength) {
720 ConsumeToken();
721
722 // We only had a single version component.
723 if (Major == 0) {
724 Diag(Tok, diag::err_zero_version);
725 return VersionTuple();
726 }
727
728 return VersionTuple(Major);
729 }
730
731 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
732 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000733 SkipUntil(tok::comma, tok::r_paren,
734 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000735 return VersionTuple();
736 }
737
738 // Parse the minor version.
739 unsigned AfterMinor = AfterMajor + 1;
740 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000741 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000742 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
743 ++AfterMinor;
744 }
745
746 if (AfterMinor == ActualLength) {
747 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000748
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000749 // We had major.minor.
750 if (Major == 0 && Minor == 0) {
751 Diag(Tok, diag::err_zero_version);
752 return VersionTuple();
753 }
754
Chad Rosier8decdee2012-06-26 22:30:43 +0000755 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000756 }
757
758 // If what follows is not a '.', we have a problem.
759 if (ThisTokBegin[AfterMinor] != '.') {
760 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000761 SkipUntil(tok::comma, tok::r_paren,
762 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosier8decdee2012-06-26 22:30:43 +0000763 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000764 }
765
766 // Parse the subminor version.
767 unsigned AfterSubminor = AfterMinor + 1;
768 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000769 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000770 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
771 ++AfterSubminor;
772 }
773
774 if (AfterSubminor != ActualLength) {
775 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000776 SkipUntil(tok::comma, tok::r_paren,
777 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000778 return VersionTuple();
779 }
780 ConsumeToken();
781 return VersionTuple(Major, Minor, Subminor);
782}
783
784/// \brief Parse the contents of the "availability" attribute.
785///
786/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000787/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000788///
789/// platform:
790/// identifier
791///
792/// version-arg-list:
793/// version-arg
794/// version-arg ',' version-arg-list
795///
796/// version-arg:
797/// 'introduced' '=' version
798/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000799/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000800/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000801/// opt-message:
802/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000803void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
804 SourceLocation AvailabilityLoc,
805 ParsedAttributes &attrs,
806 SourceLocation *endLoc) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000807 enum { Introduced, Deprecated, Obsoleted, Unknown };
808 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000809 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000810
811 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000812 BalancedDelimiterTracker T(*this, tok::l_paren);
813 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000814 Diag(Tok, diag::err_expected_lparen);
815 return;
816 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000817
818 // Parse the platform name,
819 if (Tok.isNot(tok::identifier)) {
820 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000821 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000822 return;
823 }
Richard Smith8edabd92013-09-03 18:01:40 +0000824 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000825
826 // Parse the ',' following the platform name.
827 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
828 return;
829
830 // If we haven't grabbed the pointers for the identifiers
831 // "introduced", "deprecated", and "obsoleted", do so now.
832 if (!Ident_introduced) {
833 Ident_introduced = PP.getIdentifierInfo("introduced");
834 Ident_deprecated = PP.getIdentifierInfo("deprecated");
835 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000836 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000837 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000838 }
839
840 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000841 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000842 do {
843 if (Tok.isNot(tok::identifier)) {
844 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000845 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000846 return;
847 }
848 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
849 SourceLocation KeywordLoc = ConsumeToken();
850
Douglas Gregorb53e4172011-03-26 03:35:55 +0000851 if (Keyword == Ident_unavailable) {
852 if (UnavailableLoc.isValid()) {
853 Diag(KeywordLoc, diag::err_availability_redundant)
854 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000855 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000856 UnavailableLoc = KeywordLoc;
857
858 if (Tok.isNot(tok::comma))
859 break;
860
861 ConsumeToken();
862 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000863 }
864
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000865 if (Tok.isNot(tok::equal)) {
866 Diag(Tok, diag::err_expected_equal_after)
867 << Keyword;
Alexey Bataev8fe24752013-11-18 08:17:37 +0000868 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000869 return;
870 }
871 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000872 if (Keyword == Ident_message) {
Benjamin Kramerc5617142013-09-13 17:31:48 +0000873 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbs97f84612012-11-17 19:16:52 +0000874 Diag(Tok, diag::err_expected_string_literal)
875 << /*Source='availability attribute'*/2;
Alexey Bataev8fe24752013-11-18 08:17:37 +0000876 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000877 return;
878 }
879 MessageExpr = ParseStringLiteralExpression();
880 break;
881 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000882
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000883 SourceRange VersionRange;
884 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000885
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000886 if (Version.empty()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000887 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000888 return;
889 }
890
891 unsigned Index;
892 if (Keyword == Ident_introduced)
893 Index = Introduced;
894 else if (Keyword == Ident_deprecated)
895 Index = Deprecated;
896 else if (Keyword == Ident_obsoleted)
897 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000898 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000899 Index = Unknown;
900
901 if (Index < Unknown) {
902 if (!Changes[Index].KeywordLoc.isInvalid()) {
903 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000904 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000905 << SourceRange(Changes[Index].KeywordLoc,
906 Changes[Index].VersionRange.getEnd());
907 }
908
909 Changes[Index].KeywordLoc = KeywordLoc;
910 Changes[Index].Version = Version;
911 Changes[Index].VersionRange = VersionRange;
912 } else {
913 Diag(KeywordLoc, diag::err_availability_unknown_change)
914 << Keyword << VersionRange;
915 }
916
917 if (Tok.isNot(tok::comma))
918 break;
919
920 ConsumeToken();
921 } while (true);
922
923 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000924 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000925 return;
926
927 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000928 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000929
Douglas Gregorb53e4172011-03-26 03:35:55 +0000930 // The 'unavailable' availability cannot be combined with any other
931 // availability changes. Make sure that hasn't happened.
932 if (UnavailableLoc.isValid()) {
933 bool Complained = false;
934 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
935 if (Changes[Index].KeywordLoc.isValid()) {
936 if (!Complained) {
937 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
938 << SourceRange(Changes[Index].KeywordLoc,
939 Changes[Index].VersionRange.getEnd());
940 Complained = true;
941 }
942
943 // Clear out the availability.
944 Changes[Index] = AvailabilityChange();
945 }
946 }
947 }
948
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000949 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000950 attrs.addNew(&Availability,
951 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000952 0, AvailabilityLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +0000953 Platform,
John McCall0b7e6782011-03-24 11:26:52 +0000954 Changes[Introduced],
955 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000956 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000957 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000958 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000959}
960
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000961
Bill Wendlingad017fa2012-12-20 19:22:21 +0000962// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000963// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
964
965void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
966
967void Parser::LateParsedClass::ParseLexedAttributes() {
968 Self->ParseLexedAttributes(*Class);
969}
970
971void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000972 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000973}
974
975/// Wrapper class which calls ParseLexedAttribute, after setting up the
976/// scope appropriately.
977void Parser::ParseLexedAttributes(ParsingClass &Class) {
978 // Deal with templates
979 // FIXME: Test cases to make sure this does the right thing for templates.
980 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
981 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
982 HasTemplateScope);
983 if (HasTemplateScope)
984 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
985
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000986 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000987 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000988 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000989 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
990 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
991
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000992 // Enter the scope of nested classes
993 if (!AlreadyHasClassScope)
994 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
995 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000996 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000997 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
998 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
999 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001000 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001001
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001002 if (!AlreadyHasClassScope)
1003 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1004 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001005}
1006
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001007
1008/// \brief Parse all attributes in LAs, and attach them to Decl D.
1009void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1010 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +00001011 assert(LAs.parseSoon() &&
1012 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001013 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +00001014 if (D)
1015 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001016 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +00001017 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001018 }
1019 LAs.clear();
1020}
1021
1022
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001023/// \brief Finish parsing an attribute for which parsing was delayed.
1024/// This will be called at the end of parsing a class declaration
1025/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +00001026/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001027/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001028void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1029 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001030 // Save the current token position.
1031 SourceLocation OrigLoc = Tok.getLocation();
1032
1033 // Append the current token at the end of the new token stream so that it
1034 // doesn't get lost.
1035 LA.Toks.push_back(Tok);
1036 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1037 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00001038 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001039
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001040 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +00001041 // FIXME: Do not warn on C++11 attributes, once we start supporting
1042 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001043 Diag(Tok, diag::warn_attribute_on_function_definition)
1044 << LA.AttrName.getName();
1045 }
1046
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001047 ParsedAttributes Attrs(AttrFactory);
1048 SourceLocation endLoc;
1049
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001050 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001051 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001052 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1053 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001054
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001055 // Allow 'this' within late-parsed attributes.
Richard Smithcafeb942013-06-07 02:33:37 +00001056 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1057 ND && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001058
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001059 if (LA.Decls.size() == 1) {
1060 // If the Decl is templatized, add template parameters to scope.
1061 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1062 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1063 if (HasTemplateScope)
1064 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001065
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001066 // If the Decl is on a function, add function parameters to the scope.
1067 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1068 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1069 if (HasFunScope)
1070 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001071
Michael Han6880f492012-10-03 01:56:22 +00001072 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001073 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001074
1075 if (HasFunScope) {
1076 Actions.ActOnExitFunctionContext();
1077 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1078 }
1079 if (HasTemplateScope) {
1080 TempScope.Exit();
1081 }
1082 } else {
1083 // If there are multiple decls, then the decl cannot be within the
1084 // function scope.
Michael Han6880f492012-10-03 01:56:22 +00001085 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001086 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001087 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +00001088 } else {
1089 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001090 }
1091
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001092 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1093 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1094 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001095
1096 if (Tok.getLocation() != OrigLoc) {
1097 // Due to a parsing error, we either went over the cached tokens or
1098 // there are still cached tokens left, so we skip the leftover tokens.
1099 // Since this is an uncommon situation that should be avoided, use the
1100 // expensive isBeforeInTranslationUnit call.
1101 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1102 OrigLoc))
1103 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001104 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001105 }
1106}
1107
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001108/// \brief Wrapper around a case statement checking if AttrName is
1109/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001110bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001111 return llvm::StringSwitch<bool>(AttrName)
1112 .Case("guarded_by", true)
1113 .Case("guarded_var", true)
1114 .Case("pt_guarded_by", true)
1115 .Case("pt_guarded_var", true)
1116 .Case("lockable", true)
1117 .Case("scoped_lockable", true)
1118 .Case("no_thread_safety_analysis", true)
1119 .Case("acquired_after", true)
1120 .Case("acquired_before", true)
1121 .Case("exclusive_lock_function", true)
1122 .Case("shared_lock_function", true)
1123 .Case("exclusive_trylock_function", true)
1124 .Case("shared_trylock_function", true)
1125 .Case("unlock_function", true)
1126 .Case("lock_returned", true)
1127 .Case("locks_excluded", true)
1128 .Case("exclusive_locks_required", true)
1129 .Case("shared_locks_required", true)
1130 .Default(false);
1131}
1132
1133/// \brief Parse the contents of thread safety attributes. These
1134/// should always be parsed as an expression list.
1135///
1136/// We need to special case the parsing due to the fact that if the first token
1137/// of the first argument is an identifier, the main parse loop will store
1138/// that token as a "parameter" and the rest of
1139/// the arguments will be added to a list of "arguments". However,
1140/// subsequent tokens in the first argument are lost. We instead parse each
1141/// argument as an expression and add all arguments to the list of "arguments".
1142/// In future, we will take advantage of this special case to also
1143/// deal with some argument scoping issues here (for example, referring to a
1144/// function parameter in the attribute on that function).
1145void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1146 SourceLocation AttrNameLoc,
1147 ParsedAttributes &Attrs,
1148 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001149 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001150
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001151 BalancedDelimiterTracker T(*this, tok::l_paren);
1152 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001153
Aaron Ballman624421f2013-08-31 01:11:41 +00001154 ArgsVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001155 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001156
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001157 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001158 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001159 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001160 ExprResult ArgExpr(ParseAssignmentExpression());
1161 if (ArgExpr.isInvalid()) {
1162 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001163 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001164 break;
1165 } else {
1166 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001167 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001168 if (Tok.isNot(tok::comma))
1169 break;
1170 ConsumeToken(); // Eat the comma, move to the next argument
1171 }
1172 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001173 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001174 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1175 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001176 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001177 if (EndLoc)
1178 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001179}
1180
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001181void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1182 SourceLocation AttrNameLoc,
1183 ParsedAttributes &Attrs,
1184 SourceLocation *EndLoc) {
1185 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1186
1187 BalancedDelimiterTracker T(*this, tok::l_paren);
1188 T.consumeOpen();
1189
1190 if (Tok.isNot(tok::identifier)) {
1191 Diag(Tok, diag::err_expected_ident);
1192 T.skipToEnd();
1193 return;
1194 }
Richard Smith8edabd92013-09-03 18:01:40 +00001195 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001196
1197 if (Tok.isNot(tok::comma)) {
1198 Diag(Tok, diag::err_expected_comma);
1199 T.skipToEnd();
1200 return;
1201 }
1202 ConsumeToken();
1203
1204 SourceRange MatchingCTypeRange;
1205 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1206 if (MatchingCType.isInvalid()) {
1207 T.skipToEnd();
1208 return;
1209 }
1210
1211 bool LayoutCompatible = false;
1212 bool MustBeNull = false;
1213 while (Tok.is(tok::comma)) {
1214 ConsumeToken();
1215 if (Tok.isNot(tok::identifier)) {
1216 Diag(Tok, diag::err_expected_ident);
1217 T.skipToEnd();
1218 return;
1219 }
1220 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1221 if (Flag->isStr("layout_compatible"))
1222 LayoutCompatible = true;
1223 else if (Flag->isStr("must_be_null"))
1224 MustBeNull = true;
1225 else {
1226 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1227 T.skipToEnd();
1228 return;
1229 }
1230 ConsumeToken(); // consume flag
1231 }
1232
1233 if (!T.consumeClose()) {
1234 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +00001235 ArgumentKind, MatchingCType.release(),
1236 LayoutCompatible, MustBeNull,
1237 AttributeList::AS_GNU);
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001238 }
1239
1240 if (EndLoc)
1241 *EndLoc = T.getCloseLocation();
1242}
1243
Richard Smith6ee326a2012-04-10 01:32:12 +00001244/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1245/// of a C++11 attribute-specifier in a location where an attribute is not
1246/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1247/// situation.
1248///
1249/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1250/// this doesn't appear to actually be an attribute-specifier, and the caller
1251/// should try to parse it.
1252bool Parser::DiagnoseProhibitedCXX11Attribute() {
1253 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1254
1255 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1256 case CAK_NotAttributeSpecifier:
1257 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1258 return false;
1259
1260 case CAK_InvalidAttributeSpecifier:
1261 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1262 return false;
1263
1264 case CAK_AttributeSpecifier:
1265 // Parse and discard the attributes.
1266 SourceLocation BeginLoc = ConsumeBracket();
1267 ConsumeBracket();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001268 SkipUntil(tok::r_square);
Richard Smith6ee326a2012-04-10 01:32:12 +00001269 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1270 SourceLocation EndLoc = ConsumeBracket();
1271 Diag(BeginLoc, diag::err_attributes_not_allowed)
1272 << SourceRange(BeginLoc, EndLoc);
1273 return true;
1274 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001275 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001276}
1277
Richard Smith975d52c2013-02-20 01:17:14 +00001278/// \brief We have found the opening square brackets of a C++11
1279/// attribute-specifier in a location where an attribute is not permitted, but
1280/// we know where the attributes ought to be written. Parse them anyway, and
1281/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001282void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1283 SourceLocation CorrectLocation) {
1284 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1285 Tok.is(tok::kw_alignas));
1286
1287 // Consume the attributes.
1288 SourceLocation Loc = Tok.getLocation();
1289 ParseCXX11Attributes(Attrs);
1290 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1291
1292 Diag(Loc, diag::err_attributes_not_allowed)
1293 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1294 << FixItHint::CreateRemoval(AttrRange);
1295}
1296
John McCall7f040a92010-12-24 02:08:15 +00001297void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1298 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1299 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001300}
1301
Michael Hanf64231e2012-11-06 19:34:54 +00001302void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1303 AttributeList *AttrList = attrs.getList();
1304 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001305 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001306 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001307 << AttrList->getName();
1308 AttrList->setInvalid();
1309 }
1310 AttrList = AttrList->getNext();
1311 }
1312}
1313
Reid Spencer5f016e22007-07-11 17:01:13 +00001314/// ParseDeclaration - Parse a full 'declaration', which consists of
1315/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001316/// 'Context' should be a Declarator::TheContext value. This returns the
1317/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001318///
1319/// declaration: [C99 6.7]
1320/// block-declaration ->
1321/// simple-declaration
1322/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001323/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001324/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001325/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001326/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001327/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001328/// others... [FIXME]
1329///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001330Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1331 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001332 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001333 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001334 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001335 // Must temporarily exit the objective-c container scope for
1336 // parsing c none objective-c decls.
1337 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001338
John McCalld226f652010-08-21 09:40:31 +00001339 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001340 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001341 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001342 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001343 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001344 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001345 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001346 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001347 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001348 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001349 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001350 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001351 SourceLocation InlineLoc = ConsumeToken();
1352 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1353 break;
1354 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001355 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001356 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001357 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001358 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001359 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001360 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001361 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001362 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001363 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001364 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001365 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001366 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001367 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001369 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001370 default:
John McCall7f040a92010-12-24 02:08:15 +00001371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001372 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001373
Chris Lattner682bf922009-03-29 16:50:03 +00001374 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001375 // single decl, convert it now. Alias declarations can also declare a type;
1376 // include that too if it is present.
1377 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001378}
1379
1380/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1381/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001382/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1383/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001384///[C90/C++]init-declarator-list ';' [TODO]
1385/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001386///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001387/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001388/// attribute-specifier-seq[opt] type-specifier-seq declarator
1389///
Chris Lattnercd147752009-03-29 17:27:48 +00001390/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001391/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001392///
1393/// If FRI is non-null, we might be parsing a for-range-declaration instead
1394/// of a simple-declaration. If we find that we are, we also parse the
1395/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001396Parser::DeclGroupPtrTy
1397Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1398 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001399 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001400 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001402 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001403
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00001404 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1405 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1406
1407 // If we had a free-standing type definition with a missing semicolon, we
1408 // may get this far before the problem becomes obvious.
1409 if (DS.hasTagDefinition() &&
1410 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1411 return DeclGroupPtrTy();
Abramo Bagnara06284c12012-01-07 10:52:36 +00001412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1414 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001415 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001416 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001417 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001418 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001419 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001420 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001421 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001422 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001424
Richard Smith68ea3ae2013-02-22 09:06:26 +00001425 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001426 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001427}
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Richard Smith0706df42011-10-19 21:33:05 +00001429/// Returns true if this might be the start of a declarator, or a common typo
1430/// for a declarator.
1431bool Parser::MightBeDeclarator(unsigned Context) {
1432 switch (Tok.getKind()) {
1433 case tok::annot_cxxscope:
1434 case tok::annot_template_id:
1435 case tok::caret:
1436 case tok::code_completion:
1437 case tok::coloncolon:
1438 case tok::ellipsis:
1439 case tok::kw___attribute:
1440 case tok::kw_operator:
1441 case tok::l_paren:
1442 case tok::star:
1443 return true;
1444
1445 case tok::amp:
1446 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001447 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001448
Richard Smith1c94c162012-01-09 22:31:44 +00001449 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001450 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001451 NextToken().is(tok::l_square);
1452
1453 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001454 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001455
Richard Smith0706df42011-10-19 21:33:05 +00001456 case tok::identifier:
1457 switch (NextToken().getKind()) {
1458 case tok::code_completion:
1459 case tok::coloncolon:
1460 case tok::comma:
1461 case tok::equal:
1462 case tok::equalequal: // Might be a typo for '='.
1463 case tok::kw_alignas:
1464 case tok::kw_asm:
1465 case tok::kw___attribute:
1466 case tok::l_brace:
1467 case tok::l_paren:
1468 case tok::l_square:
1469 case tok::less:
1470 case tok::r_brace:
1471 case tok::r_paren:
1472 case tok::r_square:
1473 case tok::semi:
1474 return true;
1475
1476 case tok::colon:
1477 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001478 // and in block scope it's probably a label. Inside a class definition,
1479 // this is a bit-field.
1480 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001481 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001482
1483 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001484 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001485
1486 default:
1487 return false;
1488 }
1489
1490 default:
1491 return false;
1492 }
1493}
1494
Richard Smith994d73f2012-04-11 20:59:20 +00001495/// Skip until we reach something which seems like a sensible place to pick
1496/// up parsing after a malformed declaration. This will sometimes stop sooner
1497/// than SkipUntil(tok::r_brace) would, but will never stop later.
1498void Parser::SkipMalformedDecl() {
1499 while (true) {
1500 switch (Tok.getKind()) {
1501 case tok::l_brace:
1502 // Skip until matching }, then stop. We've probably skipped over
1503 // a malformed class or function definition or similar.
1504 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001505 SkipUntil(tok::r_brace);
Richard Smith994d73f2012-04-11 20:59:20 +00001506 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1507 // This declaration isn't over yet. Keep skipping.
1508 continue;
1509 }
1510 if (Tok.is(tok::semi))
1511 ConsumeToken();
1512 return;
1513
1514 case tok::l_square:
1515 ConsumeBracket();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001516 SkipUntil(tok::r_square);
Richard Smith994d73f2012-04-11 20:59:20 +00001517 continue;
1518
1519 case tok::l_paren:
1520 ConsumeParen();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001521 SkipUntil(tok::r_paren);
Richard Smith994d73f2012-04-11 20:59:20 +00001522 continue;
1523
1524 case tok::r_brace:
1525 return;
1526
1527 case tok::semi:
1528 ConsumeToken();
1529 return;
1530
1531 case tok::kw_inline:
1532 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001533 // a good place to pick back up parsing, except in an Objective-C
1534 // @interface context.
1535 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1536 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001537 return;
1538 break;
1539
1540 case tok::kw_namespace:
1541 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001542 // place to pick back up parsing, except in an Objective-C
1543 // @interface context.
1544 if (Tok.isAtStartOfLine() &&
1545 (!ParsingInObjCContainer || CurParsedObjCImpl))
1546 return;
1547 break;
1548
1549 case tok::at:
1550 // @end is very much like } in Objective-C contexts.
1551 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1552 ParsingInObjCContainer)
1553 return;
1554 break;
1555
1556 case tok::minus:
1557 case tok::plus:
1558 // - and + probably start new method declarations in Objective-C contexts.
1559 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001560 return;
1561 break;
1562
1563 case tok::eof:
1564 return;
1565
1566 default:
1567 break;
1568 }
1569
1570 ConsumeAnyToken();
1571 }
1572}
1573
John McCalld8ac0572009-11-03 19:26:08 +00001574/// ParseDeclGroup - Having concluded that this is either a function
1575/// definition or a group of object declarations, actually parse the
1576/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001577Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1578 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001579 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001580 SourceLocation *DeclEnd,
1581 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001582 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001583 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001584 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001585
John McCalld8ac0572009-11-03 19:26:08 +00001586 // Bail out if the first declarator didn't seem well-formed.
1587 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001588 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001589 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001590 }
Mike Stump1eb44332009-09-09 15:08:12 +00001591
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001592 // Save late-parsed attributes for now; they need to be parsed in the
1593 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001594 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1595 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001596 if (D.isFunctionDeclarator())
1597 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1598
Chris Lattnerc82daef2010-07-11 22:24:20 +00001599 // Check to see if we have a function *definition* which must have a body.
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001600 if (D.isFunctionDeclarator() &&
Chris Lattnerc82daef2010-07-11 22:24:20 +00001601 // Look at the next token to make sure that this isn't a function
1602 // declaration. We have to check this because __attribute__ might be the
1603 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001604 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001605
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001606 if (AllowFunctionDefinitions) {
1607 if (isStartOfFunctionDefinition(D)) {
1608 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1609 Diag(Tok, diag::err_function_declared_typedef);
John McCalld8ac0572009-11-03 19:26:08 +00001610
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001611 // Recover by treating the 'typedef' as spurious.
1612 DS.ClearStorageClassSpecs();
1613 }
1614
1615 Decl *TheDecl =
1616 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1617 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld8ac0572009-11-03 19:26:08 +00001618 }
1619
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001620 if (isDeclarationSpecifier()) {
1621 // If there is an invalid declaration specifier right after the function
1622 // prototype, then we must be in a missing semicolon case where this isn't
1623 // actually a body. Just fall through into the code that handles it as a
1624 // prototype, and let the top-level code handle the erroneous declspec
1625 // where it would otherwise expect a comma or semicolon.
1626 } else {
1627 Diag(Tok, diag::err_expected_fn_body);
1628 SkipUntil(tok::semi);
1629 return DeclGroupPtrTy();
1630 }
John McCalld8ac0572009-11-03 19:26:08 +00001631 } else {
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001632 if (Tok.is(tok::l_brace)) {
1633 Diag(Tok, diag::err_function_definition_not_allowed);
Alexey Bataev8fe24752013-11-18 08:17:37 +00001634 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001635 }
John McCalld8ac0572009-11-03 19:26:08 +00001636 }
1637 }
1638
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001639 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001640 return DeclGroupPtrTy();
1641
1642 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1643 // must parse and analyze the for-range-initializer before the declaration is
1644 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001645 //
1646 // Handle the Objective-C for-in loop variable similarly, although we
1647 // don't need to parse the container in advance.
1648 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1649 bool IsForRangeLoop = false;
1650 if (Tok.is(tok::colon)) {
1651 IsForRangeLoop = true;
1652 FRI->ColonLoc = ConsumeToken();
1653 if (Tok.is(tok::l_brace))
1654 FRI->RangeExpr = ParseBraceInitializer();
1655 else
1656 FRI->RangeExpr = ParseExpression();
1657 }
1658
Richard Smithad762fc2011-04-14 22:09:26 +00001659 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001660 if (IsForRangeLoop)
1661 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001662 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001663 D.complete(ThisDecl);
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001664 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001665 }
1666
Chris Lattner5f9e2722011-07-23 10:55:15 +00001667 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001668 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001669 if (LateParsedAttrs.size() > 0)
1670 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001671 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001672 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001673 DeclsInGroup.push_back(FirstDecl);
1674
Richard Smith0706df42011-10-19 21:33:05 +00001675 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001676
John McCalld8ac0572009-11-03 19:26:08 +00001677 // If we don't have a comma, it is either the end of the list (a ';') or an
1678 // error, bail out.
1679 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001680 SourceLocation CommaLoc = ConsumeToken();
1681
1682 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1683 // This comma was followed by a line-break and something which can't be
1684 // the start of a declarator. The comma was probably a typo for a
1685 // semicolon.
1686 Diag(CommaLoc, diag::err_expected_semi_declaration)
1687 << FixItHint::CreateReplacement(CommaLoc, ";");
1688 ExpectSemi = false;
1689 break;
1690 }
John McCalld8ac0572009-11-03 19:26:08 +00001691
1692 // Parse the next declarator.
1693 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001694 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001695
1696 // Accept attributes in an init-declarator. In the first declarator in a
1697 // declaration, these would be part of the declspec. In subsequent
1698 // declarators, they become part of the declarator itself, so that they
1699 // don't apply to declarators after *this* one. Examples:
1700 // short __attribute__((common)) var; -> declspec
1701 // short var __attribute__((common)); -> declarator
1702 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001703 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001704
1705 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001706 if (!D.isInvalidType()) {
1707 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1708 D.complete(ThisDecl);
1709 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001710 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001711 }
John McCalld8ac0572009-11-03 19:26:08 +00001712 }
1713
1714 if (DeclEnd)
1715 *DeclEnd = Tok.getLocation();
1716
Richard Smith0706df42011-10-19 21:33:05 +00001717 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001718 ExpectAndConsumeSemi(Context == Declarator::FileContext
1719 ? diag::err_invalid_token_after_toplevel_declarator
1720 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001721 // Okay, there was no semicolon and one was expected. If we see a
1722 // declaration specifier, just assume it was missing and continue parsing.
1723 // Otherwise things are very confused and we skip to recover.
1724 if (!isDeclarationSpecifier()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001725 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner004659a2010-07-11 22:42:07 +00001726 if (Tok.is(tok::semi))
1727 ConsumeToken();
1728 }
John McCalld8ac0572009-11-03 19:26:08 +00001729 }
1730
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001731 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001732}
1733
Richard Smithad762fc2011-04-14 22:09:26 +00001734/// Parse an optional simple-asm-expr and attributes, and attach them to a
1735/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001736bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001737 // If a simple-asm-expr is present, parse it.
1738 if (Tok.is(tok::kw_asm)) {
1739 SourceLocation Loc;
1740 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1741 if (AsmLabel.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001742 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smithad762fc2011-04-14 22:09:26 +00001743 return true;
1744 }
1745
1746 D.setAsmLabel(AsmLabel.release());
1747 D.SetRangeEnd(Loc);
1748 }
1749
1750 MaybeParseGNUAttributes(D);
1751 return false;
1752}
1753
Douglas Gregor1426e532009-05-12 21:31:51 +00001754/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1755/// declarator'. This method parses the remainder of the declaration
1756/// (including any attributes or initializer, among other things) and
1757/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001758///
Reid Spencer5f016e22007-07-11 17:01:13 +00001759/// init-declarator: [C99 6.7]
1760/// declarator
1761/// declarator '=' initializer
1762/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1763/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001764/// [C++] declarator initializer[opt]
1765///
1766/// [C++] initializer:
1767/// [C++] '=' initializer-clause
1768/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001769/// [C++0x] '=' 'default' [TODO]
1770/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001771/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001772///
1773/// According to the standard grammar, =default and =delete are function
1774/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001775///
John McCalld226f652010-08-21 09:40:31 +00001776Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001777 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001778 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001779 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Richard Smithad762fc2011-04-14 22:09:26 +00001781 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1782}
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Richard Smithad762fc2011-04-14 22:09:26 +00001784Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1785 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001786 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001787 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001788 switch (TemplateInfo.Kind) {
1789 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001790 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001791 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001792
Douglas Gregord5a423b2009-09-25 18:43:00 +00001793 case ParsedTemplateInfo::Template:
Larisse Voufoef4579c2013-08-06 01:03:05 +00001794 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001795 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001796 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001797 D);
Larisse Voufo25218132013-08-06 07:33:00 +00001798 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufoef4579c2013-08-06 01:03:05 +00001799 // Re-direct this decl to refer to the templated decl so that we can
1800 // initialize it.
1801 ThisDecl = VT->getTemplatedDecl();
1802 break;
1803 }
1804 case ParsedTemplateInfo::ExplicitInstantiation: {
1805 if (Tok.is(tok::semi)) {
1806 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1807 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1808 if (ThisRes.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001809 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufoef4579c2013-08-06 01:03:05 +00001810 return 0;
1811 }
1812 ThisDecl = ThisRes.get();
1813 } else {
1814 // FIXME: This check should be for a variable template instantiation only.
1815
1816 // Check that this is a valid instantiation
1817 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1818 // If the declarator-id is not a template-id, issue a diagnostic and
1819 // recover by ignoring the 'template' keyword.
1820 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1821 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1822 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1823 } else {
1824 SourceLocation LAngleLoc =
1825 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1826 Diag(D.getIdentifierLoc(),
1827 diag::err_explicit_instantiation_with_definition)
1828 << SourceRange(TemplateInfo.TemplateLoc)
1829 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1830
1831 // Recover as if it were an explicit specialization.
1832 TemplateParameterLists FakedParamLists;
1833 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1834 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1835 LAngleLoc));
1836
1837 ThisDecl =
1838 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1839 }
1840 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00001841 break;
1842 }
1843 }
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Richard Smitha2c36462013-04-26 16:15:35 +00001845 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith34b41d92011-02-20 03:19:35 +00001846
Douglas Gregor1426e532009-05-12 21:31:51 +00001847 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001848 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001849 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001850 ConsumeToken();
Larisse Voufoef4579c2013-08-06 01:03:05 +00001851
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001852 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001853 if (D.isFunctionDeclarator())
1854 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1855 << 1 /* delete */;
1856 else
1857 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001858 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001859 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001860 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1861 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001862 else
1863 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001864 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001865 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001866 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001867 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001868 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001869
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001870 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001871 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001872 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001873 cutOffParsing();
1874 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001875 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001876
John McCall60d7b3a2010-08-24 06:29:42 +00001877 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001878
David Blaikie4e4d0842012-03-11 07:00:24 +00001879 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001880 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001881 ExitScope();
1882 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001883
Douglas Gregor1426e532009-05-12 21:31:51 +00001884 if (Init.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001885 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor00225542010-03-01 18:27:54 +00001886 Actions.ActOnInitializerError(ThisDecl);
1887 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001888 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1889 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001890 }
1891 } else if (Tok.is(tok::l_paren)) {
1892 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001893 BalancedDelimiterTracker T(*this, tok::l_paren);
1894 T.consumeOpen();
1895
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001896 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001897 CommaLocsTy CommaLocs;
1898
David Blaikie4e4d0842012-03-11 07:00:24 +00001899 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001900 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001901 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001902 }
1903
Douglas Gregor1426e532009-05-12 21:31:51 +00001904 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001905 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataev8fe24752013-11-18 08:17:37 +00001906 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001907
David Blaikie4e4d0842012-03-11 07:00:24 +00001908 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001909 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001910 ExitScope();
1911 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001912 } else {
1913 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001914 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001915
1916 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1917 "Unexpected number of commas!");
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 }
1923
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001924 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1925 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001926 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001927 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1928 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001929 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001930 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001931 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001932 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001933 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1934
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001935 if (D.getCXXScopeSpec().isSet()) {
1936 EnterScope(0);
1937 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1938 }
1939
1940 ExprResult Init(ParseBraceInitializer());
1941
1942 if (D.getCXXScopeSpec().isSet()) {
1943 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1944 ExitScope();
1945 }
1946
1947 if (Init.isInvalid()) {
1948 Actions.ActOnInitializerError(ThisDecl);
1949 } else
1950 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1951 /*DirectInit=*/true, TypeContainsAuto);
1952
Douglas Gregor1426e532009-05-12 21:31:51 +00001953 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001954 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001955 }
1956
Richard Smith483b9f32011-02-21 20:05:19 +00001957 Actions.FinalizeDeclaration(ThisDecl);
1958
Douglas Gregor1426e532009-05-12 21:31:51 +00001959 return ThisDecl;
1960}
1961
Reid Spencer5f016e22007-07-11 17:01:13 +00001962/// ParseSpecifierQualifierList
1963/// specifier-qualifier-list:
1964/// type-specifier specifier-qualifier-list[opt]
1965/// type-qualifier specifier-qualifier-list[opt]
1966/// [GNU] attributes specifier-qualifier-list[opt]
1967///
Richard Smith69730c12012-03-12 07:56:15 +00001968void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1969 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1971 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001972 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001973 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 // Validate declspec for type-name.
1976 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001977 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1978 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001979 Diag(Tok, diag::err_expected_type);
1980 DS.SetTypeSpecError();
1981 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1982 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001983 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001984 if (!DS.hasTypeSpecifier())
1985 DS.SetTypeSpecError();
1986 }
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Reid Spencer5f016e22007-07-11 17:01:13 +00001988 // Issue diagnostic and remove storage class if present.
1989 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1990 if (DS.getStorageClassSpecLoc().isValid())
1991 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1992 else
Richard Smithec642442013-04-12 22:46:28 +00001993 Diag(DS.getThreadStorageClassSpecLoc(),
1994 diag::err_typename_invalid_storageclass);
Reid Spencer5f016e22007-07-11 17:01:13 +00001995 DS.ClearStorageClassSpecs();
1996 }
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 // Issue diagnostic and remove function specfier if present.
1999 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00002000 if (DS.isInlineSpecified())
2001 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2002 if (DS.isVirtualSpecified())
2003 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2004 if (DS.isExplicitSpecified())
2005 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 DS.ClearFunctionSpecs();
2007 }
Richard Smith69730c12012-03-12 07:56:15 +00002008
2009 // Issue diagnostic and remove constexpr specfier if present.
2010 if (DS.isConstexprSpecified()) {
2011 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2012 DS.ClearConstexprSpec();
2013 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002014}
2015
Chris Lattnerc199ab32009-04-12 20:42:31 +00002016/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2017/// specified token is valid after the identifier in a declarator which
2018/// immediately follows the declspec. For example, these things are valid:
2019///
2020/// int x [ 4]; // direct-declarator
2021/// int x ( int y); // direct-declarator
2022/// int(int x ) // direct-declarator
2023/// int x ; // simple-declaration
2024/// int x = 17; // init-declarator-list
2025/// int x , y; // init-declarator-list
2026/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002027/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00002028/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00002029///
2030/// This is not, because 'x' does not immediately follow the declspec (though
2031/// ')' happens to be valid anyway).
2032/// int (x)
2033///
2034static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2035 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2036 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002037 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00002038}
2039
Chris Lattnere40c2952009-04-14 21:34:55 +00002040
2041/// ParseImplicitInt - This method is called when we have an non-typename
2042/// identifier in a declspec (which normally terminates the decl spec) when
2043/// the declspec has no type specifier. In this case, the declspec is either
2044/// malformed or is "implicit int" (in K&R and C89).
2045///
2046/// This method handles diagnosing this prettily and returns false if the
2047/// declspec is done being processed. If it recovers and thinks there may be
2048/// other pieces of declspec after it, it returns true.
2049///
Chris Lattnerf4382f52009-04-14 22:17:06 +00002050bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002051 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00002052 AccessSpecifier AS, DeclSpecContext DSC,
2053 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002054 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Chris Lattnere40c2952009-04-14 21:34:55 +00002056 SourceLocation Loc = Tok.getLocation();
2057 // If we see an identifier that is not a type name, we normally would
2058 // parse it as the identifer being declared. However, when a typename
2059 // is typo'd or the definition is not included, this will incorrectly
2060 // parse the typename as the identifier name and fall over misparsing
2061 // later parts of the diagnostic.
2062 //
2063 // As such, we try to do some look-ahead in cases where this would
2064 // otherwise be an "implicit-int" case to see if this is invalid. For
2065 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2066 // an identifier with implicit int, we'd get a parse error because the
2067 // next token is obviously invalid for a type. Parse these as a case
2068 // with an invalid type specifier.
2069 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00002070
Chris Lattnere40c2952009-04-14 21:34:55 +00002071 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00002072 // error, do lookahead to try to do better recovery. This never applies
2073 // within a type specifier. Outside of C++, we allow this even if the
2074 // language doesn't "officially" support implicit int -- we support
Richard Smith58eb3702013-04-30 22:43:51 +00002075 // implicit int as an extension in C99 and C11.
Richard Smitha971d242012-05-09 20:55:26 +00002076 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith58eb3702013-04-30 22:43:51 +00002077 !getLangOpts().CPlusPlus &&
Richard Smith69730c12012-03-12 07:56:15 +00002078 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002079 // If this token is valid for implicit int, e.g. "static x = 4", then
2080 // we just avoid eating the identifier, so it will be parsed as the
2081 // identifier in the declarator.
2082 return false;
2083 }
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Richard Smith827adaf2012-05-15 21:01:51 +00002085 if (getLangOpts().CPlusPlus &&
2086 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2087 // Don't require a type specifier if we have the 'auto' storage class
2088 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithb79b17b2013-10-15 00:00:26 +00002089 if (SS)
2090 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smith827adaf2012-05-15 21:01:51 +00002091 return false;
2092 }
2093
Chris Lattnere40c2952009-04-14 21:34:55 +00002094 // Otherwise, if we don't consume this token, we are going to emit an
2095 // error anyway. Try to recover from various common problems. Check
2096 // to see if this was a reference to a tag name without a tag specified.
2097 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00002098 //
2099 // C++ doesn't need this, and isTagName doesn't take SS.
2100 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002101 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002102 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Douglas Gregor23c94db2010-07-02 17:43:08 +00002104 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002105 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002106 case DeclSpec::TST_enum:
2107 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2108 case DeclSpec::TST_union:
2109 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2110 case DeclSpec::TST_struct:
2111 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00002112 case DeclSpec::TST_interface:
2113 TagName="__interface"; FixitTagName = "__interface ";
2114 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002115 case DeclSpec::TST_class:
2116 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00002117 }
Mike Stump1eb44332009-09-09 15:08:12 +00002118
Chris Lattnerf4382f52009-04-14 22:17:06 +00002119 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002120 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2121 LookupResult R(Actions, TokenName, SourceLocation(),
2122 Sema::LookupOrdinaryName);
2123
Chris Lattnerf4382f52009-04-14 22:17:06 +00002124 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002125 << TokenName << TagName << getLangOpts().CPlusPlus
2126 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2127
2128 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2129 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2130 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00002131 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002132 << TokenName << TagName;
2133 }
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Chris Lattnerf4382f52009-04-14 22:17:06 +00002135 // Parse this as a tag as if the missing tag were present.
2136 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00002137 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002138 else
Richard Smith69730c12012-03-12 07:56:15 +00002139 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002140 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002141 return true;
2142 }
Chris Lattnere40c2952009-04-14 21:34:55 +00002143 }
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Richard Smith8f0a7e72012-05-15 21:29:55 +00002145 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002146 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00002147 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2148 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002149 // Look ahead to the next token to try to figure out what this declaration
2150 // was supposed to be.
2151 switch (NextToken().getKind()) {
Richard Smith827adaf2012-05-15 21:01:51 +00002152 case tok::l_paren: {
2153 // static x(4); // 'x' is not a type
2154 // x(int n); // 'x' is not a type
2155 // x (*p)[]; // 'x' is a type
2156 //
2157 // Since we're in an error case (or the rare 'implicit int in C++' MS
2158 // extension), we can afford to perform a tentative parse to determine
2159 // which case we're in.
2160 TentativeParsingAction PA(*this);
2161 ConsumeToken();
2162 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2163 PA.Revert();
Richard Smithb79b17b2013-10-15 00:00:26 +00002164
2165 if (TPR != TPResult::False()) {
2166 // The identifier is followed by a parenthesized declarator.
2167 // It's supposed to be a type.
2168 break;
2169 }
2170
2171 // If we're in a context where we could be declaring a constructor,
2172 // check whether this is a constructor declaration with a bogus name.
2173 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2174 IdentifierInfo *II = Tok.getIdentifierInfo();
2175 if (Actions.isCurrentClassNameTypo(II, SS)) {
2176 Diag(Loc, diag::err_constructor_bad_name)
2177 << Tok.getIdentifierInfo() << II
2178 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2179 Tok.setIdentifierInfo(II);
2180 }
2181 }
2182 // Fall through.
Richard Smith827adaf2012-05-15 21:01:51 +00002183 }
Richard Smithb79b17b2013-10-15 00:00:26 +00002184 case tok::comma:
2185 case tok::equal:
2186 case tok::kw_asm:
2187 case tok::l_brace:
2188 case tok::l_square:
2189 case tok::semi:
2190 // This looks like a variable or function declaration. The type is
2191 // probably missing. We're done parsing decl-specifiers.
2192 if (SS)
2193 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2194 return false;
Richard Smith827adaf2012-05-15 21:01:51 +00002195
2196 default:
2197 // This is probably supposed to be a type. This includes cases like:
2198 // int f(itn);
2199 // struct S { unsinged : 4; };
2200 break;
2201 }
2202 }
2203
Chad Rosier8decdee2012-06-26 22:30:43 +00002204 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002205 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002206 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002207 IdentifierInfo *II = Tok.getIdentifierInfo();
2208 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002209 // The action emitted a diagnostic, so we don't have to.
2210 if (T) {
2211 // The action has suggested that the type T could be used. Set that as
2212 // the type in the declaration specifiers, consume the would-be type
2213 // name token, and we're done.
2214 const char *PrevSpec;
2215 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002216 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002217 DS.SetRangeEnd(Tok.getLocation());
2218 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002219 // There may be other declaration specifiers after this.
2220 return true;
2221 } else if (II != Tok.getIdentifierInfo()) {
2222 // If no type was suggested, the correction is to a keyword
2223 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002224 // There may be other declaration specifiers after this.
2225 return true;
2226 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002227
Douglas Gregora786fdb2009-10-13 23:27:22 +00002228 // Fall through; the action had no suggestion for us.
2229 } else {
2230 // The action did not emit a diagnostic, so emit one now.
2231 SourceRange R;
2232 if (SS) R = SS->getRange();
2233 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2234 }
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Douglas Gregora786fdb2009-10-13 23:27:22 +00002236 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002237 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002238 DS.SetRangeEnd(Tok.getLocation());
2239 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Chris Lattnere40c2952009-04-14 21:34:55 +00002241 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2242 // avoid rippling error messages on subsequent uses of the same type,
2243 // could be useful if #include was forgotten.
2244 return false;
2245}
2246
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002247/// \brief Determine the declaration specifier context from the declarator
2248/// context.
2249///
2250/// \param Context the declarator context, which is one of the
2251/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002252Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002253Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2254 if (Context == Declarator::MemberContext)
2255 return DSC_class;
2256 if (Context == Declarator::FileContext)
2257 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002258 if (Context == Declarator::TrailingReturnContext)
2259 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002260 return DSC_normal;
2261}
2262
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002263/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2264///
2265/// FIXME: Simply returns an alignof() expression if the argument is a
2266/// type. Ideally, the type should be propagated directly into Sema.
2267///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002268/// [C11] type-id
2269/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002270/// [C++0x] type-id ...[opt]
2271/// [C++0x] assignment-expression ...[opt]
2272ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2273 SourceLocation &EllipsisLoc) {
2274 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002275 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002276 SourceLocation TypeLoc = Tok.getLocation();
2277 ParsedType Ty = ParseTypeName().get();
2278 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002279 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2280 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002281 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002282 ER = ParseConstantExpression();
2283
Richard Smith80ad52f2013-01-02 11:42:31 +00002284 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002285 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002286
2287 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002288}
2289
2290/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2291/// attribute to Attrs.
2292///
2293/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002294/// [C11] '_Alignas' '(' type-id ')'
2295/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002296/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2297/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002298void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002299 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002300 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2301 "Not an alignment-specifier!");
2302
Richard Smith33f04a22013-01-29 01:48:07 +00002303 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2304 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002305
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002306 BalancedDelimiterTracker T(*this, tok::l_paren);
2307 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002308 return;
2309
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002310 SourceLocation EllipsisLoc;
2311 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002312 if (ArgExpr.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002313 T.skipToEnd();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002314 return;
2315 }
2316
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002317 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002318 if (EndLoc)
2319 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002320
Aaron Ballman624421f2013-08-31 01:11:41 +00002321 ArgsVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002322 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman624421f2013-08-31 01:11:41 +00002323 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2324 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002325}
2326
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002327/// Determine whether we're looking at something that might be a declarator
2328/// in a simple-declaration. If it can't possibly be a declarator, maybe
2329/// diagnose a missing semicolon after a prior tag definition in the decl
2330/// specifier.
2331///
2332/// \return \c true if an error occurred and this can't be any kind of
2333/// declaration.
2334bool
2335Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2336 DeclSpecContext DSContext,
2337 LateParsedAttrList *LateAttrs) {
2338 assert(DS.hasTagDefinition() && "shouldn't call this");
2339
2340 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
2341 bool HasMissingSemi = false;
2342
2343 if (getLangOpts().CPlusPlus &&
2344 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2345 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2346 TryAnnotateCXXScopeToken(EnteringContext)) {
2347 SkipMalformedDecl();
2348 return true;
2349 }
2350
2351 // Determine whether the following tokens could possibly be a
2352 // declarator.
2353 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2354 const Token &Next = NextToken();
2355 // These tokens cannot come after the declarator-id in a
2356 // simple-declaration, and are likely to come after a type-specifier.
2357 HasMissingSemi = Next.is(tok::star) || Next.is(tok::amp) ||
2358 Next.is(tok::ampamp) || Next.is(tok::identifier) ||
2359 Next.is(tok::annot_cxxscope) ||
2360 Next.is(tok::coloncolon);
2361 } else if (Tok.is(tok::annot_cxxscope) &&
2362 NextToken().is(tok::identifier) &&
2363 DS.getStorageClassSpec() != DeclSpec::SCS_typedef) {
2364 // We almost certainly have a missing semicolon. Look up the name and
2365 // check; if it names a type, we're missing a semicolon.
2366 CXXScopeSpec SS;
2367 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2368 Tok.getAnnotationRange(), SS);
2369 const Token &Next = NextToken();
2370 IdentifierInfo *Name = Next.getIdentifierInfo();
2371 Sema::NameClassification Classification =
2372 Actions.ClassifyName(getCurScope(), SS, Name, Next.getLocation(),
2373 NextToken(), /*IsAddressOfOperand*/false);
2374 switch (Classification.getKind()) {
2375 case Sema::NC_Error:
2376 SkipMalformedDecl();
2377 return true;
2378
2379 case Sema::NC_Keyword:
2380 case Sema::NC_NestedNameSpecifier:
2381 llvm_unreachable("typo correction and nested name specifiers not "
2382 "possible here");
2383
2384 case Sema::NC_Type:
2385 case Sema::NC_TypeTemplate:
2386 // Not a previously-declared non-type entity.
2387 HasMissingSemi = true;
2388 break;
2389
2390 case Sema::NC_Unknown:
2391 case Sema::NC_Expression:
2392 case Sema::NC_VarTemplate:
2393 case Sema::NC_FunctionTemplate:
2394 // Might be a redeclaration of a prior entity.
2395 HasMissingSemi = false;
2396 break;
2397 }
2398 } else if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2399 HasMissingSemi = true;
2400 }
2401
2402 if (!HasMissingSemi)
2403 return false;
2404
2405 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
2406 diag::err_expected_semi_after_tagdecl)
2407 << DeclSpec::getSpecifierName(DS.getTypeSpecType());
2408
2409 // Try to recover from the typo, by dropping the tag definition and parsing
2410 // the problematic tokens as a type.
2411 //
2412 // FIXME: Split the DeclSpec into pieces for the standalone
2413 // declaration and pieces for the following declaration, instead
2414 // of assuming that all the other pieces attach to new declaration,
2415 // and call ParsedFreeStandingDeclSpec as appropriate.
2416 DS.ClearTypeSpecType();
2417 ParsedTemplateInfo NotATemplate;
2418 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2419 return false;
2420}
2421
Reid Spencer5f016e22007-07-11 17:01:13 +00002422/// ParseDeclarationSpecifiers
2423/// declaration-specifiers: [C99 6.7]
2424/// storage-class-specifier declaration-specifiers[opt]
2425/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002426/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002427/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002428/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002429/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002430///
2431/// storage-class-specifier: [C99 6.7.1]
2432/// 'typedef'
2433/// 'extern'
2434/// 'static'
2435/// 'auto'
2436/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002437/// [C++] 'mutable'
Richard Smithec642442013-04-12 22:46:28 +00002438/// [C++11] 'thread_local'
2439/// [C11] '_Thread_local'
Reid Spencer5f016e22007-07-11 17:01:13 +00002440/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002441/// function-specifier: [C99 6.7.4]
2442/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002443/// [C++] 'virtual'
2444/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002445/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002446/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002447/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002448
Reid Spencer5f016e22007-07-11 17:01:13 +00002449///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002450void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002451 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002452 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002453 DeclSpecContext DSContext,
2454 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002455 if (DS.getSourceRange().isInvalid()) {
2456 DS.SetRangeStart(Tok.getLocation());
2457 DS.SetRangeEnd(Tok.getLocation());
2458 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002459
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002460 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002461 bool AttrsLastTime = false;
2462 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002463 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002464 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002465 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002466 unsigned DiagID = 0;
2467
Reid Spencer5f016e22007-07-11 17:01:13 +00002468 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002469
Reid Spencer5f016e22007-07-11 17:01:13 +00002470 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002471 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002472 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002473 if (!AttrsLastTime)
2474 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002475 else {
2476 // Reject C++11 attributes that appertain to decl specifiers as
2477 // we don't support any C++11 attributes that appertain to decl
2478 // specifiers. This also conforms to what g++ 4.8 is doing.
2479 ProhibitCXX11Attributes(attrs);
2480
Sean Hunt2edf0a22012-06-23 05:07:58 +00002481 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002482 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002483
Reid Spencer5f016e22007-07-11 17:01:13 +00002484 // If this is not a declaration specifier token, we're done reading decl
2485 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002486 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002487 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Sean Hunt2edf0a22012-06-23 05:07:58 +00002489 case tok::l_square:
2490 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002491 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002492 goto DoneWithDeclSpec;
2493
2494 ProhibitAttributes(attrs);
2495 // FIXME: It would be good to recover by accepting the attributes,
2496 // but attempting to do that now would cause serious
2497 // madness in terms of diagnostics.
2498 attrs.clear();
2499 attrs.Range = SourceRange();
2500
2501 ParseCXX11Attributes(attrs);
2502 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002503 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002504
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002505 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002506 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002507 if (DS.hasTypeSpecifier()) {
2508 bool AllowNonIdentifiers
2509 = (getCurScope()->getFlags() & (Scope::ControlScope |
2510 Scope::BlockScope |
2511 Scope::TemplateParamScope |
2512 Scope::FunctionPrototypeScope |
2513 Scope::AtCatchScope)) == 0;
2514 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002515 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002516 (DSContext == DSC_class && DS.isFriendSpecified());
2517
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002518 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002519 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002520 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002521 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002522 }
2523
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002524 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2525 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2526 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002527 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002528 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002529 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002530 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002531 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002532 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002533
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002534 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002535 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002536 }
2537
Chris Lattner5e02c472009-01-05 00:07:25 +00002538 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002539 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman5a428202013-08-15 23:59:20 +00002540 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002541 if (!DS.hasTypeSpecifier())
2542 DS.SetTypeSpecError();
2543 goto DoneWithDeclSpec;
2544 }
John McCall2e0a7152010-03-01 18:20:46 +00002545 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2546 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002547 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002548
2549 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002550 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002551 goto DoneWithDeclSpec;
2552
John McCallaa87d332009-12-12 11:40:51 +00002553 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002554 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2555 Tok.getAnnotationRange(),
2556 SS);
John McCallaa87d332009-12-12 11:40:51 +00002557
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002558 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002559 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002560 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002561 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002562 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002563 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002564
2565 // C++ [class.qual]p2:
2566 // In a lookup in which the constructor is an acceptable lookup
2567 // result and the nested-name-specifier nominates a class C:
2568 //
2569 // - if the name specified after the
2570 // nested-name-specifier, when looked up in C, is the
2571 // injected-class-name of C (Clause 9), or
2572 //
2573 // - if the name specified after the nested-name-specifier
2574 // is the same as the identifier or the
2575 // simple-template-id's template-name in the last
2576 // component of the nested-name-specifier,
2577 //
2578 // the name is instead considered to name the constructor of
2579 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002580 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002581 // Thus, if the template-name is actually the constructor
2582 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002583 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002584 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002585 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002586 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002587 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002588 if (isConstructorDeclarator()) {
2589 // The user meant this to be an out-of-line constructor
2590 // definition, but template arguments are not allowed
2591 // there. Just allow this as a constructor; we'll
2592 // complain about it later.
2593 goto DoneWithDeclSpec;
2594 }
2595
2596 // The user meant this to name a type, but it actually names
2597 // a constructor with some extraneous template
2598 // arguments. Complain, then parse it as a type as the user
2599 // intended.
2600 Diag(TemplateId->TemplateNameLoc,
2601 diag::err_out_of_line_template_id_names_constructor)
2602 << TemplateId->Name;
2603 }
2604
John McCallaa87d332009-12-12 11:40:51 +00002605 DS.getTypeSpecScope() = SS;
2606 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002607 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002608 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002609 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002610 continue;
2611 }
2612
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002613 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002614 DS.getTypeSpecScope() = SS;
2615 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002616 if (Tok.getAnnotationValue()) {
2617 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002618 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002619 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002620 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002621 if (isInvalid)
2622 break;
John McCallb3d87482010-08-24 05:47:05 +00002623 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002624 else
2625 DS.SetTypeSpecError();
2626 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2627 ConsumeToken(); // The typename
2628 }
2629
Douglas Gregor9135c722009-03-25 15:40:00 +00002630 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002631 goto DoneWithDeclSpec;
2632
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002633 // If we're in a context where the identifier could be a class name,
2634 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002635 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002636 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002637 &SS)) {
2638 if (isConstructorDeclarator())
2639 goto DoneWithDeclSpec;
2640
2641 // As noted in C++ [class.qual]p2 (cited above), when the name
2642 // of the class is qualified in a context where it could name
2643 // a constructor, its a constructor name. However, we've
2644 // looked at the declarator, and the user probably meant this
2645 // to be a type. Complain that it isn't supposed to be treated
2646 // as a type, then proceed to parse it as a type.
2647 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2648 << Next.getIdentifierInfo();
2649 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002650
John McCallb3d87482010-08-24 05:47:05 +00002651 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2652 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002653 getCurScope(), &SS,
2654 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002655 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002656 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002657
Chris Lattnerf4382f52009-04-14 22:17:06 +00002658 // If the referenced identifier is not a type, then this declspec is
2659 // erroneous: We already checked about that it has no type specifier, and
2660 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002661 // typename.
David Blaikie7247c882013-05-15 07:37:26 +00002662 if (!TypeRep) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002663 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002664 ParsedAttributesWithRange Attrs(AttrFactory);
2665 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2666 if (!Attrs.empty()) {
2667 AttrsLastTime = true;
2668 attrs.takeAllFrom(Attrs);
2669 }
2670 continue;
2671 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002672 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002673 }
Mike Stump1eb44332009-09-09 15:08:12 +00002674
John McCallaa87d332009-12-12 11:40:51 +00002675 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002676 ConsumeToken(); // The C++ scope.
2677
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002678 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002679 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002680 if (isInvalid)
2681 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002683 DS.SetRangeEnd(Tok.getLocation());
2684 ConsumeToken(); // The typename.
2685
2686 continue;
2687 }
Mike Stump1eb44332009-09-09 15:08:12 +00002688
Chris Lattner80d0c892009-01-21 19:48:37 +00002689 case tok::annot_typename: {
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002690 // If we've previously seen a tag definition, we were almost surely
2691 // missing a semicolon after it.
2692 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2693 goto DoneWithDeclSpec;
2694
John McCallb3d87482010-08-24 05:47:05 +00002695 if (Tok.getAnnotationValue()) {
2696 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002697 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002698 DiagID, T);
2699 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002700 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002701
Chris Lattner5c5db552010-04-05 18:18:31 +00002702 if (isInvalid)
2703 break;
2704
Chris Lattner80d0c892009-01-21 19:48:37 +00002705 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2706 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002707
Chris Lattner80d0c892009-01-21 19:48:37 +00002708 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2709 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002710 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002711 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002712 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002713
Chris Lattner80d0c892009-01-21 19:48:37 +00002714 continue;
2715 }
Mike Stump1eb44332009-09-09 15:08:12 +00002716
Douglas Gregorbfad9152011-04-28 15:48:45 +00002717 case tok::kw___is_signed:
2718 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2719 // typically treats it as a trait. If we see __is_signed as it appears
2720 // in libstdc++, e.g.,
2721 //
2722 // static const bool __is_signed;
2723 //
2724 // then treat __is_signed as an identifier rather than as a keyword.
2725 if (DS.getTypeSpecType() == TST_bool &&
2726 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2727 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2728 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2729 Tok.setKind(tok::identifier);
2730 }
2731
2732 // We're done with the declaration-specifiers.
2733 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002734
Chris Lattner3bd934a2008-07-26 01:18:38 +00002735 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002736 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002737 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002738 // In C++, check to see if this is a scope specifier like foo::bar::, if
2739 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002740 if (getLangOpts().CPlusPlus) {
Eli Friedman5a428202013-08-15 23:59:20 +00002741 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002742 if (!DS.hasTypeSpecifier())
2743 DS.SetTypeSpecError();
2744 goto DoneWithDeclSpec;
2745 }
2746 if (!Tok.is(tok::identifier))
2747 continue;
2748 }
Mike Stump1eb44332009-09-09 15:08:12 +00002749
Chris Lattner3bd934a2008-07-26 01:18:38 +00002750 // This identifier can only be a typedef name if we haven't already seen
2751 // a type-specifier. Without this check we misparse:
2752 // typedef int X; struct Y { short X; }; as 'short int'.
2753 if (DS.hasTypeSpecifier())
2754 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002755
John Thompson82287d12010-02-05 00:12:22 +00002756 // Check for need to substitute AltiVec keyword tokens.
2757 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2758 break;
2759
Richard Smithf63eee72012-05-09 18:56:43 +00002760 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2761 // allow the use of a typedef name as a type specifier.
2762 if (DS.isTypeAltiVecVector())
2763 goto DoneWithDeclSpec;
2764
John McCallb3d87482010-08-24 05:47:05 +00002765 ParsedType TypeRep =
2766 Actions.getTypeName(*Tok.getIdentifierInfo(),
2767 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002768
Chris Lattnerc199ab32009-04-12 20:42:31 +00002769 // If this is not a typedef name, don't parse it as part of the declspec,
2770 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002771 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002772 ParsedAttributesWithRange Attrs(AttrFactory);
2773 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2774 if (!Attrs.empty()) {
2775 AttrsLastTime = true;
2776 attrs.takeAllFrom(Attrs);
2777 }
2778 continue;
2779 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002780 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002781 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002782
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002783 // If we're in a context where the identifier could be a class name,
2784 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002785 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002786 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002787 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002788 goto DoneWithDeclSpec;
2789
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002791 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002792 if (isInvalid)
2793 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002794
Chris Lattner3bd934a2008-07-26 01:18:38 +00002795 DS.SetRangeEnd(Tok.getLocation());
2796 ConsumeToken(); // The identifier
2797
2798 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2799 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002800 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002801 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002802 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002803
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002804 // Need to support trailing type qualifiers (e.g. "id<p> const").
2805 // If a type specifier follows, it will be diagnosed elsewhere.
2806 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002807 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002808
2809 // type-name
2810 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002811 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002812 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002813 // This template-id does not refer to a type name, so we're
2814 // done with the type-specifiers.
2815 goto DoneWithDeclSpec;
2816 }
2817
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002818 // If we're in a context where the template-id could be a
2819 // constructor name or specialization, check whether this is a
2820 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002821 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002822 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002823 isConstructorDeclarator())
2824 goto DoneWithDeclSpec;
2825
Douglas Gregor39a8de12009-02-25 19:37:18 +00002826 // Turn the template-id annotation token into a type annotation
2827 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002828 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002829 continue;
2830 }
2831
Reid Spencer5f016e22007-07-11 17:01:13 +00002832 // GNU attributes support.
2833 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002834 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002835 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002836
2837 // Microsoft declspec support.
2838 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002839 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002840 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Steve Naroff239f0732008-12-25 14:16:32 +00002842 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002843 case tok::kw___forceinline: {
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002844 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002845 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002846 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002847 // FIXME: This does not work correctly if it is set to be a declspec
2848 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman624421f2013-08-31 01:11:41 +00002849 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2850 AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002851 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002852 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002853
Aaron Ballmanaa9df092013-05-22 23:25:32 +00002854 case tok::kw___sptr:
2855 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00002856 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002857 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002858 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002859 case tok::kw___cdecl:
2860 case tok::kw___stdcall:
2861 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002862 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002863 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002864 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002865 continue;
2866
Dawn Perchik52fc3142010-09-03 01:29:35 +00002867 // Borland single token adornments.
2868 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002869 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002870 continue;
2871
Peter Collingbournef315fa82011-02-14 01:42:53 +00002872 // OpenCL single token adornments.
2873 case tok::kw___kernel:
2874 ParseOpenCLAttributes(DS.getAttributes());
2875 continue;
2876
Reid Spencer5f016e22007-07-11 17:01:13 +00002877 // storage-class-specifier
2878 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002879 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2880 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002881 break;
2882 case tok::kw_extern:
Richard Smithec642442013-04-12 22:46:28 +00002883 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002884 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002885 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2886 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002887 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002888 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002889 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2890 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002891 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002892 case tok::kw_static:
Richard Smithec642442013-04-12 22:46:28 +00002893 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002894 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002895 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2896 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002897 break;
2898 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002899 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002900 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002901 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2902 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002903 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002904 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002905 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002906 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2908 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002909 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002910 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2911 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002912 break;
2913 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002914 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2915 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002917 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002918 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2919 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002920 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002921 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00002922 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2923 PrevSpec, DiagID);
2924 break;
2925 case tok::kw_thread_local:
2926 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2927 PrevSpec, DiagID);
2928 break;
2929 case tok::kw__Thread_local:
2930 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2931 Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002932 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002933
Reid Spencer5f016e22007-07-11 17:01:13 +00002934 // function-specifier
2935 case tok::kw_inline:
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002936 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002937 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002938 case tok::kw_virtual:
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002939 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002940 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002941 case tok::kw_explicit:
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002942 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002943 break;
Richard Smithde03c152013-01-17 22:16:11 +00002944 case tok::kw__Noreturn:
2945 if (!getLangOpts().C11)
2946 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002947 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smithde03c152013-01-17 22:16:11 +00002948 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002949
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002950 // alignment-specifier
2951 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002952 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002953 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002954 ParseAlignmentSpecifier(DS.getAttributes());
2955 continue;
2956
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002957 // friend
2958 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002959 if (DSContext == DSC_class)
2960 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2961 else {
2962 PrevSpec = ""; // not actually used by the diagnostic
2963 DiagID = diag::err_friend_invalid_in_context;
2964 isInvalid = true;
2965 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002966 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002967
Douglas Gregor8d267c52011-09-09 02:06:17 +00002968 // Modules
2969 case tok::kw___module_private__:
2970 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2971 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002972
Sebastian Redl2ac67232009-11-05 15:47:02 +00002973 // constexpr
2974 case tok::kw_constexpr:
2975 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2976 break;
2977
Chris Lattner80d0c892009-01-21 19:48:37 +00002978 // type-specifier
2979 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002980 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2981 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002982 break;
2983 case tok::kw_long:
2984 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002985 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2986 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002987 else
John McCallfec54012009-08-03 20:12:06 +00002988 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2989 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002990 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002991 case tok::kw___int64:
2992 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2993 DiagID);
2994 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002995 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002996 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2997 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002998 break;
2999 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00003000 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3001 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003002 break;
3003 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00003004 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3005 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003006 break;
3007 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00003008 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3009 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003010 break;
3011 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00003012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3013 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003014 break;
3015 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00003016 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3017 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003018 break;
3019 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00003020 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3021 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003022 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00003023 case tok::kw___int128:
3024 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3025 DiagID);
3026 break;
3027 case tok::kw_half:
3028 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3029 DiagID);
3030 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003031 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00003032 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3033 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003034 break;
3035 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00003036 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3037 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003038 break;
3039 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00003040 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3041 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003042 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003043 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00003044 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3045 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003046 break;
3047 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00003048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3049 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003050 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003051 case tok::kw_bool:
3052 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00003053 if (Tok.is(tok::kw_bool) &&
3054 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3055 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3056 PrevSpec = ""; // Not used by the diagnostic.
3057 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003058 // For better error recovery.
3059 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00003060 isInvalid = true;
3061 } else {
3062 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3063 DiagID);
3064 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003065 break;
3066 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00003067 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3068 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003069 break;
3070 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00003071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3072 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003073 break;
3074 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00003075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3076 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00003077 break;
John Thompson82287d12010-02-05 00:12:22 +00003078 case tok::kw___vector:
3079 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3080 break;
3081 case tok::kw___pixel:
3082 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3083 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00003084 case tok::kw_image1d_t:
3085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
3086 PrevSpec, DiagID);
3087 break;
3088 case tok::kw_image1d_array_t:
3089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
3090 PrevSpec, DiagID);
3091 break;
3092 case tok::kw_image1d_buffer_t:
3093 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3094 PrevSpec, DiagID);
3095 break;
3096 case tok::kw_image2d_t:
3097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3098 PrevSpec, DiagID);
3099 break;
3100 case tok::kw_image2d_array_t:
3101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3102 PrevSpec, DiagID);
3103 break;
3104 case tok::kw_image3d_t:
3105 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3106 PrevSpec, DiagID);
3107 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00003108 case tok::kw_sampler_t:
3109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3110 PrevSpec, DiagID);
3111 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00003112 case tok::kw_event_t:
3113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3114 PrevSpec, DiagID);
3115 break;
John McCalla5fc4722011-04-09 22:50:59 +00003116 case tok::kw___unknown_anytype:
3117 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3118 PrevSpec, DiagID);
3119 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003120
3121 // class-specifier:
3122 case tok::kw_class:
3123 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003124 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00003125 case tok::kw_union: {
3126 tok::TokenKind Kind = Tok.getKind();
3127 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00003128
3129 // These are attributes following class specifiers.
3130 // To produce better diagnostic, we parse them when
3131 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003132 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00003133 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00003134 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003135
3136 // If there are attributes following class specifier,
3137 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003138 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00003139 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00003140 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003141 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003142 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00003143 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003144
3145 // enum-specifier:
3146 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00003147 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00003148 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00003149 continue;
3150
3151 // cv-qualifier:
3152 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003153 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003154 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003155 break;
3156 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003157 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003158 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003159 break;
3160 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003161 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003162 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003163 break;
3164
Douglas Gregord57959a2009-03-27 23:10:48 +00003165 // C++ typename-specifier:
3166 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00003167 if (TryAnnotateTypeOrScopeToken()) {
3168 DS.SetTypeSpecError();
3169 goto DoneWithDeclSpec;
3170 }
3171 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00003172 continue;
3173 break;
3174
Chris Lattner80d0c892009-01-21 19:48:37 +00003175 // GNU typeof support.
3176 case tok::kw_typeof:
3177 ParseTypeofSpecifier(DS);
3178 continue;
3179
David Blaikie42d6d0c2011-12-04 05:04:18 +00003180 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00003181 ParseDecltypeSpecifier(DS);
3182 continue;
3183
Sean Huntdb5d44b2011-05-19 05:37:45 +00003184 case tok::kw___underlying_type:
3185 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00003186 continue;
3187
3188 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003189 // C11 6.7.2.4/4:
3190 // If the _Atomic keyword is immediately followed by a left parenthesis,
3191 // it is interpreted as a type specifier (with a type name), not as a
3192 // type qualifier.
3193 if (NextToken().is(tok::l_paren)) {
3194 ParseAtomicSpecifier(DS);
3195 continue;
3196 }
3197 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3198 getLangOpts());
3199 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00003200
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003201 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003202 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003203 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003204 goto DoneWithDeclSpec;
3205 case tok::kw___private:
3206 case tok::kw___global:
3207 case tok::kw___local:
3208 case tok::kw___constant:
3209 case tok::kw___read_only:
3210 case tok::kw___write_only:
3211 case tok::kw___read_write:
3212 ParseOpenCLQualifiers(DS);
3213 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00003214
Steve Naroffd3ded1f2008-06-05 00:02:44 +00003215 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00003216 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00003217 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3218 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00003219 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00003220 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00003221
Douglas Gregor46f936e2010-11-19 17:10:50 +00003222 if (!ParseObjCProtocolQualifiers(DS))
3223 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3224 << FixItHint::CreateInsertion(Loc, "id")
3225 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00003226
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00003227 // Need to support trailing type qualifiers (e.g. "id<p> const").
3228 // If a type specifier follows, it will be diagnosed elsewhere.
3229 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00003230 }
John McCallfec54012009-08-03 20:12:06 +00003231 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00003232 if (isInvalid) {
3233 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00003234 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00003235
Douglas Gregorae2fb142010-08-23 14:34:43 +00003236 if (DiagID == diag::ext_duplicate_declspec)
3237 Diag(Tok, DiagID)
3238 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3239 else
3240 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003241 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00003242
Chris Lattner81c018d2008-03-13 06:29:04 +00003243 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003244 if (DiagID != diag::err_bool_redeclaration)
3245 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00003246
3247 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003248 }
3249}
Douglas Gregoradcac882008-12-01 23:54:00 +00003250
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003251/// ParseStructDeclaration - Parse a struct declaration without the terminating
3252/// semicolon.
3253///
Reid Spencer5f016e22007-07-11 17:01:13 +00003254/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003255/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003256/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003257/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003258/// struct-declarator-list:
3259/// struct-declarator
3260/// struct-declarator-list ',' struct-declarator
3261/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3262/// struct-declarator:
3263/// declarator
3264/// [GNU] declarator attributes[opt]
3265/// declarator[opt] ':' constant-expression
3266/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3267///
Chris Lattnere1359422008-04-10 06:46:29 +00003268void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003269ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003270
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003271 if (Tok.is(tok::kw___extension__)) {
3272 // __extension__ silences extension warnings in the subexpression.
3273 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003274 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003275 return ParseStructDeclaration(DS, Fields);
3276 }
Mike Stump1eb44332009-09-09 15:08:12 +00003277
Steve Naroff28a7ca82007-08-20 22:28:22 +00003278 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003279 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003281 // If there are no declarators, this is a free-standing declaration
3282 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003283 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003284 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3285 DS);
3286 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003287 return;
3288 }
3289
3290 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003291 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003292 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003293 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003294 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003295 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003296
Bill Wendlingad017fa2012-12-20 19:22:21 +00003297 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003298 if (!FirstDeclarator)
3299 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003300
Steve Naroff28a7ca82007-08-20 22:28:22 +00003301 /// struct-declarator: declarator
3302 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003303 if (Tok.isNot(tok::colon)) {
3304 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3305 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003306 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003307 }
Mike Stump1eb44332009-09-09 15:08:12 +00003308
Chris Lattner04d66662007-10-09 17:33:22 +00003309 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003310 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003311 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003312 if (Res.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00003313 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003314 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003315 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003316 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003317
Steve Naroff28a7ca82007-08-20 22:28:22 +00003318 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003319 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003320
John McCallbdd563e2009-11-03 02:38:08 +00003321 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003322 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003323
Steve Naroff28a7ca82007-08-20 22:28:22 +00003324 // If we don't have a comma, it is either the end of the list (a ';')
3325 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003326 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003327 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003328
Steve Naroff28a7ca82007-08-20 22:28:22 +00003329 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003330 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003331
John McCallbdd563e2009-11-03 02:38:08 +00003332 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003333 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003334}
3335
3336/// ParseStructUnionBody
3337/// struct-contents:
3338/// struct-declaration-list
3339/// [EXT] empty
3340/// [GNU] "struct-declaration-list" without terminatoring ';'
3341/// struct-declaration-list:
3342/// struct-declaration
3343/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003344/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003345///
Reid Spencer5f016e22007-07-11 17:01:13 +00003346void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003347 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003348 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3349 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003350 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003351
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003352 BalancedDelimiterTracker T(*this, tok::l_brace);
3353 if (T.consumeOpen())
3354 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003355
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003356 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003357 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003358
Chris Lattner5f9e2722011-07-23 10:55:15 +00003359 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003360
Reid Spencer5f016e22007-07-11 17:01:13 +00003361 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003362 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003363 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003364
Reid Spencer5f016e22007-07-11 17:01:13 +00003365 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003366 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003367 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003368 continue;
3369 }
Chris Lattnere1359422008-04-10 06:46:29 +00003370
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003371 // Parse _Static_assert declaration.
3372 if (Tok.is(tok::kw__Static_assert)) {
3373 SourceLocation DeclEnd;
3374 ParseStaticAssertDeclaration(DeclEnd);
3375 continue;
3376 }
3377
Argyrios Kyrtzidisbd957452013-04-18 01:42:35 +00003378 if (Tok.is(tok::annot_pragma_pack)) {
3379 HandlePragmaPack();
3380 continue;
3381 }
3382
3383 if (Tok.is(tok::annot_pragma_align)) {
3384 HandlePragmaAlign();
3385 continue;
3386 }
3387
John McCallbdd563e2009-11-03 02:38:08 +00003388 if (!Tok.is(tok::at)) {
3389 struct CFieldCallback : FieldCallback {
3390 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003391 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003392 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003393
John McCalld226f652010-08-21 09:40:31 +00003394 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003395 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003396 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3397
Eli Friedmandcdff462012-08-08 23:53:27 +00003398 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003399 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003400 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003401 FD.D.getDeclSpec().getSourceRange().getBegin(),
3402 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003403 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003404 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003405 }
John McCallbdd563e2009-11-03 02:38:08 +00003406 } Callback(*this, TagDecl, FieldDecls);
3407
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003408 // Parse all the comma separated declarators.
3409 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003410 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003411 } else { // Handle @defs
3412 ConsumeToken();
3413 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3414 Diag(Tok, diag::err_unexpected_at);
Alexey Bataev8fe24752013-11-18 08:17:37 +00003415 SkipUntil(tok::semi);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003416 continue;
3417 }
3418 ConsumeToken();
3419 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3420 if (!Tok.is(tok::identifier)) {
3421 Diag(Tok, diag::err_expected_ident);
Alexey Bataev8fe24752013-11-18 08:17:37 +00003422 SkipUntil(tok::semi);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003423 continue;
3424 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003425 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003426 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003427 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003428 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3429 ConsumeToken();
3430 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003431 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003432
Chris Lattner04d66662007-10-09 17:33:22 +00003433 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003434 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003435 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003436 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003437 break;
3438 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003439 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3440 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Alexey Bataev8fe24752013-11-18 08:17:37 +00003441 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003442 // If we stopped at a ';', eat it.
3443 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 }
3445 }
Mike Stump1eb44332009-09-09 15:08:12 +00003446
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003447 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003448
John McCall0b7e6782011-03-24 11:26:52 +00003449 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003450 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003451 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003452
Douglas Gregor23c94db2010-07-02 17:43:08 +00003453 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003454 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003455 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003456 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003457 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003458 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3459 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003460}
3461
Reid Spencer5f016e22007-07-11 17:01:13 +00003462/// ParseEnumSpecifier
3463/// enum-specifier: [C99 6.7.2.2]
3464/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003465///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003466/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3467/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003468/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3469/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003470/// 'enum' identifier
3471/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003472///
Richard Smith1af83c42012-03-23 03:33:32 +00003473/// [C++11] enum-head '{' enumerator-list[opt] '}'
3474/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003475///
Richard Smith1af83c42012-03-23 03:33:32 +00003476/// enum-head: [C++11]
3477/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3478/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3479/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003480///
Richard Smith1af83c42012-03-23 03:33:32 +00003481/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003482/// 'enum'
3483/// 'enum' 'class'
3484/// 'enum' 'struct'
3485///
Richard Smith1af83c42012-03-23 03:33:32 +00003486/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003487/// ':' type-specifier-seq
3488///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003489/// [C++] elaborated-type-specifier:
3490/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3491///
Chris Lattner4c97d762009-04-12 21:49:30 +00003492void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003493 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003494 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003495 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003496 if (Tok.is(tok::code_completion)) {
3497 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003498 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003499 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003500 }
John McCall57c13002011-07-06 05:58:41 +00003501
Sean Hunt2edf0a22012-06-23 05:07:58 +00003502 // If attributes exist after tag, parse them.
3503 ParsedAttributesWithRange attrs(AttrFactory);
3504 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003505 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003506
3507 // If declspecs exist after tag, parse them.
3508 while (Tok.is(tok::kw___declspec))
3509 ParseMicrosoftDeclSpec(attrs);
3510
Richard Smithbdad7a22012-01-10 01:33:14 +00003511 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003512 bool IsScopedUsingClassTag = false;
3513
John McCall1e12b3d2012-06-23 22:30:04 +00003514 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieued5a2922013-04-23 02:47:36 +00003515 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3516 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3517 : diag::ext_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003518 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003519 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003520
Bill Wendlingad017fa2012-12-20 19:22:21 +00003521 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003522 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003523 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003524
3525 // They are allowed afterwards, though.
3526 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003527 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003528 while (Tok.is(tok::kw___declspec))
3529 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003530 }
Richard Smith1af83c42012-03-23 03:33:32 +00003531
John McCall13489672012-05-07 06:16:58 +00003532 // C++11 [temp.explicit]p12:
3533 // The usual access controls do not apply to names used to specify
3534 // explicit instantiations.
3535 // We extend this to also cover explicit specializations. Note that
3536 // we don't suppress if this turns out to be an elaborated type
3537 // specifier.
3538 bool shouldDelayDiagsInTag =
3539 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3540 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3541 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003542
Richard Smith7796eb52012-03-12 08:56:40 +00003543 // Enum definitions should not be parsed in a trailing-return-type.
3544 bool AllowDeclaration = DSC != DSC_trailing;
3545
3546 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003547 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003548 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003549
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003550 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003551 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003552 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3553 // if a fixed underlying type is allowed.
3554 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003555
3556 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003557 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003558 return;
3559
3560 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003561 Diag(Tok, diag::err_expected_ident);
3562 if (Tok.isNot(tok::l_brace)) {
3563 // Has no name and is not a definition.
3564 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataev8fe24752013-11-18 08:17:37 +00003565 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003566 return;
3567 }
3568 }
3569 }
Mike Stump1eb44332009-09-09 15:08:12 +00003570
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003571 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003572 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003573 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003574 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003575
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003576 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataev8fe24752013-11-18 08:17:37 +00003577 SkipUntil(tok::comma, StopAtSemi);
Reid Spencer5f016e22007-07-11 17:01:13 +00003578 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003579 }
Mike Stump1eb44332009-09-09 15:08:12 +00003580
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003581 // If an identifier is present, consume and remember it.
3582 IdentifierInfo *Name = 0;
3583 SourceLocation NameLoc;
3584 if (Tok.is(tok::identifier)) {
3585 Name = Tok.getIdentifierInfo();
3586 NameLoc = ConsumeToken();
3587 }
Mike Stump1eb44332009-09-09 15:08:12 +00003588
Richard Smithbdad7a22012-01-10 01:33:14 +00003589 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003590 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3591 // declaration of a scoped enumeration.
3592 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003593 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003594 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003595 }
3596
John McCall13489672012-05-07 06:16:58 +00003597 // Okay, end the suppression area. We'll decide whether to emit the
3598 // diagnostics in a second.
3599 if (shouldDelayDiagsInTag)
3600 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003601
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003602 TypeResult BaseType;
3603
Douglas Gregora61b3e72010-12-01 17:42:47 +00003604 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003605 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003606 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003607 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003608 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003609 // If we're in class scope, this can either be an enum declaration with
3610 // an underlying type, or a declaration of a bitfield member. We try to
3611 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003612 // (integer literal, sizeof); if it's still ambiguous, we then consider
3613 // anything that's a simple-type-specifier followed by '(' as an
3614 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003615 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003616 EnterExpressionEvaluationContext Unevaluated(Actions,
3617 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003618 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003619 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003620 // bit-field. This is the common case.
3621 if (TPR == TPResult::True())
3622 PossibleBitfield = true;
3623 // If the next token starts a type-specifier-seq, it may be either a
3624 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003625 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003626 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003627 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003628 GetLookAheadToken(2).getKind() == tok::semi) {
3629 // Consume the ':'.
3630 ConsumeToken();
3631 } else {
3632 // We have the start of a type-specifier-seq, so we have to perform
3633 // tentative parsing to determine whether we have an expression or a
3634 // type.
3635 TentativeParsingAction TPA(*this);
3636
3637 // Consume the ':'.
3638 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003639
3640 // If we see a type specifier followed by an open-brace, we have an
3641 // ambiguity between an underlying type and a C++11 braced
3642 // function-style cast. Resolve this by always treating it as an
3643 // underlying type.
3644 // FIXME: The standard is not entirely clear on how to disambiguate in
3645 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003646 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003647 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003648 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003649 // We'll parse this as a bitfield later.
3650 PossibleBitfield = true;
3651 TPA.Revert();
3652 } else {
3653 // We have a type-specifier-seq.
3654 TPA.Commit();
3655 }
3656 }
3657 } else {
3658 // Consume the ':'.
3659 ConsumeToken();
3660 }
3661
3662 if (!PossibleBitfield) {
3663 SourceRange Range;
3664 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003665
Richard Smith80ad52f2013-01-02 11:42:31 +00003666 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003667 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003668 } else if (!getLangOpts().ObjC2) {
3669 if (getLangOpts().CPlusPlus)
3670 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3671 else
3672 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3673 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003674 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003675 }
3676
Richard Smithbdad7a22012-01-10 01:33:14 +00003677 // There are four options here. If we have 'friend enum foo;' then this is a
3678 // friend declaration, and cannot have an accompanying definition. If we have
3679 // 'enum foo;', then this is a forward declaration. If we have
3680 // 'enum foo {...' then this is a definition. Otherwise we have something
3681 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003682 //
3683 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3684 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3685 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3686 //
John McCallf312b1e2010-08-26 23:41:50 +00003687 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003688 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003689 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003690 } else if (Tok.is(tok::l_brace)) {
3691 if (DS.isFriendSpecified()) {
3692 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3693 << SourceRange(DS.getFriendSpecLoc());
3694 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00003695 SkipUntil(tok::r_brace, StopAtSemi);
John McCall13489672012-05-07 06:16:58 +00003696 TUK = Sema::TUK_Friend;
3697 } else {
3698 TUK = Sema::TUK_Definition;
3699 }
Richard Smithc9f35172012-06-25 21:37:02 +00003700 } else if (DSC != DSC_type_specifier &&
3701 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003702 (Tok.isAtStartOfLine() &&
3703 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003704 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3705 if (Tok.isNot(tok::semi)) {
3706 // A semicolon was missing after this declaration. Diagnose and recover.
3707 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3708 "enum");
3709 PP.EnterToken(Tok);
3710 Tok.setKind(tok::semi);
3711 }
John McCall13489672012-05-07 06:16:58 +00003712 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003713 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003714 }
3715
3716 // If this is an elaborated type specifier, and we delayed
3717 // diagnostics before, just merge them into the current pool.
3718 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3719 diagsFromTag.redelay();
3720 }
Richard Smith1af83c42012-03-23 03:33:32 +00003721
3722 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003723 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003724 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003725 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003726 // Skip the rest of this declarator, up until the comma or semicolon.
3727 Diag(Tok, diag::err_enum_template);
Alexey Bataev8fe24752013-11-18 08:17:37 +00003728 SkipUntil(tok::comma, StopAtSemi);
Richard Smith1af83c42012-03-23 03:33:32 +00003729 return;
3730 }
3731
3732 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3733 // Enumerations can't be explicitly instantiated.
3734 DS.SetTypeSpecError();
3735 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3736 return;
3737 }
3738
3739 assert(TemplateInfo.TemplateParams && "no template parameters");
3740 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3741 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003742 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003743
Sean Hunt2edf0a22012-06-23 05:07:58 +00003744 if (TUK == Sema::TUK_Reference)
3745 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003746
Douglas Gregorb9075602011-02-22 02:55:24 +00003747 if (!Name && TUK != Sema::TUK_Definition) {
3748 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003749
Douglas Gregorb9075602011-02-22 02:55:24 +00003750 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataev8fe24752013-11-18 08:17:37 +00003751 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregorb9075602011-02-22 02:55:24 +00003752 return;
3753 }
Richard Smith1af83c42012-03-23 03:33:32 +00003754
Douglas Gregor402abb52009-05-28 23:31:59 +00003755 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003756 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003757 const char *PrevSpec = 0;
3758 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003759 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003760 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003761 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003762 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003763 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003764
Douglas Gregor48c89f42010-04-24 16:38:41 +00003765 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003766 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003767 // dependent tag.
3768 if (!Name) {
3769 DS.SetTypeSpecError();
3770 Diag(Tok, diag::err_expected_type_name_after_typename);
3771 return;
3772 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003773
Douglas Gregor23c94db2010-07-02 17:43:08 +00003774 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003775 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003776 NameLoc);
3777 if (Type.isInvalid()) {
3778 DS.SetTypeSpecError();
3779 return;
3780 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003781
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003782 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3783 NameLoc.isValid() ? NameLoc : StartLoc,
3784 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003785 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003786
Douglas Gregor48c89f42010-04-24 16:38:41 +00003787 return;
3788 }
Mike Stump1eb44332009-09-09 15:08:12 +00003789
John McCalld226f652010-08-21 09:40:31 +00003790 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003791 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003792 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003793 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003794 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00003795 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor48c89f42010-04-24 16:38:41 +00003796 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003797
Douglas Gregor48c89f42010-04-24 16:38:41 +00003798 DS.SetTypeSpecError();
3799 return;
3800 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003801
Richard Smithc9f35172012-06-25 21:37:02 +00003802 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003803 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003804
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003805 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3806 NameLoc.isValid() ? NameLoc : StartLoc,
3807 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003808 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003809}
3810
3811/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3812/// enumerator-list:
3813/// enumerator
3814/// enumerator-list ',' enumerator
3815/// enumerator:
3816/// enumeration-constant
3817/// enumeration-constant '=' constant-expression
3818/// enumeration-constant:
3819/// identifier
3820///
John McCalld226f652010-08-21 09:40:31 +00003821void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003822 // Enter the scope of the enum body and start the definition.
3823 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003824 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003825
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003826 BalancedDelimiterTracker T(*this, tok::l_brace);
3827 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003828
Chris Lattner7946dd32007-08-27 17:24:30 +00003829 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003830 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003831 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003832
Chris Lattner5f9e2722011-07-23 10:55:15 +00003833 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003834
John McCalld226f652010-08-21 09:40:31 +00003835 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003836
Reid Spencer5f016e22007-07-11 17:01:13 +00003837 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003838 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003839 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3840 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003841
John McCall5b629aa2010-10-22 23:36:17 +00003842 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003843 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003844 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003845 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003846 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003847
Reid Spencer5f016e22007-07-11 17:01:13 +00003848 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003849 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003850 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003851
Chris Lattner04d66662007-10-09 17:33:22 +00003852 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003853 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003854 AssignedVal = ParseConstantExpression();
3855 if (AssignedVal.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00003856 SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch);
Reid Spencer5f016e22007-07-11 17:01:13 +00003857 }
Mike Stump1eb44332009-09-09 15:08:12 +00003858
Reid Spencer5f016e22007-07-11 17:01:13 +00003859 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003860 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3861 LastEnumConstDecl,
3862 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003863 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003864 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003865 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003866
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 EnumConstantDecls.push_back(EnumConstDecl);
3868 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003869
Douglas Gregor751f6922010-09-07 14:51:08 +00003870 if (Tok.is(tok::identifier)) {
3871 // We're missing a comma between enumerators.
3872 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003873 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003874 << FixItHint::CreateInsertion(Loc, ", ");
3875 continue;
3876 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003877
Chris Lattner04d66662007-10-09 17:33:22 +00003878 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003879 break;
3880 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003881
Richard Smith7fe62082011-10-15 05:09:34 +00003882 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003883 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003884 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3885 diag::ext_enumerator_list_comma_cxx :
3886 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003887 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003888 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003889 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3890 << FixItHint::CreateRemoval(CommaLoc);
3891 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003892 }
Mike Stump1eb44332009-09-09 15:08:12 +00003893
Reid Spencer5f016e22007-07-11 17:01:13 +00003894 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003895 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003896
Reid Spencer5f016e22007-07-11 17:01:13 +00003897 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003898 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003899 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003900
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003901 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenko9ff2b422013-04-27 20:23:52 +00003902 EnumDecl, EnumConstantDecls,
3903 getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003904 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003905
Douglas Gregor72de6672009-01-08 20:45:30 +00003906 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003907 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3908 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003909
3910 // The next token must be valid after an enum definition. If not, a ';'
3911 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003912 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3913 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003914 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3915 // Push this token back into the preprocessor and change our current token
3916 // to ';' so that the rest of the code recovers as though there were an
3917 // ';' after the definition.
3918 PP.EnterToken(Tok);
3919 Tok.setKind(tok::semi);
3920 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003921}
3922
3923/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003924/// start of a type-qualifier-list.
3925bool Parser::isTypeQualifier() const {
3926 switch (Tok.getKind()) {
3927 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003928
3929 // type-qualifier only in OpenCL
3930 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003931 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003932
Steve Naroff5f8aa692008-02-11 23:15:56 +00003933 // type-qualifier
3934 case tok::kw_const:
3935 case tok::kw_volatile:
3936 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003937 case tok::kw___private:
3938 case tok::kw___local:
3939 case tok::kw___global:
3940 case tok::kw___constant:
3941 case tok::kw___read_only:
3942 case tok::kw___read_write:
3943 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003944 return true;
3945 }
3946}
3947
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003948/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3949/// is definitely a type-specifier. Return false if it isn't part of a type
3950/// specifier or if we're not sure.
3951bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3952 switch (Tok.getKind()) {
3953 default: return false;
3954 // type-specifiers
3955 case tok::kw_short:
3956 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003957 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003958 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003959 case tok::kw_signed:
3960 case tok::kw_unsigned:
3961 case tok::kw__Complex:
3962 case tok::kw__Imaginary:
3963 case tok::kw_void:
3964 case tok::kw_char:
3965 case tok::kw_wchar_t:
3966 case tok::kw_char16_t:
3967 case tok::kw_char32_t:
3968 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003969 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003970 case tok::kw_float:
3971 case tok::kw_double:
3972 case tok::kw_bool:
3973 case tok::kw__Bool:
3974 case tok::kw__Decimal32:
3975 case tok::kw__Decimal64:
3976 case tok::kw__Decimal128:
3977 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003978
Guy Benyeib13621d2012-12-18 14:38:23 +00003979 // OpenCL specific types:
3980 case tok::kw_image1d_t:
3981 case tok::kw_image1d_array_t:
3982 case tok::kw_image1d_buffer_t:
3983 case tok::kw_image2d_t:
3984 case tok::kw_image2d_array_t:
3985 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003986 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003987 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003988
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003989 // struct-or-union-specifier (C99) or class-specifier (C++)
3990 case tok::kw_class:
3991 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003992 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003993 case tok::kw_union:
3994 // enum-specifier
3995 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003996
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003997 // typedef-name
3998 case tok::annot_typename:
3999 return true;
4000 }
4001}
4002
Steve Naroff5f8aa692008-02-11 23:15:56 +00004003/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00004004/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004005bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00004006 switch (Tok.getKind()) {
4007 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004008
Chris Lattner166a8fc2009-01-04 23:41:41 +00004009 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00004010 if (TryAltiVecVectorToken())
4011 return true;
4012 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00004013 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004014 // Annotate typenames and C++ scope specifiers. If we get one, just
4015 // recurse to handle whatever we get.
4016 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004017 return true;
4018 if (Tok.is(tok::identifier))
4019 return false;
4020 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00004021
Chris Lattner166a8fc2009-01-04 23:41:41 +00004022 case tok::coloncolon: // ::foo::bar
4023 if (NextToken().is(tok::kw_new) || // ::new
4024 NextToken().is(tok::kw_delete)) // ::delete
4025 return false;
4026
Chris Lattner166a8fc2009-01-04 23:41:41 +00004027 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004028 return true;
4029 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Reid Spencer5f016e22007-07-11 17:01:13 +00004031 // GNU attributes support.
4032 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004033 // GNU typeof support.
4034 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004035
Reid Spencer5f016e22007-07-11 17:01:13 +00004036 // type-specifiers
4037 case tok::kw_short:
4038 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004039 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004040 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004041 case tok::kw_signed:
4042 case tok::kw_unsigned:
4043 case tok::kw__Complex:
4044 case tok::kw__Imaginary:
4045 case tok::kw_void:
4046 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004047 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004048 case tok::kw_char16_t:
4049 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00004050 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004051 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004052 case tok::kw_float:
4053 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004054 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004055 case tok::kw__Bool:
4056 case tok::kw__Decimal32:
4057 case tok::kw__Decimal64:
4058 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004059 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004060
Guy Benyeib13621d2012-12-18 14:38:23 +00004061 // OpenCL specific types:
4062 case tok::kw_image1d_t:
4063 case tok::kw_image1d_array_t:
4064 case tok::kw_image1d_buffer_t:
4065 case tok::kw_image2d_t:
4066 case tok::kw_image2d_array_t:
4067 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00004068 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00004069 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00004070
Chris Lattner99dc9142008-04-13 18:59:07 +00004071 // struct-or-union-specifier (C99) or class-specifier (C++)
4072 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004073 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00004074 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004075 case tok::kw_union:
4076 // enum-specifier
4077 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004078
Reid Spencer5f016e22007-07-11 17:01:13 +00004079 // type-qualifier
4080 case tok::kw_const:
4081 case tok::kw_volatile:
4082 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004083
John McCallb8a8de32012-11-14 00:49:39 +00004084 // Debugger support.
4085 case tok::kw___unknown_anytype:
4086
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004087 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00004088 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00004089 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004090
Chris Lattner7c186be2008-10-20 00:25:30 +00004091 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4092 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004093 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004094
Steve Naroff239f0732008-12-25 14:16:32 +00004095 case tok::kw___cdecl:
4096 case tok::kw___stdcall:
4097 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004098 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004099 case tok::kw___w64:
4100 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004101 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004102 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004103 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004104
4105 case tok::kw___private:
4106 case tok::kw___local:
4107 case tok::kw___global:
4108 case tok::kw___constant:
4109 case tok::kw___read_only:
4110 case tok::kw___read_write:
4111 case tok::kw___write_only:
4112
Eli Friedman290eeb02009-06-08 23:27:34 +00004113 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004114
4115 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004116 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00004117
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004118 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004119 case tok::kw__Atomic:
4120 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004121 }
4122}
4123
4124/// isDeclarationSpecifier() - Return true if the current token is part of a
4125/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00004126///
4127/// \param DisambiguatingWithExpression True to indicate that the purpose of
4128/// this check is to disambiguate between an expression and a declaration.
4129bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004130 switch (Tok.getKind()) {
4131 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004132
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004133 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004134 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004135
Chris Lattner166a8fc2009-01-04 23:41:41 +00004136 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00004137 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00004138 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00004139 return false;
John Thompson82287d12010-02-05 00:12:22 +00004140 if (TryAltiVecVectorToken())
4141 return true;
4142 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004143 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00004144 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004145 // Annotate typenames and C++ scope specifiers. If we get one, just
4146 // recurse to handle whatever we get.
4147 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004148 return true;
4149 if (Tok.is(tok::identifier))
4150 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004151
Douglas Gregor9497a732010-09-16 01:51:54 +00004152 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00004153 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00004154 // expression is permitted, then this is probably a class message send
4155 // missing the initial '['. In this case, we won't consider this to be
4156 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00004157 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00004158 isStartOfObjCClassMessageMissingOpenBracket())
4159 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004160
John McCall9ba61662010-02-26 08:45:28 +00004161 return isDeclarationSpecifier();
4162
Chris Lattner166a8fc2009-01-04 23:41:41 +00004163 case tok::coloncolon: // ::foo::bar
4164 if (NextToken().is(tok::kw_new) || // ::new
4165 NextToken().is(tok::kw_delete)) // ::delete
4166 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004167
Chris Lattner166a8fc2009-01-04 23:41:41 +00004168 // Annotate typenames and C++ scope specifiers. If we get one, just
4169 // recurse to handle whatever we get.
4170 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004171 return true;
4172 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004173
Reid Spencer5f016e22007-07-11 17:01:13 +00004174 // storage-class-specifier
4175 case tok::kw_typedef:
4176 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00004177 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00004178 case tok::kw_static:
4179 case tok::kw_auto:
4180 case tok::kw_register:
4181 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00004182 case tok::kw_thread_local:
4183 case tok::kw__Thread_local:
Mike Stump1eb44332009-09-09 15:08:12 +00004184
Douglas Gregor8d267c52011-09-09 02:06:17 +00004185 // Modules
4186 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00004187
John McCallb8a8de32012-11-14 00:49:39 +00004188 // Debugger support
4189 case tok::kw___unknown_anytype:
4190
Reid Spencer5f016e22007-07-11 17:01:13 +00004191 // type-specifiers
4192 case tok::kw_short:
4193 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004194 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004195 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004196 case tok::kw_signed:
4197 case tok::kw_unsigned:
4198 case tok::kw__Complex:
4199 case tok::kw__Imaginary:
4200 case tok::kw_void:
4201 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004202 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004203 case tok::kw_char16_t:
4204 case tok::kw_char32_t:
4205
Reid Spencer5f016e22007-07-11 17:01:13 +00004206 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004207 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004208 case tok::kw_float:
4209 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004210 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004211 case tok::kw__Bool:
4212 case tok::kw__Decimal32:
4213 case tok::kw__Decimal64:
4214 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004215 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004216
Guy Benyeib13621d2012-12-18 14:38:23 +00004217 // OpenCL specific types:
4218 case tok::kw_image1d_t:
4219 case tok::kw_image1d_array_t:
4220 case tok::kw_image1d_buffer_t:
4221 case tok::kw_image2d_t:
4222 case tok::kw_image2d_array_t:
4223 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00004224 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00004225 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00004226
Chris Lattner99dc9142008-04-13 18:59:07 +00004227 // struct-or-union-specifier (C99) or class-specifier (C++)
4228 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004229 case tok::kw_struct:
4230 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00004231 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004232 // enum-specifier
4233 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004234
Reid Spencer5f016e22007-07-11 17:01:13 +00004235 // type-qualifier
4236 case tok::kw_const:
4237 case tok::kw_volatile:
4238 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004239
Reid Spencer5f016e22007-07-11 17:01:13 +00004240 // function-specifier
4241 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00004242 case tok::kw_virtual:
4243 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00004244 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004245
Richard Smith4cd81c52013-01-29 09:02:09 +00004246 // alignment-specifier
4247 case tok::kw__Alignas:
4248
Richard Smith53aec2a2012-10-25 00:00:53 +00004249 // friend keyword.
4250 case tok::kw_friend:
4251
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00004252 // static_assert-declaration
4253 case tok::kw__Static_assert:
4254
Chris Lattner1ef08762007-08-09 17:01:07 +00004255 // GNU typeof support.
4256 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004257
Chris Lattner1ef08762007-08-09 17:01:07 +00004258 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004259 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00004260
Richard Smith53aec2a2012-10-25 00:00:53 +00004261 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004262 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00004263 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00004264
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004265 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004266 case tok::kw__Atomic:
4267 return true;
4268
Chris Lattnerf3948c42008-07-26 03:38:44 +00004269 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4270 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004271 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004272
Douglas Gregord9d75e52011-04-27 05:41:15 +00004273 // typedef-name
4274 case tok::annot_typename:
4275 return !DisambiguatingWithExpression ||
4276 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00004277
Steve Naroff47f52092009-01-06 19:34:12 +00004278 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004279 case tok::kw___cdecl:
4280 case tok::kw___stdcall:
4281 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004282 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004283 case tok::kw___w64:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004284 case tok::kw___sptr:
4285 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004286 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004287 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004288 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004289 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004290 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004291
4292 case tok::kw___private:
4293 case tok::kw___local:
4294 case tok::kw___global:
4295 case tok::kw___constant:
4296 case tok::kw___read_only:
4297 case tok::kw___read_write:
4298 case tok::kw___write_only:
4299
Eli Friedman290eeb02009-06-08 23:27:34 +00004300 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004301 }
4302}
4303
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004304bool Parser::isConstructorDeclarator() {
4305 TentativeParsingAction TPA(*this);
4306
4307 // Parse the C++ scope specifier.
4308 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004309 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004310 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004311 TPA.Revert();
4312 return false;
4313 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004314
4315 // Parse the constructor name.
4316 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4317 // We already know that we have a constructor name; just consume
4318 // the token.
4319 ConsumeToken();
4320 } else {
4321 TPA.Revert();
4322 return false;
4323 }
4324
Richard Smith22592862012-03-27 23:05:05 +00004325 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004326 if (Tok.isNot(tok::l_paren)) {
4327 TPA.Revert();
4328 return false;
4329 }
4330 ConsumeParen();
4331
Richard Smith22592862012-03-27 23:05:05 +00004332 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4333 // that we have a constructor.
4334 if (Tok.is(tok::r_paren) ||
4335 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004336 TPA.Revert();
4337 return true;
4338 }
4339
Richard Smith9ec28912013-09-06 00:12:20 +00004340 // A C++11 attribute here signals that we have a constructor, and is an
4341 // attribute on the first constructor parameter.
4342 if (getLangOpts().CPlusPlus11 &&
4343 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4344 /*OuterMightBeMessageSend*/ true)) {
4345 TPA.Revert();
4346 return true;
4347 }
4348
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004349 // If we need to, enter the specified scope.
4350 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004351 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004352 DeclScopeObj.EnterDeclaratorScope();
4353
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004354 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004355 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004356 MaybeParseMicrosoftAttributes(Attrs);
4357
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004358 // Check whether the next token(s) are part of a declaration
4359 // specifier, in which case we have the start of a parameter and,
4360 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004361 bool IsConstructor = false;
4362 if (isDeclarationSpecifier())
4363 IsConstructor = true;
4364 else if (Tok.is(tok::identifier) ||
4365 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4366 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4367 // This might be a parenthesized member name, but is more likely to
4368 // be a constructor declaration with an invalid argument type. Keep
4369 // looking.
4370 if (Tok.is(tok::annot_cxxscope))
4371 ConsumeToken();
4372 ConsumeToken();
4373
4374 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004375 // which must have one of the following syntactic forms (see the
4376 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004377 switch (Tok.getKind()) {
4378 case tok::l_paren:
4379 // C(X ( int));
4380 case tok::l_square:
4381 // C(X [ 5]);
4382 // C(X [ [attribute]]);
4383 case tok::coloncolon:
4384 // C(X :: Y);
4385 // C(X :: *p);
4386 case tok::r_paren:
4387 // C(X )
4388 // Assume this isn't a constructor, rather than assuming it's a
4389 // constructor with an unnamed parameter of an ill-formed type.
4390 break;
4391
4392 default:
4393 IsConstructor = true;
4394 break;
4395 }
4396 }
4397
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004398 TPA.Revert();
4399 return IsConstructor;
4400}
Reid Spencer5f016e22007-07-11 17:01:13 +00004401
4402/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004403/// type-qualifier-list: [C99 6.7.5]
4404/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004405/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004406/// [ only if VendorAttributesAllowed=true ]
4407/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004408/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004409/// [ only if VendorAttributesAllowed=true ]
4410/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004411/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004412/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004413///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004414void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4415 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004416 bool CXX11AttributesAllowed,
4417 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004418 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004419 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004420 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004421 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004422 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004423 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004424
4425 SourceLocation EndLoc;
4426
Reid Spencer5f016e22007-07-11 17:01:13 +00004427 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004428 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004429 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004430 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004431 SourceLocation Loc = Tok.getLocation();
4432
4433 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004434 case tok::code_completion:
4435 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004436 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004437
Reid Spencer5f016e22007-07-11 17:01:13 +00004438 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004439 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004440 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004441 break;
4442 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004443 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004444 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004445 break;
4446 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004447 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004448 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004449 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004450 case tok::kw__Atomic:
4451 if (!AtomicAllowed)
4452 goto DoneWithTypeQuals;
4453 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4454 getLangOpts());
4455 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004456
4457 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004458 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004459 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004460 goto DoneWithTypeQuals;
4461 case tok::kw___private:
4462 case tok::kw___global:
4463 case tok::kw___local:
4464 case tok::kw___constant:
4465 case tok::kw___read_only:
4466 case tok::kw___write_only:
4467 case tok::kw___read_write:
4468 ParseOpenCLQualifiers(DS);
4469 break;
4470
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004471 case tok::kw___sptr:
4472 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004473 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004474 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004475 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004476 case tok::kw___cdecl:
4477 case tok::kw___stdcall:
4478 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004479 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004480 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004481 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004482 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004483 continue;
4484 }
4485 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004486 case tok::kw___pascal:
4487 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004488 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004489 continue;
4490 }
4491 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004492 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004493 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004494 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004495 continue; // do *not* consume the next token!
4496 }
4497 // otherwise, FALL THROUGH!
4498 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004499 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004500 // If this is not a type-qualifier token, we're done reading type
4501 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004502 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004503 if (EndLoc.isValid())
4504 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004505 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004506 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004507
Reid Spencer5f016e22007-07-11 17:01:13 +00004508 // If the specifier combination wasn't legal, issue a diagnostic.
4509 if (isInvalid) {
4510 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004511 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004512 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004513 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004514 }
4515}
4516
4517
4518/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4519///
4520void Parser::ParseDeclarator(Declarator &D) {
4521 /// This implements the 'declarator' production in the C grammar, then checks
4522 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004523 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004524}
4525
Richard Smith9988f282012-03-29 01:16:42 +00004526static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4527 if (Kind == tok::star || Kind == tok::caret)
4528 return true;
4529
4530 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4531 if (!Lang.CPlusPlus)
4532 return false;
4533
4534 return Kind == tok::amp || Kind == tok::ampamp;
4535}
4536
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004537/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4538/// is parsed by the function passed to it. Pass null, and the direct-declarator
4539/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004540/// ptr-operator production.
4541///
Richard Smith0706df42011-10-19 21:33:05 +00004542/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004543/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4544/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004545///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004546/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4547/// [C] pointer[opt] direct-declarator
4548/// [C++] direct-declarator
4549/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004550///
4551/// pointer: [C99 6.7.5]
4552/// '*' type-qualifier-list[opt]
4553/// '*' type-qualifier-list[opt] pointer
4554///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004555/// ptr-operator:
4556/// '*' cv-qualifier-seq[opt]
4557/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004558/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004559/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004560/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004561/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004562void Parser::ParseDeclaratorInternal(Declarator &D,
4563 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004564 if (Diags.hasAllExtensionsSilenced())
4565 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004566
Sebastian Redlf30208a2009-01-24 21:16:55 +00004567 // C++ member pointers start with a '::' or a nested-name.
4568 // Member pointers get special handling, since there's no place for the
4569 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004570 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004571 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4572 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004573 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4574 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004575 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004576 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004577
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004578 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004579 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004580 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004581 if (D.mayHaveIdentifier())
4582 D.getCXXScopeSpec() = SS;
4583 else
4584 AnnotateScopeToken(SS, true);
4585
Sebastian Redlf30208a2009-01-24 21:16:55 +00004586 if (DirectDeclParser)
4587 (this->*DirectDeclParser)(D);
4588 return;
4589 }
4590
4591 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004592 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004593 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004594 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004595 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004596
4597 // Recurse to parse whatever is left.
4598 ParseDeclaratorInternal(D, DirectDeclParser);
4599
4600 // Sema will have to catch (syntactically invalid) pointers into global
4601 // scope. It has to catch pointers into namespace scope anyway.
4602 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004603 Loc),
4604 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004605 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004606 return;
4607 }
4608 }
4609
4610 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004611 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004612 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004613 if (DirectDeclParser)
4614 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004615 return;
4616 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004617
Sebastian Redl05532f22009-03-15 22:02:01 +00004618 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4619 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004620 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004621 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004622
Chris Lattner9af55002009-03-27 04:18:06 +00004623 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004624 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004625 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004626
Richard Smith6ee326a2012-04-10 01:32:12 +00004627 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004628 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004629 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004630
Reid Spencer5f016e22007-07-11 17:01:13 +00004631 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004632 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004633 if (Kind == tok::star)
4634 // Remember that we parsed a pointer type, and remember the type-quals.
4635 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004636 DS.getConstSpecLoc(),
4637 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004638 DS.getRestrictSpecLoc()),
4639 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004640 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004641 else
4642 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004643 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004644 Loc),
4645 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004646 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004647 } else {
4648 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004649 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004650
Sebastian Redl743de1f2009-03-23 00:00:23 +00004651 // Complain about rvalue references in C++03, but then go on and build
4652 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004653 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004654 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004655 diag::warn_cxx98_compat_rvalue_reference :
4656 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004657
Richard Smith6ee326a2012-04-10 01:32:12 +00004658 // GNU-style and C++11 attributes are allowed here, as is restrict.
4659 ParseTypeQualifierListOpt(DS);
4660 D.ExtendWithDeclSpec(DS);
4661
Reid Spencer5f016e22007-07-11 17:01:13 +00004662 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4663 // cv-qualifiers are introduced through the use of a typedef or of a
4664 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004665 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4666 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4667 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004668 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004669 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4670 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004671 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004672 // 'restrict' is permitted as an extension.
4673 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4674 Diag(DS.getAtomicSpecLoc(),
4675 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004676 }
4677
4678 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004679 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004680
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004681 if (D.getNumTypeObjects() > 0) {
4682 // C++ [dcl.ref]p4: There shall be no references to references.
4683 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4684 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004685 if (const IdentifierInfo *II = D.getIdentifier())
4686 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4687 << II;
4688 else
4689 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4690 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004691
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004692 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004693 // can go ahead and build the (technically ill-formed)
4694 // declarator: reference collapsing will take care of it.
4695 }
4696 }
4697
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004698 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004699 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004700 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004701 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004702 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004703 }
4704}
4705
Richard Smith9988f282012-03-29 01:16:42 +00004706static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4707 SourceLocation EllipsisLoc) {
4708 if (EllipsisLoc.isValid()) {
4709 FixItHint Insertion;
4710 if (!D.getEllipsisLoc().isValid()) {
4711 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4712 D.setEllipsisLoc(EllipsisLoc);
4713 }
4714 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4715 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4716 }
4717}
4718
Reid Spencer5f016e22007-07-11 17:01:13 +00004719/// ParseDirectDeclarator
4720/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004721/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004722/// '(' declarator ')'
4723/// [GNU] '(' attributes declarator ')'
4724/// [C90] direct-declarator '[' constant-expression[opt] ']'
4725/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4726/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4727/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4728/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004729/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4730/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004731/// direct-declarator '(' parameter-type-list ')'
4732/// direct-declarator '(' identifier-list[opt] ')'
4733/// [GNU] direct-declarator '(' parameter-forward-declarations
4734/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004735/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4736/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004737/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4738/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4739/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004740/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004741/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004742///
4743/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004744/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004745/// '::'[opt] nested-name-specifier[opt] type-name
4746///
4747/// id-expression: [C++ 5.1]
4748/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004749/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004750///
4751/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004752/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004753/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004754/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004755/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004756/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004757///
Richard Smith5d8388c2012-03-27 01:42:32 +00004758/// Note, any additional constructs added here may need corresponding changes
4759/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004760void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004761 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004762
David Blaikie4e4d0842012-03-11 07:00:24 +00004763 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004764 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004765 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004766 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4767 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004768 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004769 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004770 }
4771
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004772 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004773 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004774 // Change the declaration context for name lookup, until this function
4775 // is exited (and the declarator has been parsed).
4776 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004777 }
4778
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004779 // C++0x [dcl.fct]p14:
4780 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004781 // of a parameter-declaration-clause without a preceding comma. In
4782 // this case, the ellipsis is parsed as part of the
4783 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004784 // parameter pack that has not been expanded; otherwise, it is parsed
4785 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004786 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004787 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Valifad9e132013-09-26 19:54:12 +00004788 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004789 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004790 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004791 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004792 !Actions.containsUnexpandedParameterPacks(D))) {
4793 SourceLocation EllipsisLoc = ConsumeToken();
4794 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4795 // The ellipsis was put in the wrong place. Recover, and explain to
4796 // the user what they should have done.
4797 ParseDeclarator(D);
4798 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4799 return;
4800 } else
4801 D.setEllipsisLoc(EllipsisLoc);
4802
4803 // The ellipsis can't be followed by a parenthesized declarator. We
4804 // check for that in ParseParenDeclarator, after we have disambiguated
4805 // the l_paren token.
4806 }
4807
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004808 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4809 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4810 // We found something that indicates the start of an unqualified-id.
4811 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004812 bool AllowConstructorName;
4813 if (D.getDeclSpec().hasTypeSpecifier())
4814 AllowConstructorName = false;
4815 else if (D.getCXXScopeSpec().isSet())
4816 AllowConstructorName =
4817 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004818 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004819 else
4820 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4821
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004822 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004823 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4824 /*EnteringContext=*/true,
4825 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004826 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004827 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004828 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004829 D.getName()) ||
4830 // Once we're past the identifier, if the scope was bad, mark the
4831 // whole declarator bad.
4832 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004833 D.SetIdentifier(0, Tok.getLocation());
4834 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004835 } else {
4836 // Parsed the unqualified-id; update range information and move along.
4837 if (D.getSourceRange().getBegin().isInvalid())
4838 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4839 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004840 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004841 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004842 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004843 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004844 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004845 "There's a C++-specific check for tok::identifier above");
4846 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4847 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4848 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004849 goto PastIdentifier;
Richard Smitha38253c2013-07-11 05:10:21 +00004850 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smith8d1ab8a2013-10-13 22:12:28 +00004851 // A virt-specifier isn't treated as an identifier if it appears after a
4852 // trailing-return-type.
4853 if (D.getContext() != Declarator::TrailingReturnContext ||
4854 !isCXX11VirtSpecifier(Tok)) {
4855 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4856 << FixItHint::CreateRemoval(Tok.getLocation());
4857 D.SetIdentifier(0, Tok.getLocation());
4858 ConsumeToken();
4859 goto PastIdentifier;
4860 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004861 }
Richard Smith9988f282012-03-29 01:16:42 +00004862
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004863 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004864 // direct-declarator: '(' declarator ')'
4865 // direct-declarator: '(' attributes declarator ')'
4866 // Example: 'char (*X)' or 'int (*XX)(void)'
4867 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004868
4869 // If the declarator was parenthesized, we entered the declarator
4870 // scope when parsing the parenthesized declarator, then exited
4871 // the scope already. Re-enter the scope, if we need to.
4872 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004873 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004874 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004875 if (!D.isInvalidType() &&
4876 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004877 // Change the declaration context for name lookup, until this function
4878 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004879 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004880 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004881 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004882 // This could be something simple like "int" (in which case the declarator
4883 // portion is empty), if an abstract-declarator is allowed.
4884 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004885
4886 // The grammar for abstract-pack-declarator does not allow grouping parens.
4887 // FIXME: Revisit this once core issue 1488 is resolved.
4888 if (D.hasEllipsis() && D.hasGroupingParens())
4889 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4890 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004891 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004892 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004893 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004894 if (D.getContext() == Declarator::MemberContext)
4895 Diag(Tok, diag::err_expected_member_name_or_semi)
4896 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004897 else if (getLangOpts().CPlusPlus) {
4898 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4899 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieuefb288c2013-09-05 02:31:33 +00004900 else {
4901 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4902 if (Tok.isAtStartOfLine() && Loc.isValid())
4903 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4904 << getLangOpts().CPlusPlus;
4905 else
4906 Diag(Tok, diag::err_expected_unqualified_id)
4907 << getLangOpts().CPlusPlus;
4908 }
Richard Trieudb55c04c2013-01-26 02:31:38 +00004909 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004910 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004911 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004912 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004913 }
Mike Stump1eb44332009-09-09 15:08:12 +00004914
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004915 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004916 assert(D.isPastIdentifier() &&
4917 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004918
Richard Smith6ee326a2012-04-10 01:32:12 +00004919 // Don't parse attributes unless we have parsed an unparenthesized name.
4920 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004921 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004922
Reid Spencer5f016e22007-07-11 17:01:13 +00004923 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004924 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004925 // Enter function-declaration scope, limiting any declarators to the
4926 // function prototype scope, including parameter declarators.
4927 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004928 Scope::FunctionPrototypeScope|Scope::DeclScope|
4929 (D.isFunctionDeclaratorAFunctionDeclaration()
4930 ? Scope::FunctionDeclarationScope : 0));
4931
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004932 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4933 // In such a case, check if we actually have a function declarator; if it
4934 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004935 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004936 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4937 // The name of the declarator, if any, is tentatively declared within
4938 // a possible direct initializer.
4939 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4940 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4941 TentativelyDeclaredIdentifiers.pop_back();
4942 if (!IsFunctionDecl)
4943 break;
4944 }
John McCall0b7e6782011-03-24 11:26:52 +00004945 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004946 BalancedDelimiterTracker T(*this, tok::l_paren);
4947 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004948 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004949 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004950 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004951 ParseBracketDeclarator(D);
4952 } else {
4953 break;
4954 }
4955 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004956}
Reid Spencer5f016e22007-07-11 17:01:13 +00004957
Chris Lattneref4715c2008-04-06 05:45:57 +00004958/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4959/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004960/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004961/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4962///
4963/// direct-declarator:
4964/// '(' declarator ')'
4965/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004966/// direct-declarator '(' parameter-type-list ')'
4967/// direct-declarator '(' identifier-list[opt] ')'
4968/// [GNU] direct-declarator '(' parameter-forward-declarations
4969/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004970///
4971void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004972 BalancedDelimiterTracker T(*this, tok::l_paren);
4973 T.consumeOpen();
4974
Chris Lattneref4715c2008-04-06 05:45:57 +00004975 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004976
Chris Lattner7399ee02008-10-20 02:05:46 +00004977 // Eat any attributes before we look at whether this is a grouping or function
4978 // declarator paren. If this is a grouping paren, the attribute applies to
4979 // the type being built up, for example:
4980 // int (__attribute__(()) *x)(long y)
4981 // If this ends up not being a grouping paren, the attribute applies to the
4982 // first argument, for example:
4983 // int (__attribute__(()) int x)
4984 // In either case, we need to eat any attributes to be able to determine what
4985 // sort of paren this is.
4986 //
John McCall0b7e6782011-03-24 11:26:52 +00004987 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004988 bool RequiresArg = false;
4989 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004990 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004991
Chris Lattner7399ee02008-10-20 02:05:46 +00004992 // We require that the argument list (if this is a non-grouping paren) be
4993 // present even if the attribute list was empty.
4994 RequiresArg = true;
4995 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004996
Steve Naroff239f0732008-12-25 14:16:32 +00004997 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004998 ParseMicrosoftTypeAttributes(attrs);
4999
Dawn Perchik52fc3142010-09-03 01:29:35 +00005000 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00005001 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00005002 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005003
Chris Lattneref4715c2008-04-06 05:45:57 +00005004 // If we haven't past the identifier yet (or where the identifier would be
5005 // stored, if this is an abstract declarator), then this is probably just
5006 // grouping parens. However, if this could be an abstract-declarator, then
5007 // this could also be the start of function arguments (consider 'void()').
5008 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00005009
Chris Lattneref4715c2008-04-06 05:45:57 +00005010 if (!D.mayOmitIdentifier()) {
5011 // If this can't be an abstract-declarator, this *must* be a grouping
5012 // paren, because we haven't seen the identifier yet.
5013 isGrouping = true;
5014 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00005015 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5016 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00005017 isDeclarationSpecifier() || // 'int(int)' is a function.
5018 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00005019 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5020 // considered to be a type, not a K&R identifier-list.
5021 isGrouping = false;
5022 } else {
5023 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5024 isGrouping = true;
5025 }
Mike Stump1eb44332009-09-09 15:08:12 +00005026
Chris Lattneref4715c2008-04-06 05:45:57 +00005027 // If this is a grouping paren, handle:
5028 // direct-declarator: '(' declarator ')'
5029 // direct-declarator: '(' attributes declarator ')'
5030 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00005031 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5032 D.setEllipsisLoc(SourceLocation());
5033
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00005034 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00005035 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00005036 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00005037 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005038 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00005039 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005040 T.getCloseLocation()),
5041 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00005042
5043 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00005044
5045 // An ellipsis cannot be placed outside parentheses.
5046 if (EllipsisLoc.isValid())
5047 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5048
Chris Lattneref4715c2008-04-06 05:45:57 +00005049 return;
5050 }
Mike Stump1eb44332009-09-09 15:08:12 +00005051
Chris Lattneref4715c2008-04-06 05:45:57 +00005052 // Okay, if this wasn't a grouping paren, it must be the start of a function
5053 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00005054 // identifier (and remember where it would have been), then call into
5055 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00005056 D.SetIdentifier(0, Tok.getLocation());
5057
David Blaikie42d6d0c2011-12-04 05:04:18 +00005058 // Enter function-declaration scope, limiting any declarators to the
5059 // function prototype scope, including parameter declarators.
5060 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00005061 Scope::FunctionPrototypeScope | Scope::DeclScope |
5062 (D.isFunctionDeclaratorAFunctionDeclaration()
5063 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00005064 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00005065 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00005066}
5067
5068/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5069/// declarator D up to a paren, which indicates that we are parsing function
5070/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00005071///
Richard Smith6ee326a2012-04-10 01:32:12 +00005072/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5073/// immediately after the open paren - they should be considered to be the
5074/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00005075///
Richard Smith6ee326a2012-04-10 01:32:12 +00005076/// If RequiresArg is true, then the first argument of the function is required
5077/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005078///
Richard Smith6ee326a2012-04-10 01:32:12 +00005079/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5080/// (C++11) ref-qualifier[opt], exception-specification[opt],
5081/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5082///
5083/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005084/// dynamic-exception-specification
5085/// noexcept-specification
5086///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005087void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00005088 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005089 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00005090 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005091 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00005092 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00005093 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005094 // lparen is already consumed!
5095 assert(D.isPastIdentifier() && "Should not call before identifier!");
5096
5097 // This should be true when the function has typed arguments.
5098 // Otherwise, it is treated as a K&R-style function.
5099 bool HasProto = false;
5100 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005101 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005102 // Remember where we see an ellipsis, if any.
5103 SourceLocation EllipsisLoc;
5104
5105 DeclSpec DS(AttrFactory);
5106 bool RefQualifierIsLValueRef = true;
5107 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00005108 SourceLocation ConstQualifierLoc;
5109 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005110 ExceptionSpecificationType ESpecType = EST_None;
5111 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005112 SmallVector<ParsedType, 2> DynamicExceptions;
5113 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005114 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00005115 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00005116 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00005117
James Molloy16f1f712012-02-29 10:24:19 +00005118 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005119 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5120 EndLoc is the end location for the function declarator.
5121 They differ for trailing return types. */
5122 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005123 SourceLocation LParenLoc, RParenLoc;
5124 LParenLoc = Tracker.getOpenLocation();
5125 StartLoc = LParenLoc;
5126
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005127 if (isFunctionDeclaratorIdentifierList()) {
5128 if (RequiresArg)
5129 Diag(Tok, diag::err_argument_required_after_attribute);
5130
5131 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5132
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005133 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005134 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005135 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005136 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005137 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005138 if (Tok.isNot(tok::r_paren))
Faisal Valifad9e132013-09-26 19:54:12 +00005139 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5140 EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005141 else if (RequiresArg)
5142 Diag(Tok, diag::err_argument_required_after_attribute);
5143
David Blaikie4e4d0842012-03-11 07:00:24 +00005144 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005145
5146 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005147 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005148 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005149 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005150 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005151
David Blaikie4e4d0842012-03-11 07:00:24 +00005152 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005153 // FIXME: Accept these components in any order, and produce fixits to
5154 // correct the order if the user gets it wrong. Ideally we should deal
5155 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005156
5157 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005158 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5159 /*CXX11AttributesAllowed*/ false,
5160 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00005161 if (!DS.getSourceRange().getEnd().isInvalid()) {
5162 EndLoc = DS.getSourceRange().getEnd();
5163 ConstQualifierLoc = DS.getConstSpecLoc();
5164 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5165 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005166
5167 // Parse ref-qualifier[opt].
5168 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00005169 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00005170 diag::warn_cxx98_compat_ref_qualifier :
5171 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00005172
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005173 RefQualifierIsLValueRef = Tok.is(tok::amp);
5174 RefQualifierLoc = ConsumeToken();
5175 EndLoc = RefQualifierLoc;
5176 }
5177
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005178 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00005179 // If a declaration declares a member function or member function
5180 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005181 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00005182 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005183 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00005184 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00005185 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00005186 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00005187 (D.getContext() == Declarator::MemberContext
5188 ? !D.getDeclSpec().isFriendSpecified()
5189 : D.getContext() == Declarator::FileContext &&
5190 D.getCXXScopeSpec().isValid() &&
5191 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005192 Sema::CXXThisScopeRAII ThisScope(Actions,
5193 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00005194 DS.getTypeQualifiers() |
Richard Smith84046262013-04-21 01:08:50 +00005195 (D.getDeclSpec().isConstexprSpecified() &&
5196 !getLangOpts().CPlusPlus1y
Richard Smith7b19cb12013-01-14 01:55:13 +00005197 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005198 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00005199
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005200 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00005201 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00005202 DynamicExceptions,
5203 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00005204 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005205 if (ESpecType != EST_None)
5206 EndLoc = ESpecRange.getEnd();
5207
Richard Smith6ee326a2012-04-10 01:32:12 +00005208 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5209 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005210 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00005211
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005212 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005213 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00005214 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00005215 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005216 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5217 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005218 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00005219 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00005220 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005221 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005222 }
5223 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005224 }
5225
5226 // Remember that we parsed a function type, and remember the attributes.
5227 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005228 IsAmbiguous,
5229 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005230 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005231 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005232 DS.getTypeQualifiers(),
5233 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00005234 RefQualifierLoc, ConstQualifierLoc,
5235 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00005236 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005237 ESpecType, ESpecRange.getBegin(),
5238 DynamicExceptions.data(),
5239 DynamicExceptionRanges.data(),
5240 DynamicExceptions.size(),
5241 NoexceptExpr.isUsable() ?
5242 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005243 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005244 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00005245 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00005246
5247 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005248}
5249
5250/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5251/// identifier list form for a K&R-style function: void foo(a,b,c)
5252///
5253/// Note that identifier-lists are only allowed for normal declarators, not for
5254/// abstract-declarators.
5255bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00005256 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005257 && Tok.is(tok::identifier)
5258 && !TryAltiVecVectorToken()
5259 // K&R identifier lists can't have typedefs as identifiers, per C99
5260 // 6.7.5.3p11.
5261 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5262 // Identifier lists follow a really simple grammar: the identifiers can
5263 // be followed *only* by a ", identifier" or ")". However, K&R
5264 // identifier lists are really rare in the brave new modern world, and
5265 // it is very common for someone to typo a type in a non-K&R style
5266 // list. If we are presented with something like: "void foo(intptr x,
5267 // float y)", we don't want to start parsing the function declarator as
5268 // though it is a K&R style declarator just because intptr is an
5269 // invalid type.
5270 //
5271 // To handle this, we check to see if the token after the first
5272 // identifier is a "," or ")". Only then do we parse it as an
5273 // identifier list.
5274 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5275}
5276
5277/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5278/// we found a K&R-style identifier list instead of a typed parameter list.
5279///
5280/// After returning, ParamInfo will hold the parsed parameters.
5281///
5282/// identifier-list: [C99 6.7.5]
5283/// identifier
5284/// identifier-list ',' identifier
5285///
5286void Parser::ParseFunctionDeclaratorIdentifierList(
5287 Declarator &D,
Craig Topper6b9240e2013-07-05 19:34:19 +00005288 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005289 // If there was no identifier specified for the declarator, either we are in
5290 // an abstract-declarator, or we are in a parameter declarator which was found
5291 // to be abstract. In abstract-declarators, identifier lists are not valid:
5292 // diagnose this.
5293 if (!D.getIdentifier())
5294 Diag(Tok, diag::ext_ident_list_in_param);
5295
5296 // Maintain an efficient lookup of params we have seen so far.
5297 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5298
5299 while (1) {
5300 // If this isn't an identifier, report the error and skip until ')'.
5301 if (Tok.isNot(tok::identifier)) {
5302 Diag(Tok, diag::err_expected_ident);
Alexey Bataev8fe24752013-11-18 08:17:37 +00005303 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005304 // Forget we parsed anything.
5305 ParamInfo.clear();
5306 return;
5307 }
5308
5309 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5310
5311 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5312 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5313 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5314
5315 // Verify that the argument identifier has not already been mentioned.
5316 if (!ParamsSoFar.insert(ParmII)) {
5317 Diag(Tok, diag::err_param_redefinition) << ParmII;
5318 } else {
5319 // Remember this identifier in ParamInfo.
5320 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5321 Tok.getLocation(),
5322 0));
5323 }
5324
5325 // Eat the identifier.
5326 ConsumeToken();
5327
5328 // The list continues if we see a comma.
5329 if (Tok.isNot(tok::comma))
5330 break;
5331 ConsumeToken();
5332 }
5333}
5334
5335/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5336/// after the opening parenthesis. This function will not parse a K&R-style
5337/// identifier list.
5338///
Richard Smith6ce48a72012-04-11 04:01:28 +00005339/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5340/// caller parsed those arguments immediately after the open paren - they should
5341/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005342///
5343/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5344/// be the location of the ellipsis, if any was parsed.
5345///
Reid Spencer5f016e22007-07-11 17:01:13 +00005346/// parameter-type-list: [C99 6.7.5]
5347/// parameter-list
5348/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005349/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005350///
5351/// parameter-list: [C99 6.7.5]
5352/// parameter-declaration
5353/// parameter-list ',' parameter-declaration
5354///
5355/// parameter-declaration: [C99 6.7.5]
5356/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005357/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005358/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005359/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005360/// declaration-specifiers abstract-declarator[opt]
5361/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005362/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005363/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005364/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005365///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005366void Parser::ParseParameterDeclarationClause(
5367 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005368 ParsedAttributes &FirstArgAttrs,
Craig Topper6b9240e2013-07-05 19:34:19 +00005369 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005370 SourceLocation &EllipsisLoc) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005371 while (1) {
5372 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005373 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5374 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005375 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005376 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005377 }
Mike Stump1eb44332009-09-09 15:08:12 +00005378
Chris Lattnerf97409f2008-04-06 06:57:35 +00005379 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005380 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005381 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005382
Richard Smith6ce48a72012-04-11 04:01:28 +00005383 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005384 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005385
John McCall7f040a92010-12-24 02:08:15 +00005386 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005387 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005388
5389 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005390
5391 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005392 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005393 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005394 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5395 // too much hassle.
5396 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005397
Chris Lattnere64c5492009-02-27 18:38:20 +00005398 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005399
Faisal Valifad9e132013-09-26 19:54:12 +00005400
5401 // Parse the declarator. This is "PrototypeContext" or
5402 // "LambdaExprParameterContext", because we must accept either
5403 // 'declarator' or 'abstract-declarator' here.
5404 Declarator ParmDeclarator(DS,
5405 D.getContext() == Declarator::LambdaExprContext ?
5406 Declarator::LambdaExprParameterContext :
5407 Declarator::PrototypeContext);
5408 ParseDeclarator(ParmDeclarator);
Chris Lattnerf97409f2008-04-06 06:57:35 +00005409
5410 // Parse GNU attributes, if present.
Faisal Valifad9e132013-09-26 19:54:12 +00005411 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump1eb44332009-09-09 15:08:12 +00005412
Chris Lattnerf97409f2008-04-06 06:57:35 +00005413 // Remember this parsed parameter in ParamInfo.
Faisal Valifad9e132013-09-26 19:54:12 +00005414 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005415
Douglas Gregor72b505b2008-12-16 21:30:33 +00005416 // DefArgToks is used when the parsing of default arguments needs
5417 // to be delayed.
5418 CachedTokens *DefArgToks = 0;
5419
Chris Lattnerf97409f2008-04-06 06:57:35 +00005420 // If no parameter was specified, verify that *something* was specified,
5421 // otherwise we have a missing type and identifier.
Faisal Valifad9e132013-09-26 19:54:12 +00005422 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5423 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005424 // Completely missing, emit error.
5425 Diag(DSStart, diag::err_missing_param);
5426 } else {
5427 // Otherwise, we have something. Add it and let semantic analysis try
5428 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005429
Chris Lattnerf97409f2008-04-06 06:57:35 +00005430 // Inform the actions module about the parameter declarator, so it gets
5431 // added to the current scope.
Faisal Valifad9e132013-09-26 19:54:12 +00005432 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5433 ParmDeclarator);
Chris Lattner04421082008-04-08 04:40:51 +00005434 // Parse the default argument, if any. We parse the default
5435 // arguments in all dialects; the semantic analysis in
5436 // ActOnParamDefaultArgument will reject the default argument in
5437 // C.
5438 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005439 SourceLocation EqualLoc = Tok.getLocation();
5440
Chris Lattner04421082008-04-08 04:40:51 +00005441 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005442 if (D.getContext() == Declarator::MemberContext) {
5443 // If we're inside a class definition, cache the tokens
5444 // corresponding to the default argument. We'll actually parse
5445 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005446 // FIXME: Can we use a smart pointer for Toks?
5447 DefArgToks = new CachedTokens;
5448
Richard Smith9bd3cdc2013-09-12 23:28:08 +00005449 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005450 delete DefArgToks;
5451 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005452 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005453 } else {
5454 // Mark the end of the default argument so that we know when to
5455 // stop when we parse it later on.
5456 Token DefArgEnd;
5457 DefArgEnd.startToken();
5458 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5459 DefArgEnd.setLocation(Tok.getLocation());
5460 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005461 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005462 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005463 }
Chris Lattner04421082008-04-08 04:40:51 +00005464 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005465 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005466 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005467
Chad Rosier8decdee2012-06-26 22:30:43 +00005468 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005469 // used.
5470 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005471 Sema::PotentiallyEvaluatedIfUsed,
5472 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005473
Sebastian Redl84407ba2012-03-14 15:54:00 +00005474 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005475 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005476 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005477 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005478 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005479 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005480 if (DefArgResult.isInvalid()) {
5481 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataev8fe24752013-11-18 08:17:37 +00005482 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor72b505b2008-12-16 21:30:33 +00005483 } else {
5484 // Inform the actions module about the default argument
5485 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005486 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005487 }
Chris Lattner04421082008-04-08 04:40:51 +00005488 }
5489 }
Mike Stump1eb44332009-09-09 15:08:12 +00005490
5491 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Valifad9e132013-09-26 19:54:12 +00005492 ParmDeclarator.getIdentifierLoc(),
5493 Param, DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005494 }
5495
5496 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005497 if (Tok.isNot(tok::comma)) {
5498 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005499 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005500
David Blaikie4e4d0842012-03-11 07:00:24 +00005501 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005502 // We have ellipsis without a preceding ',', which is ill-formed
5503 // in C. Complain and provide the fix.
5504 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005505 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005506 }
5507 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005508
Douglas Gregored5d6512009-09-22 21:41:40 +00005509 break;
5510 }
Mike Stump1eb44332009-09-09 15:08:12 +00005511
Chris Lattnerf97409f2008-04-06 06:57:35 +00005512 // Consume the comma.
5513 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005514 }
Mike Stump1eb44332009-09-09 15:08:12 +00005515
Chris Lattner66d28652008-04-06 06:34:08 +00005516}
Chris Lattneref4715c2008-04-06 05:45:57 +00005517
Reid Spencer5f016e22007-07-11 17:01:13 +00005518/// [C90] direct-declarator '[' constant-expression[opt] ']'
5519/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5520/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5521/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5522/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005523/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5524/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005525void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005526 if (CheckProhibitedCXX11Attribute())
5527 return;
5528
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005529 BalancedDelimiterTracker T(*this, tok::l_square);
5530 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005531
Chris Lattner378c7e42008-12-18 07:27:21 +00005532 // C array syntax has many features, but by-far the most common is [] and [4].
5533 // This code does a fast path to handle some of the most obvious cases.
5534 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005535 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005536 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005537 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005538
Chris Lattner378c7e42008-12-18 07:27:21 +00005539 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005540 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005541 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005542 T.getOpenLocation(),
5543 T.getCloseLocation()),
5544 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005545 return;
5546 } else if (Tok.getKind() == tok::numeric_constant &&
5547 GetLookAheadToken(1).is(tok::r_square)) {
5548 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005549 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005550 ConsumeToken();
5551
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005552 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005553 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005554 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005555
Chris Lattner378c7e42008-12-18 07:27:21 +00005556 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005557 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005558 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005559 T.getOpenLocation(),
5560 T.getCloseLocation()),
5561 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005562 return;
5563 }
Mike Stump1eb44332009-09-09 15:08:12 +00005564
Reid Spencer5f016e22007-07-11 17:01:13 +00005565 // If valid, this location is the position where we read the 'static' keyword.
5566 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005567 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005568 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005569
Reid Spencer5f016e22007-07-11 17:01:13 +00005570 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005571 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005572 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005573 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005574
Reid Spencer5f016e22007-07-11 17:01:13 +00005575 // If we haven't already read 'static', check to see if there is one after the
5576 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005577 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005578 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005579
Reid Spencer5f016e22007-07-11 17:01:13 +00005580 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5581 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005582 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005583
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005584 // Handle the case where we have '[*]' as the array size. However, a leading
5585 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005586 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005587 // infrequent, use of lookahead is not costly here.
5588 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005589 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005590
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005591 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005592 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005593 StaticLoc = SourceLocation(); // Drop the static.
5594 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005595 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005596 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005597 // Note, in C89, this production uses the constant-expr production instead
5598 // of assignment-expr. The only difference is that assignment-expr allows
5599 // things like '=' and '*='. Sema rejects these in C89 mode because they
5600 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005601
Douglas Gregore0762c92009-06-19 23:52:42 +00005602 // Parse the constant-expression or assignment-expression now (depending
5603 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005604 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005605 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005606 } else {
5607 EnterExpressionEvaluationContext Unevaluated(Actions,
5608 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005609 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005610 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005611 }
Mike Stump1eb44332009-09-09 15:08:12 +00005612
Reid Spencer5f016e22007-07-11 17:01:13 +00005613 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005614 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005615 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005616 // If the expression was invalid, skip it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00005617 SkipUntil(tok::r_square, StopAtSemi);
Reid Spencer5f016e22007-07-11 17:01:13 +00005618 return;
5619 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005620
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005621 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005622
John McCall0b7e6782011-03-24 11:26:52 +00005623 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005624 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005625
Chris Lattner378c7e42008-12-18 07:27:21 +00005626 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005627 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005628 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005629 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005630 T.getOpenLocation(),
5631 T.getCloseLocation()),
5632 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005633}
5634
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005635/// [GNU] typeof-specifier:
5636/// typeof ( expressions )
5637/// typeof ( type-name )
5638/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005639///
5640void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005641 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005642 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005643 SourceLocation StartLoc = ConsumeToken();
5644
John McCallcfb708c2010-01-13 20:03:27 +00005645 const bool hasParens = Tok.is(tok::l_paren);
5646
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005647 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5648 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005649
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005650 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005651 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005652 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005653 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5654 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005655 if (hasParens)
5656 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005657
5658 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005659 // FIXME: Not accurate, the range gets one token more than it should.
5660 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005661 else
5662 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005663
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005664 if (isCastExpr) {
5665 if (!CastTy) {
5666 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005667 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005668 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005669
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005670 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005671 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005672 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5673 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005674 DiagID, CastTy))
5675 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005676 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005677 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005678
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005679 // If we get here, the operand to the typeof was an expresion.
5680 if (Operand.isInvalid()) {
5681 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005682 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005683 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005684
Eli Friedman71b8fb52012-01-21 01:01:51 +00005685 // We might need to transform the operand if it is potentially evaluated.
5686 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5687 if (Operand.isInvalid()) {
5688 DS.SetTypeSpecError();
5689 return;
5690 }
5691
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005692 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005693 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005694 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5695 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005696 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005697 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005698}
Chris Lattner1b492422010-02-28 18:33:55 +00005699
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005700/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005701/// _Atomic ( type-name )
5702///
5703void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005704 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5705 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005706
5707 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005708 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005709 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005710 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005711
5712 TypeResult Result = ParseTypeName();
5713 if (Result.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00005714 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedmanb001de72011-10-06 23:00:33 +00005715 return;
5716 }
5717
5718 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005719 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005720
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005721 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005722 return;
5723
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005724 DS.setTypeofParensRange(T.getRange());
5725 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005726
5727 const char *PrevSpec = 0;
5728 unsigned DiagID;
5729 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5730 DiagID, Result.release()))
5731 Diag(StartLoc, DiagID) << PrevSpec;
5732}
5733
Chris Lattner1b492422010-02-28 18:33:55 +00005734
5735/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5736/// from TryAltiVecVectorToken.
5737bool Parser::TryAltiVecVectorTokenOutOfLine() {
5738 Token Next = NextToken();
5739 switch (Next.getKind()) {
5740 default: return false;
5741 case tok::kw_short:
5742 case tok::kw_long:
5743 case tok::kw_signed:
5744 case tok::kw_unsigned:
5745 case tok::kw_void:
5746 case tok::kw_char:
5747 case tok::kw_int:
5748 case tok::kw_float:
5749 case tok::kw_double:
5750 case tok::kw_bool:
5751 case tok::kw___pixel:
5752 Tok.setKind(tok::kw___vector);
5753 return true;
5754 case tok::identifier:
5755 if (Next.getIdentifierInfo() == Ident_pixel) {
5756 Tok.setKind(tok::kw___vector);
5757 return true;
5758 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005759 if (Next.getIdentifierInfo() == Ident_bool) {
5760 Tok.setKind(tok::kw___vector);
5761 return true;
5762 }
Chris Lattner1b492422010-02-28 18:33:55 +00005763 return false;
5764 }
5765}
5766
5767bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5768 const char *&PrevSpec, unsigned &DiagID,
5769 bool &isInvalid) {
5770 if (Tok.getIdentifierInfo() == Ident_vector) {
5771 Token Next = NextToken();
5772 switch (Next.getKind()) {
5773 case tok::kw_short:
5774 case tok::kw_long:
5775 case tok::kw_signed:
5776 case tok::kw_unsigned:
5777 case tok::kw_void:
5778 case tok::kw_char:
5779 case tok::kw_int:
5780 case tok::kw_float:
5781 case tok::kw_double:
5782 case tok::kw_bool:
5783 case tok::kw___pixel:
5784 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5785 return true;
5786 case tok::identifier:
5787 if (Next.getIdentifierInfo() == Ident_pixel) {
5788 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5789 return true;
5790 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005791 if (Next.getIdentifierInfo() == Ident_bool) {
5792 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5793 return true;
5794 }
Chris Lattner1b492422010-02-28 18:33:55 +00005795 break;
5796 default:
5797 break;
5798 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005799 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005800 DS.isTypeAltiVecVector()) {
5801 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5802 return true;
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005803 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5804 DS.isTypeAltiVecVector()) {
5805 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5806 return true;
Chris Lattner1b492422010-02-28 18:33:55 +00005807 }
5808 return false;
5809}