blob: 784f69ee68d938a03a03cdb78cfeda95d95aaf49 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Larisse Voufo725de3e2013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-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
Alexis Hunt96d5c762009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +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
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000102///
Richard Smithf7ca0c02013-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:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105///
Richard Smithf7ca0c02013-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 Smithb12bf692011-10-17 21:20:17 +0000116///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
120 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000121 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000122
Chris Lattner76c72282007-10-09 17:33:22 +0000123 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 ConsumeToken();
125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
126 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000127 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000128 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 }
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000131 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000132 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 }
134 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000135 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
136 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000137 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 if (Tok.is(tok::l_paren)) {
147 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000149 LateParsedAttribute *LA =
150 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
151 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000152
Bill Wendling44426052012-12-20 19:22:21 +0000153 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000154 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000155 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000156 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Caitlin Sadowski9385dd72011-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 Stump11289f42009-09-09 15:08:12 +0000160
Caitlin Sadowski9385dd72011-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 Han23214e52012-10-03 01:56:22 +0000166 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000167 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 }
169 } else {
Aaron Ballman00e99962013-08-31 01:11:41 +0000170 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
171 AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000172 }
173 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000175 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Richard Smith66e71682013-10-24 01:07:54 +0000177 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000178 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000179 if (endLoc)
180 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000181 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000183
Aaron Ballman4768b312013-11-04 12:55:56 +0000184/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
185static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000186 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
187 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-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 Smith66e71682013-10-24 01:07:54 +0000194#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000195 .Default(false);
196}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000197
Aaron Ballman4768b312013-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 Smithfeefaf52013-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 Smithb1f9a282013-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 Han23214e52012-10-03 01:56:22 +0000240/// Parse the arguments to a parameterized GNU attribute or
241/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000242void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
243 SourceLocation AttrNameLoc,
244 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000245 SourceLocation *EndLoc,
246 IdentifierInfo *ScopeName,
247 SourceLocation ScopeLoc,
248 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000249
250 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
251
Richard Smith66e71682013-10-24 01:07:54 +0000252 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000253 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000254
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000255 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-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 Smith66e71682013-10-24 01:07:54 +0000258 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000259 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
260 return;
261 }
Richard Smithb1f9a282013-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 Sadowski9385dd72011-09-08 17:42:22 +0000265 if (IsThreadSafetyAttribute(AttrName->getName())) {
266 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
267 return;
268 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000269 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000270 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000271 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
272 return;
273 }
Aaron Ballman4768b312013-11-04 12:55:56 +0000274 // Some attributes expect solely a type parameter.
275 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000276 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
277 return;
278 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000279
Richard Smith66e71682013-10-24 01:07:54 +0000280 // Ignore the left paren location for now.
281 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000282
Aaron Ballman00e99962013-08-31 01:11:41 +0000283 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000284
Richard Smithb1f9a282013-10-31 01:56:18 +0000285 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000286 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000287 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-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 Smithb1f9a282013-10-31 01:56:18 +0000293 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000294 }
Richard Smithb12bf692011-10-17 21:20:17 +0000295
Richard Smithb1f9a282013-10-31 01:56:18 +0000296 if (IsIdentifierArg)
297 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000298 }
299
Richard Smithb1f9a282013-10-31 01:56:18 +0000300 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000301 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000302 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000303 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000304
Richard Smithb12bf692011-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 Bataevee6507d2013-11-18 08:17:37 +0000309 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000310 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000311 }
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000316 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000317 }
Richard Smithb12bf692011-10-17 21:20:17 +0000318
319 SourceLocation RParen = Tok.getLocation();
Richard Smithb1f9a282013-10-31 01:56:18 +0000320 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000321 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000322 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
323 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000324 }
325}
326
Chad Rosierc1183952012-06-26 22:30:43 +0000327/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000328/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000329void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000330 SourceLocation AttrNameLoc,
331 ParsedAttributes &Attrs)
332{
333 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000334 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000335 AttrName->getNameStart(), tok::r_paren))
336 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000337
Aaron Ballman478faed2012-06-19 22:09:27 +0000338 ExprResult ArgExpr(ParseConstantExpression());
339 if (ArgExpr.isInvalid()) {
340 T.skipToEnd();
341 return;
342 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000343 ArgsUnion ExprList = ArgExpr.take();
344 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
345 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000346
347 T.consumeClose();
348}
349
Chad Rosierc1183952012-06-26 22:30:43 +0000350/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-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 Ballman444eb6e2013-05-04 16:58:37 +0000368 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000369 .Default(false);
370}
371
Chad Rosierc1183952012-06-26 22:30:43 +0000372/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000375void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-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 Rosierc1183952012-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 Ballman478faed2012-06-19 22:09:27 +0000389 // not.
390 if (Tok.getKind() == tok::l_paren)
391 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
392 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000393 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000396 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000397 // must be named get or put.
John McCall5e77d762013-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 Ballman478faed2012-06-19 22:09:27 +0000401 return;
John McCall5e77d762013-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 Ballman00e99962013-08-31 01:11:41 +0000506 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000507 AccessorNames[AK_Get], AccessorNames[AK_Put],
508 AttributeList::AS_Declspec);
509 }
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000516 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-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 Friedman06de2b52009-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 Ballman478faed2012-06-19 22:09:27 +0000534void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000535 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000536
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000537 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000538 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000539 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000540 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000541 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000542
Chad Rosierc1183952012-06-26 22:30:43 +0000543 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000549 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-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 Olesene1c0ae62012-06-19 21:48:43 +0000554 }
Aaron Ballman478faed2012-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 Olesene1c0ae62012-06-19 21:48:43 +0000565 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000566 AttrName = PP.getIdentifierInfo(Str);
567 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000568 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000569 AttrName = Tok.getIdentifierInfo();
570 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000571 }
Chad Rosierc1183952012-06-26 22:30:43 +0000572
Aaron Ballman478faed2012-06-19 22:09:27 +0000573 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-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 Ballman478faed2012-06-19 22:09:27 +0000576 // (for instance, SAL declspecs in older versions of MSVC).
577 //
Chad Rosierc1183952012-06-26 22:30:43 +0000578 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000579 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000580 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
581 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000582 else
583 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000584 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000585 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000586}
587
John McCall53fa7142010-12-24 02:08:15 +0000588void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000589 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000590 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000591 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000592 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-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 Friedman53339e02009-06-08 23:27:34 +0000595 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
596 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000597 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
598 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000599 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000600}
601
John McCall53fa7142010-12-24 02:08:15 +0000602void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-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 Ballman00e99962013-08-31 01:11:41 +0000607 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
608 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000609 }
John McCall53fa7142010-12-24 02:08:15 +0000610}
611
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000612void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
613 // Treat these like attributes
614 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000615 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000616 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000617 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
618 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000619 }
620}
621
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000622void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000623 // FIXME: The mapping from attribute spelling to semantics should be
624 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000625 SourceLocation Loc = Tok.getLocation();
626 switch(Tok.getKind()) {
627 // OpenCL qualifiers:
628 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000629 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000630 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000631 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000632 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000633 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000634
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000635 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000636 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000637 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000638 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000639 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000640
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000641 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000642 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000643 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000644 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000645 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000646
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000647 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000648 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000649 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000650 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000651 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000652
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000653 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000654 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000655 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000656 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000658
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000660 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000661 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000662 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000663 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000664
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000665 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000666 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000667 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000668 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000669 break;
670 default: break;
671 }
672}
673
Douglas Gregor20b2ebd2011-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 Bataevee6507d2013-11-18 08:17:37 +0000685 SkipUntil(tok::comma, tok::r_paren,
686 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-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 Noblesmith2c1dd272012-02-05 02:13:05 +0000694 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-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 Rosea7d03842013-02-08 22:30:41 +0000707 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-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 Bataevee6507d2013-11-18 08:17:37 +0000714 SkipUntil(tok::comma, tok::r_paren,
715 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-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 Bataevee6507d2013-11-18 08:17:37 +0000733 SkipUntil(tok::comma, tok::r_paren,
734 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-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 Rosea7d03842013-02-08 22:30:41 +0000741 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000742 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
743 ++AfterMinor;
744 }
745
746 if (AfterMinor == ActualLength) {
747 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000748
Douglas Gregor20b2ebd2011-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 Rosierc1183952012-06-26 22:30:43 +0000755 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-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 Bataevee6507d2013-11-18 08:17:37 +0000761 SkipUntil(tok::comma, tok::r_paren,
762 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000763 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000764 }
765
766 // Parse the subminor version.
767 unsigned AfterSubminor = AfterMinor + 1;
768 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000769 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-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 Bataevee6507d2013-11-18 08:17:37 +0000776 SkipUntil(tok::comma, tok::r_paren,
777 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000787/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-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 Gregorfdd417f2012-03-11 04:53:21 +0000799/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000800/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000801/// opt-message:
802/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000803void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
804 SourceLocation AvailabilityLoc,
805 ParsedAttributes &attrs,
806 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000807 enum { Introduced, Deprecated, Obsoleted, Unknown };
808 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000809 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000810
811 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000812 BalancedDelimiterTracker T(*this, tok::l_paren);
813 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000814 Diag(Tok, diag::err_expected_lparen);
815 return;
816 }
Douglas Gregor20b2ebd2011-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 Bataevee6507d2013-11-18 08:17:37 +0000821 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000822 return;
823 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000824 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-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 Gregor7ab142b2011-03-26 03:35:55 +0000836 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000837 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000838 }
839
840 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000841 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000842 do {
843 if (Tok.isNot(tok::identifier)) {
844 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000845 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000846 return;
847 }
848 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
849 SourceLocation KeywordLoc = ConsumeToken();
850
Douglas Gregor7ab142b2011-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 Rosierc1183952012-06-26 22:30:43 +0000855 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000856 UnavailableLoc = KeywordLoc;
857
858 if (Tok.isNot(tok::comma))
859 break;
860
861 ConsumeToken();
862 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000863 }
864
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000865 if (Tok.isNot(tok::equal)) {
866 Diag(Tok, diag::err_expected_equal_after)
867 << Keyword;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000868 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000869 return;
870 }
871 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000872 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000873 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000874 Diag(Tok, diag::err_expected_string_literal)
875 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000876 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000877 return;
878 }
879 MessageExpr = ParseStringLiteralExpression();
880 break;
881 }
Chad Rosierc1183952012-06-26 22:30:43 +0000882
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000883 SourceRange VersionRange;
884 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000885
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000886 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000887 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-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 Rosierc1183952012-06-26 22:30:43 +0000898 else
Douglas Gregor20b2ebd2011-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 Rosierc1183952012-06-26 22:30:43 +0000904 << Keyword
Douglas Gregor20b2ebd2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000924 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000925 return;
926
927 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000928 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000929
Douglas Gregor7ab142b2011-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 Gregor20b2ebd2011-03-23 00:50:03 +0000949 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000950 attrs.addNew(&Availability,
951 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000952 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000953 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000954 Changes[Introduced],
955 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000956 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000957 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000958 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000959}
960
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000961
Bill Wendling44426052012-12-20 19:22:21 +0000962// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-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 Hutchins3fc6e4a2012-02-16 16:50:43 +0000972 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-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 Gregor3024f072012-04-16 07:05:22 +0000986 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000987 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000988 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000989 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
990 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
991
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000992 // Enter the scope of nested classes
993 if (!AlreadyHasClassScope)
994 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
995 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000996 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000997 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
998 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
999 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001000 }
Chad Rosierc1183952012-06-26 22:30:43 +00001001
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001002 if (!AlreadyHasClassScope)
1003 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1004 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001005}
1006
DeLesley Hutchins3fc6e4a2012-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 Hutchins66e300e2012-11-02 21:44:32 +00001011 assert(LAs.parseSoon() &&
1012 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001013 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001014 if (D)
1015 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001016 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001017 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001018 }
1019 LAs.clear();
1020}
1021
1022
Caitlin Sadowski9385dd72011-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 Rosierc1183952012-06-26 22:30:43 +00001026/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001027/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001028void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1029 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-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 Kyrtzidisc36633c2013-03-27 23:58:17 +00001038 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001039
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001040 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001041 // FIXME: Do not warn on C++11 attributes, once we start supporting
1042 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001043 Diag(Tok, diag::warn_attribute_on_function_definition)
1044 << LA.AttrName.getName();
1045 }
1046
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001047 ParsedAttributes Attrs(AttrFactory);
1048 SourceLocation endLoc;
1049
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001050 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001051 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001052 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1053 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001054
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001055 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001056 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1057 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001058
DeLesley Hutchinsf1150d32012-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 Hutchinsbd2ee132012-03-02 22:12:59 +00001065
DeLesley Hutchinsf1150d32012-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 Hutchinsbd2ee132012-03-02 22:12:59 +00001071
Michael Han23214e52012-10-03 01:56:22 +00001072 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001073 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-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 Han23214e52012-10-03 01:56:22 +00001085 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001086 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001087 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001088 } else {
1089 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001090 }
1091
DeLesley Hutchinsbd2ee132012-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 Sadowski9385dd72011-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 Gregor0cf55e92012-03-08 01:00:17 +00001104 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001105 }
1106}
1107
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001108/// \brief Wrapper around a case statement checking if AttrName is
1109/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001110bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-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 Sadowski9385dd72011-09-08 17:42:22 +00001149 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001150
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001151 BalancedDelimiterTracker T(*this, tok::l_paren);
1152 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001153
Aaron Ballman00e99962013-08-31 01:11:41 +00001154 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001155 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001156
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001157 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001158 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001159 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001160 ExprResult ArgExpr(ParseAssignmentExpression());
1161 if (ArgExpr.isInvalid()) {
1162 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001163 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001164 break;
1165 } else {
1166 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001167 }
Caitlin Sadowski9385dd72011-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 Hutchins30398dd2012-01-20 22:50:54 +00001173 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001174 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1175 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001176 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001177 if (EndLoc)
1178 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001179}
1180
Dmitri Gribenkoe4a5a902012-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 Smithfeefaf52013-09-03 18:01:40 +00001195 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-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 Ballman00e99962013-08-31 01:11:41 +00001235 ArgumentKind, MatchingCType.release(),
1236 LayoutCompatible, MustBeNull,
1237 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001238 }
1239
1240 if (EndLoc)
1241 *EndLoc = T.getCloseLocation();
1242}
1243
Richard Smith7bdcc4a2012-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 Bataevee6507d2013-11-18 08:17:37 +00001268 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-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 Carruthd8f7d382012-04-10 16:03:08 +00001275 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001276}
1277
Richard Smith98155ad2013-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 Smith4c96e992013-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 McCall53fa7142010-12-24 02:08:15 +00001297void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1298 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1299 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001300}
1301
Michael Han64536a62012-11-06 19:34:54 +00001302void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1303 AttributeList *AttrList = attrs.getList();
1304 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001305 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001306 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001307 << AttrList->getName();
1308 AttrList->setInvalid();
1309 }
1310 AttrList = AttrList->getNext();
1311 }
1312}
1313
Chris Lattner53361ac2006-08-10 05:19:57 +00001314/// ParseDeclaration - Parse a full 'declaration', which consists of
1315/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001316/// 'Context' should be a Declarator::TheContext value. This returns the
1317/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001318///
1319/// declaration: [C99 6.7]
1320/// block-declaration ->
1321/// simple-declaration
1322/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001323/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001324/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001325/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001326/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001327/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001328/// others... [FIXME]
1329///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001330Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1331 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001332 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001333 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001334 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-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 Rosierc1183952012-06-26 22:30:43 +00001338
John McCall48871652010-08-21 09:40:31 +00001339 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001340 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001341 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001342 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001343 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001344 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001345 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001346 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001347 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001348 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001349 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001350 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001351 SourceLocation InlineLoc = ConsumeToken();
1352 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1353 break;
1354 }
Chad Rosierc1183952012-06-26 22:30:43 +00001355 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001356 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001357 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001358 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001359 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001360 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001361 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001362 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001363 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001364 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001365 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001366 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001367 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001369 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001370 default:
John McCall53fa7142010-12-24 02:08:15 +00001371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001372 }
Chad Rosierc1183952012-06-26 22:30:43 +00001373
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001374 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-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 Lattnera5235172007-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] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001382/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1383/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001384///[C90/C++]init-declarator-list ';' [TODO]
1385/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001386///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001387/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001388/// attribute-specifier-seq[opt] type-specifier-seq declarator
1389///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001390/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001391/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-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.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001396Parser::DeclGroupPtrTy
1397Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1398 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001399 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001400 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001401 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001402 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001403
Richard Smith404dfb42013-11-19 22:47:36 +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 Bagnara1cd83682012-01-07 10:52:36 +00001412
Chris Lattner0e894622006-08-13 19:58:17 +00001413 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1414 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001415 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001416 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001417 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001418 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001419 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001420 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001421 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001422 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001423 }
Chad Rosierc1183952012-06-26 22:30:43 +00001424
Richard Smith2386c8b2013-02-22 09:06:26 +00001425 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001426 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001427}
Mike Stump11289f42009-09-09 15:08:12 +00001428
Richard Smith09f76ee2011-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 Blaikiebbafb8a2012-03-11 07:00:24 +00001447 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001448
Richard Smithc8a79032012-01-09 22:31:44 +00001449 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001450 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-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 Blaikiebbafb8a2012-03-11 07:00:24 +00001454 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001455
Richard Smith09f76ee2011-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 Smithc8a79032012-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 Blaikiebbafb8a2012-03-11 07:00:24 +00001481 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001482
1483 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001484 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001485
1486 default:
1487 return false;
1488 }
1489
1490 default:
1491 return false;
1492 }
1493}
1494
Richard Smithb8caac82012-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 Bataevee6507d2013-11-18 08:17:37 +00001505 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-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 Bataevee6507d2013-11-18 08:17:37 +00001516 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001517 continue;
1518
1519 case tok::l_paren:
1520 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001521 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-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 Rose12e730c2012-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 Smithb8caac82012-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 Rose12e730c2012-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 Smithb8caac82012-04-11 20:59:20 +00001560 return;
1561 break;
1562
1563 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001564 case tok::annot_module_begin:
1565 case tok::annot_module_end:
1566 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001567 return;
1568
1569 default:
1570 break;
1571 }
1572
1573 ConsumeAnyToken();
1574 }
1575}
1576
John McCalld5a36322009-11-03 19:26:08 +00001577/// ParseDeclGroup - Having concluded that this is either a function
1578/// definition or a group of object declarations, actually parse the
1579/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001580Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1581 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001582 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001583 SourceLocation *DeclEnd,
1584 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001585 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001586 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001587 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001588
John McCalld5a36322009-11-03 19:26:08 +00001589 // Bail out if the first declarator didn't seem well-formed.
1590 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001591 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001592 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001593 }
Mike Stump11289f42009-09-09 15:08:12 +00001594
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001595 // Save late-parsed attributes for now; they need to be parsed in the
1596 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001597 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1598 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001599 if (D.isFunctionDeclarator())
1600 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1601
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001602 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001603 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001604 // Look at the next token to make sure that this isn't a function
1605 // declaration. We have to check this because __attribute__ might be the
1606 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001607 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001608
Douglas Gregor012efe22013-04-16 16:01:32 +00001609 if (AllowFunctionDefinitions) {
1610 if (isStartOfFunctionDefinition(D)) {
1611 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1612 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001613
Douglas Gregor012efe22013-04-16 16:01:32 +00001614 // Recover by treating the 'typedef' as spurious.
1615 DS.ClearStorageClassSpecs();
1616 }
1617
1618 Decl *TheDecl =
1619 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1620 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001621 }
1622
Douglas Gregor012efe22013-04-16 16:01:32 +00001623 if (isDeclarationSpecifier()) {
1624 // If there is an invalid declaration specifier right after the function
1625 // prototype, then we must be in a missing semicolon case where this isn't
1626 // actually a body. Just fall through into the code that handles it as a
1627 // prototype, and let the top-level code handle the erroneous declspec
1628 // where it would otherwise expect a comma or semicolon.
1629 } else {
1630 Diag(Tok, diag::err_expected_fn_body);
1631 SkipUntil(tok::semi);
1632 return DeclGroupPtrTy();
1633 }
John McCalld5a36322009-11-03 19:26:08 +00001634 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001635 if (Tok.is(tok::l_brace)) {
1636 Diag(Tok, diag::err_function_definition_not_allowed);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001637 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor012efe22013-04-16 16:01:32 +00001638 }
John McCalld5a36322009-11-03 19:26:08 +00001639 }
1640 }
1641
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001642 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001643 return DeclGroupPtrTy();
1644
1645 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1646 // must parse and analyze the for-range-initializer before the declaration is
1647 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001648 //
1649 // Handle the Objective-C for-in loop variable similarly, although we
1650 // don't need to parse the container in advance.
1651 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1652 bool IsForRangeLoop = false;
1653 if (Tok.is(tok::colon)) {
1654 IsForRangeLoop = true;
1655 FRI->ColonLoc = ConsumeToken();
1656 if (Tok.is(tok::l_brace))
1657 FRI->RangeExpr = ParseBraceInitializer();
1658 else
1659 FRI->RangeExpr = ParseExpression();
1660 }
1661
Richard Smith02e85f32011-04-14 22:09:26 +00001662 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001663 if (IsForRangeLoop)
1664 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001665 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001666 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001667 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001668 }
1669
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001670 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001671 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001672 if (LateParsedAttrs.size() > 0)
1673 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001674 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001675 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001676 DeclsInGroup.push_back(FirstDecl);
1677
Richard Smith09f76ee2011-10-19 21:33:05 +00001678 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001679
John McCalld5a36322009-11-03 19:26:08 +00001680 // If we don't have a comma, it is either the end of the list (a ';') or an
1681 // error, bail out.
1682 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001683 SourceLocation CommaLoc = ConsumeToken();
1684
1685 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1686 // This comma was followed by a line-break and something which can't be
1687 // the start of a declarator. The comma was probably a typo for a
1688 // semicolon.
1689 Diag(CommaLoc, diag::err_expected_semi_declaration)
1690 << FixItHint::CreateReplacement(CommaLoc, ";");
1691 ExpectSemi = false;
1692 break;
1693 }
John McCalld5a36322009-11-03 19:26:08 +00001694
1695 // Parse the next declarator.
1696 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001697 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001698
1699 // Accept attributes in an init-declarator. In the first declarator in a
1700 // declaration, these would be part of the declspec. In subsequent
1701 // declarators, they become part of the declarator itself, so that they
1702 // don't apply to declarators after *this* one. Examples:
1703 // short __attribute__((common)) var; -> declspec
1704 // short var __attribute__((common)); -> declarator
1705 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001706 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001707
1708 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001709 if (!D.isInvalidType()) {
1710 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1711 D.complete(ThisDecl);
1712 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001713 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001714 }
John McCalld5a36322009-11-03 19:26:08 +00001715 }
1716
1717 if (DeclEnd)
1718 *DeclEnd = Tok.getLocation();
1719
Richard Smith09f76ee2011-10-19 21:33:05 +00001720 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001721 ExpectAndConsumeSemi(Context == Declarator::FileContext
1722 ? diag::err_invalid_token_after_toplevel_declarator
1723 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001724 // Okay, there was no semicolon and one was expected. If we see a
1725 // declaration specifier, just assume it was missing and continue parsing.
1726 // Otherwise things are very confused and we skip to recover.
1727 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001728 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner13901342010-07-11 22:42:07 +00001729 if (Tok.is(tok::semi))
1730 ConsumeToken();
1731 }
John McCalld5a36322009-11-03 19:26:08 +00001732 }
1733
Rafael Espindolaab417692013-07-09 12:05:01 +00001734 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001735}
1736
Richard Smith02e85f32011-04-14 22:09:26 +00001737/// Parse an optional simple-asm-expr and attributes, and attach them to a
1738/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001739bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001740 // If a simple-asm-expr is present, parse it.
1741 if (Tok.is(tok::kw_asm)) {
1742 SourceLocation Loc;
1743 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1744 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001745 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001746 return true;
1747 }
1748
1749 D.setAsmLabel(AsmLabel.release());
1750 D.SetRangeEnd(Loc);
1751 }
1752
1753 MaybeParseGNUAttributes(D);
1754 return false;
1755}
1756
Douglas Gregor23996282009-05-12 21:31:51 +00001757/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1758/// declarator'. This method parses the remainder of the declaration
1759/// (including any attributes or initializer, among other things) and
1760/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001761///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001762/// init-declarator: [C99 6.7]
1763/// declarator
1764/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001765/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1766/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001767/// [C++] declarator initializer[opt]
1768///
1769/// [C++] initializer:
1770/// [C++] '=' initializer-clause
1771/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001772/// [C++0x] '=' 'default' [TODO]
1773/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001774/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001775///
1776/// According to the standard grammar, =default and =delete are function
1777/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001778///
John McCall48871652010-08-21 09:40:31 +00001779Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001780 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001781 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001782 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001783
Richard Smith02e85f32011-04-14 22:09:26 +00001784 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1785}
Mike Stump11289f42009-09-09 15:08:12 +00001786
Richard Smith02e85f32011-04-14 22:09:26 +00001787Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1788 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001789 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001790 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001791 switch (TemplateInfo.Kind) {
1792 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001793 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001794 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001795
Douglas Gregor450f00842009-09-25 18:43:00 +00001796 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001797 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001798 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001799 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001800 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001801 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001802 // Re-direct this decl to refer to the templated decl so that we can
1803 // initialize it.
1804 ThisDecl = VT->getTemplatedDecl();
1805 break;
1806 }
1807 case ParsedTemplateInfo::ExplicitInstantiation: {
1808 if (Tok.is(tok::semi)) {
1809 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1810 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1811 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001812 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001813 return 0;
1814 }
1815 ThisDecl = ThisRes.get();
1816 } else {
1817 // FIXME: This check should be for a variable template instantiation only.
1818
1819 // Check that this is a valid instantiation
1820 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1821 // If the declarator-id is not a template-id, issue a diagnostic and
1822 // recover by ignoring the 'template' keyword.
1823 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1824 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1825 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1826 } else {
1827 SourceLocation LAngleLoc =
1828 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1829 Diag(D.getIdentifierLoc(),
1830 diag::err_explicit_instantiation_with_definition)
1831 << SourceRange(TemplateInfo.TemplateLoc)
1832 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1833
1834 // Recover as if it were an explicit specialization.
1835 TemplateParameterLists FakedParamLists;
1836 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1837 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1838 LAngleLoc));
1839
1840 ThisDecl =
1841 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1842 }
1843 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001844 break;
1845 }
1846 }
Mike Stump11289f42009-09-09 15:08:12 +00001847
Richard Smith74aeef52013-04-26 16:15:35 +00001848 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001849
Douglas Gregor23996282009-05-12 21:31:51 +00001850 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001851 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001852 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001853 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001854
Anders Carlsson991285e2010-09-24 21:25:25 +00001855 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001856 if (D.isFunctionDeclarator())
1857 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1858 << 1 /* delete */;
1859 else
1860 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001861 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001862 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001863 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1864 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001865 else
1866 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001867 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001868 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001869 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001870 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001871 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001872
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001873 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001874 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001875 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001876 cutOffParsing();
1877 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001878 }
Chad Rosierc1183952012-06-26 22:30:43 +00001879
John McCalldadc5752010-08-24 06:29:42 +00001880 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001881
David Blaikiebbafb8a2012-03-11 07:00:24 +00001882 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001883 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001884 ExitScope();
1885 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001886
Douglas Gregor23996282009-05-12 21:31:51 +00001887 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001888 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001889 Actions.ActOnInitializerError(ThisDecl);
1890 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001891 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1892 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001893 }
1894 } else if (Tok.is(tok::l_paren)) {
1895 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001896 BalancedDelimiterTracker T(*this, tok::l_paren);
1897 T.consumeOpen();
1898
Benjamin Kramerf0623432012-08-23 22:51:59 +00001899 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001900 CommaLocsTy CommaLocs;
1901
David Blaikiebbafb8a2012-03-11 07:00:24 +00001902 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001903 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001904 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001905 }
1906
Douglas Gregor23996282009-05-12 21:31:51 +00001907 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001908 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001909 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001910
David Blaikiebbafb8a2012-03-11 07:00:24 +00001911 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001912 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001913 ExitScope();
1914 }
Douglas Gregor23996282009-05-12 21:31:51 +00001915 } else {
1916 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001917 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001918
1919 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1920 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001921
David Blaikiebbafb8a2012-03-11 07:00:24 +00001922 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001923 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001924 ExitScope();
1925 }
1926
Sebastian Redla9351792012-02-11 23:51:47 +00001927 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1928 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001929 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001930 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1931 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001932 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001933 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001934 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001935 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001936 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1937
Sebastian Redl3da34892011-06-05 12:23:16 +00001938 if (D.getCXXScopeSpec().isSet()) {
1939 EnterScope(0);
1940 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1941 }
1942
1943 ExprResult Init(ParseBraceInitializer());
1944
1945 if (D.getCXXScopeSpec().isSet()) {
1946 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1947 ExitScope();
1948 }
1949
1950 if (Init.isInvalid()) {
1951 Actions.ActOnInitializerError(ThisDecl);
1952 } else
1953 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1954 /*DirectInit=*/true, TypeContainsAuto);
1955
Douglas Gregor23996282009-05-12 21:31:51 +00001956 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001957 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001958 }
1959
Richard Smithb2bc2e62011-02-21 20:05:19 +00001960 Actions.FinalizeDeclaration(ThisDecl);
1961
Douglas Gregor23996282009-05-12 21:31:51 +00001962 return ThisDecl;
1963}
1964
Chris Lattner1890ac82006-08-13 01:16:23 +00001965/// ParseSpecifierQualifierList
1966/// specifier-qualifier-list:
1967/// type-specifier specifier-qualifier-list[opt]
1968/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001969/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001970///
Richard Smithc5b05522012-03-12 07:56:15 +00001971void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1972 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001973 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1974 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001975 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001976 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001977
Chris Lattner1890ac82006-08-13 01:16:23 +00001978 // Validate declspec for type-name.
1979 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001980 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1981 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001982 Diag(Tok, diag::err_expected_type);
1983 DS.SetTypeSpecError();
1984 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1985 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001986 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001987 if (!DS.hasTypeSpecifier())
1988 DS.SetTypeSpecError();
1989 }
Mike Stump11289f42009-09-09 15:08:12 +00001990
Chris Lattner1b22eed2006-11-28 05:12:07 +00001991 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001992 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001993 if (DS.getStorageClassSpecLoc().isValid())
1994 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1995 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001996 Diag(DS.getThreadStorageClassSpecLoc(),
1997 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001998 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001999 }
Mike Stump11289f42009-09-09 15:08:12 +00002000
Chris Lattner1b22eed2006-11-28 05:12:07 +00002001 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002002 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002003 if (DS.isInlineSpecified())
2004 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2005 if (DS.isVirtualSpecified())
2006 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2007 if (DS.isExplicitSpecified())
2008 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002009 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002010 }
Richard Smithc5b05522012-03-12 07:56:15 +00002011
2012 // Issue diagnostic and remove constexpr specfier if present.
2013 if (DS.isConstexprSpecified()) {
2014 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2015 DS.ClearConstexprSpec();
2016 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002017}
Chris Lattner53361ac2006-08-10 05:19:57 +00002018
Chris Lattner6cc055a2009-04-12 20:42:31 +00002019/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2020/// specified token is valid after the identifier in a declarator which
2021/// immediately follows the declspec. For example, these things are valid:
2022///
2023/// int x [ 4]; // direct-declarator
2024/// int x ( int y); // direct-declarator
2025/// int(int x ) // direct-declarator
2026/// int x ; // simple-declaration
2027/// int x = 17; // init-declarator-list
2028/// int x , y; // init-declarator-list
2029/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002030/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002031/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002032///
2033/// This is not, because 'x' does not immediately follow the declspec (though
2034/// ')' happens to be valid anyway).
2035/// int (x)
2036///
2037static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2038 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2039 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002040 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002041}
2042
Chris Lattner20a0c612009-04-14 21:34:55 +00002043
2044/// ParseImplicitInt - This method is called when we have an non-typename
2045/// identifier in a declspec (which normally terminates the decl spec) when
2046/// the declspec has no type specifier. In this case, the declspec is either
2047/// malformed or is "implicit int" (in K&R and C89).
2048///
2049/// This method handles diagnosing this prettily and returns false if the
2050/// declspec is done being processed. If it recovers and thinks there may be
2051/// other pieces of declspec after it, it returns true.
2052///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002053bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002054 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002055 AccessSpecifier AS, DeclSpecContext DSC,
2056 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002057 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattner20a0c612009-04-14 21:34:55 +00002059 SourceLocation Loc = Tok.getLocation();
2060 // If we see an identifier that is not a type name, we normally would
2061 // parse it as the identifer being declared. However, when a typename
2062 // is typo'd or the definition is not included, this will incorrectly
2063 // parse the typename as the identifier name and fall over misparsing
2064 // later parts of the diagnostic.
2065 //
2066 // As such, we try to do some look-ahead in cases where this would
2067 // otherwise be an "implicit-int" case to see if this is invalid. For
2068 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2069 // an identifier with implicit int, we'd get a parse error because the
2070 // next token is obviously invalid for a type. Parse these as a case
2071 // with an invalid type specifier.
2072 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002073
Chris Lattner20a0c612009-04-14 21:34:55 +00002074 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002075 // error, do lookahead to try to do better recovery. This never applies
2076 // within a type specifier. Outside of C++, we allow this even if the
2077 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002078 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002079 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002080 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002081 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002082 // If this token is valid for implicit int, e.g. "static x = 4", then
2083 // we just avoid eating the identifier, so it will be parsed as the
2084 // identifier in the declarator.
2085 return false;
2086 }
Mike Stump11289f42009-09-09 15:08:12 +00002087
Richard Smitha952ebb2012-05-15 21:01:51 +00002088 if (getLangOpts().CPlusPlus &&
2089 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2090 // Don't require a type specifier if we have the 'auto' storage class
2091 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002092 if (SS)
2093 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002094 return false;
2095 }
2096
Chris Lattner20a0c612009-04-14 21:34:55 +00002097 // Otherwise, if we don't consume this token, we are going to emit an
2098 // error anyway. Try to recover from various common problems. Check
2099 // to see if this was a reference to a tag name without a tag specified.
2100 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002101 //
2102 // C++ doesn't need this, and isTagName doesn't take SS.
2103 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002104 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002105 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002106
Douglas Gregor0be31a22010-07-02 17:43:08 +00002107 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002108 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002109 case DeclSpec::TST_enum:
2110 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2111 case DeclSpec::TST_union:
2112 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2113 case DeclSpec::TST_struct:
2114 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002115 case DeclSpec::TST_interface:
2116 TagName="__interface"; FixitTagName = "__interface ";
2117 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002118 case DeclSpec::TST_class:
2119 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002120 }
Mike Stump11289f42009-09-09 15:08:12 +00002121
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002122 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002123 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2124 LookupResult R(Actions, TokenName, SourceLocation(),
2125 Sema::LookupOrdinaryName);
2126
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002127 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002128 << TokenName << TagName << getLangOpts().CPlusPlus
2129 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2130
2131 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2132 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2133 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002134 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002135 << TokenName << TagName;
2136 }
Mike Stump11289f42009-09-09 15:08:12 +00002137
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002138 // Parse this as a tag as if the missing tag were present.
2139 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002140 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002141 else
Richard Smithc5b05522012-03-12 07:56:15 +00002142 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002143 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002144 return true;
2145 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002146 }
Mike Stump11289f42009-09-09 15:08:12 +00002147
Richard Smithfe904f02012-05-15 21:29:55 +00002148 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002149 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002150 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2151 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002152 // Look ahead to the next token to try to figure out what this declaration
2153 // was supposed to be.
2154 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002155 case tok::l_paren: {
2156 // static x(4); // 'x' is not a type
2157 // x(int n); // 'x' is not a type
2158 // x (*p)[]; // 'x' is a type
2159 //
2160 // Since we're in an error case (or the rare 'implicit int in C++' MS
2161 // extension), we can afford to perform a tentative parse to determine
2162 // which case we're in.
2163 TentativeParsingAction PA(*this);
2164 ConsumeToken();
2165 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2166 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002167
2168 if (TPR != TPResult::False()) {
2169 // The identifier is followed by a parenthesized declarator.
2170 // It's supposed to be a type.
2171 break;
2172 }
2173
2174 // If we're in a context where we could be declaring a constructor,
2175 // check whether this is a constructor declaration with a bogus name.
2176 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2177 IdentifierInfo *II = Tok.getIdentifierInfo();
2178 if (Actions.isCurrentClassNameTypo(II, SS)) {
2179 Diag(Loc, diag::err_constructor_bad_name)
2180 << Tok.getIdentifierInfo() << II
2181 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2182 Tok.setIdentifierInfo(II);
2183 }
2184 }
2185 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002186 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002187 case tok::comma:
2188 case tok::equal:
2189 case tok::kw_asm:
2190 case tok::l_brace:
2191 case tok::l_square:
2192 case tok::semi:
2193 // This looks like a variable or function declaration. The type is
2194 // probably missing. We're done parsing decl-specifiers.
2195 if (SS)
2196 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2197 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002198
2199 default:
2200 // This is probably supposed to be a type. This includes cases like:
2201 // int f(itn);
2202 // struct S { unsinged : 4; };
2203 break;
2204 }
2205 }
2206
Chad Rosierc1183952012-06-26 22:30:43 +00002207 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002208 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002209 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002210 IdentifierInfo *II = Tok.getIdentifierInfo();
2211 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002212 // The action emitted a diagnostic, so we don't have to.
2213 if (T) {
2214 // The action has suggested that the type T could be used. Set that as
2215 // the type in the declaration specifiers, consume the would-be type
2216 // name token, and we're done.
2217 const char *PrevSpec;
2218 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002219 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002220 DS.SetRangeEnd(Tok.getLocation());
2221 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002222 // There may be other declaration specifiers after this.
2223 return true;
2224 } else if (II != Tok.getIdentifierInfo()) {
2225 // If no type was suggested, the correction is to a keyword
2226 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002227 // There may be other declaration specifiers after this.
2228 return true;
2229 }
Chad Rosierc1183952012-06-26 22:30:43 +00002230
Douglas Gregor15e56022009-10-13 23:27:22 +00002231 // Fall through; the action had no suggestion for us.
2232 } else {
2233 // The action did not emit a diagnostic, so emit one now.
2234 SourceRange R;
2235 if (SS) R = SS->getRange();
2236 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2237 }
Mike Stump11289f42009-09-09 15:08:12 +00002238
Douglas Gregor15e56022009-10-13 23:27:22 +00002239 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002240 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002241 DS.SetRangeEnd(Tok.getLocation());
2242 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002243
Chris Lattner20a0c612009-04-14 21:34:55 +00002244 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2245 // avoid rippling error messages on subsequent uses of the same type,
2246 // could be useful if #include was forgotten.
2247 return false;
2248}
2249
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002250/// \brief Determine the declaration specifier context from the declarator
2251/// context.
2252///
2253/// \param Context the declarator context, which is one of the
2254/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002255Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002256Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2257 if (Context == Declarator::MemberContext)
2258 return DSC_class;
2259 if (Context == Declarator::FileContext)
2260 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002261 if (Context == Declarator::TrailingReturnContext)
2262 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002263 return DSC_normal;
2264}
2265
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002266/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2267///
2268/// FIXME: Simply returns an alignof() expression if the argument is a
2269/// type. Ideally, the type should be propagated directly into Sema.
2270///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002271/// [C11] type-id
2272/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002273/// [C++0x] type-id ...[opt]
2274/// [C++0x] assignment-expression ...[opt]
2275ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2276 SourceLocation &EllipsisLoc) {
2277 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002278 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002279 SourceLocation TypeLoc = Tok.getLocation();
2280 ParsedType Ty = ParseTypeName().get();
2281 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002282 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2283 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002284 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002285 ER = ParseConstantExpression();
2286
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002287 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002288 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002289
2290 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002291}
2292
2293/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2294/// attribute to Attrs.
2295///
2296/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002297/// [C11] '_Alignas' '(' type-id ')'
2298/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002299/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2300/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002301void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002302 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002303 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2304 "Not an alignment-specifier!");
2305
Richard Smithd11c7a12013-01-29 01:48:07 +00002306 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2307 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002308
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002309 BalancedDelimiterTracker T(*this, tok::l_paren);
2310 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002311 return;
2312
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002313 SourceLocation EllipsisLoc;
2314 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002315 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002316 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002317 return;
2318 }
2319
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002320 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002321 if (EndLoc)
2322 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002323
Aaron Ballman00e99962013-08-31 01:11:41 +00002324 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002325 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002326 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2327 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002328}
2329
Richard Smith404dfb42013-11-19 22:47:36 +00002330/// Determine whether we're looking at something that might be a declarator
2331/// in a simple-declaration. If it can't possibly be a declarator, maybe
2332/// diagnose a missing semicolon after a prior tag definition in the decl
2333/// specifier.
2334///
2335/// \return \c true if an error occurred and this can't be any kind of
2336/// declaration.
2337bool
2338Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2339 DeclSpecContext DSContext,
2340 LateParsedAttrList *LateAttrs) {
2341 assert(DS.hasTagDefinition() && "shouldn't call this");
2342
2343 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002344
2345 if (getLangOpts().CPlusPlus &&
2346 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2347 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2348 TryAnnotateCXXScopeToken(EnteringContext)) {
2349 SkipMalformedDecl();
2350 return true;
2351 }
2352
Richard Smith698875a2013-11-20 23:40:57 +00002353 bool HasScope = Tok.is(tok::annot_cxxscope);
2354 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2355 Token AfterScope = HasScope ? NextToken() : Tok;
2356
Richard Smith404dfb42013-11-19 22:47:36 +00002357 // Determine whether the following tokens could possibly be a
2358 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002359 bool MightBeDeclarator = true;
2360 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2361 // A declarator-id can't start with 'typename'.
2362 MightBeDeclarator = false;
2363 } else if (AfterScope.is(tok::annot_template_id)) {
2364 // If we have a type expressed as a template-id, this cannot be a
2365 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2366 TemplateIdAnnotation *Annot =
2367 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2368 if (Annot->Kind == TNK_Type_template)
2369 MightBeDeclarator = false;
2370 } else if (AfterScope.is(tok::identifier)) {
2371 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2372
Richard Smith404dfb42013-11-19 22:47:36 +00002373 // These tokens cannot come after the declarator-id in a
2374 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002375 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2376 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2377 Next.is(tok::coloncolon)) {
2378 // Missing a semicolon.
2379 MightBeDeclarator = false;
2380 } else if (HasScope) {
2381 // If the declarator-id has a scope specifier, it must redeclare a
2382 // previously-declared entity. If that's a type (and this is not a
2383 // typedef), that's an error.
2384 CXXScopeSpec SS;
2385 Actions.RestoreNestedNameSpecifierAnnotation(
2386 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2387 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2388 Sema::NameClassification Classification = Actions.ClassifyName(
2389 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2390 /*IsAddressOfOperand*/false);
2391 switch (Classification.getKind()) {
2392 case Sema::NC_Error:
2393 SkipMalformedDecl();
2394 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002395
Richard Smith698875a2013-11-20 23:40:57 +00002396 case Sema::NC_Keyword:
2397 case Sema::NC_NestedNameSpecifier:
2398 llvm_unreachable("typo correction and nested name specifiers not "
2399 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002400
Richard Smith698875a2013-11-20 23:40:57 +00002401 case Sema::NC_Type:
2402 case Sema::NC_TypeTemplate:
2403 // Not a previously-declared non-type entity.
2404 MightBeDeclarator = false;
2405 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002406
Richard Smith698875a2013-11-20 23:40:57 +00002407 case Sema::NC_Unknown:
2408 case Sema::NC_Expression:
2409 case Sema::NC_VarTemplate:
2410 case Sema::NC_FunctionTemplate:
2411 // Might be a redeclaration of a prior entity.
2412 break;
2413 }
Richard Smith404dfb42013-11-19 22:47:36 +00002414 }
Richard Smith404dfb42013-11-19 22:47:36 +00002415 }
2416
Richard Smith698875a2013-11-20 23:40:57 +00002417 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002418 return false;
2419
2420 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
2421 diag::err_expected_semi_after_tagdecl)
2422 << DeclSpec::getSpecifierName(DS.getTypeSpecType());
2423
2424 // Try to recover from the typo, by dropping the tag definition and parsing
2425 // the problematic tokens as a type.
2426 //
2427 // FIXME: Split the DeclSpec into pieces for the standalone
2428 // declaration and pieces for the following declaration, instead
2429 // of assuming that all the other pieces attach to new declaration,
2430 // and call ParsedFreeStandingDeclSpec as appropriate.
2431 DS.ClearTypeSpecType();
2432 ParsedTemplateInfo NotATemplate;
2433 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2434 return false;
2435}
2436
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002437/// ParseDeclarationSpecifiers
2438/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002439/// storage-class-specifier declaration-specifiers[opt]
2440/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002441/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002442/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002443/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002444/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002445///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002446/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002447/// 'typedef'
2448/// 'extern'
2449/// 'static'
2450/// 'auto'
2451/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002452/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002453/// [C++11] 'thread_local'
2454/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002455/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002456/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002457/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002458/// [C++] 'virtual'
2459/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002460/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002461/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002462/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002463
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002464///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002465void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002466 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002467 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002468 DeclSpecContext DSContext,
2469 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002470 if (DS.getSourceRange().isInvalid()) {
2471 DS.SetRangeStart(Tok.getLocation());
2472 DS.SetRangeEnd(Tok.getLocation());
2473 }
Chad Rosierc1183952012-06-26 22:30:43 +00002474
Douglas Gregordf593fb2011-11-07 17:33:42 +00002475 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002476 bool AttrsLastTime = false;
2477 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002478 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002479 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002480 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002481 unsigned DiagID = 0;
2482
Chris Lattner4d8f8732006-11-28 05:05:08 +00002483 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002484
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002485 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002486 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002487 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002488 if (!AttrsLastTime)
2489 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002490 else {
2491 // Reject C++11 attributes that appertain to decl specifiers as
2492 // we don't support any C++11 attributes that appertain to decl
2493 // specifiers. This also conforms to what g++ 4.8 is doing.
2494 ProhibitCXX11Attributes(attrs);
2495
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002496 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002497 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002498
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002499 // If this is not a declaration specifier token, we're done reading decl
2500 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002501 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002502 return;
Mike Stump11289f42009-09-09 15:08:12 +00002503
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002504 case tok::l_square:
2505 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002506 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002507 goto DoneWithDeclSpec;
2508
2509 ProhibitAttributes(attrs);
2510 // FIXME: It would be good to recover by accepting the attributes,
2511 // but attempting to do that now would cause serious
2512 // madness in terms of diagnostics.
2513 attrs.clear();
2514 attrs.Range = SourceRange();
2515
2516 ParseCXX11Attributes(attrs);
2517 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002518 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002519
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002520 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002521 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002522 if (DS.hasTypeSpecifier()) {
2523 bool AllowNonIdentifiers
2524 = (getCurScope()->getFlags() & (Scope::ControlScope |
2525 Scope::BlockScope |
2526 Scope::TemplateParamScope |
2527 Scope::FunctionPrototypeScope |
2528 Scope::AtCatchScope)) == 0;
2529 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002530 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002531 (DSContext == DSC_class && DS.isFriendSpecified());
2532
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002533 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002534 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002535 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002536 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002537 }
2538
Douglas Gregor80039242011-02-15 20:33:25 +00002539 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2540 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2541 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002542 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002543 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002544 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002545 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002546 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002547 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002548
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002549 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002550 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002551 }
2552
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002553 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002554 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002555 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002556 if (!DS.hasTypeSpecifier())
2557 DS.SetTypeSpecError();
2558 goto DoneWithDeclSpec;
2559 }
John McCall8bc2a702010-03-01 18:20:46 +00002560 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2561 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002562 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002563
2564 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002565 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002566 goto DoneWithDeclSpec;
2567
John McCall9dab4e62009-12-12 11:40:51 +00002568 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002569 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2570 Tok.getAnnotationRange(),
2571 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002572
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002573 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002574 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002575 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002576 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002577 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002578 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002579
2580 // C++ [class.qual]p2:
2581 // In a lookup in which the constructor is an acceptable lookup
2582 // result and the nested-name-specifier nominates a class C:
2583 //
2584 // - if the name specified after the
2585 // nested-name-specifier, when looked up in C, is the
2586 // injected-class-name of C (Clause 9), or
2587 //
2588 // - if the name specified after the nested-name-specifier
2589 // is the same as the identifier or the
2590 // simple-template-id's template-name in the last
2591 // component of the nested-name-specifier,
2592 //
2593 // the name is instead considered to name the constructor of
2594 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002595 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002596 // Thus, if the template-name is actually the constructor
2597 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002598 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002599 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002600 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002601 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002602 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002603 if (isConstructorDeclarator()) {
2604 // The user meant this to be an out-of-line constructor
2605 // definition, but template arguments are not allowed
2606 // there. Just allow this as a constructor; we'll
2607 // complain about it later.
2608 goto DoneWithDeclSpec;
2609 }
2610
2611 // The user meant this to name a type, but it actually names
2612 // a constructor with some extraneous template
2613 // arguments. Complain, then parse it as a type as the user
2614 // intended.
2615 Diag(TemplateId->TemplateNameLoc,
2616 diag::err_out_of_line_template_id_names_constructor)
2617 << TemplateId->Name;
2618 }
2619
John McCall9dab4e62009-12-12 11:40:51 +00002620 DS.getTypeSpecScope() = SS;
2621 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002622 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002623 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002624 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002625 continue;
2626 }
2627
Douglas Gregorc5790df2009-09-28 07:26:33 +00002628 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002629 DS.getTypeSpecScope() = SS;
2630 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002631 if (Tok.getAnnotationValue()) {
2632 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002633 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002634 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002635 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002636 if (isInvalid)
2637 break;
John McCallba7bf592010-08-24 05:47:05 +00002638 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002639 else
2640 DS.SetTypeSpecError();
2641 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2642 ConsumeToken(); // The typename
2643 }
2644
Douglas Gregor167fa622009-03-25 15:40:00 +00002645 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002646 goto DoneWithDeclSpec;
2647
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002648 // If we're in a context where the identifier could be a class name,
2649 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002650 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002651 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002652 &SS)) {
2653 if (isConstructorDeclarator())
2654 goto DoneWithDeclSpec;
2655
2656 // As noted in C++ [class.qual]p2 (cited above), when the name
2657 // of the class is qualified in a context where it could name
2658 // a constructor, its a constructor name. However, we've
2659 // looked at the declarator, and the user probably meant this
2660 // to be a type. Complain that it isn't supposed to be treated
2661 // as a type, then proceed to parse it as a type.
2662 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2663 << Next.getIdentifierInfo();
2664 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002665
John McCallba7bf592010-08-24 05:47:05 +00002666 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2667 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002668 getCurScope(), &SS,
2669 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002670 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002671 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002672
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002673 // If the referenced identifier is not a type, then this declspec is
2674 // erroneous: We already checked about that it has no type specifier, and
2675 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002676 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002677 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002678 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002679 ParsedAttributesWithRange Attrs(AttrFactory);
2680 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2681 if (!Attrs.empty()) {
2682 AttrsLastTime = true;
2683 attrs.takeAllFrom(Attrs);
2684 }
2685 continue;
2686 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002687 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002688 }
Mike Stump11289f42009-09-09 15:08:12 +00002689
John McCall9dab4e62009-12-12 11:40:51 +00002690 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002691 ConsumeToken(); // The C++ scope.
2692
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002694 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002695 if (isInvalid)
2696 break;
Mike Stump11289f42009-09-09 15:08:12 +00002697
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002698 DS.SetRangeEnd(Tok.getLocation());
2699 ConsumeToken(); // The typename.
2700
2701 continue;
2702 }
Mike Stump11289f42009-09-09 15:08:12 +00002703
Chris Lattnere387d9e2009-01-21 19:48:37 +00002704 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002705 // If we've previously seen a tag definition, we were almost surely
2706 // missing a semicolon after it.
2707 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2708 goto DoneWithDeclSpec;
2709
John McCallba7bf592010-08-24 05:47:05 +00002710 if (Tok.getAnnotationValue()) {
2711 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002713 DiagID, T);
2714 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002715 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002716
Chris Lattner005fc1b2010-04-05 18:18:31 +00002717 if (isInvalid)
2718 break;
2719
Chris Lattnere387d9e2009-01-21 19:48:37 +00002720 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2721 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002722
Chris Lattnere387d9e2009-01-21 19:48:37 +00002723 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2724 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002725 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002726 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002727 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002728
Chris Lattnere387d9e2009-01-21 19:48:37 +00002729 continue;
2730 }
Mike Stump11289f42009-09-09 15:08:12 +00002731
Douglas Gregor06873092011-04-28 15:48:45 +00002732 case tok::kw___is_signed:
2733 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2734 // typically treats it as a trait. If we see __is_signed as it appears
2735 // in libstdc++, e.g.,
2736 //
2737 // static const bool __is_signed;
2738 //
2739 // then treat __is_signed as an identifier rather than as a keyword.
2740 if (DS.getTypeSpecType() == TST_bool &&
2741 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2742 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2743 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2744 Tok.setKind(tok::identifier);
2745 }
2746
2747 // We're done with the declaration-specifiers.
2748 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002749
Chris Lattner16fac4f2008-07-26 01:18:38 +00002750 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002751 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002752 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002753 // In C++, check to see if this is a scope specifier like foo::bar::, if
2754 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002755 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002756 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002757 if (!DS.hasTypeSpecifier())
2758 DS.SetTypeSpecError();
2759 goto DoneWithDeclSpec;
2760 }
2761 if (!Tok.is(tok::identifier))
2762 continue;
2763 }
Mike Stump11289f42009-09-09 15:08:12 +00002764
Chris Lattner16fac4f2008-07-26 01:18:38 +00002765 // This identifier can only be a typedef name if we haven't already seen
2766 // a type-specifier. Without this check we misparse:
2767 // typedef int X; struct Y { short X; }; as 'short int'.
2768 if (DS.hasTypeSpecifier())
2769 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002770
John Thompson22334602010-02-05 00:12:22 +00002771 // Check for need to substitute AltiVec keyword tokens.
2772 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2773 break;
2774
Richard Smith3092a3b2012-05-09 18:56:43 +00002775 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2776 // allow the use of a typedef name as a type specifier.
2777 if (DS.isTypeAltiVecVector())
2778 goto DoneWithDeclSpec;
2779
John McCallba7bf592010-08-24 05:47:05 +00002780 ParsedType TypeRep =
2781 Actions.getTypeName(*Tok.getIdentifierInfo(),
2782 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002783
Chris Lattner6cc055a2009-04-12 20:42:31 +00002784 // If this is not a typedef name, don't parse it as part of the declspec,
2785 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002786 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002787 ParsedAttributesWithRange Attrs(AttrFactory);
2788 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2789 if (!Attrs.empty()) {
2790 AttrsLastTime = true;
2791 attrs.takeAllFrom(Attrs);
2792 }
2793 continue;
2794 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002795 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002796 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002797
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002798 // If we're in a context where the identifier could be a class name,
2799 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002800 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002801 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002802 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002803 goto DoneWithDeclSpec;
2804
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002805 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002806 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002807 if (isInvalid)
2808 break;
Mike Stump11289f42009-09-09 15:08:12 +00002809
Chris Lattner16fac4f2008-07-26 01:18:38 +00002810 DS.SetRangeEnd(Tok.getLocation());
2811 ConsumeToken(); // The identifier
2812
2813 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2814 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002815 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002816 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002817 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002818
Steve Naroffcd5e7822008-09-22 10:28:57 +00002819 // Need to support trailing type qualifiers (e.g. "id<p> const").
2820 // If a type specifier follows, it will be diagnosed elsewhere.
2821 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002822 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002823
2824 // type-name
2825 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002826 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002827 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002828 // This template-id does not refer to a type name, so we're
2829 // done with the type-specifiers.
2830 goto DoneWithDeclSpec;
2831 }
2832
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002833 // If we're in a context where the template-id could be a
2834 // constructor name or specialization, check whether this is a
2835 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002836 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002837 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002838 isConstructorDeclarator())
2839 goto DoneWithDeclSpec;
2840
Douglas Gregor7f741122009-02-25 19:37:18 +00002841 // Turn the template-id annotation token into a type annotation
2842 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002843 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002844 continue;
2845 }
2846
Chris Lattnere37e2332006-08-15 04:50:22 +00002847 // GNU attributes support.
2848 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002849 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002850 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002851
2852 // Microsoft declspec support.
2853 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002854 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002855 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002856
Steve Naroff44ac7772008-12-25 14:16:32 +00002857 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002858 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002859 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002860 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002861 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002862 // FIXME: This does not work correctly if it is set to be a declspec
2863 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002864 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2865 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002866 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002867 }
Eli Friedman53339e02009-06-08 23:27:34 +00002868
Aaron Ballman317a77f2013-05-22 23:25:32 +00002869 case tok::kw___sptr:
2870 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002871 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002872 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002873 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002874 case tok::kw___cdecl:
2875 case tok::kw___stdcall:
2876 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002877 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002878 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002879 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002880 continue;
2881
Dawn Perchik335e16b2010-09-03 01:29:35 +00002882 // Borland single token adornments.
2883 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002884 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002885 continue;
2886
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002887 // OpenCL single token adornments.
2888 case tok::kw___kernel:
2889 ParseOpenCLAttributes(DS.getAttributes());
2890 continue;
2891
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002892 // storage-class-specifier
2893 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002894 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2895 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002896 break;
2897 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002898 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002899 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002900 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2901 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002902 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002903 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002904 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2905 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002906 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002907 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002908 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002909 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002910 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2911 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002912 break;
2913 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002914 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002915 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002916 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2917 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002918 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002919 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002920 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002921 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2923 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002924 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002925 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2926 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002927 break;
2928 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002929 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2930 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002931 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002932 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002933 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2934 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002935 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002936 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002937 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2938 PrevSpec, DiagID);
2939 break;
2940 case tok::kw_thread_local:
2941 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2942 PrevSpec, DiagID);
2943 break;
2944 case tok::kw__Thread_local:
2945 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2946 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002947 break;
Mike Stump11289f42009-09-09 15:08:12 +00002948
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002949 // function-specifier
2950 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002951 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002952 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002953 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002954 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002955 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002956 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002957 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002958 break;
Richard Smith0015f092013-01-17 22:16:11 +00002959 case tok::kw__Noreturn:
2960 if (!getLangOpts().C11)
2961 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002962 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002963 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002964
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002965 // alignment-specifier
2966 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002967 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002968 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002969 ParseAlignmentSpecifier(DS.getAttributes());
2970 continue;
2971
Anders Carlssoncd8db412009-05-06 04:46:28 +00002972 // friend
2973 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002974 if (DSContext == DSC_class)
2975 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2976 else {
2977 PrevSpec = ""; // not actually used by the diagnostic
2978 DiagID = diag::err_friend_invalid_in_context;
2979 isInvalid = true;
2980 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002981 break;
Mike Stump11289f42009-09-09 15:08:12 +00002982
Douglas Gregor26701a42011-09-09 02:06:17 +00002983 // Modules
2984 case tok::kw___module_private__:
2985 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2986 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002987
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002988 // constexpr
2989 case tok::kw_constexpr:
2990 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2991 break;
2992
Chris Lattnere387d9e2009-01-21 19:48:37 +00002993 // type-specifier
2994 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002995 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2996 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002997 break;
2998 case tok::kw_long:
2999 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003000 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3001 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003002 else
John McCall49bfce42009-08-03 20:12:06 +00003003 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3004 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003005 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003006 case tok::kw___int64:
3007 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3008 DiagID);
3009 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003010 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003011 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3012 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003013 break;
3014 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003015 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3016 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003017 break;
3018 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003019 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3020 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003021 break;
3022 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003023 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3024 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003025 break;
3026 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3028 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003029 break;
3030 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3032 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003033 break;
3034 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3036 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003037 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003038 case tok::kw___int128:
3039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3040 DiagID);
3041 break;
3042 case tok::kw_half:
3043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3044 DiagID);
3045 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003046 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3048 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003049 break;
3050 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3052 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003053 break;
3054 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3056 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003057 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003058 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3060 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003061 break;
3062 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3064 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003065 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003066 case tok::kw_bool:
3067 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003068 if (Tok.is(tok::kw_bool) &&
3069 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3070 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3071 PrevSpec = ""; // Not used by the diagnostic.
3072 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003073 // For better error recovery.
3074 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003075 isInvalid = true;
3076 } else {
3077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3078 DiagID);
3079 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003080 break;
3081 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003082 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3083 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003084 break;
3085 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003086 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3087 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003088 break;
3089 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003090 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3091 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003092 break;
John Thompson22334602010-02-05 00:12:22 +00003093 case tok::kw___vector:
3094 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3095 break;
3096 case tok::kw___pixel:
3097 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3098 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003099 case tok::kw_image1d_t:
3100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
3101 PrevSpec, DiagID);
3102 break;
3103 case tok::kw_image1d_array_t:
3104 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
3105 PrevSpec, DiagID);
3106 break;
3107 case tok::kw_image1d_buffer_t:
3108 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3109 PrevSpec, DiagID);
3110 break;
3111 case tok::kw_image2d_t:
3112 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3113 PrevSpec, DiagID);
3114 break;
3115 case tok::kw_image2d_array_t:
3116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3117 PrevSpec, DiagID);
3118 break;
3119 case tok::kw_image3d_t:
3120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3121 PrevSpec, DiagID);
3122 break;
Guy Benyei61054192013-02-07 10:55:47 +00003123 case tok::kw_sampler_t:
3124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3125 PrevSpec, DiagID);
3126 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003127 case tok::kw_event_t:
3128 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3129 PrevSpec, DiagID);
3130 break;
John McCall39439732011-04-09 22:50:59 +00003131 case tok::kw___unknown_anytype:
3132 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3133 PrevSpec, DiagID);
3134 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003135
3136 // class-specifier:
3137 case tok::kw_class:
3138 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003139 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003140 case tok::kw_union: {
3141 tok::TokenKind Kind = Tok.getKind();
3142 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003143
3144 // These are attributes following class specifiers.
3145 // To produce better diagnostic, we parse them when
3146 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003147 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003148 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003149 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003150
3151 // If there are attributes following class specifier,
3152 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003153 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003154 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003155 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003156 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003157 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003158 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003159
3160 // enum-specifier:
3161 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003162 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003163 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003164 continue;
3165
3166 // cv-qualifier:
3167 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003168 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003169 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003170 break;
3171 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003172 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003173 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003174 break;
3175 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003176 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003177 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003178 break;
3179
Douglas Gregor333489b2009-03-27 23:10:48 +00003180 // C++ typename-specifier:
3181 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003182 if (TryAnnotateTypeOrScopeToken()) {
3183 DS.SetTypeSpecError();
3184 goto DoneWithDeclSpec;
3185 }
3186 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003187 continue;
3188 break;
3189
Chris Lattnere387d9e2009-01-21 19:48:37 +00003190 // GNU typeof support.
3191 case tok::kw_typeof:
3192 ParseTypeofSpecifier(DS);
3193 continue;
3194
David Blaikie15a430a2011-12-04 05:04:18 +00003195 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003196 ParseDecltypeSpecifier(DS);
3197 continue;
3198
Alexis Hunt4a257072011-05-19 05:37:45 +00003199 case tok::kw___underlying_type:
3200 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003201 continue;
3202
3203 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003204 // C11 6.7.2.4/4:
3205 // If the _Atomic keyword is immediately followed by a left parenthesis,
3206 // it is interpreted as a type specifier (with a type name), not as a
3207 // type qualifier.
3208 if (NextToken().is(tok::l_paren)) {
3209 ParseAtomicSpecifier(DS);
3210 continue;
3211 }
3212 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3213 getLangOpts());
3214 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003215
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003216 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003217 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003218 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003219 goto DoneWithDeclSpec;
3220 case tok::kw___private:
3221 case tok::kw___global:
3222 case tok::kw___local:
3223 case tok::kw___constant:
3224 case tok::kw___read_only:
3225 case tok::kw___write_only:
3226 case tok::kw___read_write:
3227 ParseOpenCLQualifiers(DS);
3228 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003229
Steve Naroffcfdf6162008-06-05 00:02:44 +00003230 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003231 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003232 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3233 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003234 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003235 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003236
Douglas Gregor3a001f42010-11-19 17:10:50 +00003237 if (!ParseObjCProtocolQualifiers(DS))
3238 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3239 << FixItHint::CreateInsertion(Loc, "id")
3240 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003241
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003242 // Need to support trailing type qualifiers (e.g. "id<p> const").
3243 // If a type specifier follows, it will be diagnosed elsewhere.
3244 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003245 }
John McCall49bfce42009-08-03 20:12:06 +00003246 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003247 if (isInvalid) {
3248 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003249 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003250
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003251 if (DiagID == diag::ext_duplicate_declspec)
3252 Diag(Tok, DiagID)
3253 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3254 else
3255 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003256 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003257
Chris Lattner2e232092008-03-13 06:29:04 +00003258 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003259 if (DiagID != diag::err_bool_redeclaration)
3260 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003261
3262 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003263 }
3264}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003265
Chris Lattner70ae4912007-10-29 04:42:53 +00003266/// ParseStructDeclaration - Parse a struct declaration without the terminating
3267/// semicolon.
3268///
Chris Lattner90a26b02007-01-23 04:38:16 +00003269/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003270/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003271/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003272/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003273/// struct-declarator-list:
3274/// struct-declarator
3275/// struct-declarator-list ',' struct-declarator
3276/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3277/// struct-declarator:
3278/// declarator
3279/// [GNU] declarator attributes[opt]
3280/// declarator[opt] ':' constant-expression
3281/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3282///
Chris Lattnera12405b2008-04-10 06:46:29 +00003283void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003284ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003285
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003286 if (Tok.is(tok::kw___extension__)) {
3287 // __extension__ silences extension warnings in the subexpression.
3288 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003289 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003290 return ParseStructDeclaration(DS, Fields);
3291 }
Mike Stump11289f42009-09-09 15:08:12 +00003292
Steve Naroff97170802007-08-20 22:28:22 +00003293 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003294 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003295
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003296 // If there are no declarators, this is a free-standing declaration
3297 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003298 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003299 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3300 DS);
3301 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003302 return;
3303 }
3304
3305 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003306 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003307 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003308 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003309 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003310 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003311
Bill Wendling44426052012-12-20 19:22:21 +00003312 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003313 if (!FirstDeclarator)
3314 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003315
Steve Naroff97170802007-08-20 22:28:22 +00003316 /// struct-declarator: declarator
3317 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003318 if (Tok.isNot(tok::colon)) {
3319 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3320 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003321 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003322 }
Mike Stump11289f42009-09-09 15:08:12 +00003323
Chris Lattner76c72282007-10-09 17:33:22 +00003324 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003325 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003326 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003327 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003328 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003329 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003330 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003331 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003332
Steve Naroff97170802007-08-20 22:28:22 +00003333 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003334 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003335
John McCallcfefb6d2009-11-03 02:38:08 +00003336 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003337 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003338
Steve Naroff97170802007-08-20 22:28:22 +00003339 // If we don't have a comma, it is either the end of the list (a ';')
3340 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003341 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003342 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003343
Steve Naroff97170802007-08-20 22:28:22 +00003344 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003345 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003346
John McCallcfefb6d2009-11-03 02:38:08 +00003347 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003348 }
Steve Naroff97170802007-08-20 22:28:22 +00003349}
3350
3351/// ParseStructUnionBody
3352/// struct-contents:
3353/// struct-declaration-list
3354/// [EXT] empty
3355/// [GNU] "struct-declaration-list" without terminatoring ';'
3356/// struct-declaration-list:
3357/// struct-declaration
3358/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003359/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003360///
Chris Lattner1300fb92007-01-23 23:42:53 +00003361void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003362 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003363 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3364 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003365 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003366
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003367 BalancedDelimiterTracker T(*this, tok::l_brace);
3368 if (T.consumeOpen())
3369 return;
Mike Stump11289f42009-09-09 15:08:12 +00003370
Douglas Gregor658b9552009-01-09 22:42:13 +00003371 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003372 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003373
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003374 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003375
Chris Lattner7b9ace62007-01-23 20:11:08 +00003376 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003377 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003378 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003379
Chris Lattner736ed5d2007-06-09 05:59:07 +00003380 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003381 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003382 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003383 continue;
3384 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003385
Andy Gibbsc804e082013-04-03 09:46:04 +00003386 // Parse _Static_assert declaration.
3387 if (Tok.is(tok::kw__Static_assert)) {
3388 SourceLocation DeclEnd;
3389 ParseStaticAssertDeclaration(DeclEnd);
3390 continue;
3391 }
3392
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003393 if (Tok.is(tok::annot_pragma_pack)) {
3394 HandlePragmaPack();
3395 continue;
3396 }
3397
3398 if (Tok.is(tok::annot_pragma_align)) {
3399 HandlePragmaAlign();
3400 continue;
3401 }
3402
John McCallcfefb6d2009-11-03 02:38:08 +00003403 if (!Tok.is(tok::at)) {
3404 struct CFieldCallback : FieldCallback {
3405 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003406 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003407 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003408
John McCall48871652010-08-21 09:40:31 +00003409 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003410 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003411 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3412
Eli Friedman934dbbf2012-08-08 23:53:27 +00003413 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003414 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003415 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003416 FD.D.getDeclSpec().getSourceRange().getBegin(),
3417 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003418 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003419 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003420 }
John McCallcfefb6d2009-11-03 02:38:08 +00003421 } Callback(*this, TagDecl, FieldDecls);
3422
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003423 // Parse all the comma separated declarators.
3424 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003425 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003426 } else { // Handle @defs
3427 ConsumeToken();
3428 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3429 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003430 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003431 continue;
3432 }
3433 ConsumeToken();
3434 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3435 if (!Tok.is(tok::identifier)) {
3436 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003437 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003438 continue;
3439 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003440 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003441 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003442 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003443 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3444 ConsumeToken();
3445 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003446 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003447
Chris Lattner76c72282007-10-09 17:33:22 +00003448 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003449 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003450 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003451 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003452 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003453 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003454 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3455 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003456 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner245c5332010-02-02 00:37:27 +00003457 // If we stopped at a ';', eat it.
3458 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003459 }
3460 }
Mike Stump11289f42009-09-09 15:08:12 +00003461
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003462 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003463
John McCall084e83d2011-03-24 11:26:52 +00003464 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003465 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003466 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003467
Douglas Gregor0be31a22010-07-02 17:43:08 +00003468 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003469 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003470 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003471 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003472 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003473 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3474 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003475}
3476
Chris Lattner3b561a32006-08-13 00:12:11 +00003477/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003478/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003479/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003480///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003481/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3482/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003483/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3484/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003485/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003486/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003487///
Richard Smith7d137e32012-03-23 03:33:32 +00003488/// [C++11] enum-head '{' enumerator-list[opt] '}'
3489/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003490///
Richard Smith7d137e32012-03-23 03:33:32 +00003491/// enum-head: [C++11]
3492/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3493/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3494/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003495///
Richard Smith7d137e32012-03-23 03:33:32 +00003496/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003497/// 'enum'
3498/// 'enum' 'class'
3499/// 'enum' 'struct'
3500///
Richard Smith7d137e32012-03-23 03:33:32 +00003501/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003502/// ':' type-specifier-seq
3503///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003504/// [C++] elaborated-type-specifier:
3505/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3506///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003507void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003508 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003509 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003510 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003511 if (Tok.is(tok::code_completion)) {
3512 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003513 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003514 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003515 }
John McCallcb432fa2011-07-06 05:58:41 +00003516
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003517 // If attributes exist after tag, parse them.
3518 ParsedAttributesWithRange attrs(AttrFactory);
3519 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003520 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003521
3522 // If declspecs exist after tag, parse them.
3523 while (Tok.is(tok::kw___declspec))
3524 ParseMicrosoftDeclSpec(attrs);
3525
Richard Smith0f8ee222012-01-10 01:33:14 +00003526 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003527 bool IsScopedUsingClassTag = false;
3528
John McCallbeae29a2012-06-23 22:30:04 +00003529 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003530 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3531 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3532 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003533 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003534 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003535
Bill Wendling44426052012-12-20 19:22:21 +00003536 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003537 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003538 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003539
3540 // They are allowed afterwards, though.
3541 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003542 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003543 while (Tok.is(tok::kw___declspec))
3544 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003545 }
Richard Smith7d137e32012-03-23 03:33:32 +00003546
John McCall6347b682012-05-07 06:16:58 +00003547 // C++11 [temp.explicit]p12:
3548 // The usual access controls do not apply to names used to specify
3549 // explicit instantiations.
3550 // We extend this to also cover explicit specializations. Note that
3551 // we don't suppress if this turns out to be an elaborated type
3552 // specifier.
3553 bool shouldDelayDiagsInTag =
3554 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3555 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3556 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003557
Richard Smithbfdb1082012-03-12 08:56:40 +00003558 // Enum definitions should not be parsed in a trailing-return-type.
3559 bool AllowDeclaration = DSC != DSC_trailing;
3560
3561 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003562 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003563 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003564
Abramo Bagnarad7548482010-05-19 21:37:53 +00003565 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003566 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003567 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3568 // if a fixed underlying type is allowed.
3569 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003570
3571 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003572 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003573 return;
3574
3575 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003576 Diag(Tok, diag::err_expected_ident);
3577 if (Tok.isNot(tok::l_brace)) {
3578 // Has no name and is not a definition.
3579 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003580 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003581 return;
3582 }
3583 }
3584 }
Mike Stump11289f42009-09-09 15:08:12 +00003585
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003586 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003587 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003588 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003589 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003590
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003591 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003592 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003593 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003594 }
Mike Stump11289f42009-09-09 15:08:12 +00003595
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003596 // If an identifier is present, consume and remember it.
3597 IdentifierInfo *Name = 0;
3598 SourceLocation NameLoc;
3599 if (Tok.is(tok::identifier)) {
3600 Name = Tok.getIdentifierInfo();
3601 NameLoc = ConsumeToken();
3602 }
Mike Stump11289f42009-09-09 15:08:12 +00003603
Richard Smith0f8ee222012-01-10 01:33:14 +00003604 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003605 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3606 // declaration of a scoped enumeration.
3607 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003608 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003609 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003610 }
3611
John McCall6347b682012-05-07 06:16:58 +00003612 // Okay, end the suppression area. We'll decide whether to emit the
3613 // diagnostics in a second.
3614 if (shouldDelayDiagsInTag)
3615 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003616
Douglas Gregor0bf31402010-10-08 23:50:27 +00003617 TypeResult BaseType;
3618
Douglas Gregord1f69f62010-12-01 17:42:47 +00003619 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003620 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003621 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003622 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003623 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003624 // If we're in class scope, this can either be an enum declaration with
3625 // an underlying type, or a declaration of a bitfield member. We try to
3626 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003627 // (integer literal, sizeof); if it's still ambiguous, we then consider
3628 // anything that's a simple-type-specifier followed by '(' as an
3629 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003630 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003631 EnterExpressionEvaluationContext Unevaluated(Actions,
3632 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003633 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003634 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003635 // bit-field. This is the common case.
3636 if (TPR == TPResult::True())
3637 PossibleBitfield = true;
3638 // If the next token starts a type-specifier-seq, it may be either a
3639 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003640 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003641 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003642 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003643 GetLookAheadToken(2).getKind() == tok::semi) {
3644 // Consume the ':'.
3645 ConsumeToken();
3646 } else {
3647 // We have the start of a type-specifier-seq, so we have to perform
3648 // tentative parsing to determine whether we have an expression or a
3649 // type.
3650 TentativeParsingAction TPA(*this);
3651
3652 // Consume the ':'.
3653 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003654
3655 // If we see a type specifier followed by an open-brace, we have an
3656 // ambiguity between an underlying type and a C++11 braced
3657 // function-style cast. Resolve this by always treating it as an
3658 // underlying type.
3659 // FIXME: The standard is not entirely clear on how to disambiguate in
3660 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003661 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003662 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003663 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003664 // We'll parse this as a bitfield later.
3665 PossibleBitfield = true;
3666 TPA.Revert();
3667 } else {
3668 // We have a type-specifier-seq.
3669 TPA.Commit();
3670 }
3671 }
3672 } else {
3673 // Consume the ':'.
3674 ConsumeToken();
3675 }
3676
3677 if (!PossibleBitfield) {
3678 SourceRange Range;
3679 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003680
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003681 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003682 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003683 } else if (!getLangOpts().ObjC2) {
3684 if (getLangOpts().CPlusPlus)
3685 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3686 else
3687 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3688 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003689 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003690 }
3691
Richard Smith0f8ee222012-01-10 01:33:14 +00003692 // There are four options here. If we have 'friend enum foo;' then this is a
3693 // friend declaration, and cannot have an accompanying definition. If we have
3694 // 'enum foo;', then this is a forward declaration. If we have
3695 // 'enum foo {...' then this is a definition. Otherwise we have something
3696 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003697 //
3698 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3699 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3700 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3701 //
John McCallfaf5fb42010-08-26 23:41:50 +00003702 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003703 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003704 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003705 } else if (Tok.is(tok::l_brace)) {
3706 if (DS.isFriendSpecified()) {
3707 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3708 << SourceRange(DS.getFriendSpecLoc());
3709 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003710 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003711 TUK = Sema::TUK_Friend;
3712 } else {
3713 TUK = Sema::TUK_Definition;
3714 }
Richard Smith369b9f92012-06-25 21:37:02 +00003715 } else if (DSC != DSC_type_specifier &&
3716 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003717 (Tok.isAtStartOfLine() &&
3718 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003719 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3720 if (Tok.isNot(tok::semi)) {
3721 // A semicolon was missing after this declaration. Diagnose and recover.
3722 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3723 "enum");
3724 PP.EnterToken(Tok);
3725 Tok.setKind(tok::semi);
3726 }
John McCall6347b682012-05-07 06:16:58 +00003727 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003728 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003729 }
3730
3731 // If this is an elaborated type specifier, and we delayed
3732 // diagnostics before, just merge them into the current pool.
3733 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3734 diagsFromTag.redelay();
3735 }
Richard Smith7d137e32012-03-23 03:33:32 +00003736
3737 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003738 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003739 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003740 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003741 // Skip the rest of this declarator, up until the comma or semicolon.
3742 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003743 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003744 return;
3745 }
3746
3747 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3748 // Enumerations can't be explicitly instantiated.
3749 DS.SetTypeSpecError();
3750 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3751 return;
3752 }
3753
3754 assert(TemplateInfo.TemplateParams && "no template parameters");
3755 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3756 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003757 }
Chad Rosierc1183952012-06-26 22:30:43 +00003758
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003759 if (TUK == Sema::TUK_Reference)
3760 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003761
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003762 if (!Name && TUK != Sema::TUK_Definition) {
3763 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003764
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003765 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003766 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003767 return;
3768 }
Richard Smith7d137e32012-03-23 03:33:32 +00003769
Douglas Gregord6ab8742009-05-28 23:31:59 +00003770 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003771 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003772 const char *PrevSpec = 0;
3773 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003774 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003775 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003776 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003777 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003778 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003779
Douglas Gregorba41d012010-04-24 16:38:41 +00003780 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003781 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003782 // dependent tag.
3783 if (!Name) {
3784 DS.SetTypeSpecError();
3785 Diag(Tok, diag::err_expected_type_name_after_typename);
3786 return;
3787 }
Chad Rosierc1183952012-06-26 22:30:43 +00003788
Douglas Gregor0be31a22010-07-02 17:43:08 +00003789 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003790 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003791 NameLoc);
3792 if (Type.isInvalid()) {
3793 DS.SetTypeSpecError();
3794 return;
3795 }
Chad Rosierc1183952012-06-26 22:30:43 +00003796
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003797 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3798 NameLoc.isValid() ? NameLoc : StartLoc,
3799 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003800 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003801
Douglas Gregorba41d012010-04-24 16:38:41 +00003802 return;
3803 }
Mike Stump11289f42009-09-09 15:08:12 +00003804
John McCall48871652010-08-21 09:40:31 +00003805 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003806 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003807 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003808 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003809 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003810 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003811 }
Chad Rosierc1183952012-06-26 22:30:43 +00003812
Douglas Gregorba41d012010-04-24 16:38:41 +00003813 DS.SetTypeSpecError();
3814 return;
3815 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003816
Richard Smith369b9f92012-06-25 21:37:02 +00003817 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003818 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003819
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003820 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3821 NameLoc.isValid() ? NameLoc : StartLoc,
3822 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003823 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003824}
3825
Chris Lattnerc1915e22007-01-25 07:29:02 +00003826/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3827/// enumerator-list:
3828/// enumerator
3829/// enumerator-list ',' enumerator
3830/// enumerator:
3831/// enumeration-constant
3832/// enumeration-constant '=' constant-expression
3833/// enumeration-constant:
3834/// identifier
3835///
John McCall48871652010-08-21 09:40:31 +00003836void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003837 // Enter the scope of the enum body and start the definition.
3838 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003839 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003840
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003841 BalancedDelimiterTracker T(*this, tok::l_brace);
3842 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003843
Chris Lattner37256fb2007-08-27 17:24:30 +00003844 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003845 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003846 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003847
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003848 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003849
John McCall48871652010-08-21 09:40:31 +00003850 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003851
Chris Lattnerc1915e22007-01-25 07:29:02 +00003852 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003853 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003854 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3855 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003856
John McCall811a0f52010-10-22 23:36:17 +00003857 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003858 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003859 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003860 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003861 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003862
Chris Lattnerc1915e22007-01-25 07:29:02 +00003863 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003864 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003865 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003866
Chris Lattner76c72282007-10-09 17:33:22 +00003867 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003868 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003869 AssignedVal = ParseConstantExpression();
3870 if (AssignedVal.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003871 SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003872 }
Mike Stump11289f42009-09-09 15:08:12 +00003873
Chris Lattnerc1915e22007-01-25 07:29:02 +00003874 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003875 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3876 LastEnumConstDecl,
3877 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003878 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003879 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003880 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003881
Chris Lattner4ef40012007-06-11 01:28:17 +00003882 EnumConstantDecls.push_back(EnumConstDecl);
3883 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003884
Douglas Gregorce66d022010-09-07 14:51:08 +00003885 if (Tok.is(tok::identifier)) {
3886 // We're missing a comma between enumerators.
3887 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003888 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003889 << FixItHint::CreateInsertion(Loc, ", ");
3890 continue;
3891 }
Chad Rosierc1183952012-06-26 22:30:43 +00003892
Chris Lattner76c72282007-10-09 17:33:22 +00003893 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003894 break;
3895 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003896
Richard Smith5d164bc2011-10-15 05:09:34 +00003897 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003898 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003899 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3900 diag::ext_enumerator_list_comma_cxx :
3901 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003902 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003903 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003904 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3905 << FixItHint::CreateRemoval(CommaLoc);
3906 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003907 }
Mike Stump11289f42009-09-09 15:08:12 +00003908
Chris Lattnerc1915e22007-01-25 07:29:02 +00003909 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003910 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003911
Chris Lattnerc1915e22007-01-25 07:29:02 +00003912 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003913 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003914 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003915
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003916 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003917 EnumDecl, EnumConstantDecls,
3918 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003919 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003920
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003921 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003922 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3923 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003924
3925 // The next token must be valid after an enum definition. If not, a ';'
3926 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003927 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3928 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003929 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3930 // Push this token back into the preprocessor and change our current token
3931 // to ';' so that the rest of the code recovers as though there were an
3932 // ';' after the definition.
3933 PP.EnterToken(Tok);
3934 Tok.setKind(tok::semi);
3935 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003936}
Chris Lattner3b561a32006-08-13 00:12:11 +00003937
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003938/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003939/// start of a type-qualifier-list.
3940bool Parser::isTypeQualifier() const {
3941 switch (Tok.getKind()) {
3942 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003943
3944 // type-qualifier only in OpenCL
3945 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003946 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003947
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003948 // type-qualifier
3949 case tok::kw_const:
3950 case tok::kw_volatile:
3951 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003952 case tok::kw___private:
3953 case tok::kw___local:
3954 case tok::kw___global:
3955 case tok::kw___constant:
3956 case tok::kw___read_only:
3957 case tok::kw___read_write:
3958 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003959 return true;
3960 }
3961}
3962
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003963/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3964/// is definitely a type-specifier. Return false if it isn't part of a type
3965/// specifier or if we're not sure.
3966bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3967 switch (Tok.getKind()) {
3968 default: return false;
3969 // type-specifiers
3970 case tok::kw_short:
3971 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003972 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003973 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003974 case tok::kw_signed:
3975 case tok::kw_unsigned:
3976 case tok::kw__Complex:
3977 case tok::kw__Imaginary:
3978 case tok::kw_void:
3979 case tok::kw_char:
3980 case tok::kw_wchar_t:
3981 case tok::kw_char16_t:
3982 case tok::kw_char32_t:
3983 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003984 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003985 case tok::kw_float:
3986 case tok::kw_double:
3987 case tok::kw_bool:
3988 case tok::kw__Bool:
3989 case tok::kw__Decimal32:
3990 case tok::kw__Decimal64:
3991 case tok::kw__Decimal128:
3992 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003993
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003994 // OpenCL specific types:
3995 case tok::kw_image1d_t:
3996 case tok::kw_image1d_array_t:
3997 case tok::kw_image1d_buffer_t:
3998 case tok::kw_image2d_t:
3999 case tok::kw_image2d_array_t:
4000 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004001 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004002 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004003
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004004 // struct-or-union-specifier (C99) or class-specifier (C++)
4005 case tok::kw_class:
4006 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004007 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004008 case tok::kw_union:
4009 // enum-specifier
4010 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004011
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004012 // typedef-name
4013 case tok::annot_typename:
4014 return true;
4015 }
4016}
4017
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004018/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004019/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004020bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004021 switch (Tok.getKind()) {
4022 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004023
Chris Lattner020bab92009-01-04 23:41:41 +00004024 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004025 if (TryAltiVecVectorToken())
4026 return true;
4027 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004028 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004029 // Annotate typenames and C++ scope specifiers. If we get one, just
4030 // recurse to handle whatever we get.
4031 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004032 return true;
4033 if (Tok.is(tok::identifier))
4034 return false;
4035 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004036
Chris Lattner020bab92009-01-04 23:41:41 +00004037 case tok::coloncolon: // ::foo::bar
4038 if (NextToken().is(tok::kw_new) || // ::new
4039 NextToken().is(tok::kw_delete)) // ::delete
4040 return false;
4041
Chris Lattner020bab92009-01-04 23:41:41 +00004042 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004043 return true;
4044 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004045
Chris Lattnere37e2332006-08-15 04:50:22 +00004046 // GNU attributes support.
4047 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004048 // GNU typeof support.
4049 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004050
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004051 // type-specifiers
4052 case tok::kw_short:
4053 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004054 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004055 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004056 case tok::kw_signed:
4057 case tok::kw_unsigned:
4058 case tok::kw__Complex:
4059 case tok::kw__Imaginary:
4060 case tok::kw_void:
4061 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004062 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004063 case tok::kw_char16_t:
4064 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004065 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004066 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004067 case tok::kw_float:
4068 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004069 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004070 case tok::kw__Bool:
4071 case tok::kw__Decimal32:
4072 case tok::kw__Decimal64:
4073 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004074 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004075
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004076 // OpenCL specific types:
4077 case tok::kw_image1d_t:
4078 case tok::kw_image1d_array_t:
4079 case tok::kw_image1d_buffer_t:
4080 case tok::kw_image2d_t:
4081 case tok::kw_image2d_array_t:
4082 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004083 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004084 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004085
Chris Lattner861a2262008-04-13 18:59:07 +00004086 // struct-or-union-specifier (C99) or class-specifier (C++)
4087 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004088 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004089 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004090 case tok::kw_union:
4091 // enum-specifier
4092 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004093
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004094 // type-qualifier
4095 case tok::kw_const:
4096 case tok::kw_volatile:
4097 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004098
John McCallea0a39e2012-11-14 00:49:39 +00004099 // Debugger support.
4100 case tok::kw___unknown_anytype:
4101
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004102 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004103 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004104 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004105
Chris Lattner409bf7d2008-10-20 00:25:30 +00004106 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4107 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004108 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004109
Steve Naroff44ac7772008-12-25 14:16:32 +00004110 case tok::kw___cdecl:
4111 case tok::kw___stdcall:
4112 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004113 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004114 case tok::kw___w64:
4115 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004116 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004117 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004118 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004119
4120 case tok::kw___private:
4121 case tok::kw___local:
4122 case tok::kw___global:
4123 case tok::kw___constant:
4124 case tok::kw___read_only:
4125 case tok::kw___read_write:
4126 case tok::kw___write_only:
4127
Eli Friedman53339e02009-06-08 23:27:34 +00004128 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004129
4130 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004131 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00004132
Richard Smith8e1ac332013-03-28 01:55:44 +00004133 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004134 case tok::kw__Atomic:
4135 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004136 }
4137}
4138
Chris Lattneracd58a32006-08-06 17:24:14 +00004139/// isDeclarationSpecifier() - Return true if the current token is part of a
4140/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004141///
4142/// \param DisambiguatingWithExpression True to indicate that the purpose of
4143/// this check is to disambiguate between an expression and a declaration.
4144bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004145 switch (Tok.getKind()) {
4146 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004147
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004148 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004149 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004150
Chris Lattner020bab92009-01-04 23:41:41 +00004151 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004152 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004153 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004154 return false;
John Thompson22334602010-02-05 00:12:22 +00004155 if (TryAltiVecVectorToken())
4156 return true;
4157 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004158 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004159 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004160 // Annotate typenames and C++ scope specifiers. If we get one, just
4161 // recurse to handle whatever we get.
4162 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004163 return true;
4164 if (Tok.is(tok::identifier))
4165 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004166
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004167 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004168 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004169 // expression is permitted, then this is probably a class message send
4170 // missing the initial '['. In this case, we won't consider this to be
4171 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004172 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004173 isStartOfObjCClassMessageMissingOpenBracket())
4174 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004175
John McCall1f476a12010-02-26 08:45:28 +00004176 return isDeclarationSpecifier();
4177
Chris Lattner020bab92009-01-04 23:41:41 +00004178 case tok::coloncolon: // ::foo::bar
4179 if (NextToken().is(tok::kw_new) || // ::new
4180 NextToken().is(tok::kw_delete)) // ::delete
4181 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004182
Chris Lattner020bab92009-01-04 23:41:41 +00004183 // Annotate typenames and C++ scope specifiers. If we get one, just
4184 // recurse to handle whatever we get.
4185 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004186 return true;
4187 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004188
Chris Lattneracd58a32006-08-06 17:24:14 +00004189 // storage-class-specifier
4190 case tok::kw_typedef:
4191 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004192 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004193 case tok::kw_static:
4194 case tok::kw_auto:
4195 case tok::kw_register:
4196 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004197 case tok::kw_thread_local:
4198 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004199
Douglas Gregor26701a42011-09-09 02:06:17 +00004200 // Modules
4201 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004202
John McCallea0a39e2012-11-14 00:49:39 +00004203 // Debugger support
4204 case tok::kw___unknown_anytype:
4205
Chris Lattneracd58a32006-08-06 17:24:14 +00004206 // type-specifiers
4207 case tok::kw_short:
4208 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004209 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004210 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004211 case tok::kw_signed:
4212 case tok::kw_unsigned:
4213 case tok::kw__Complex:
4214 case tok::kw__Imaginary:
4215 case tok::kw_void:
4216 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004217 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004218 case tok::kw_char16_t:
4219 case tok::kw_char32_t:
4220
Chris Lattneracd58a32006-08-06 17:24:14 +00004221 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004222 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004223 case tok::kw_float:
4224 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004225 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004226 case tok::kw__Bool:
4227 case tok::kw__Decimal32:
4228 case tok::kw__Decimal64:
4229 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004230 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004231
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004232 // OpenCL specific types:
4233 case tok::kw_image1d_t:
4234 case tok::kw_image1d_array_t:
4235 case tok::kw_image1d_buffer_t:
4236 case tok::kw_image2d_t:
4237 case tok::kw_image2d_array_t:
4238 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004239 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004240 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004241
Chris Lattner861a2262008-04-13 18:59:07 +00004242 // struct-or-union-specifier (C99) or class-specifier (C++)
4243 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004244 case tok::kw_struct:
4245 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004246 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004247 // enum-specifier
4248 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004249
Chris Lattneracd58a32006-08-06 17:24:14 +00004250 // type-qualifier
4251 case tok::kw_const:
4252 case tok::kw_volatile:
4253 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004254
Chris Lattneracd58a32006-08-06 17:24:14 +00004255 // function-specifier
4256 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004257 case tok::kw_virtual:
4258 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004259 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004260
Richard Smith1dba27c2013-01-29 09:02:09 +00004261 // alignment-specifier
4262 case tok::kw__Alignas:
4263
Richard Smithd16fe122012-10-25 00:00:53 +00004264 // friend keyword.
4265 case tok::kw_friend:
4266
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004267 // static_assert-declaration
4268 case tok::kw__Static_assert:
4269
Chris Lattner599e47e2007-08-09 17:01:07 +00004270 // GNU typeof support.
4271 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004272
Chris Lattner599e47e2007-08-09 17:01:07 +00004273 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004274 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004275
Richard Smithd16fe122012-10-25 00:00:53 +00004276 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004277 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004278 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004279
Richard Smith8e1ac332013-03-28 01:55:44 +00004280 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004281 case tok::kw__Atomic:
4282 return true;
4283
Chris Lattner8b2ec162008-07-26 03:38:44 +00004284 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4285 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004286 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004287
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004288 // typedef-name
4289 case tok::annot_typename:
4290 return !DisambiguatingWithExpression ||
4291 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004292
Steve Narofff192fab2009-01-06 19:34:12 +00004293 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004294 case tok::kw___cdecl:
4295 case tok::kw___stdcall:
4296 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004297 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004298 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004299 case tok::kw___sptr:
4300 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004301 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004302 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004303 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004304 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004305 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004306
4307 case tok::kw___private:
4308 case tok::kw___local:
4309 case tok::kw___global:
4310 case tok::kw___constant:
4311 case tok::kw___read_only:
4312 case tok::kw___read_write:
4313 case tok::kw___write_only:
4314
Eli Friedman53339e02009-06-08 23:27:34 +00004315 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004316 }
4317}
4318
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004319bool Parser::isConstructorDeclarator() {
4320 TentativeParsingAction TPA(*this);
4321
4322 // Parse the C++ scope specifier.
4323 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004324 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004325 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004326 TPA.Revert();
4327 return false;
4328 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004329
4330 // Parse the constructor name.
4331 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4332 // We already know that we have a constructor name; just consume
4333 // the token.
4334 ConsumeToken();
4335 } else {
4336 TPA.Revert();
4337 return false;
4338 }
4339
Richard Smith43f340f2012-03-27 23:05:05 +00004340 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004341 if (Tok.isNot(tok::l_paren)) {
4342 TPA.Revert();
4343 return false;
4344 }
4345 ConsumeParen();
4346
Richard Smith43f340f2012-03-27 23:05:05 +00004347 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4348 // that we have a constructor.
4349 if (Tok.is(tok::r_paren) ||
4350 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004351 TPA.Revert();
4352 return true;
4353 }
4354
Richard Smithf2163662013-09-06 00:12:20 +00004355 // A C++11 attribute here signals that we have a constructor, and is an
4356 // attribute on the first constructor parameter.
4357 if (getLangOpts().CPlusPlus11 &&
4358 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4359 /*OuterMightBeMessageSend*/ true)) {
4360 TPA.Revert();
4361 return true;
4362 }
4363
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004364 // If we need to, enter the specified scope.
4365 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004366 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004367 DeclScopeObj.EnterDeclaratorScope();
4368
Francois Pichet79f3a872011-01-31 04:54:32 +00004369 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004370 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004371 MaybeParseMicrosoftAttributes(Attrs);
4372
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004373 // Check whether the next token(s) are part of a declaration
4374 // specifier, in which case we have the start of a parameter and,
4375 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004376 bool IsConstructor = false;
4377 if (isDeclarationSpecifier())
4378 IsConstructor = true;
4379 else if (Tok.is(tok::identifier) ||
4380 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4381 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4382 // This might be a parenthesized member name, but is more likely to
4383 // be a constructor declaration with an invalid argument type. Keep
4384 // looking.
4385 if (Tok.is(tok::annot_cxxscope))
4386 ConsumeToken();
4387 ConsumeToken();
4388
4389 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004390 // which must have one of the following syntactic forms (see the
4391 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004392 switch (Tok.getKind()) {
4393 case tok::l_paren:
4394 // C(X ( int));
4395 case tok::l_square:
4396 // C(X [ 5]);
4397 // C(X [ [attribute]]);
4398 case tok::coloncolon:
4399 // C(X :: Y);
4400 // C(X :: *p);
4401 case tok::r_paren:
4402 // C(X )
4403 // Assume this isn't a constructor, rather than assuming it's a
4404 // constructor with an unnamed parameter of an ill-formed type.
4405 break;
4406
4407 default:
4408 IsConstructor = true;
4409 break;
4410 }
4411 }
4412
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004413 TPA.Revert();
4414 return IsConstructor;
4415}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004416
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004417/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004418/// type-qualifier-list: [C99 6.7.5]
4419/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004420/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004421/// [ only if VendorAttributesAllowed=true ]
4422/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004423/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004424/// [ only if VendorAttributesAllowed=true ]
4425/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004426/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004427/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004428///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004429void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4430 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004431 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004432 bool AtomicAllowed,
4433 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004434 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004435 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004436 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004437 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004438 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004439 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004440
4441 SourceLocation EndLoc;
4442
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004443 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004444 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004445 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004446 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004447 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004448
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004449 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004450 case tok::code_completion:
4451 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004452 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004453
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004454 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004455 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004456 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004457 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004458 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004459 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004460 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004461 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004462 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004463 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004464 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004465 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004466 case tok::kw__Atomic:
4467 if (!AtomicAllowed)
4468 goto DoneWithTypeQuals;
4469 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4470 getLangOpts());
4471 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004472
4473 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004474 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004475 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004476 goto DoneWithTypeQuals;
4477 case tok::kw___private:
4478 case tok::kw___global:
4479 case tok::kw___local:
4480 case tok::kw___constant:
4481 case tok::kw___read_only:
4482 case tok::kw___write_only:
4483 case tok::kw___read_write:
4484 ParseOpenCLQualifiers(DS);
4485 break;
4486
Aaron Ballman317a77f2013-05-22 23:25:32 +00004487 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004488 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4489 // with the MS modifier keyword.
4490 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
4491 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi) &&
4492 PP.getSourceManager().isInSystemHeader(Loc)) {
4493 Tok.setKind(tok::identifier);
4494 continue;
4495 }
4496 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004497 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004498 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004499 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004500 case tok::kw___cdecl:
4501 case tok::kw___stdcall:
4502 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004503 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004504 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004505 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004506 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004507 continue;
4508 }
4509 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004510 case tok::kw___pascal:
4511 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004512 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004513 continue;
4514 }
4515 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004516 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004517 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004518 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004519 continue; // do *not* consume the next token!
4520 }
4521 // otherwise, FALL THROUGH!
4522 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004523 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004524 // If this is not a type-qualifier token, we're done reading type
4525 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004526 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004527 if (EndLoc.isValid())
4528 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004529 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004530 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004531
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004532 // If the specifier combination wasn't legal, issue a diagnostic.
4533 if (isInvalid) {
4534 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004535 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004536 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004537 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004538 }
4539}
4540
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004541
4542/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4543///
4544void Parser::ParseDeclarator(Declarator &D) {
4545 /// This implements the 'declarator' production in the C grammar, then checks
4546 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004547 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004548}
4549
Richard Smith0efa75c2012-03-29 01:16:42 +00004550static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4551 if (Kind == tok::star || Kind == tok::caret)
4552 return true;
4553
4554 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4555 if (!Lang.CPlusPlus)
4556 return false;
4557
4558 return Kind == tok::amp || Kind == tok::ampamp;
4559}
4560
Sebastian Redlbd150f42008-11-21 19:14:01 +00004561/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4562/// is parsed by the function passed to it. Pass null, and the direct-declarator
4563/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004564/// ptr-operator production.
4565///
Richard Smith09f76ee2011-10-19 21:33:05 +00004566/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004567/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4568/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004569///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004570/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4571/// [C] pointer[opt] direct-declarator
4572/// [C++] direct-declarator
4573/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004574///
4575/// pointer: [C99 6.7.5]
4576/// '*' type-qualifier-list[opt]
4577/// '*' type-qualifier-list[opt] pointer
4578///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004579/// ptr-operator:
4580/// '*' cv-qualifier-seq[opt]
4581/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004582/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004583/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004584/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004585/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004586void Parser::ParseDeclaratorInternal(Declarator &D,
4587 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004588 if (Diags.hasAllExtensionsSilenced())
4589 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004590
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004591 // C++ member pointers start with a '::' or a nested-name.
4592 // Member pointers get special handling, since there's no place for the
4593 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004594 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004595 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4596 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004597 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4598 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004599 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004600 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004601
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004602 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004603 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004604 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004605 if (D.mayHaveIdentifier())
4606 D.getCXXScopeSpec() = SS;
4607 else
4608 AnnotateScopeToken(SS, true);
4609
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004610 if (DirectDeclParser)
4611 (this->*DirectDeclParser)(D);
4612 return;
4613 }
4614
4615 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004616 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004617 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004618 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004619 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004620
4621 // Recurse to parse whatever is left.
4622 ParseDeclaratorInternal(D, DirectDeclParser);
4623
4624 // Sema will have to catch (syntactically invalid) pointers into global
4625 // scope. It has to catch pointers into namespace scope anyway.
4626 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004627 Loc),
4628 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004629 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004630 return;
4631 }
4632 }
4633
4634 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004635 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004636 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004637 if (DirectDeclParser)
4638 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004639 return;
4640 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004641
Sebastian Redled0f3b02009-03-15 22:02:01 +00004642 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4643 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004644 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004645 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004646
Chris Lattner9eac9312009-03-27 04:18:06 +00004647 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004648 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004649 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004650
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004651 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004652 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004653 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004654
Bill Wendling3708c182007-05-27 10:15:43 +00004655 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004656 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004657 if (Kind == tok::star)
4658 // Remember that we parsed a pointer type, and remember the type-quals.
4659 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004660 DS.getConstSpecLoc(),
4661 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004662 DS.getRestrictSpecLoc()),
4663 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004664 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004665 else
4666 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004667 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004668 Loc),
4669 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004670 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004671 } else {
4672 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004673 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004674
Sebastian Redl3b27be62009-03-23 00:00:23 +00004675 // Complain about rvalue references in C++03, but then go on and build
4676 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004677 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004678 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004679 diag::warn_cxx98_compat_rvalue_reference :
4680 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004681
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004682 // GNU-style and C++11 attributes are allowed here, as is restrict.
4683 ParseTypeQualifierListOpt(DS);
4684 D.ExtendWithDeclSpec(DS);
4685
Bill Wendling93efb222007-06-02 23:28:54 +00004686 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4687 // cv-qualifiers are introduced through the use of a typedef or of a
4688 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004689 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4690 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4691 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004692 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004693 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4694 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004695 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004696 // 'restrict' is permitted as an extension.
4697 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4698 Diag(DS.getAtomicSpecLoc(),
4699 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004700 }
Bill Wendling3708c182007-05-27 10:15:43 +00004701
4702 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004703 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004704
Douglas Gregor66583c52008-11-03 15:51:28 +00004705 if (D.getNumTypeObjects() > 0) {
4706 // C++ [dcl.ref]p4: There shall be no references to references.
4707 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4708 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004709 if (const IdentifierInfo *II = D.getIdentifier())
4710 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4711 << II;
4712 else
4713 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4714 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004715
Sebastian Redlbd150f42008-11-21 19:14:01 +00004716 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004717 // can go ahead and build the (technically ill-formed)
4718 // declarator: reference collapsing will take care of it.
4719 }
4720 }
4721
Richard Smith8e1ac332013-03-28 01:55:44 +00004722 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004723 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004724 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004725 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004726 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004727 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004728}
4729
Richard Smith0efa75c2012-03-29 01:16:42 +00004730static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4731 SourceLocation EllipsisLoc) {
4732 if (EllipsisLoc.isValid()) {
4733 FixItHint Insertion;
4734 if (!D.getEllipsisLoc().isValid()) {
4735 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4736 D.setEllipsisLoc(EllipsisLoc);
4737 }
4738 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4739 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4740 }
4741}
4742
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004743/// ParseDirectDeclarator
4744/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004745/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004746/// '(' declarator ')'
4747/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004748/// [C90] direct-declarator '[' constant-expression[opt] ']'
4749/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4750/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4751/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4752/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004753/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4754/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004755/// direct-declarator '(' parameter-type-list ')'
4756/// direct-declarator '(' identifier-list[opt] ')'
4757/// [GNU] direct-declarator '(' parameter-forward-declarations
4758/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004759/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4760/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004761/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4762/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4763/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004764/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004765/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004766///
4767/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004768/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004769/// '::'[opt] nested-name-specifier[opt] type-name
4770///
4771/// id-expression: [C++ 5.1]
4772/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004773/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004774///
4775/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004776/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004777/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004778/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004779/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004780/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004781///
Richard Smith1453e312012-03-27 01:42:32 +00004782/// Note, any additional constructs added here may need corresponding changes
4783/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004784void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004785 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004786
David Blaikiebbafb8a2012-03-11 07:00:24 +00004787 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004788 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004789 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004790 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4791 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004792 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004793 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004794 }
4795
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004796 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004797 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004798 // Change the declaration context for name lookup, until this function
4799 // is exited (and the declarator has been parsed).
4800 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004801 }
4802
Douglas Gregor27b4c162010-12-23 22:44:42 +00004803 // C++0x [dcl.fct]p14:
4804 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004805 // of a parameter-declaration-clause without a preceding comma. In
4806 // this case, the ellipsis is parsed as part of the
4807 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004808 // parameter pack that has not been expanded; otherwise, it is parsed
4809 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004810 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004811 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004812 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004813 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004814 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004815 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004816 !Actions.containsUnexpandedParameterPacks(D))) {
4817 SourceLocation EllipsisLoc = ConsumeToken();
4818 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4819 // The ellipsis was put in the wrong place. Recover, and explain to
4820 // the user what they should have done.
4821 ParseDeclarator(D);
4822 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4823 return;
4824 } else
4825 D.setEllipsisLoc(EllipsisLoc);
4826
4827 // The ellipsis can't be followed by a parenthesized declarator. We
4828 // check for that in ParseParenDeclarator, after we have disambiguated
4829 // the l_paren token.
4830 }
4831
Douglas Gregor7861a802009-11-03 01:35:08 +00004832 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4833 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4834 // We found something that indicates the start of an unqualified-id.
4835 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004836 bool AllowConstructorName;
4837 if (D.getDeclSpec().hasTypeSpecifier())
4838 AllowConstructorName = false;
4839 else if (D.getCXXScopeSpec().isSet())
4840 AllowConstructorName =
4841 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004842 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004843 else
4844 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4845
Abramo Bagnara7945c982012-01-27 09:46:47 +00004846 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004847 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4848 /*EnteringContext=*/true,
4849 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004850 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004851 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004852 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004853 D.getName()) ||
4854 // Once we're past the identifier, if the scope was bad, mark the
4855 // whole declarator bad.
4856 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004857 D.SetIdentifier(0, Tok.getLocation());
4858 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004859 } else {
4860 // Parsed the unqualified-id; update range information and move along.
4861 if (D.getSourceRange().getBegin().isInvalid())
4862 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4863 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004864 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004865 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004866 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004867 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004868 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004869 "There's a C++-specific check for tok::identifier above");
4870 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4871 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4872 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004873 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004874 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004875 // A virt-specifier isn't treated as an identifier if it appears after a
4876 // trailing-return-type.
4877 if (D.getContext() != Declarator::TrailingReturnContext ||
4878 !isCXX11VirtSpecifier(Tok)) {
4879 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4880 << FixItHint::CreateRemoval(Tok.getLocation());
4881 D.SetIdentifier(0, Tok.getLocation());
4882 ConsumeToken();
4883 goto PastIdentifier;
4884 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004885 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004886
Douglas Gregor7861a802009-11-03 01:35:08 +00004887 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004888 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004889 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004890 // Example: 'char (*X)' or 'int (*XX)(void)'
4891 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004892
4893 // If the declarator was parenthesized, we entered the declarator
4894 // scope when parsing the parenthesized declarator, then exited
4895 // the scope already. Re-enter the scope, if we need to.
4896 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004897 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004898 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004899 if (!D.isInvalidType() &&
4900 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004901 // Change the declaration context for name lookup, until this function
4902 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004903 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004904 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004905 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004906 // This could be something simple like "int" (in which case the declarator
4907 // portion is empty), if an abstract-declarator is allowed.
4908 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004909
4910 // The grammar for abstract-pack-declarator does not allow grouping parens.
4911 // FIXME: Revisit this once core issue 1488 is resolved.
4912 if (D.hasEllipsis() && D.hasGroupingParens())
4913 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4914 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004915 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004916 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004917 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004918 if (D.getContext() == Declarator::MemberContext)
4919 Diag(Tok, diag::err_expected_member_name_or_semi)
4920 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004921 else if (getLangOpts().CPlusPlus) {
4922 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4923 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004924 else {
4925 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4926 if (Tok.isAtStartOfLine() && Loc.isValid())
4927 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4928 << getLangOpts().CPlusPlus;
4929 else
4930 Diag(Tok, diag::err_expected_unqualified_id)
4931 << getLangOpts().CPlusPlus;
4932 }
Richard Trieu9c672672013-01-26 02:31:38 +00004933 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004934 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004935 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004936 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004937 }
Mike Stump11289f42009-09-09 15:08:12 +00004938
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004939 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004940 assert(D.isPastIdentifier() &&
4941 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004942
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004943 // Don't parse attributes unless we have parsed an unparenthesized name.
4944 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004945 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004946
Chris Lattneracd58a32006-08-06 17:24:14 +00004947 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004948 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004949 // Enter function-declaration scope, limiting any declarators to the
4950 // function prototype scope, including parameter declarators.
4951 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004952 Scope::FunctionPrototypeScope|Scope::DeclScope|
4953 (D.isFunctionDeclaratorAFunctionDeclaration()
4954 ? Scope::FunctionDeclarationScope : 0));
4955
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004956 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4957 // In such a case, check if we actually have a function declarator; if it
4958 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004959 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004960 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4961 // The name of the declarator, if any, is tentatively declared within
4962 // a possible direct initializer.
4963 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4964 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4965 TentativelyDeclaredIdentifiers.pop_back();
4966 if (!IsFunctionDecl)
4967 break;
4968 }
John McCall084e83d2011-03-24 11:26:52 +00004969 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004970 BalancedDelimiterTracker T(*this, tok::l_paren);
4971 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004972 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004973 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004974 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004975 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004976 } else {
4977 break;
4978 }
4979 }
Chad Rosierc1183952012-06-26 22:30:43 +00004980}
Chris Lattneracd58a32006-08-06 17:24:14 +00004981
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004982/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4983/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004984/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004985/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4986///
4987/// direct-declarator:
4988/// '(' declarator ')'
4989/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004990/// direct-declarator '(' parameter-type-list ')'
4991/// direct-declarator '(' identifier-list[opt] ')'
4992/// [GNU] direct-declarator '(' parameter-forward-declarations
4993/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004994///
4995void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004996 BalancedDelimiterTracker T(*this, tok::l_paren);
4997 T.consumeOpen();
4998
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004999 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00005000
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005001 // Eat any attributes before we look at whether this is a grouping or function
5002 // declarator paren. If this is a grouping paren, the attribute applies to
5003 // the type being built up, for example:
5004 // int (__attribute__(()) *x)(long y)
5005 // If this ends up not being a grouping paren, the attribute applies to the
5006 // first argument, for example:
5007 // int (__attribute__(()) int x)
5008 // In either case, we need to eat any attributes to be able to determine what
5009 // sort of paren this is.
5010 //
John McCall084e83d2011-03-24 11:26:52 +00005011 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005012 bool RequiresArg = false;
5013 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005014 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005015
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005016 // We require that the argument list (if this is a non-grouping paren) be
5017 // present even if the attribute list was empty.
5018 RequiresArg = true;
5019 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005020
Steve Naroff44ac7772008-12-25 14:16:32 +00005021 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005022 ParseMicrosoftTypeAttributes(attrs);
5023
Dawn Perchik335e16b2010-09-03 01:29:35 +00005024 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005025 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005026 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005027
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005028 // If we haven't past the identifier yet (or where the identifier would be
5029 // stored, if this is an abstract declarator), then this is probably just
5030 // grouping parens. However, if this could be an abstract-declarator, then
5031 // this could also be the start of function arguments (consider 'void()').
5032 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005033
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005034 if (!D.mayOmitIdentifier()) {
5035 // If this can't be an abstract-declarator, this *must* be a grouping
5036 // paren, because we haven't seen the identifier yet.
5037 isGrouping = true;
5038 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005039 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5040 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005041 isDeclarationSpecifier() || // 'int(int)' is a function.
5042 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005043 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5044 // considered to be a type, not a K&R identifier-list.
5045 isGrouping = false;
5046 } else {
5047 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5048 isGrouping = true;
5049 }
Mike Stump11289f42009-09-09 15:08:12 +00005050
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005051 // If this is a grouping paren, handle:
5052 // direct-declarator: '(' declarator ')'
5053 // direct-declarator: '(' attributes declarator ')'
5054 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005055 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5056 D.setEllipsisLoc(SourceLocation());
5057
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005058 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005059 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005060 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005061 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005062 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005063 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005064 T.getCloseLocation()),
5065 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005066
5067 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005068
5069 // An ellipsis cannot be placed outside parentheses.
5070 if (EllipsisLoc.isValid())
5071 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5072
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005073 return;
5074 }
Mike Stump11289f42009-09-09 15:08:12 +00005075
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005076 // Okay, if this wasn't a grouping paren, it must be the start of a function
5077 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005078 // identifier (and remember where it would have been), then call into
5079 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005080 D.SetIdentifier(0, Tok.getLocation());
5081
David Blaikie15a430a2011-12-04 05:04:18 +00005082 // Enter function-declaration scope, limiting any declarators to the
5083 // function prototype scope, including parameter declarators.
5084 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005085 Scope::FunctionPrototypeScope | Scope::DeclScope |
5086 (D.isFunctionDeclaratorAFunctionDeclaration()
5087 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005088 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005089 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005090}
5091
5092/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5093/// declarator D up to a paren, which indicates that we are parsing function
5094/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005095///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005096/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5097/// immediately after the open paren - they should be considered to be the
5098/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005099///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005100/// If RequiresArg is true, then the first argument of the function is required
5101/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005102///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005103/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5104/// (C++11) ref-qualifier[opt], exception-specification[opt],
5105/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5106///
5107/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005108/// dynamic-exception-specification
5109/// noexcept-specification
5110///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005111void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005112 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005113 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005114 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005115 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005116 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005117 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005118 // lparen is already consumed!
5119 assert(D.isPastIdentifier() && "Should not call before identifier!");
5120
5121 // This should be true when the function has typed arguments.
5122 // Otherwise, it is treated as a K&R-style function.
5123 bool HasProto = false;
5124 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005125 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005126 // Remember where we see an ellipsis, if any.
5127 SourceLocation EllipsisLoc;
5128
5129 DeclSpec DS(AttrFactory);
5130 bool RefQualifierIsLValueRef = true;
5131 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005132 SourceLocation ConstQualifierLoc;
5133 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005134 ExceptionSpecificationType ESpecType = EST_None;
5135 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005136 SmallVector<ParsedType, 2> DynamicExceptions;
5137 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005138 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005139 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005140 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005141
James Molloy6f8780b2012-02-29 10:24:19 +00005142 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005143 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5144 EndLoc is the end location for the function declarator.
5145 They differ for trailing return types. */
5146 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005147 SourceLocation LParenLoc, RParenLoc;
5148 LParenLoc = Tracker.getOpenLocation();
5149 StartLoc = LParenLoc;
5150
Douglas Gregor9e66af42011-07-05 16:44:18 +00005151 if (isFunctionDeclaratorIdentifierList()) {
5152 if (RequiresArg)
5153 Diag(Tok, diag::err_argument_required_after_attribute);
5154
5155 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5156
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005157 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005158 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005159 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005160 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005161 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005162 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005163 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5164 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005165 else if (RequiresArg)
5166 Diag(Tok, diag::err_argument_required_after_attribute);
5167
David Blaikiebbafb8a2012-03-11 07:00:24 +00005168 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005169
5170 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005171 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005172 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005173 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005174 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005175
David Blaikiebbafb8a2012-03-11 07:00:24 +00005176 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005177 // FIXME: Accept these components in any order, and produce fixits to
5178 // correct the order if the user gets it wrong. Ideally we should deal
5179 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005180
5181 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005182 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5183 /*CXX11AttributesAllowed*/ false,
5184 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005185 if (!DS.getSourceRange().getEnd().isInvalid()) {
5186 EndLoc = DS.getSourceRange().getEnd();
5187 ConstQualifierLoc = DS.getConstSpecLoc();
5188 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5189 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005190
5191 // Parse ref-qualifier[opt].
5192 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005193 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005194 diag::warn_cxx98_compat_ref_qualifier :
5195 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005196
Douglas Gregor9e66af42011-07-05 16:44:18 +00005197 RefQualifierIsLValueRef = Tok.is(tok::amp);
5198 RefQualifierLoc = ConsumeToken();
5199 EndLoc = RefQualifierLoc;
5200 }
5201
Douglas Gregor3024f072012-04-16 07:05:22 +00005202 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005203 // If a declaration declares a member function or member function
5204 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005205 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005206 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005207 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005208 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005209 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005210 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005211 (D.getContext() == Declarator::MemberContext
5212 ? !D.getDeclSpec().isFriendSpecified()
5213 : D.getContext() == Declarator::FileContext &&
5214 D.getCXXScopeSpec().isValid() &&
5215 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005216 Sema::CXXThisScopeRAII ThisScope(Actions,
5217 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005218 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005219 (D.getDeclSpec().isConstexprSpecified() &&
5220 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005221 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005222 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005223
Douglas Gregor9e66af42011-07-05 16:44:18 +00005224 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005225 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005226 DynamicExceptions,
5227 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005228 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005229 if (ESpecType != EST_None)
5230 EndLoc = ESpecRange.getEnd();
5231
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005232 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5233 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005234 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005235
Douglas Gregor9e66af42011-07-05 16:44:18 +00005236 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005237 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005238 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005239 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005240 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5241 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005242 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005243 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005244 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005245 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005246 }
5247 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005248 }
5249
5250 // Remember that we parsed a function type, and remember the attributes.
5251 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005252 IsAmbiguous,
5253 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005254 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005255 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005256 DS.getTypeQualifiers(),
5257 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005258 RefQualifierLoc, ConstQualifierLoc,
5259 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005260 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005261 ESpecType, ESpecRange.getBegin(),
5262 DynamicExceptions.data(),
5263 DynamicExceptionRanges.data(),
5264 DynamicExceptions.size(),
5265 NoexceptExpr.isUsable() ?
5266 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005267 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005268 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005269 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005270
5271 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005272}
5273
5274/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5275/// identifier list form for a K&R-style function: void foo(a,b,c)
5276///
5277/// Note that identifier-lists are only allowed for normal declarators, not for
5278/// abstract-declarators.
5279bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005280 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005281 && Tok.is(tok::identifier)
5282 && !TryAltiVecVectorToken()
5283 // K&R identifier lists can't have typedefs as identifiers, per C99
5284 // 6.7.5.3p11.
5285 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5286 // Identifier lists follow a really simple grammar: the identifiers can
5287 // be followed *only* by a ", identifier" or ")". However, K&R
5288 // identifier lists are really rare in the brave new modern world, and
5289 // it is very common for someone to typo a type in a non-K&R style
5290 // list. If we are presented with something like: "void foo(intptr x,
5291 // float y)", we don't want to start parsing the function declarator as
5292 // though it is a K&R style declarator just because intptr is an
5293 // invalid type.
5294 //
5295 // To handle this, we check to see if the token after the first
5296 // identifier is a "," or ")". Only then do we parse it as an
5297 // identifier list.
5298 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5299}
5300
5301/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5302/// we found a K&R-style identifier list instead of a typed parameter list.
5303///
5304/// After returning, ParamInfo will hold the parsed parameters.
5305///
5306/// identifier-list: [C99 6.7.5]
5307/// identifier
5308/// identifier-list ',' identifier
5309///
5310void Parser::ParseFunctionDeclaratorIdentifierList(
5311 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005312 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005313 // If there was no identifier specified for the declarator, either we are in
5314 // an abstract-declarator, or we are in a parameter declarator which was found
5315 // to be abstract. In abstract-declarators, identifier lists are not valid:
5316 // diagnose this.
5317 if (!D.getIdentifier())
5318 Diag(Tok, diag::ext_ident_list_in_param);
5319
5320 // Maintain an efficient lookup of params we have seen so far.
5321 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5322
5323 while (1) {
5324 // If this isn't an identifier, report the error and skip until ')'.
5325 if (Tok.isNot(tok::identifier)) {
5326 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005327 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005328 // Forget we parsed anything.
5329 ParamInfo.clear();
5330 return;
5331 }
5332
5333 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5334
5335 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5336 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5337 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5338
5339 // Verify that the argument identifier has not already been mentioned.
5340 if (!ParamsSoFar.insert(ParmII)) {
5341 Diag(Tok, diag::err_param_redefinition) << ParmII;
5342 } else {
5343 // Remember this identifier in ParamInfo.
5344 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5345 Tok.getLocation(),
5346 0));
5347 }
5348
5349 // Eat the identifier.
5350 ConsumeToken();
5351
5352 // The list continues if we see a comma.
5353 if (Tok.isNot(tok::comma))
5354 break;
5355 ConsumeToken();
5356 }
5357}
5358
5359/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5360/// after the opening parenthesis. This function will not parse a K&R-style
5361/// identifier list.
5362///
Richard Smith2620cd92012-04-11 04:01:28 +00005363/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5364/// caller parsed those arguments immediately after the open paren - they should
5365/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005366///
5367/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5368/// be the location of the ellipsis, if any was parsed.
5369///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005370/// parameter-type-list: [C99 6.7.5]
5371/// parameter-list
5372/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005373/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005374///
5375/// parameter-list: [C99 6.7.5]
5376/// parameter-declaration
5377/// parameter-list ',' parameter-declaration
5378///
5379/// parameter-declaration: [C99 6.7.5]
5380/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005381/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005382/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005383/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005384/// declaration-specifiers abstract-declarator[opt]
5385/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005386/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005387/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005388/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005389///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005390void Parser::ParseParameterDeclarationClause(
5391 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005392 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005393 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005394 SourceLocation &EllipsisLoc) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005395 while (1) {
5396 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005397 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5398 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005399 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005400 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005401 }
Mike Stump11289f42009-09-09 15:08:12 +00005402
Chris Lattner371ed4e2008-04-06 06:57:35 +00005403 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005404 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005405 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005406
Richard Smith2620cd92012-04-11 04:01:28 +00005407 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005408 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005409
John McCall53fa7142010-12-24 02:08:15 +00005410 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005411 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005412
5413 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005414
5415 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005416 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005417 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005418 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5419 // too much hassle.
5420 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005421
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005422 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005423
Faisal Vali2b391ab2013-09-26 19:54:12 +00005424
5425 // Parse the declarator. This is "PrototypeContext" or
5426 // "LambdaExprParameterContext", because we must accept either
5427 // 'declarator' or 'abstract-declarator' here.
5428 Declarator ParmDeclarator(DS,
5429 D.getContext() == Declarator::LambdaExprContext ?
5430 Declarator::LambdaExprParameterContext :
5431 Declarator::PrototypeContext);
5432 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005433
5434 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005435 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005436
Chris Lattner371ed4e2008-04-06 06:57:35 +00005437 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005438 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005439
Douglas Gregor4d87df52008-12-16 21:30:33 +00005440 // DefArgToks is used when the parsing of default arguments needs
5441 // to be delayed.
5442 CachedTokens *DefArgToks = 0;
5443
Chris Lattner371ed4e2008-04-06 06:57:35 +00005444 // If no parameter was specified, verify that *something* was specified,
5445 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005446 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5447 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005448 // Completely missing, emit error.
5449 Diag(DSStart, diag::err_missing_param);
5450 } else {
5451 // Otherwise, we have something. Add it and let semantic analysis try
5452 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005453
Chris Lattner371ed4e2008-04-06 06:57:35 +00005454 // Inform the actions module about the parameter declarator, so it gets
5455 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005456 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5457 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005458 // Parse the default argument, if any. We parse the default
5459 // arguments in all dialects; the semantic analysis in
5460 // ActOnParamDefaultArgument will reject the default argument in
5461 // C.
5462 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005463 SourceLocation EqualLoc = Tok.getLocation();
5464
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005465 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005466 if (D.getContext() == Declarator::MemberContext) {
5467 // If we're inside a class definition, cache the tokens
5468 // corresponding to the default argument. We'll actually parse
5469 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005470 // FIXME: Can we use a smart pointer for Toks?
5471 DefArgToks = new CachedTokens;
5472
Richard Smith1fff95c2013-09-12 23:28:08 +00005473 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005474 delete DefArgToks;
5475 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005476 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005477 } else {
5478 // Mark the end of the default argument so that we know when to
5479 // stop when we parse it later on.
5480 Token DefArgEnd;
5481 DefArgEnd.startToken();
5482 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5483 DefArgEnd.setLocation(Tok.getLocation());
5484 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005485 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005486 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005487 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005488 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005489 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005490 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005491
Chad Rosierc1183952012-06-26 22:30:43 +00005492 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005493 // used.
5494 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005495 Sema::PotentiallyEvaluatedIfUsed,
5496 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005497
Sebastian Redldb63af22012-03-14 15:54:00 +00005498 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005499 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005500 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005501 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005502 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005503 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005504 if (DefArgResult.isInvalid()) {
5505 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005506 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005507 } else {
5508 // Inform the actions module about the default argument
5509 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005510 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005511 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005512 }
5513 }
Mike Stump11289f42009-09-09 15:08:12 +00005514
5515 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005516 ParmDeclarator.getIdentifierLoc(),
5517 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005518 }
5519
5520 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005521 if (Tok.isNot(tok::comma)) {
5522 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005523 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005524
David Blaikiebbafb8a2012-03-11 07:00:24 +00005525 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005526 // We have ellipsis without a preceding ',', which is ill-formed
5527 // in C. Complain and provide the fix.
5528 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005529 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005530 }
5531 }
Chad Rosierc1183952012-06-26 22:30:43 +00005532
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005533 break;
5534 }
Mike Stump11289f42009-09-09 15:08:12 +00005535
Chris Lattner371ed4e2008-04-06 06:57:35 +00005536 // Consume the comma.
5537 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005538 }
Mike Stump11289f42009-09-09 15:08:12 +00005539
Chris Lattner6c940e62008-04-06 06:34:08 +00005540}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005541
Chris Lattnere8074e62006-08-06 18:30:15 +00005542/// [C90] direct-declarator '[' constant-expression[opt] ']'
5543/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5544/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5545/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5546/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005547/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5548/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005549void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005550 if (CheckProhibitedCXX11Attribute())
5551 return;
5552
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005553 BalancedDelimiterTracker T(*this, tok::l_square);
5554 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005555
Chris Lattner84a11622008-12-18 07:27:21 +00005556 // C array syntax has many features, but by-far the most common is [] and [4].
5557 // This code does a fast path to handle some of the most obvious cases.
5558 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005559 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005560 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005561 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005562
Chris Lattner84a11622008-12-18 07:27:21 +00005563 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005564 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005565 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005566 T.getOpenLocation(),
5567 T.getCloseLocation()),
5568 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005569 return;
5570 } else if (Tok.getKind() == tok::numeric_constant &&
5571 GetLookAheadToken(1).is(tok::r_square)) {
5572 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005573 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005574 ConsumeToken();
5575
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005576 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005577 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005578 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005579
Chris Lattner84a11622008-12-18 07:27:21 +00005580 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005581 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005582 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005583 T.getOpenLocation(),
5584 T.getCloseLocation()),
5585 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005586 return;
5587 }
Mike Stump11289f42009-09-09 15:08:12 +00005588
Chris Lattnere8074e62006-08-06 18:30:15 +00005589 // If valid, this location is the position where we read the 'static' keyword.
5590 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005591 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005592 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005593
Chris Lattnere8074e62006-08-06 18:30:15 +00005594 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005595 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005596 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005597 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005598
Chris Lattnere8074e62006-08-06 18:30:15 +00005599 // If we haven't already read 'static', check to see if there is one after the
5600 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005601 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005602 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005603
Chris Lattnere8074e62006-08-06 18:30:15 +00005604 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005605 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005606 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005607
Chris Lattner521ff2b2008-04-06 05:26:30 +00005608 // Handle the case where we have '[*]' as the array size. However, a leading
5609 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005610 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005611 // infrequent, use of lookahead is not costly here.
5612 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005613 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005614
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005615 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005616 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005617 StaticLoc = SourceLocation(); // Drop the static.
5618 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005619 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005620 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005621 // Note, in C89, this production uses the constant-expr production instead
5622 // of assignment-expr. The only difference is that assignment-expr allows
5623 // things like '=' and '*='. Sema rejects these in C89 mode because they
5624 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005625
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005626 // Parse the constant-expression or assignment-expression now (depending
5627 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005628 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005629 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005630 } else {
5631 EnterExpressionEvaluationContext Unevaluated(Actions,
5632 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005633 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005634 }
Chris Lattner62591722006-08-12 18:40:58 +00005635 }
Mike Stump11289f42009-09-09 15:08:12 +00005636
Chris Lattner62591722006-08-12 18:40:58 +00005637 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005638 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005639 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005640 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005641 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005642 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005643 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005644
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005645 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005646
John McCall084e83d2011-03-24 11:26:52 +00005647 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005648 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005649
Chris Lattner84a11622008-12-18 07:27:21 +00005650 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005651 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005652 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005653 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005654 T.getOpenLocation(),
5655 T.getCloseLocation()),
5656 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005657}
5658
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005659/// [GNU] typeof-specifier:
5660/// typeof ( expressions )
5661/// typeof ( type-name )
5662/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005663///
5664void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005665 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005666 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005667 SourceLocation StartLoc = ConsumeToken();
5668
John McCalle8595032010-01-13 20:03:27 +00005669 const bool hasParens = Tok.is(tok::l_paren);
5670
Eli Friedman15681d62012-09-26 04:34:21 +00005671 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5672 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005673
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005674 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005675 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005676 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005677 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5678 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005679 if (hasParens)
5680 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005681
5682 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005683 // FIXME: Not accurate, the range gets one token more than it should.
5684 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005685 else
5686 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005687
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005688 if (isCastExpr) {
5689 if (!CastTy) {
5690 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005691 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005692 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005693
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005694 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005695 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005696 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5697 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005698 DiagID, CastTy))
5699 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005700 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005701 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005702
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005703 // If we get here, the operand to the typeof was an expresion.
5704 if (Operand.isInvalid()) {
5705 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005706 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005707 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005708
Eli Friedmane0afc982012-01-21 01:01:51 +00005709 // We might need to transform the operand if it is potentially evaluated.
5710 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5711 if (Operand.isInvalid()) {
5712 DS.SetTypeSpecError();
5713 return;
5714 }
5715
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005716 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005717 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005718 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5719 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005720 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005721 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005722}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005723
Benjamin Kramere56f3932011-12-23 17:00:35 +00005724/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005725/// _Atomic ( type-name )
5726///
5727void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005728 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5729 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005730
5731 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005732 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005733 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005734 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005735
5736 TypeResult Result = ParseTypeName();
5737 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005738 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005739 return;
5740 }
5741
5742 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005743 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005744
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005745 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005746 return;
5747
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005748 DS.setTypeofParensRange(T.getRange());
5749 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005750
5751 const char *PrevSpec = 0;
5752 unsigned DiagID;
5753 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5754 DiagID, Result.release()))
5755 Diag(StartLoc, DiagID) << PrevSpec;
5756}
5757
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005758
5759/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5760/// from TryAltiVecVectorToken.
5761bool Parser::TryAltiVecVectorTokenOutOfLine() {
5762 Token Next = NextToken();
5763 switch (Next.getKind()) {
5764 default: return false;
5765 case tok::kw_short:
5766 case tok::kw_long:
5767 case tok::kw_signed:
5768 case tok::kw_unsigned:
5769 case tok::kw_void:
5770 case tok::kw_char:
5771 case tok::kw_int:
5772 case tok::kw_float:
5773 case tok::kw_double:
5774 case tok::kw_bool:
5775 case tok::kw___pixel:
5776 Tok.setKind(tok::kw___vector);
5777 return true;
5778 case tok::identifier:
5779 if (Next.getIdentifierInfo() == Ident_pixel) {
5780 Tok.setKind(tok::kw___vector);
5781 return true;
5782 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005783 if (Next.getIdentifierInfo() == Ident_bool) {
5784 Tok.setKind(tok::kw___vector);
5785 return true;
5786 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005787 return false;
5788 }
5789}
5790
5791bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5792 const char *&PrevSpec, unsigned &DiagID,
5793 bool &isInvalid) {
5794 if (Tok.getIdentifierInfo() == Ident_vector) {
5795 Token Next = NextToken();
5796 switch (Next.getKind()) {
5797 case tok::kw_short:
5798 case tok::kw_long:
5799 case tok::kw_signed:
5800 case tok::kw_unsigned:
5801 case tok::kw_void:
5802 case tok::kw_char:
5803 case tok::kw_int:
5804 case tok::kw_float:
5805 case tok::kw_double:
5806 case tok::kw_bool:
5807 case tok::kw___pixel:
5808 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5809 return true;
5810 case tok::identifier:
5811 if (Next.getIdentifierInfo() == Ident_pixel) {
5812 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5813 return true;
5814 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005815 if (Next.getIdentifierInfo() == Ident_bool) {
5816 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5817 return true;
5818 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005819 break;
5820 default:
5821 break;
5822 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005823 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005824 DS.isTypeAltiVecVector()) {
5825 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5826 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005827 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5828 DS.isTypeAltiVecVector()) {
5829 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5830 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005831 }
5832 return false;
5833}