blob: 75d7fc68955c8dadd96d558a260af4a70c12fe40 [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 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000262
263 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
264 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
265 return;
266 }
267
Richard Smithb1f9a282013-10-31 01:56:18 +0000268 // Thread safety attributes are parsed in an unevaluated context.
269 // FIXME: Share the bulk of the parsing code here and just pull out
270 // the unevaluated context.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000271 if (IsThreadSafetyAttribute(AttrName->getName())) {
272 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
273 return;
274 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000275 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000276 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000277 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
278 return;
279 }
Aaron Ballman4768b312013-11-04 12:55:56 +0000280 // Some attributes expect solely a type parameter.
281 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000282 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
283 return;
284 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000285
Richard Smith66e71682013-10-24 01:07:54 +0000286 // Ignore the left paren location for now.
287 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000288
Aaron Ballman00e99962013-08-31 01:11:41 +0000289 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000290
Richard Smithb1f9a282013-10-31 01:56:18 +0000291 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000292 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000293 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000294
295 // If we don't know how to parse this attribute, but this is the only
296 // token in this argument, assume it's meant to be an identifier.
Aaron Ballman66037472013-12-04 15:32:26 +0000297 if (AttrKind == AttributeList::UnknownAttribute ||
298 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000299 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000300 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000301 }
Richard Smithb12bf692011-10-17 21:20:17 +0000302
Richard Smithb1f9a282013-10-31 01:56:18 +0000303 if (IsIdentifierArg)
304 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000305 }
306
Richard Smithb1f9a282013-10-31 01:56:18 +0000307 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000308 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000309 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000310 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000311
Richard Smithb12bf692011-10-17 21:20:17 +0000312 // Parse the non-empty comma-separated list of expressions.
313 while (1) {
314 ExprResult ArgExpr(ParseAssignmentExpression());
315 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000316 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000317 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000318 }
Richard Smithb12bf692011-10-17 21:20:17 +0000319 ArgExprs.push_back(ArgExpr.release());
320 if (Tok.isNot(tok::comma))
321 break;
322 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000323 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000324 }
Richard Smithb12bf692011-10-17 21:20:17 +0000325
326 SourceLocation RParen = Tok.getLocation();
Richard Smithb1f9a282013-10-31 01:56:18 +0000327 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000328 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000329 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
330 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000331 }
332}
333
Chad Rosierc1183952012-06-26 22:30:43 +0000334/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000335/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000336void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000337 SourceLocation AttrNameLoc,
338 ParsedAttributes &Attrs)
339{
340 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000341 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000342 AttrName->getNameStart(), tok::r_paren))
343 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000344
Aaron Ballman478faed2012-06-19 22:09:27 +0000345 ExprResult ArgExpr(ParseConstantExpression());
346 if (ArgExpr.isInvalid()) {
347 T.skipToEnd();
348 return;
349 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000350 ArgsUnion ExprList = ArgExpr.take();
351 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
352 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000353
354 T.consumeClose();
355}
356
Chad Rosierc1183952012-06-26 22:30:43 +0000357/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000358/// arguments.
359bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
360 return llvm::StringSwitch<bool>(Ident->getName())
361 .Case("dllimport", true)
362 .Case("dllexport", true)
363 .Case("noreturn", true)
364 .Case("nothrow", true)
365 .Case("noinline", true)
366 .Case("naked", true)
367 .Case("appdomain", true)
368 .Case("process", true)
369 .Case("jitintrinsic", true)
370 .Case("noalias", true)
371 .Case("restrict", true)
372 .Case("novtable", true)
373 .Case("selectany", true)
374 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000375 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000376 .Default(false);
377}
378
Chad Rosierc1183952012-06-26 22:30:43 +0000379/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000380/// parameters). Will return false if we properly handled the declspec, or
381/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000382void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000383 SourceLocation Loc,
384 ParsedAttributes &Attrs) {
385 // Try to handle the easy case first -- these declspecs all take a single
386 // parameter as their argument.
387 if (llvm::StringSwitch<bool>(Ident->getName())
388 .Case("uuid", true)
389 .Case("align", true)
390 .Case("allocate", true)
391 .Default(false)) {
392 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
393 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000394 // The deprecated declspec has an optional single argument, so we will
395 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000396 // not.
397 if (Tok.getKind() == tok::l_paren)
398 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
399 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000400 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000401 } else if (Ident->getName() == "property") {
402 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000403 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000404 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000405 if (Tok.isNot(tok::l_paren)) {
406 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
407 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000408 return;
John McCall5e77d762013-04-16 07:28:30 +0000409 }
410 BalancedDelimiterTracker T(*this, tok::l_paren);
411 T.expectAndConsume(diag::err_expected_lparen_after,
412 Ident->getNameStart(), tok::r_paren);
413
414 enum AccessorKind {
415 AK_Invalid = -1,
416 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
417 };
418 IdentifierInfo *AccessorNames[] = { 0, 0 };
419 bool HasInvalidAccessor = false;
420
421 // Parse the accessor specifications.
422 while (true) {
423 // Stop if this doesn't look like an accessor spec.
424 if (!Tok.is(tok::identifier)) {
425 // If the user wrote a completely empty list, use a special diagnostic.
426 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
427 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
428 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
429 break;
430 }
431
432 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
433 break;
434 }
435
436 AccessorKind Kind;
437 SourceLocation KindLoc = Tok.getLocation();
438 StringRef KindStr = Tok.getIdentifierInfo()->getName();
439 if (KindStr == "get") {
440 Kind = AK_Get;
441 } else if (KindStr == "put") {
442 Kind = AK_Put;
443
444 // Recover from the common mistake of using 'set' instead of 'put'.
445 } else if (KindStr == "set") {
446 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
447 << FixItHint::CreateReplacement(KindLoc, "put");
448 Kind = AK_Put;
449
450 // Handle the mistake of forgetting the accessor kind by skipping
451 // this accessor.
452 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
453 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
454 ConsumeToken();
455 HasInvalidAccessor = true;
456 goto next_property_accessor;
457
458 // Otherwise, complain about the unknown accessor kind.
459 } else {
460 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
461 HasInvalidAccessor = true;
462 Kind = AK_Invalid;
463
464 // Try to keep parsing unless it doesn't look like an accessor spec.
465 if (!NextToken().is(tok::equal)) break;
466 }
467
468 // Consume the identifier.
469 ConsumeToken();
470
471 // Consume the '='.
472 if (Tok.is(tok::equal)) {
473 ConsumeToken();
474 } else {
475 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
476 << KindStr;
477 break;
478 }
479
480 // Expect the method name.
481 if (!Tok.is(tok::identifier)) {
482 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
483 break;
484 }
485
486 if (Kind == AK_Invalid) {
487 // Just drop invalid accessors.
488 } else if (AccessorNames[Kind] != NULL) {
489 // Complain about the repeated accessor, ignore it, and keep parsing.
490 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
491 } else {
492 AccessorNames[Kind] = Tok.getIdentifierInfo();
493 }
494 ConsumeToken();
495
496 next_property_accessor:
497 // Keep processing accessors until we run out.
498 if (Tok.is(tok::comma)) {
499 ConsumeAnyToken();
500 continue;
501
502 // If we run into the ')', stop without consuming it.
503 } else if (Tok.is(tok::r_paren)) {
504 break;
505 } else {
506 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
507 break;
508 }
509 }
510
511 // Only add the property attribute if it was well-formed.
512 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000513 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000514 AccessorNames[AK_Get], AccessorNames[AK_Put],
515 AttributeList::AS_Declspec);
516 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000517 T.skipToEnd();
518 } else {
519 // We don't recognize this as a valid declspec, but instead of creating the
520 // attribute and allowing sema to warn about it, we will warn here instead.
521 // This is because some attributes have multiple spellings, but we need to
522 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000523 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000524 // both locations.
525 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
526
527 // If there's an open paren, we should eat the open and close parens under
528 // the assumption that this unknown declspec has parameters.
529 BalancedDelimiterTracker T(*this, tok::l_paren);
530 if (!T.consumeOpen())
531 T.skipToEnd();
532 }
533}
534
Eli Friedman06de2b52009-06-08 07:21:15 +0000535/// [MS] decl-specifier:
536/// __declspec ( extended-decl-modifier-seq )
537///
538/// [MS] extended-decl-modifier-seq:
539/// extended-decl-modifier[opt]
540/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000541void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000542 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000543
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000544 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000545 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000546 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000547 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000548 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000549
Chad Rosierc1183952012-06-26 22:30:43 +0000550 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000551 // you can specify multiple attributes per declspec.
552 while (Tok.getKind() != tok::r_paren) {
553 // We expect either a well-known identifier or a generic string. Anything
554 // else is a malformed declspec.
555 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000556 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000557 Tok.getKind() != tok::kw_restrict) {
558 Diag(Tok, diag::err_ms_declspec_type);
559 T.skipToEnd();
560 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000561 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000562
563 IdentifierInfo *AttrName;
564 SourceLocation AttrNameLoc;
565 if (IsString) {
566 SmallString<8> StrBuffer;
567 bool Invalid = false;
568 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
569 if (Invalid) {
570 T.skipToEnd();
571 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000572 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000573 AttrName = PP.getIdentifierInfo(Str);
574 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000575 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000576 AttrName = Tok.getIdentifierInfo();
577 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000578 }
Chad Rosierc1183952012-06-26 22:30:43 +0000579
Aaron Ballman478faed2012-06-19 22:09:27 +0000580 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000581 // If we have a generic string, we will allow it because there is no
582 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000583 // (for instance, SAL declspecs in older versions of MSVC).
584 //
Chad Rosierc1183952012-06-26 22:30:43 +0000585 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000586 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000587 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
588 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000589 else
590 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000591 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000592 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000593}
594
John McCall53fa7142010-12-24 02:08:15 +0000595void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000596 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000597 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000598 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000599 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000600 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
601 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000602 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
603 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000604 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
605 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000606 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000607}
608
John McCall53fa7142010-12-24 02:08:15 +0000609void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000610 // Treat these like attributes
611 while (Tok.is(tok::kw___pascal)) {
612 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
613 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000614 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
615 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000616 }
John McCall53fa7142010-12-24 02:08:15 +0000617}
618
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000619void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
620 // Treat these like attributes
621 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000622 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000623 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000624 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
625 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000626 }
627}
628
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000629void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000630 // FIXME: The mapping from attribute spelling to semantics should be
631 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000632 SourceLocation Loc = Tok.getLocation();
633 switch(Tok.getKind()) {
634 // OpenCL qualifiers:
635 case tok::kw___private:
John McCall084e83d2011-03-24 11:26:52 +0000636 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000637 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000638 PP.getIdentifierInfo("address_space"), Loc, 0);
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___global:
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_global);
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___local:
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_local);
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___constant:
John McCall084e83d2011-03-24 11:26:52 +0000654 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000655 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000656 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
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___read_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_read_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___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000666 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000667 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000668 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000669 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000670
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000671 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000672 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000673 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000674 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000675 break;
676 default: break;
677 }
678}
679
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000680/// \brief Parse a version number.
681///
682/// version:
683/// simple-integer
684/// simple-integer ',' simple-integer
685/// simple-integer ',' simple-integer ',' simple-integer
686VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
687 Range = Tok.getLocation();
688
689 if (!Tok.is(tok::numeric_constant)) {
690 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000691 SkipUntil(tok::comma, tok::r_paren,
692 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000693 return VersionTuple();
694 }
695
696 // Parse the major (and possibly minor and subminor) versions, which
697 // are stored in the numeric constant. We utilize a quirk of the
698 // lexer, which is that it handles something like 1.2.3 as a single
699 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000700 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000701 Buffer.resize(Tok.getLength()+1);
702 const char *ThisTokBegin = &Buffer[0];
703
704 // Get the spelling of the token, which eliminates trigraphs, etc.
705 bool Invalid = false;
706 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
707 if (Invalid)
708 return VersionTuple();
709
710 // Parse the major version.
711 unsigned AfterMajor = 0;
712 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000713 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000714 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
715 ++AfterMajor;
716 }
717
718 if (AfterMajor == 0) {
719 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000720 SkipUntil(tok::comma, tok::r_paren,
721 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000722 return VersionTuple();
723 }
724
725 if (AfterMajor == ActualLength) {
726 ConsumeToken();
727
728 // We only had a single version component.
729 if (Major == 0) {
730 Diag(Tok, diag::err_zero_version);
731 return VersionTuple();
732 }
733
734 return VersionTuple(Major);
735 }
736
737 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
738 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000739 SkipUntil(tok::comma, tok::r_paren,
740 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000741 return VersionTuple();
742 }
743
744 // Parse the minor version.
745 unsigned AfterMinor = AfterMajor + 1;
746 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000747 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000748 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
749 ++AfterMinor;
750 }
751
752 if (AfterMinor == ActualLength) {
753 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000754
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000755 // We had major.minor.
756 if (Major == 0 && Minor == 0) {
757 Diag(Tok, diag::err_zero_version);
758 return VersionTuple();
759 }
760
Chad Rosierc1183952012-06-26 22:30:43 +0000761 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000762 }
763
764 // If what follows is not a '.', we have a problem.
765 if (ThisTokBegin[AfterMinor] != '.') {
766 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000767 SkipUntil(tok::comma, tok::r_paren,
768 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000769 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000770 }
771
772 // Parse the subminor version.
773 unsigned AfterSubminor = AfterMinor + 1;
774 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000775 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000776 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
777 ++AfterSubminor;
778 }
779
780 if (AfterSubminor != ActualLength) {
781 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000782 SkipUntil(tok::comma, tok::r_paren,
783 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000784 return VersionTuple();
785 }
786 ConsumeToken();
787 return VersionTuple(Major, Minor, Subminor);
788}
789
790/// \brief Parse the contents of the "availability" attribute.
791///
792/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000793/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000794///
795/// platform:
796/// identifier
797///
798/// version-arg-list:
799/// version-arg
800/// version-arg ',' version-arg-list
801///
802/// version-arg:
803/// 'introduced' '=' version
804/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000805/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000806/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000807/// opt-message:
808/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000809void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
810 SourceLocation AvailabilityLoc,
811 ParsedAttributes &attrs,
812 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000813 enum { Introduced, Deprecated, Obsoleted, Unknown };
814 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000815 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000816
817 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000818 BalancedDelimiterTracker T(*this, tok::l_paren);
819 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000820 Diag(Tok, diag::err_expected_lparen);
821 return;
822 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000823
824 // Parse the platform name,
825 if (Tok.isNot(tok::identifier)) {
826 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000827 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000828 return;
829 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000830 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000831
832 // Parse the ',' following the platform name.
833 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
834 return;
835
836 // If we haven't grabbed the pointers for the identifiers
837 // "introduced", "deprecated", and "obsoleted", do so now.
838 if (!Ident_introduced) {
839 Ident_introduced = PP.getIdentifierInfo("introduced");
840 Ident_deprecated = PP.getIdentifierInfo("deprecated");
841 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000842 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000843 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000844 }
845
846 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000847 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000848 do {
849 if (Tok.isNot(tok::identifier)) {
850 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000851 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000852 return;
853 }
854 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
855 SourceLocation KeywordLoc = ConsumeToken();
856
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000857 if (Keyword == Ident_unavailable) {
858 if (UnavailableLoc.isValid()) {
859 Diag(KeywordLoc, diag::err_availability_redundant)
860 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000861 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000862 UnavailableLoc = KeywordLoc;
863
864 if (Tok.isNot(tok::comma))
865 break;
866
867 ConsumeToken();
868 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000869 }
870
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000871 if (Tok.isNot(tok::equal)) {
872 Diag(Tok, diag::err_expected_equal_after)
873 << Keyword;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000874 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000875 return;
876 }
877 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000878 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000879 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000880 Diag(Tok, diag::err_expected_string_literal)
881 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000882 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000883 return;
884 }
885 MessageExpr = ParseStringLiteralExpression();
886 break;
887 }
Chad Rosierc1183952012-06-26 22:30:43 +0000888
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000889 SourceRange VersionRange;
890 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000891
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000892 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000893 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000894 return;
895 }
896
897 unsigned Index;
898 if (Keyword == Ident_introduced)
899 Index = Introduced;
900 else if (Keyword == Ident_deprecated)
901 Index = Deprecated;
902 else if (Keyword == Ident_obsoleted)
903 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000904 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000905 Index = Unknown;
906
907 if (Index < Unknown) {
908 if (!Changes[Index].KeywordLoc.isInvalid()) {
909 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000910 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000911 << SourceRange(Changes[Index].KeywordLoc,
912 Changes[Index].VersionRange.getEnd());
913 }
914
915 Changes[Index].KeywordLoc = KeywordLoc;
916 Changes[Index].Version = Version;
917 Changes[Index].VersionRange = VersionRange;
918 } else {
919 Diag(KeywordLoc, diag::err_availability_unknown_change)
920 << Keyword << VersionRange;
921 }
922
923 if (Tok.isNot(tok::comma))
924 break;
925
926 ConsumeToken();
927 } while (true);
928
929 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000930 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000931 return;
932
933 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000934 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000935
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000936 // The 'unavailable' availability cannot be combined with any other
937 // availability changes. Make sure that hasn't happened.
938 if (UnavailableLoc.isValid()) {
939 bool Complained = false;
940 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
941 if (Changes[Index].KeywordLoc.isValid()) {
942 if (!Complained) {
943 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
944 << SourceRange(Changes[Index].KeywordLoc,
945 Changes[Index].VersionRange.getEnd());
946 Complained = true;
947 }
948
949 // Clear out the availability.
950 Changes[Index] = AvailabilityChange();
951 }
952 }
953 }
954
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000955 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000956 attrs.addNew(&Availability,
957 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000958 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000959 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000960 Changes[Introduced],
961 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000962 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000963 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000964 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000965}
966
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000967/// \brief Parse the contents of the "objc_bridge_related" attribute.
968/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
969/// related_class:
970/// Identifier
971///
972/// opt-class_method:
973/// Identifier: | <empty>
974///
975/// opt-instance_method:
976/// Identifier | <empty>
977///
978void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
979 SourceLocation ObjCBridgeRelatedLoc,
980 ParsedAttributes &attrs,
981 SourceLocation *endLoc) {
982 // Opening '('.
983 BalancedDelimiterTracker T(*this, tok::l_paren);
984 if (T.consumeOpen()) {
985 Diag(Tok, diag::err_expected_lparen);
986 return;
987 }
988
989 // Parse the related class name.
990 if (Tok.isNot(tok::identifier)) {
991 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
992 SkipUntil(tok::r_paren, StopAtSemi);
993 return;
994 }
995 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
996 if (Tok.isNot(tok::comma)) {
997 Diag(Tok, diag::err_expected_comma);
998 SkipUntil(tok::r_paren, StopAtSemi);
999 return;
1000 }
1001 ConsumeToken();
1002
1003 // Parse optional class method name.
1004 IdentifierLoc *ClassMethod = 0;
1005 if (Tok.is(tok::identifier)) {
1006 ClassMethod = ParseIdentifierLoc();
1007 if (Tok.isNot(tok::colon)) {
1008 Diag(Tok, diag::err_objcbridge_related_selector_name);
1009 SkipUntil(tok::r_paren, StopAtSemi);
1010 return;
1011 }
1012 ConsumeToken();
1013 }
1014 if (Tok.isNot(tok::comma)) {
1015 if (Tok.is(tok::colon))
1016 Diag(Tok, diag::err_objcbridge_related_selector_name);
1017 else
1018 Diag(Tok, diag::err_expected_comma);
1019 SkipUntil(tok::r_paren, StopAtSemi);
1020 return;
1021 }
1022 ConsumeToken();
1023
1024 // Parse optional instance method name.
1025 IdentifierLoc *InstanceMethod = 0;
1026 if (Tok.is(tok::identifier))
1027 InstanceMethod = ParseIdentifierLoc();
1028 else if (Tok.isNot(tok::r_paren)) {
1029 Diag(Tok, diag::err_expected_rparen);
1030 SkipUntil(tok::r_paren, StopAtSemi);
1031 return;
1032 }
1033
1034 // Closing ')'.
1035 if (T.consumeClose())
1036 return;
1037
1038 if (endLoc)
1039 *endLoc = T.getCloseLocation();
1040
1041 // Record this attribute
1042 attrs.addNew(&ObjCBridgeRelated,
1043 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1044 0, ObjCBridgeRelatedLoc,
1045 RelatedClass,
1046 ClassMethod,
1047 InstanceMethod,
1048 AttributeList::AS_GNU);
1049
1050}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001051
Bill Wendling44426052012-12-20 19:22:21 +00001052// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001053// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1054
1055void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1056
1057void Parser::LateParsedClass::ParseLexedAttributes() {
1058 Self->ParseLexedAttributes(*Class);
1059}
1060
1061void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001062 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001063}
1064
1065/// Wrapper class which calls ParseLexedAttribute, after setting up the
1066/// scope appropriately.
1067void Parser::ParseLexedAttributes(ParsingClass &Class) {
1068 // Deal with templates
1069 // FIXME: Test cases to make sure this does the right thing for templates.
1070 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1071 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1072 HasTemplateScope);
1073 if (HasTemplateScope)
1074 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1075
Douglas Gregor3024f072012-04-16 07:05:22 +00001076 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001077 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001078 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001079 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1080 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1081
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001082 // Enter the scope of nested classes
1083 if (!AlreadyHasClassScope)
1084 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1085 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001086 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001087 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1088 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1089 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001090 }
Chad Rosierc1183952012-06-26 22:30:43 +00001091
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001092 if (!AlreadyHasClassScope)
1093 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1094 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001095}
1096
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001097
1098/// \brief Parse all attributes in LAs, and attach them to Decl D.
1099void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1100 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001101 assert(LAs.parseSoon() &&
1102 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001103 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001104 if (D)
1105 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001106 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001107 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001108 }
1109 LAs.clear();
1110}
1111
1112
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001113/// \brief Finish parsing an attribute for which parsing was delayed.
1114/// This will be called at the end of parsing a class declaration
1115/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001116/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001117/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001118void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1119 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001120 // Save the current token position.
1121 SourceLocation OrigLoc = Tok.getLocation();
1122
1123 // Append the current token at the end of the new token stream so that it
1124 // doesn't get lost.
1125 LA.Toks.push_back(Tok);
1126 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1127 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001128 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001129
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001130 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001131 // FIXME: Do not warn on C++11 attributes, once we start supporting
1132 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001133 Diag(Tok, diag::warn_attribute_on_function_definition)
1134 << LA.AttrName.getName();
1135 }
1136
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001137 ParsedAttributes Attrs(AttrFactory);
1138 SourceLocation endLoc;
1139
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001140 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001141 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001142 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1143 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001144
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001145 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001146 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1147 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001148
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001149 if (LA.Decls.size() == 1) {
1150 // If the Decl is templatized, add template parameters to scope.
1151 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1152 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1153 if (HasTemplateScope)
1154 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001155
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001156 // If the Decl is on a function, add function parameters to the scope.
1157 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1158 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1159 if (HasFunScope)
1160 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001161
Michael Han23214e52012-10-03 01:56:22 +00001162 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001163 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001164
1165 if (HasFunScope) {
1166 Actions.ActOnExitFunctionContext();
1167 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1168 }
1169 if (HasTemplateScope) {
1170 TempScope.Exit();
1171 }
1172 } else {
1173 // If there are multiple decls, then the decl cannot be within the
1174 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001175 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001176 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001177 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001178 } else {
1179 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001180 }
1181
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001182 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1183 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1184 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001185
1186 if (Tok.getLocation() != OrigLoc) {
1187 // Due to a parsing error, we either went over the cached tokens or
1188 // there are still cached tokens left, so we skip the leftover tokens.
1189 // Since this is an uncommon situation that should be avoided, use the
1190 // expensive isBeforeInTranslationUnit call.
1191 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1192 OrigLoc))
1193 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001194 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001195 }
1196}
1197
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001198/// \brief Wrapper around a case statement checking if AttrName is
1199/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001200bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001201 return llvm::StringSwitch<bool>(AttrName)
1202 .Case("guarded_by", true)
1203 .Case("guarded_var", true)
1204 .Case("pt_guarded_by", true)
1205 .Case("pt_guarded_var", true)
1206 .Case("lockable", true)
1207 .Case("scoped_lockable", true)
1208 .Case("no_thread_safety_analysis", true)
1209 .Case("acquired_after", true)
1210 .Case("acquired_before", true)
1211 .Case("exclusive_lock_function", true)
1212 .Case("shared_lock_function", true)
1213 .Case("exclusive_trylock_function", true)
1214 .Case("shared_trylock_function", true)
1215 .Case("unlock_function", true)
1216 .Case("lock_returned", true)
1217 .Case("locks_excluded", true)
1218 .Case("exclusive_locks_required", true)
1219 .Case("shared_locks_required", true)
1220 .Default(false);
1221}
1222
1223/// \brief Parse the contents of thread safety attributes. These
1224/// should always be parsed as an expression list.
1225///
1226/// We need to special case the parsing due to the fact that if the first token
1227/// of the first argument is an identifier, the main parse loop will store
1228/// that token as a "parameter" and the rest of
1229/// the arguments will be added to a list of "arguments". However,
1230/// subsequent tokens in the first argument are lost. We instead parse each
1231/// argument as an expression and add all arguments to the list of "arguments".
1232/// In future, we will take advantage of this special case to also
1233/// deal with some argument scoping issues here (for example, referring to a
1234/// function parameter in the attribute on that function).
1235void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1236 SourceLocation AttrNameLoc,
1237 ParsedAttributes &Attrs,
1238 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001239 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001240
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001241 BalancedDelimiterTracker T(*this, tok::l_paren);
1242 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001243
Aaron Ballman00e99962013-08-31 01:11:41 +00001244 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001245 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001246
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001247 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001248 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001249 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001250 ExprResult ArgExpr(ParseAssignmentExpression());
1251 if (ArgExpr.isInvalid()) {
1252 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001253 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001254 break;
1255 } else {
1256 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001257 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001258 if (Tok.isNot(tok::comma))
1259 break;
1260 ConsumeToken(); // Eat the comma, move to the next argument
1261 }
1262 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001263 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001264 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1265 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001266 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001267 if (EndLoc)
1268 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001269}
1270
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001271void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1272 SourceLocation AttrNameLoc,
1273 ParsedAttributes &Attrs,
1274 SourceLocation *EndLoc) {
1275 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1276
1277 BalancedDelimiterTracker T(*this, tok::l_paren);
1278 T.consumeOpen();
1279
1280 if (Tok.isNot(tok::identifier)) {
1281 Diag(Tok, diag::err_expected_ident);
1282 T.skipToEnd();
1283 return;
1284 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001285 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001286
1287 if (Tok.isNot(tok::comma)) {
1288 Diag(Tok, diag::err_expected_comma);
1289 T.skipToEnd();
1290 return;
1291 }
1292 ConsumeToken();
1293
1294 SourceRange MatchingCTypeRange;
1295 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1296 if (MatchingCType.isInvalid()) {
1297 T.skipToEnd();
1298 return;
1299 }
1300
1301 bool LayoutCompatible = false;
1302 bool MustBeNull = false;
1303 while (Tok.is(tok::comma)) {
1304 ConsumeToken();
1305 if (Tok.isNot(tok::identifier)) {
1306 Diag(Tok, diag::err_expected_ident);
1307 T.skipToEnd();
1308 return;
1309 }
1310 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1311 if (Flag->isStr("layout_compatible"))
1312 LayoutCompatible = true;
1313 else if (Flag->isStr("must_be_null"))
1314 MustBeNull = true;
1315 else {
1316 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1317 T.skipToEnd();
1318 return;
1319 }
1320 ConsumeToken(); // consume flag
1321 }
1322
1323 if (!T.consumeClose()) {
1324 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001325 ArgumentKind, MatchingCType.release(),
1326 LayoutCompatible, MustBeNull,
1327 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001328 }
1329
1330 if (EndLoc)
1331 *EndLoc = T.getCloseLocation();
1332}
1333
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001334/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1335/// of a C++11 attribute-specifier in a location where an attribute is not
1336/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1337/// situation.
1338///
1339/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1340/// this doesn't appear to actually be an attribute-specifier, and the caller
1341/// should try to parse it.
1342bool Parser::DiagnoseProhibitedCXX11Attribute() {
1343 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1344
1345 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1346 case CAK_NotAttributeSpecifier:
1347 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1348 return false;
1349
1350 case CAK_InvalidAttributeSpecifier:
1351 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1352 return false;
1353
1354 case CAK_AttributeSpecifier:
1355 // Parse and discard the attributes.
1356 SourceLocation BeginLoc = ConsumeBracket();
1357 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001358 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001359 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1360 SourceLocation EndLoc = ConsumeBracket();
1361 Diag(BeginLoc, diag::err_attributes_not_allowed)
1362 << SourceRange(BeginLoc, EndLoc);
1363 return true;
1364 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001365 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001366}
1367
Richard Smith98155ad2013-02-20 01:17:14 +00001368/// \brief We have found the opening square brackets of a C++11
1369/// attribute-specifier in a location where an attribute is not permitted, but
1370/// we know where the attributes ought to be written. Parse them anyway, and
1371/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001372void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1373 SourceLocation CorrectLocation) {
1374 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1375 Tok.is(tok::kw_alignas));
1376
1377 // Consume the attributes.
1378 SourceLocation Loc = Tok.getLocation();
1379 ParseCXX11Attributes(Attrs);
1380 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1381
1382 Diag(Loc, diag::err_attributes_not_allowed)
1383 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1384 << FixItHint::CreateRemoval(AttrRange);
1385}
1386
John McCall53fa7142010-12-24 02:08:15 +00001387void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1388 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1389 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001390}
1391
Michael Han64536a62012-11-06 19:34:54 +00001392void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1393 AttributeList *AttrList = attrs.getList();
1394 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001395 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001396 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001397 << AttrList->getName();
1398 AttrList->setInvalid();
1399 }
1400 AttrList = AttrList->getNext();
1401 }
1402}
1403
Chris Lattner53361ac2006-08-10 05:19:57 +00001404/// ParseDeclaration - Parse a full 'declaration', which consists of
1405/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001406/// 'Context' should be a Declarator::TheContext value. This returns the
1407/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001408///
1409/// declaration: [C99 6.7]
1410/// block-declaration ->
1411/// simple-declaration
1412/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001413/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001414/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001415/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001416/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001417/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001418/// others... [FIXME]
1419///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001420Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1421 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001422 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001423 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001424 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001425 // Must temporarily exit the objective-c container scope for
1426 // parsing c none objective-c decls.
1427 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001428
John McCall48871652010-08-21 09:40:31 +00001429 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001430 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001431 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001432 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001433 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001434 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001435 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001436 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001437 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001438 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001439 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001440 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001441 SourceLocation InlineLoc = ConsumeToken();
1442 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1443 break;
1444 }
Chad Rosierc1183952012-06-26 22:30:43 +00001445 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001446 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001447 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001448 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001449 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001450 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001451 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001452 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001453 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001454 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001455 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001456 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001457 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001458 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001459 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001460 default:
John McCall53fa7142010-12-24 02:08:15 +00001461 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001462 }
Chad Rosierc1183952012-06-26 22:30:43 +00001463
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001464 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001465 // single decl, convert it now. Alias declarations can also declare a type;
1466 // include that too if it is present.
1467 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001468}
1469
1470/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1471/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001472/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1473/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001474///[C90/C++]init-declarator-list ';' [TODO]
1475/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001476///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001477/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001478/// attribute-specifier-seq[opt] type-specifier-seq declarator
1479///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001480/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001481/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001482///
1483/// If FRI is non-null, we might be parsing a for-range-declaration instead
1484/// of a simple-declaration. If we find that we are, we also parse the
1485/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001486Parser::DeclGroupPtrTy
1487Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1488 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001489 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001490 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001491 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001492 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001493
Richard Smith404dfb42013-11-19 22:47:36 +00001494 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1495 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1496
1497 // If we had a free-standing type definition with a missing semicolon, we
1498 // may get this far before the problem becomes obvious.
1499 if (DS.hasTagDefinition() &&
1500 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1501 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001502
Chris Lattner0e894622006-08-13 19:58:17 +00001503 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1504 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001505 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001506 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001507 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001508 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001509 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001510 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001511 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001512 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001513 }
Chad Rosierc1183952012-06-26 22:30:43 +00001514
Richard Smith2386c8b2013-02-22 09:06:26 +00001515 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001516 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001517}
Mike Stump11289f42009-09-09 15:08:12 +00001518
Richard Smith09f76ee2011-10-19 21:33:05 +00001519/// Returns true if this might be the start of a declarator, or a common typo
1520/// for a declarator.
1521bool Parser::MightBeDeclarator(unsigned Context) {
1522 switch (Tok.getKind()) {
1523 case tok::annot_cxxscope:
1524 case tok::annot_template_id:
1525 case tok::caret:
1526 case tok::code_completion:
1527 case tok::coloncolon:
1528 case tok::ellipsis:
1529 case tok::kw___attribute:
1530 case tok::kw_operator:
1531 case tok::l_paren:
1532 case tok::star:
1533 return true;
1534
1535 case tok::amp:
1536 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001537 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001538
Richard Smithc8a79032012-01-09 22:31:44 +00001539 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001540 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001541 NextToken().is(tok::l_square);
1542
1543 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001544 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001545
Richard Smith09f76ee2011-10-19 21:33:05 +00001546 case tok::identifier:
1547 switch (NextToken().getKind()) {
1548 case tok::code_completion:
1549 case tok::coloncolon:
1550 case tok::comma:
1551 case tok::equal:
1552 case tok::equalequal: // Might be a typo for '='.
1553 case tok::kw_alignas:
1554 case tok::kw_asm:
1555 case tok::kw___attribute:
1556 case tok::l_brace:
1557 case tok::l_paren:
1558 case tok::l_square:
1559 case tok::less:
1560 case tok::r_brace:
1561 case tok::r_paren:
1562 case tok::r_square:
1563 case tok::semi:
1564 return true;
1565
1566 case tok::colon:
1567 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001568 // and in block scope it's probably a label. Inside a class definition,
1569 // this is a bit-field.
1570 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001571 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001572
1573 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001574 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001575
1576 default:
1577 return false;
1578 }
1579
1580 default:
1581 return false;
1582 }
1583}
1584
Richard Smithb8caac82012-04-11 20:59:20 +00001585/// Skip until we reach something which seems like a sensible place to pick
1586/// up parsing after a malformed declaration. This will sometimes stop sooner
1587/// than SkipUntil(tok::r_brace) would, but will never stop later.
1588void Parser::SkipMalformedDecl() {
1589 while (true) {
1590 switch (Tok.getKind()) {
1591 case tok::l_brace:
1592 // Skip until matching }, then stop. We've probably skipped over
1593 // a malformed class or function definition or similar.
1594 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001595 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001596 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1597 // This declaration isn't over yet. Keep skipping.
1598 continue;
1599 }
1600 if (Tok.is(tok::semi))
1601 ConsumeToken();
1602 return;
1603
1604 case tok::l_square:
1605 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001606 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001607 continue;
1608
1609 case tok::l_paren:
1610 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001611 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001612 continue;
1613
1614 case tok::r_brace:
1615 return;
1616
1617 case tok::semi:
1618 ConsumeToken();
1619 return;
1620
1621 case tok::kw_inline:
1622 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001623 // a good place to pick back up parsing, except in an Objective-C
1624 // @interface context.
1625 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1626 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001627 return;
1628 break;
1629
1630 case tok::kw_namespace:
1631 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001632 // place to pick back up parsing, except in an Objective-C
1633 // @interface context.
1634 if (Tok.isAtStartOfLine() &&
1635 (!ParsingInObjCContainer || CurParsedObjCImpl))
1636 return;
1637 break;
1638
1639 case tok::at:
1640 // @end is very much like } in Objective-C contexts.
1641 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1642 ParsingInObjCContainer)
1643 return;
1644 break;
1645
1646 case tok::minus:
1647 case tok::plus:
1648 // - and + probably start new method declarations in Objective-C contexts.
1649 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001650 return;
1651 break;
1652
1653 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001654 case tok::annot_module_begin:
1655 case tok::annot_module_end:
1656 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001657 return;
1658
1659 default:
1660 break;
1661 }
1662
1663 ConsumeAnyToken();
1664 }
1665}
1666
John McCalld5a36322009-11-03 19:26:08 +00001667/// ParseDeclGroup - Having concluded that this is either a function
1668/// definition or a group of object declarations, actually parse the
1669/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001670Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1671 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001672 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001673 SourceLocation *DeclEnd,
1674 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001675 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001676 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001677 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001678
John McCalld5a36322009-11-03 19:26:08 +00001679 // Bail out if the first declarator didn't seem well-formed.
1680 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001681 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001682 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001683 }
Mike Stump11289f42009-09-09 15:08:12 +00001684
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001685 // Save late-parsed attributes for now; they need to be parsed in the
1686 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001687 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1688 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001689 if (D.isFunctionDeclarator())
1690 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1691
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001692 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001693 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001694 // Look at the next token to make sure that this isn't a function
1695 // declaration. We have to check this because __attribute__ might be the
1696 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001697 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001698
Douglas Gregor012efe22013-04-16 16:01:32 +00001699 if (AllowFunctionDefinitions) {
1700 if (isStartOfFunctionDefinition(D)) {
1701 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1702 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001703
Douglas Gregor012efe22013-04-16 16:01:32 +00001704 // Recover by treating the 'typedef' as spurious.
1705 DS.ClearStorageClassSpecs();
1706 }
1707
1708 Decl *TheDecl =
1709 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1710 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001711 }
1712
Douglas Gregor012efe22013-04-16 16:01:32 +00001713 if (isDeclarationSpecifier()) {
1714 // If there is an invalid declaration specifier right after the function
1715 // prototype, then we must be in a missing semicolon case where this isn't
1716 // actually a body. Just fall through into the code that handles it as a
1717 // prototype, and let the top-level code handle the erroneous declspec
1718 // where it would otherwise expect a comma or semicolon.
1719 } else {
1720 Diag(Tok, diag::err_expected_fn_body);
1721 SkipUntil(tok::semi);
1722 return DeclGroupPtrTy();
1723 }
John McCalld5a36322009-11-03 19:26:08 +00001724 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001725 if (Tok.is(tok::l_brace)) {
1726 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001727 SkipMalformedDecl();
1728 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001729 }
John McCalld5a36322009-11-03 19:26:08 +00001730 }
1731 }
1732
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001733 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001734 return DeclGroupPtrTy();
1735
1736 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1737 // must parse and analyze the for-range-initializer before the declaration is
1738 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001739 //
1740 // Handle the Objective-C for-in loop variable similarly, although we
1741 // don't need to parse the container in advance.
1742 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1743 bool IsForRangeLoop = false;
1744 if (Tok.is(tok::colon)) {
1745 IsForRangeLoop = true;
1746 FRI->ColonLoc = ConsumeToken();
1747 if (Tok.is(tok::l_brace))
1748 FRI->RangeExpr = ParseBraceInitializer();
1749 else
1750 FRI->RangeExpr = ParseExpression();
1751 }
1752
Richard Smith02e85f32011-04-14 22:09:26 +00001753 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001754 if (IsForRangeLoop)
1755 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001756 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001757 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001758 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001759 }
1760
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001761 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001762 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001763 if (LateParsedAttrs.size() > 0)
1764 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001765 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001766 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001767 DeclsInGroup.push_back(FirstDecl);
1768
Richard Smith09f76ee2011-10-19 21:33:05 +00001769 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001770
John McCalld5a36322009-11-03 19:26:08 +00001771 // If we don't have a comma, it is either the end of the list (a ';') or an
1772 // error, bail out.
1773 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001774 SourceLocation CommaLoc = ConsumeToken();
1775
1776 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1777 // This comma was followed by a line-break and something which can't be
1778 // the start of a declarator. The comma was probably a typo for a
1779 // semicolon.
1780 Diag(CommaLoc, diag::err_expected_semi_declaration)
1781 << FixItHint::CreateReplacement(CommaLoc, ";");
1782 ExpectSemi = false;
1783 break;
1784 }
John McCalld5a36322009-11-03 19:26:08 +00001785
1786 // Parse the next declarator.
1787 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001788 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001789
1790 // Accept attributes in an init-declarator. In the first declarator in a
1791 // declaration, these would be part of the declspec. In subsequent
1792 // declarators, they become part of the declarator itself, so that they
1793 // don't apply to declarators after *this* one. Examples:
1794 // short __attribute__((common)) var; -> declspec
1795 // short var __attribute__((common)); -> declarator
1796 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001797 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001798
1799 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001800 if (!D.isInvalidType()) {
1801 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1802 D.complete(ThisDecl);
1803 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001804 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001805 }
John McCalld5a36322009-11-03 19:26:08 +00001806 }
1807
1808 if (DeclEnd)
1809 *DeclEnd = Tok.getLocation();
1810
Richard Smith09f76ee2011-10-19 21:33:05 +00001811 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001812 ExpectAndConsumeSemi(Context == Declarator::FileContext
1813 ? diag::err_invalid_token_after_toplevel_declarator
1814 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001815 // Okay, there was no semicolon and one was expected. If we see a
1816 // declaration specifier, just assume it was missing and continue parsing.
1817 // Otherwise things are very confused and we skip to recover.
1818 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001819 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner13901342010-07-11 22:42:07 +00001820 if (Tok.is(tok::semi))
1821 ConsumeToken();
1822 }
John McCalld5a36322009-11-03 19:26:08 +00001823 }
1824
Rafael Espindolaab417692013-07-09 12:05:01 +00001825 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001826}
1827
Richard Smith02e85f32011-04-14 22:09:26 +00001828/// Parse an optional simple-asm-expr and attributes, and attach them to a
1829/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001830bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001831 // If a simple-asm-expr is present, parse it.
1832 if (Tok.is(tok::kw_asm)) {
1833 SourceLocation Loc;
1834 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1835 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001836 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001837 return true;
1838 }
1839
1840 D.setAsmLabel(AsmLabel.release());
1841 D.SetRangeEnd(Loc);
1842 }
1843
1844 MaybeParseGNUAttributes(D);
1845 return false;
1846}
1847
Douglas Gregor23996282009-05-12 21:31:51 +00001848/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1849/// declarator'. This method parses the remainder of the declaration
1850/// (including any attributes or initializer, among other things) and
1851/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001852///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001853/// init-declarator: [C99 6.7]
1854/// declarator
1855/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001856/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1857/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001858/// [C++] declarator initializer[opt]
1859///
1860/// [C++] initializer:
1861/// [C++] '=' initializer-clause
1862/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001863/// [C++0x] '=' 'default' [TODO]
1864/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001865/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001866///
1867/// According to the standard grammar, =default and =delete are function
1868/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001869///
John McCall48871652010-08-21 09:40:31 +00001870Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001871 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001872 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001873 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001874
Richard Smith02e85f32011-04-14 22:09:26 +00001875 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1876}
Mike Stump11289f42009-09-09 15:08:12 +00001877
Richard Smith02e85f32011-04-14 22:09:26 +00001878Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1879 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001880 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001881 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001882 switch (TemplateInfo.Kind) {
1883 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001884 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001885 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001886
Douglas Gregor450f00842009-09-25 18:43:00 +00001887 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001888 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001889 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001890 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001891 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001892 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001893 // Re-direct this decl to refer to the templated decl so that we can
1894 // initialize it.
1895 ThisDecl = VT->getTemplatedDecl();
1896 break;
1897 }
1898 case ParsedTemplateInfo::ExplicitInstantiation: {
1899 if (Tok.is(tok::semi)) {
1900 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1901 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1902 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001903 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001904 return 0;
1905 }
1906 ThisDecl = ThisRes.get();
1907 } else {
1908 // FIXME: This check should be for a variable template instantiation only.
1909
1910 // Check that this is a valid instantiation
1911 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1912 // If the declarator-id is not a template-id, issue a diagnostic and
1913 // recover by ignoring the 'template' keyword.
1914 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1915 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1916 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1917 } else {
1918 SourceLocation LAngleLoc =
1919 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1920 Diag(D.getIdentifierLoc(),
1921 diag::err_explicit_instantiation_with_definition)
1922 << SourceRange(TemplateInfo.TemplateLoc)
1923 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1924
1925 // Recover as if it were an explicit specialization.
1926 TemplateParameterLists FakedParamLists;
1927 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1928 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1929 LAngleLoc));
1930
1931 ThisDecl =
1932 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1933 }
1934 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001935 break;
1936 }
1937 }
Mike Stump11289f42009-09-09 15:08:12 +00001938
Richard Smith74aeef52013-04-26 16:15:35 +00001939 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001940
Douglas Gregor23996282009-05-12 21:31:51 +00001941 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001942 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001943 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001944 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001945
Anders Carlsson991285e2010-09-24 21:25:25 +00001946 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001947 if (D.isFunctionDeclarator())
1948 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1949 << 1 /* delete */;
1950 else
1951 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001952 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001953 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001954 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1955 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001956 else
1957 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001958 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001959 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001960 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001961 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001962 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001963
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001964 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001965 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001966 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001967 cutOffParsing();
1968 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001969 }
Chad Rosierc1183952012-06-26 22:30:43 +00001970
John McCalldadc5752010-08-24 06:29:42 +00001971 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001972
David Blaikiebbafb8a2012-03-11 07:00:24 +00001973 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001974 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001975 ExitScope();
1976 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001977
Douglas Gregor23996282009-05-12 21:31:51 +00001978 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001979 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001980 Actions.ActOnInitializerError(ThisDecl);
1981 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001982 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1983 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001984 }
1985 } else if (Tok.is(tok::l_paren)) {
1986 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001987 BalancedDelimiterTracker T(*this, tok::l_paren);
1988 T.consumeOpen();
1989
Benjamin Kramerf0623432012-08-23 22:51:59 +00001990 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001991 CommaLocsTy CommaLocs;
1992
David Blaikiebbafb8a2012-03-11 07:00:24 +00001993 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001994 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001995 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001996 }
1997
Douglas Gregor23996282009-05-12 21:31:51 +00001998 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001999 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002000 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00002001
David Blaikiebbafb8a2012-03-11 07:00:24 +00002002 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002003 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00002004 ExitScope();
2005 }
Douglas Gregor23996282009-05-12 21:31:51 +00002006 } else {
2007 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002008 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00002009
2010 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
2011 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00002012
David Blaikiebbafb8a2012-03-11 07:00:24 +00002013 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002014 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00002015 ExitScope();
2016 }
2017
Sebastian Redla9351792012-02-11 23:51:47 +00002018 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
2019 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002020 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00002021 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
2022 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00002023 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002024 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00002025 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00002026 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00002027 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2028
Sebastian Redl3da34892011-06-05 12:23:16 +00002029 if (D.getCXXScopeSpec().isSet()) {
2030 EnterScope(0);
2031 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
2032 }
2033
2034 ExprResult Init(ParseBraceInitializer());
2035
2036 if (D.getCXXScopeSpec().isSet()) {
2037 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
2038 ExitScope();
2039 }
2040
2041 if (Init.isInvalid()) {
2042 Actions.ActOnInitializerError(ThisDecl);
2043 } else
2044 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
2045 /*DirectInit=*/true, TypeContainsAuto);
2046
Douglas Gregor23996282009-05-12 21:31:51 +00002047 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00002048 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00002049 }
2050
Richard Smithb2bc2e62011-02-21 20:05:19 +00002051 Actions.FinalizeDeclaration(ThisDecl);
2052
Douglas Gregor23996282009-05-12 21:31:51 +00002053 return ThisDecl;
2054}
2055
Chris Lattner1890ac82006-08-13 01:16:23 +00002056/// ParseSpecifierQualifierList
2057/// specifier-qualifier-list:
2058/// type-specifier specifier-qualifier-list[opt]
2059/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002060/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002061///
Richard Smithc5b05522012-03-12 07:56:15 +00002062void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2063 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002064 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2065 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002066 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00002067 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002068
Chris Lattner1890ac82006-08-13 01:16:23 +00002069 // Validate declspec for type-name.
2070 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00002071 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
2072 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002073 Diag(Tok, diag::err_expected_type);
2074 DS.SetTypeSpecError();
2075 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2076 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002077 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002078 if (!DS.hasTypeSpecifier())
2079 DS.SetTypeSpecError();
2080 }
Mike Stump11289f42009-09-09 15:08:12 +00002081
Chris Lattner1b22eed2006-11-28 05:12:07 +00002082 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002083 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002084 if (DS.getStorageClassSpecLoc().isValid())
2085 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2086 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002087 Diag(DS.getThreadStorageClassSpecLoc(),
2088 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002089 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002090 }
Mike Stump11289f42009-09-09 15:08:12 +00002091
Chris Lattner1b22eed2006-11-28 05:12:07 +00002092 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002093 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002094 if (DS.isInlineSpecified())
2095 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2096 if (DS.isVirtualSpecified())
2097 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2098 if (DS.isExplicitSpecified())
2099 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002100 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002101 }
Richard Smithc5b05522012-03-12 07:56:15 +00002102
2103 // Issue diagnostic and remove constexpr specfier if present.
2104 if (DS.isConstexprSpecified()) {
2105 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2106 DS.ClearConstexprSpec();
2107 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002108}
Chris Lattner53361ac2006-08-10 05:19:57 +00002109
Chris Lattner6cc055a2009-04-12 20:42:31 +00002110/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2111/// specified token is valid after the identifier in a declarator which
2112/// immediately follows the declspec. For example, these things are valid:
2113///
2114/// int x [ 4]; // direct-declarator
2115/// int x ( int y); // direct-declarator
2116/// int(int x ) // direct-declarator
2117/// int x ; // simple-declaration
2118/// int x = 17; // init-declarator-list
2119/// int x , y; // init-declarator-list
2120/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002121/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002122/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002123///
2124/// This is not, because 'x' does not immediately follow the declspec (though
2125/// ')' happens to be valid anyway).
2126/// int (x)
2127///
2128static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2129 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2130 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002131 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002132}
2133
Chris Lattner20a0c612009-04-14 21:34:55 +00002134
2135/// ParseImplicitInt - This method is called when we have an non-typename
2136/// identifier in a declspec (which normally terminates the decl spec) when
2137/// the declspec has no type specifier. In this case, the declspec is either
2138/// malformed or is "implicit int" (in K&R and C89).
2139///
2140/// This method handles diagnosing this prettily and returns false if the
2141/// declspec is done being processed. If it recovers and thinks there may be
2142/// other pieces of declspec after it, it returns true.
2143///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002144bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002145 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002146 AccessSpecifier AS, DeclSpecContext DSC,
2147 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002148 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002149
Chris Lattner20a0c612009-04-14 21:34:55 +00002150 SourceLocation Loc = Tok.getLocation();
2151 // If we see an identifier that is not a type name, we normally would
2152 // parse it as the identifer being declared. However, when a typename
2153 // is typo'd or the definition is not included, this will incorrectly
2154 // parse the typename as the identifier name and fall over misparsing
2155 // later parts of the diagnostic.
2156 //
2157 // As such, we try to do some look-ahead in cases where this would
2158 // otherwise be an "implicit-int" case to see if this is invalid. For
2159 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2160 // an identifier with implicit int, we'd get a parse error because the
2161 // next token is obviously invalid for a type. Parse these as a case
2162 // with an invalid type specifier.
2163 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002164
Chris Lattner20a0c612009-04-14 21:34:55 +00002165 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002166 // error, do lookahead to try to do better recovery. This never applies
2167 // within a type specifier. Outside of C++, we allow this even if the
2168 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002169 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002170 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002171 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002172 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002173 // If this token is valid for implicit int, e.g. "static x = 4", then
2174 // we just avoid eating the identifier, so it will be parsed as the
2175 // identifier in the declarator.
2176 return false;
2177 }
Mike Stump11289f42009-09-09 15:08:12 +00002178
Richard Smitha952ebb2012-05-15 21:01:51 +00002179 if (getLangOpts().CPlusPlus &&
2180 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2181 // Don't require a type specifier if we have the 'auto' storage class
2182 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002183 if (SS)
2184 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002185 return false;
2186 }
2187
Chris Lattner20a0c612009-04-14 21:34:55 +00002188 // Otherwise, if we don't consume this token, we are going to emit an
2189 // error anyway. Try to recover from various common problems. Check
2190 // to see if this was a reference to a tag name without a tag specified.
2191 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002192 //
2193 // C++ doesn't need this, and isTagName doesn't take SS.
2194 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002195 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002196 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002197
Douglas Gregor0be31a22010-07-02 17:43:08 +00002198 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002199 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002200 case DeclSpec::TST_enum:
2201 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2202 case DeclSpec::TST_union:
2203 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2204 case DeclSpec::TST_struct:
2205 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002206 case DeclSpec::TST_interface:
2207 TagName="__interface"; FixitTagName = "__interface ";
2208 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002209 case DeclSpec::TST_class:
2210 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002211 }
Mike Stump11289f42009-09-09 15:08:12 +00002212
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002213 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002214 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2215 LookupResult R(Actions, TokenName, SourceLocation(),
2216 Sema::LookupOrdinaryName);
2217
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002218 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002219 << TokenName << TagName << getLangOpts().CPlusPlus
2220 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2221
2222 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2223 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2224 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002225 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002226 << TokenName << TagName;
2227 }
Mike Stump11289f42009-09-09 15:08:12 +00002228
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002229 // Parse this as a tag as if the missing tag were present.
2230 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002231 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002232 else
Richard Smithc5b05522012-03-12 07:56:15 +00002233 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002234 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002235 return true;
2236 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002237 }
Mike Stump11289f42009-09-09 15:08:12 +00002238
Richard Smithfe904f02012-05-15 21:29:55 +00002239 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002240 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002241 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2242 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002243 // Look ahead to the next token to try to figure out what this declaration
2244 // was supposed to be.
2245 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002246 case tok::l_paren: {
2247 // static x(4); // 'x' is not a type
2248 // x(int n); // 'x' is not a type
2249 // x (*p)[]; // 'x' is a type
2250 //
2251 // Since we're in an error case (or the rare 'implicit int in C++' MS
2252 // extension), we can afford to perform a tentative parse to determine
2253 // which case we're in.
2254 TentativeParsingAction PA(*this);
2255 ConsumeToken();
2256 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2257 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002258
2259 if (TPR != TPResult::False()) {
2260 // The identifier is followed by a parenthesized declarator.
2261 // It's supposed to be a type.
2262 break;
2263 }
2264
2265 // If we're in a context where we could be declaring a constructor,
2266 // check whether this is a constructor declaration with a bogus name.
2267 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2268 IdentifierInfo *II = Tok.getIdentifierInfo();
2269 if (Actions.isCurrentClassNameTypo(II, SS)) {
2270 Diag(Loc, diag::err_constructor_bad_name)
2271 << Tok.getIdentifierInfo() << II
2272 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2273 Tok.setIdentifierInfo(II);
2274 }
2275 }
2276 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002277 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002278 case tok::comma:
2279 case tok::equal:
2280 case tok::kw_asm:
2281 case tok::l_brace:
2282 case tok::l_square:
2283 case tok::semi:
2284 // This looks like a variable or function declaration. The type is
2285 // probably missing. We're done parsing decl-specifiers.
2286 if (SS)
2287 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2288 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002289
2290 default:
2291 // This is probably supposed to be a type. This includes cases like:
2292 // int f(itn);
2293 // struct S { unsinged : 4; };
2294 break;
2295 }
2296 }
2297
Chad Rosierc1183952012-06-26 22:30:43 +00002298 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002299 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002300 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002301 IdentifierInfo *II = Tok.getIdentifierInfo();
2302 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002303 // The action emitted a diagnostic, so we don't have to.
2304 if (T) {
2305 // The action has suggested that the type T could be used. Set that as
2306 // the type in the declaration specifiers, consume the would-be type
2307 // name token, and we're done.
2308 const char *PrevSpec;
2309 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002310 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002311 DS.SetRangeEnd(Tok.getLocation());
2312 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002313 // There may be other declaration specifiers after this.
2314 return true;
2315 } else if (II != Tok.getIdentifierInfo()) {
2316 // If no type was suggested, the correction is to a keyword
2317 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002318 // There may be other declaration specifiers after this.
2319 return true;
2320 }
Chad Rosierc1183952012-06-26 22:30:43 +00002321
Douglas Gregor15e56022009-10-13 23:27:22 +00002322 // Fall through; the action had no suggestion for us.
2323 } else {
2324 // The action did not emit a diagnostic, so emit one now.
2325 SourceRange R;
2326 if (SS) R = SS->getRange();
2327 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2328 }
Mike Stump11289f42009-09-09 15:08:12 +00002329
Douglas Gregor15e56022009-10-13 23:27:22 +00002330 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002331 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002332 DS.SetRangeEnd(Tok.getLocation());
2333 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002334
Chris Lattner20a0c612009-04-14 21:34:55 +00002335 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2336 // avoid rippling error messages on subsequent uses of the same type,
2337 // could be useful if #include was forgotten.
2338 return false;
2339}
2340
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002341/// \brief Determine the declaration specifier context from the declarator
2342/// context.
2343///
2344/// \param Context the declarator context, which is one of the
2345/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002346Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002347Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2348 if (Context == Declarator::MemberContext)
2349 return DSC_class;
2350 if (Context == Declarator::FileContext)
2351 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002352 if (Context == Declarator::TrailingReturnContext)
2353 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002354 return DSC_normal;
2355}
2356
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002357/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2358///
2359/// FIXME: Simply returns an alignof() expression if the argument is a
2360/// type. Ideally, the type should be propagated directly into Sema.
2361///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002362/// [C11] type-id
2363/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002364/// [C++0x] type-id ...[opt]
2365/// [C++0x] assignment-expression ...[opt]
2366ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2367 SourceLocation &EllipsisLoc) {
2368 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002369 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002370 SourceLocation TypeLoc = Tok.getLocation();
2371 ParsedType Ty = ParseTypeName().get();
2372 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002373 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2374 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002375 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002376 ER = ParseConstantExpression();
2377
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002378 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002379 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002380
2381 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002382}
2383
2384/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2385/// attribute to Attrs.
2386///
2387/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002388/// [C11] '_Alignas' '(' type-id ')'
2389/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002390/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2391/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002392void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002393 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002394 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2395 "Not an alignment-specifier!");
2396
Richard Smithd11c7a12013-01-29 01:48:07 +00002397 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2398 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002399
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002400 BalancedDelimiterTracker T(*this, tok::l_paren);
2401 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002402 return;
2403
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002404 SourceLocation EllipsisLoc;
2405 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002406 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002407 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002408 return;
2409 }
2410
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002411 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002412 if (EndLoc)
2413 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002414
Aaron Ballman00e99962013-08-31 01:11:41 +00002415 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002416 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002417 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2418 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002419}
2420
Richard Smith404dfb42013-11-19 22:47:36 +00002421/// Determine whether we're looking at something that might be a declarator
2422/// in a simple-declaration. If it can't possibly be a declarator, maybe
2423/// diagnose a missing semicolon after a prior tag definition in the decl
2424/// specifier.
2425///
2426/// \return \c true if an error occurred and this can't be any kind of
2427/// declaration.
2428bool
2429Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2430 DeclSpecContext DSContext,
2431 LateParsedAttrList *LateAttrs) {
2432 assert(DS.hasTagDefinition() && "shouldn't call this");
2433
2434 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002435
2436 if (getLangOpts().CPlusPlus &&
2437 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2438 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2439 TryAnnotateCXXScopeToken(EnteringContext)) {
2440 SkipMalformedDecl();
2441 return true;
2442 }
2443
Richard Smith698875a2013-11-20 23:40:57 +00002444 bool HasScope = Tok.is(tok::annot_cxxscope);
2445 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2446 Token AfterScope = HasScope ? NextToken() : Tok;
2447
Richard Smith404dfb42013-11-19 22:47:36 +00002448 // Determine whether the following tokens could possibly be a
2449 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002450 bool MightBeDeclarator = true;
2451 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2452 // A declarator-id can't start with 'typename'.
2453 MightBeDeclarator = false;
2454 } else if (AfterScope.is(tok::annot_template_id)) {
2455 // If we have a type expressed as a template-id, this cannot be a
2456 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2457 TemplateIdAnnotation *Annot =
2458 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2459 if (Annot->Kind == TNK_Type_template)
2460 MightBeDeclarator = false;
2461 } else if (AfterScope.is(tok::identifier)) {
2462 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2463
Richard Smith404dfb42013-11-19 22:47:36 +00002464 // These tokens cannot come after the declarator-id in a
2465 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002466 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2467 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2468 Next.is(tok::coloncolon)) {
2469 // Missing a semicolon.
2470 MightBeDeclarator = false;
2471 } else if (HasScope) {
2472 // If the declarator-id has a scope specifier, it must redeclare a
2473 // previously-declared entity. If that's a type (and this is not a
2474 // typedef), that's an error.
2475 CXXScopeSpec SS;
2476 Actions.RestoreNestedNameSpecifierAnnotation(
2477 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2478 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2479 Sema::NameClassification Classification = Actions.ClassifyName(
2480 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2481 /*IsAddressOfOperand*/false);
2482 switch (Classification.getKind()) {
2483 case Sema::NC_Error:
2484 SkipMalformedDecl();
2485 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002486
Richard Smith698875a2013-11-20 23:40:57 +00002487 case Sema::NC_Keyword:
2488 case Sema::NC_NestedNameSpecifier:
2489 llvm_unreachable("typo correction and nested name specifiers not "
2490 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002491
Richard Smith698875a2013-11-20 23:40:57 +00002492 case Sema::NC_Type:
2493 case Sema::NC_TypeTemplate:
2494 // Not a previously-declared non-type entity.
2495 MightBeDeclarator = false;
2496 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002497
Richard Smith698875a2013-11-20 23:40:57 +00002498 case Sema::NC_Unknown:
2499 case Sema::NC_Expression:
2500 case Sema::NC_VarTemplate:
2501 case Sema::NC_FunctionTemplate:
2502 // Might be a redeclaration of a prior entity.
2503 break;
2504 }
Richard Smith404dfb42013-11-19 22:47:36 +00002505 }
Richard Smith404dfb42013-11-19 22:47:36 +00002506 }
2507
Richard Smith698875a2013-11-20 23:40:57 +00002508 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002509 return false;
2510
2511 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
2512 diag::err_expected_semi_after_tagdecl)
2513 << DeclSpec::getSpecifierName(DS.getTypeSpecType());
2514
2515 // Try to recover from the typo, by dropping the tag definition and parsing
2516 // the problematic tokens as a type.
2517 //
2518 // FIXME: Split the DeclSpec into pieces for the standalone
2519 // declaration and pieces for the following declaration, instead
2520 // of assuming that all the other pieces attach to new declaration,
2521 // and call ParsedFreeStandingDeclSpec as appropriate.
2522 DS.ClearTypeSpecType();
2523 ParsedTemplateInfo NotATemplate;
2524 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2525 return false;
2526}
2527
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002528/// ParseDeclarationSpecifiers
2529/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002530/// storage-class-specifier declaration-specifiers[opt]
2531/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002532/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002533/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002534/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002535/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002536///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002537/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002538/// 'typedef'
2539/// 'extern'
2540/// 'static'
2541/// 'auto'
2542/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002543/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002544/// [C++11] 'thread_local'
2545/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002546/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002547/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002548/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002549/// [C++] 'virtual'
2550/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002551/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002552/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002553/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002554
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002555///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002556void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002557 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002558 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002559 DeclSpecContext DSContext,
2560 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002561 if (DS.getSourceRange().isInvalid()) {
2562 DS.SetRangeStart(Tok.getLocation());
2563 DS.SetRangeEnd(Tok.getLocation());
2564 }
Chad Rosierc1183952012-06-26 22:30:43 +00002565
Douglas Gregordf593fb2011-11-07 17:33:42 +00002566 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002567 bool AttrsLastTime = false;
2568 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002569 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002570 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002571 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002572 unsigned DiagID = 0;
2573
Chris Lattner4d8f8732006-11-28 05:05:08 +00002574 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002575
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002576 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002577 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002578 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002579 if (!AttrsLastTime)
2580 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002581 else {
2582 // Reject C++11 attributes that appertain to decl specifiers as
2583 // we don't support any C++11 attributes that appertain to decl
2584 // specifiers. This also conforms to what g++ 4.8 is doing.
2585 ProhibitCXX11Attributes(attrs);
2586
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002587 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002588 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002589
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002590 // If this is not a declaration specifier token, we're done reading decl
2591 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002592 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002593 return;
Mike Stump11289f42009-09-09 15:08:12 +00002594
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002595 case tok::l_square:
2596 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002597 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002598 goto DoneWithDeclSpec;
2599
2600 ProhibitAttributes(attrs);
2601 // FIXME: It would be good to recover by accepting the attributes,
2602 // but attempting to do that now would cause serious
2603 // madness in terms of diagnostics.
2604 attrs.clear();
2605 attrs.Range = SourceRange();
2606
2607 ParseCXX11Attributes(attrs);
2608 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002609 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002610
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002611 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002612 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002613 if (DS.hasTypeSpecifier()) {
2614 bool AllowNonIdentifiers
2615 = (getCurScope()->getFlags() & (Scope::ControlScope |
2616 Scope::BlockScope |
2617 Scope::TemplateParamScope |
2618 Scope::FunctionPrototypeScope |
2619 Scope::AtCatchScope)) == 0;
2620 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002621 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002622 (DSContext == DSC_class && DS.isFriendSpecified());
2623
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002624 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002625 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002626 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002627 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002628 }
2629
Douglas Gregor80039242011-02-15 20:33:25 +00002630 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2631 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2632 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002633 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002634 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002635 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002636 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002637 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002638 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002639
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002640 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002641 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002642 }
2643
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002644 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002645 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002646 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002647 if (!DS.hasTypeSpecifier())
2648 DS.SetTypeSpecError();
2649 goto DoneWithDeclSpec;
2650 }
John McCall8bc2a702010-03-01 18:20:46 +00002651 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2652 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002653 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002654
2655 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002656 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002657 goto DoneWithDeclSpec;
2658
John McCall9dab4e62009-12-12 11:40:51 +00002659 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002660 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2661 Tok.getAnnotationRange(),
2662 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002663
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002664 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002665 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002666 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002667 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002668 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002669 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002670
2671 // C++ [class.qual]p2:
2672 // In a lookup in which the constructor is an acceptable lookup
2673 // result and the nested-name-specifier nominates a class C:
2674 //
2675 // - if the name specified after the
2676 // nested-name-specifier, when looked up in C, is the
2677 // injected-class-name of C (Clause 9), or
2678 //
2679 // - if the name specified after the nested-name-specifier
2680 // is the same as the identifier or the
2681 // simple-template-id's template-name in the last
2682 // component of the nested-name-specifier,
2683 //
2684 // the name is instead considered to name the constructor of
2685 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002686 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002687 // Thus, if the template-name is actually the constructor
2688 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002689 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002690 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002691 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002692 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002693 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002694 if (isConstructorDeclarator()) {
2695 // The user meant this to be an out-of-line constructor
2696 // definition, but template arguments are not allowed
2697 // there. Just allow this as a constructor; we'll
2698 // complain about it later.
2699 goto DoneWithDeclSpec;
2700 }
2701
2702 // The user meant this to name a type, but it actually names
2703 // a constructor with some extraneous template
2704 // arguments. Complain, then parse it as a type as the user
2705 // intended.
2706 Diag(TemplateId->TemplateNameLoc,
2707 diag::err_out_of_line_template_id_names_constructor)
2708 << TemplateId->Name;
2709 }
2710
John McCall9dab4e62009-12-12 11:40:51 +00002711 DS.getTypeSpecScope() = SS;
2712 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002713 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002714 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002715 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002716 continue;
2717 }
2718
Douglas Gregorc5790df2009-09-28 07:26:33 +00002719 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002720 DS.getTypeSpecScope() = SS;
2721 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002722 if (Tok.getAnnotationValue()) {
2723 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002725 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002726 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002727 if (isInvalid)
2728 break;
John McCallba7bf592010-08-24 05:47:05 +00002729 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002730 else
2731 DS.SetTypeSpecError();
2732 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2733 ConsumeToken(); // The typename
2734 }
2735
Douglas Gregor167fa622009-03-25 15:40:00 +00002736 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002737 goto DoneWithDeclSpec;
2738
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002739 // If we're in a context where the identifier could be a class name,
2740 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002741 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002742 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002743 &SS)) {
2744 if (isConstructorDeclarator())
2745 goto DoneWithDeclSpec;
2746
2747 // As noted in C++ [class.qual]p2 (cited above), when the name
2748 // of the class is qualified in a context where it could name
2749 // a constructor, its a constructor name. However, we've
2750 // looked at the declarator, and the user probably meant this
2751 // to be a type. Complain that it isn't supposed to be treated
2752 // as a type, then proceed to parse it as a type.
2753 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2754 << Next.getIdentifierInfo();
2755 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002756
John McCallba7bf592010-08-24 05:47:05 +00002757 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2758 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002759 getCurScope(), &SS,
2760 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002761 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002762 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002763
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002764 // If the referenced identifier is not a type, then this declspec is
2765 // erroneous: We already checked about that it has no type specifier, and
2766 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002767 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002768 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002769 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002770 ParsedAttributesWithRange Attrs(AttrFactory);
2771 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2772 if (!Attrs.empty()) {
2773 AttrsLastTime = true;
2774 attrs.takeAllFrom(Attrs);
2775 }
2776 continue;
2777 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002778 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002779 }
Mike Stump11289f42009-09-09 15:08:12 +00002780
John McCall9dab4e62009-12-12 11:40:51 +00002781 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002782 ConsumeToken(); // The C++ scope.
2783
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002784 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002785 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002786 if (isInvalid)
2787 break;
Mike Stump11289f42009-09-09 15:08:12 +00002788
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002789 DS.SetRangeEnd(Tok.getLocation());
2790 ConsumeToken(); // The typename.
2791
2792 continue;
2793 }
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattnere387d9e2009-01-21 19:48:37 +00002795 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002796 // If we've previously seen a tag definition, we were almost surely
2797 // missing a semicolon after it.
2798 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2799 goto DoneWithDeclSpec;
2800
John McCallba7bf592010-08-24 05:47:05 +00002801 if (Tok.getAnnotationValue()) {
2802 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002803 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002804 DiagID, T);
2805 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002806 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002807
Chris Lattner005fc1b2010-04-05 18:18:31 +00002808 if (isInvalid)
2809 break;
2810
Chris Lattnere387d9e2009-01-21 19:48:37 +00002811 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2812 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002813
Chris Lattnere387d9e2009-01-21 19:48:37 +00002814 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2815 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002816 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002817 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002818 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002819
Chris Lattnere387d9e2009-01-21 19:48:37 +00002820 continue;
2821 }
Mike Stump11289f42009-09-09 15:08:12 +00002822
Douglas Gregor06873092011-04-28 15:48:45 +00002823 case tok::kw___is_signed:
2824 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2825 // typically treats it as a trait. If we see __is_signed as it appears
2826 // in libstdc++, e.g.,
2827 //
2828 // static const bool __is_signed;
2829 //
2830 // then treat __is_signed as an identifier rather than as a keyword.
2831 if (DS.getTypeSpecType() == TST_bool &&
2832 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002833 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2834 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002835
2836 // We're done with the declaration-specifiers.
2837 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002838
Chris Lattner16fac4f2008-07-26 01:18:38 +00002839 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002840 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002841 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002842 // In C++, check to see if this is a scope specifier like foo::bar::, if
2843 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002844 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002845 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002846 if (!DS.hasTypeSpecifier())
2847 DS.SetTypeSpecError();
2848 goto DoneWithDeclSpec;
2849 }
2850 if (!Tok.is(tok::identifier))
2851 continue;
2852 }
Mike Stump11289f42009-09-09 15:08:12 +00002853
Chris Lattner16fac4f2008-07-26 01:18:38 +00002854 // This identifier can only be a typedef name if we haven't already seen
2855 // a type-specifier. Without this check we misparse:
2856 // typedef int X; struct Y { short X; }; as 'short int'.
2857 if (DS.hasTypeSpecifier())
2858 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002859
John Thompson22334602010-02-05 00:12:22 +00002860 // Check for need to substitute AltiVec keyword tokens.
2861 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2862 break;
2863
Richard Smith3092a3b2012-05-09 18:56:43 +00002864 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2865 // allow the use of a typedef name as a type specifier.
2866 if (DS.isTypeAltiVecVector())
2867 goto DoneWithDeclSpec;
2868
John McCallba7bf592010-08-24 05:47:05 +00002869 ParsedType TypeRep =
2870 Actions.getTypeName(*Tok.getIdentifierInfo(),
2871 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002872
Chris Lattner6cc055a2009-04-12 20:42:31 +00002873 // If this is not a typedef name, don't parse it as part of the declspec,
2874 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002875 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002876 ParsedAttributesWithRange Attrs(AttrFactory);
2877 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2878 if (!Attrs.empty()) {
2879 AttrsLastTime = true;
2880 attrs.takeAllFrom(Attrs);
2881 }
2882 continue;
2883 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002884 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002885 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002886
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002887 // If we're in a context where the identifier could be a class name,
2888 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002889 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002890 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002891 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002892 goto DoneWithDeclSpec;
2893
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002894 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002895 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002896 if (isInvalid)
2897 break;
Mike Stump11289f42009-09-09 15:08:12 +00002898
Chris Lattner16fac4f2008-07-26 01:18:38 +00002899 DS.SetRangeEnd(Tok.getLocation());
2900 ConsumeToken(); // The identifier
2901
2902 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2903 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002904 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002905 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002906 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002907
Steve Naroffcd5e7822008-09-22 10:28:57 +00002908 // Need to support trailing type qualifiers (e.g. "id<p> const").
2909 // If a type specifier follows, it will be diagnosed elsewhere.
2910 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002911 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002912
2913 // type-name
2914 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002915 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002916 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002917 // This template-id does not refer to a type name, so we're
2918 // done with the type-specifiers.
2919 goto DoneWithDeclSpec;
2920 }
2921
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002922 // If we're in a context where the template-id could be a
2923 // constructor name or specialization, check whether this is a
2924 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002925 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002926 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002927 isConstructorDeclarator())
2928 goto DoneWithDeclSpec;
2929
Douglas Gregor7f741122009-02-25 19:37:18 +00002930 // Turn the template-id annotation token into a type annotation
2931 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002932 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002933 continue;
2934 }
2935
Chris Lattnere37e2332006-08-15 04:50:22 +00002936 // GNU attributes support.
2937 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002938 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002939 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002940
2941 // Microsoft declspec support.
2942 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002943 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002944 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002945
Steve Naroff44ac7772008-12-25 14:16:32 +00002946 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002947 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002948 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002949 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002950 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002951 // FIXME: This does not work correctly if it is set to be a declspec
2952 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002953 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2954 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002955 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002956 }
Eli Friedman53339e02009-06-08 23:27:34 +00002957
Aaron Ballman317a77f2013-05-22 23:25:32 +00002958 case tok::kw___sptr:
2959 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002960 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002961 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002962 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002963 case tok::kw___cdecl:
2964 case tok::kw___stdcall:
2965 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002966 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002967 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002968 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002969 continue;
2970
Dawn Perchik335e16b2010-09-03 01:29:35 +00002971 // Borland single token adornments.
2972 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002973 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002974 continue;
2975
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002976 // OpenCL single token adornments.
2977 case tok::kw___kernel:
2978 ParseOpenCLAttributes(DS.getAttributes());
2979 continue;
2980
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002981 // storage-class-specifier
2982 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002983 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2984 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002985 break;
2986 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002987 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002988 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002989 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2990 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002991 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002992 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002993 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2994 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002995 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002996 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002997 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002998 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002999 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
3000 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003001 break;
3002 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003003 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003004 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003005 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
3006 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003007 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00003008 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003009 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00003010 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003011 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
3012 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00003013 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003014 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
3015 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003016 break;
3017 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003018 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
3019 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003020 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003021 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003022 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
3023 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003024 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003025 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00003026 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3027 PrevSpec, DiagID);
3028 break;
3029 case tok::kw_thread_local:
3030 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3031 PrevSpec, DiagID);
3032 break;
3033 case tok::kw__Thread_local:
3034 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3035 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003036 break;
Mike Stump11289f42009-09-09 15:08:12 +00003037
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003038 // function-specifier
3039 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003040 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003041 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003042 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00003043 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003044 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003045 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00003046 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003047 break;
Richard Smith0015f092013-01-17 22:16:11 +00003048 case tok::kw__Noreturn:
3049 if (!getLangOpts().C11)
3050 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003051 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003052 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003053
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003054 // alignment-specifier
3055 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003056 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003057 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003058 ParseAlignmentSpecifier(DS.getAttributes());
3059 continue;
3060
Anders Carlssoncd8db412009-05-06 04:46:28 +00003061 // friend
3062 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00003063 if (DSContext == DSC_class)
3064 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3065 else {
3066 PrevSpec = ""; // not actually used by the diagnostic
3067 DiagID = diag::err_friend_invalid_in_context;
3068 isInvalid = true;
3069 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003070 break;
Mike Stump11289f42009-09-09 15:08:12 +00003071
Douglas Gregor26701a42011-09-09 02:06:17 +00003072 // Modules
3073 case tok::kw___module_private__:
3074 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3075 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003076
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003077 // constexpr
3078 case tok::kw_constexpr:
3079 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3080 break;
3081
Chris Lattnere387d9e2009-01-21 19:48:37 +00003082 // type-specifier
3083 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003084 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
3085 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003086 break;
3087 case tok::kw_long:
3088 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003089 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3090 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003091 else
John McCall49bfce42009-08-03 20:12:06 +00003092 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3093 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003094 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003095 case tok::kw___int64:
3096 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3097 DiagID);
3098 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003099 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003100 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3101 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003102 break;
3103 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003104 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3105 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003106 break;
3107 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003108 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3109 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003110 break;
3111 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003112 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3113 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003114 break;
3115 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3117 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003118 break;
3119 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3121 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003122 break;
3123 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3125 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003126 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003127 case tok::kw___int128:
3128 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3129 DiagID);
3130 break;
3131 case tok::kw_half:
3132 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3133 DiagID);
3134 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003135 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003136 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3137 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003138 break;
3139 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003140 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3141 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003142 break;
3143 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003144 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3145 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003146 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003147 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3149 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003150 break;
3151 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003152 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3153 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003154 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003155 case tok::kw_bool:
3156 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003157 if (Tok.is(tok::kw_bool) &&
3158 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3159 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3160 PrevSpec = ""; // Not used by the diagnostic.
3161 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003162 // For better error recovery.
3163 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003164 isInvalid = true;
3165 } else {
3166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3167 DiagID);
3168 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003169 break;
3170 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003171 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3172 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003173 break;
3174 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003175 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3176 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003177 break;
3178 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003179 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3180 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003181 break;
John Thompson22334602010-02-05 00:12:22 +00003182 case tok::kw___vector:
3183 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3184 break;
3185 case tok::kw___pixel:
3186 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3187 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003188 case tok::kw_image1d_t:
3189 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
3190 PrevSpec, DiagID);
3191 break;
3192 case tok::kw_image1d_array_t:
3193 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
3194 PrevSpec, DiagID);
3195 break;
3196 case tok::kw_image1d_buffer_t:
3197 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3198 PrevSpec, DiagID);
3199 break;
3200 case tok::kw_image2d_t:
3201 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3202 PrevSpec, DiagID);
3203 break;
3204 case tok::kw_image2d_array_t:
3205 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3206 PrevSpec, DiagID);
3207 break;
3208 case tok::kw_image3d_t:
3209 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3210 PrevSpec, DiagID);
3211 break;
Guy Benyei61054192013-02-07 10:55:47 +00003212 case tok::kw_sampler_t:
3213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3214 PrevSpec, DiagID);
3215 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003216 case tok::kw_event_t:
3217 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3218 PrevSpec, DiagID);
3219 break;
John McCall39439732011-04-09 22:50:59 +00003220 case tok::kw___unknown_anytype:
3221 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3222 PrevSpec, DiagID);
3223 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003224
3225 // class-specifier:
3226 case tok::kw_class:
3227 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003228 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003229 case tok::kw_union: {
3230 tok::TokenKind Kind = Tok.getKind();
3231 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003232
3233 // These are attributes following class specifiers.
3234 // To produce better diagnostic, we parse them when
3235 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003236 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003237 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003238 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003239
3240 // If there are attributes following class specifier,
3241 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003242 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003243 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003244 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003245 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003246 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003247 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003248
3249 // enum-specifier:
3250 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003251 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003252 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003253 continue;
3254
3255 // cv-qualifier:
3256 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003257 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003258 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003259 break;
3260 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003261 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003262 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003263 break;
3264 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003265 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003266 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003267 break;
3268
Douglas Gregor333489b2009-03-27 23:10:48 +00003269 // C++ typename-specifier:
3270 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003271 if (TryAnnotateTypeOrScopeToken()) {
3272 DS.SetTypeSpecError();
3273 goto DoneWithDeclSpec;
3274 }
3275 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003276 continue;
3277 break;
3278
Chris Lattnere387d9e2009-01-21 19:48:37 +00003279 // GNU typeof support.
3280 case tok::kw_typeof:
3281 ParseTypeofSpecifier(DS);
3282 continue;
3283
David Blaikie15a430a2011-12-04 05:04:18 +00003284 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003285 ParseDecltypeSpecifier(DS);
3286 continue;
3287
Alexis Hunt4a257072011-05-19 05:37:45 +00003288 case tok::kw___underlying_type:
3289 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003290 continue;
3291
3292 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003293 // C11 6.7.2.4/4:
3294 // If the _Atomic keyword is immediately followed by a left parenthesis,
3295 // it is interpreted as a type specifier (with a type name), not as a
3296 // type qualifier.
3297 if (NextToken().is(tok::l_paren)) {
3298 ParseAtomicSpecifier(DS);
3299 continue;
3300 }
3301 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3302 getLangOpts());
3303 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003304
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003305 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003306 case tok::kw___private:
3307 case tok::kw___global:
3308 case tok::kw___local:
3309 case tok::kw___constant:
3310 case tok::kw___read_only:
3311 case tok::kw___write_only:
3312 case tok::kw___read_write:
3313 ParseOpenCLQualifiers(DS);
3314 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003315
Steve Naroffcfdf6162008-06-05 00:02:44 +00003316 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003317 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003318 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3319 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003320 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003321 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003322
Douglas Gregor3a001f42010-11-19 17:10:50 +00003323 if (!ParseObjCProtocolQualifiers(DS))
3324 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3325 << FixItHint::CreateInsertion(Loc, "id")
3326 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003327
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003328 // Need to support trailing type qualifiers (e.g. "id<p> const").
3329 // If a type specifier follows, it will be diagnosed elsewhere.
3330 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003331 }
John McCall49bfce42009-08-03 20:12:06 +00003332 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003333 if (isInvalid) {
3334 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003335 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003336
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003337 if (DiagID == diag::ext_duplicate_declspec)
3338 Diag(Tok, DiagID)
3339 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3340 else
3341 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003342 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003343
Chris Lattner2e232092008-03-13 06:29:04 +00003344 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003345 if (DiagID != diag::err_bool_redeclaration)
3346 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003347
3348 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003349 }
3350}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003351
Chris Lattner70ae4912007-10-29 04:42:53 +00003352/// ParseStructDeclaration - Parse a struct declaration without the terminating
3353/// semicolon.
3354///
Chris Lattner90a26b02007-01-23 04:38:16 +00003355/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003356/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003357/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003358/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003359/// struct-declarator-list:
3360/// struct-declarator
3361/// struct-declarator-list ',' struct-declarator
3362/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3363/// struct-declarator:
3364/// declarator
3365/// [GNU] declarator attributes[opt]
3366/// declarator[opt] ':' constant-expression
3367/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3368///
Chris Lattnera12405b2008-04-10 06:46:29 +00003369void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003370ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003371
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003372 if (Tok.is(tok::kw___extension__)) {
3373 // __extension__ silences extension warnings in the subexpression.
3374 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003375 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003376 return ParseStructDeclaration(DS, Fields);
3377 }
Mike Stump11289f42009-09-09 15:08:12 +00003378
Steve Naroff97170802007-08-20 22:28:22 +00003379 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003380 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003381
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003382 // If there are no declarators, this is a free-standing declaration
3383 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003384 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003385 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3386 DS);
3387 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003388 return;
3389 }
3390
3391 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003392 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003393 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003394 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003395 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003396 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003397
Bill Wendling44426052012-12-20 19:22:21 +00003398 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003399 if (!FirstDeclarator)
3400 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003401
Steve Naroff97170802007-08-20 22:28:22 +00003402 /// struct-declarator: declarator
3403 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003404 if (Tok.isNot(tok::colon)) {
3405 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3406 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003407 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003408 }
Mike Stump11289f42009-09-09 15:08:12 +00003409
Chris Lattner76c72282007-10-09 17:33:22 +00003410 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003411 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003412 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003413 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003414 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003415 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003416 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003417 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003418
Steve Naroff97170802007-08-20 22:28:22 +00003419 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003420 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003421
John McCallcfefb6d2009-11-03 02:38:08 +00003422 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003423 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003424
Steve Naroff97170802007-08-20 22:28:22 +00003425 // If we don't have a comma, it is either the end of the list (a ';')
3426 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003427 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003428 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003429
Steve Naroff97170802007-08-20 22:28:22 +00003430 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003431 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003432
John McCallcfefb6d2009-11-03 02:38:08 +00003433 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003434 }
Steve Naroff97170802007-08-20 22:28:22 +00003435}
3436
3437/// ParseStructUnionBody
3438/// struct-contents:
3439/// struct-declaration-list
3440/// [EXT] empty
3441/// [GNU] "struct-declaration-list" without terminatoring ';'
3442/// struct-declaration-list:
3443/// struct-declaration
3444/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003445/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003446///
Chris Lattner1300fb92007-01-23 23:42:53 +00003447void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003448 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003449 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3450 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003451 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003452
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003453 BalancedDelimiterTracker T(*this, tok::l_brace);
3454 if (T.consumeOpen())
3455 return;
Mike Stump11289f42009-09-09 15:08:12 +00003456
Douglas Gregor658b9552009-01-09 22:42:13 +00003457 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003458 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003459
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003460 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003461
Chris Lattner7b9ace62007-01-23 20:11:08 +00003462 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003463 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003464 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003465
Chris Lattner736ed5d2007-06-09 05:59:07 +00003466 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003467 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003468 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003469 continue;
3470 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003471
Andy Gibbsc804e082013-04-03 09:46:04 +00003472 // Parse _Static_assert declaration.
3473 if (Tok.is(tok::kw__Static_assert)) {
3474 SourceLocation DeclEnd;
3475 ParseStaticAssertDeclaration(DeclEnd);
3476 continue;
3477 }
3478
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003479 if (Tok.is(tok::annot_pragma_pack)) {
3480 HandlePragmaPack();
3481 continue;
3482 }
3483
3484 if (Tok.is(tok::annot_pragma_align)) {
3485 HandlePragmaAlign();
3486 continue;
3487 }
3488
John McCallcfefb6d2009-11-03 02:38:08 +00003489 if (!Tok.is(tok::at)) {
3490 struct CFieldCallback : FieldCallback {
3491 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003492 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003493 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003494
John McCall48871652010-08-21 09:40:31 +00003495 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003496 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003497 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3498
Eli Friedman934dbbf2012-08-08 23:53:27 +00003499 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003500 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003501 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003502 FD.D.getDeclSpec().getSourceRange().getBegin(),
3503 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003504 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003505 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003506 }
John McCallcfefb6d2009-11-03 02:38:08 +00003507 } Callback(*this, TagDecl, FieldDecls);
3508
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003509 // Parse all the comma separated declarators.
3510 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003511 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003512 } else { // Handle @defs
3513 ConsumeToken();
3514 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3515 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003516 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003517 continue;
3518 }
3519 ConsumeToken();
3520 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3521 if (!Tok.is(tok::identifier)) {
3522 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003523 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003524 continue;
3525 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003526 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003527 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003528 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003529 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3530 ConsumeToken();
3531 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003532 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003533
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003534 if (TryConsumeToken(tok::semi))
3535 continue;
3536
3537 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003538 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003539 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003540 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003541
3542 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3543 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3544 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3545 // If we stopped at a ';', eat it.
3546 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003547 }
Mike Stump11289f42009-09-09 15:08:12 +00003548
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003549 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003550
John McCall084e83d2011-03-24 11:26:52 +00003551 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003552 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003553 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003554
Douglas Gregor0be31a22010-07-02 17:43:08 +00003555 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003556 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003557 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003558 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003559 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003560 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3561 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003562}
3563
Chris Lattner3b561a32006-08-13 00:12:11 +00003564/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003565/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003566/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003567///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003568/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3569/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003570/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3571/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003572/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003573/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003574///
Richard Smith7d137e32012-03-23 03:33:32 +00003575/// [C++11] enum-head '{' enumerator-list[opt] '}'
3576/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003577///
Richard Smith7d137e32012-03-23 03:33:32 +00003578/// enum-head: [C++11]
3579/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3580/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3581/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003582///
Richard Smith7d137e32012-03-23 03:33:32 +00003583/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003584/// 'enum'
3585/// 'enum' 'class'
3586/// 'enum' 'struct'
3587///
Richard Smith7d137e32012-03-23 03:33:32 +00003588/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003589/// ':' type-specifier-seq
3590///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003591/// [C++] elaborated-type-specifier:
3592/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3593///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003594void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003595 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003596 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003597 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003598 if (Tok.is(tok::code_completion)) {
3599 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003600 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003601 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003602 }
John McCallcb432fa2011-07-06 05:58:41 +00003603
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003604 // If attributes exist after tag, parse them.
3605 ParsedAttributesWithRange attrs(AttrFactory);
3606 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003607 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003608
3609 // If declspecs exist after tag, parse them.
3610 while (Tok.is(tok::kw___declspec))
3611 ParseMicrosoftDeclSpec(attrs);
3612
Richard Smith0f8ee222012-01-10 01:33:14 +00003613 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003614 bool IsScopedUsingClassTag = false;
3615
John McCallbeae29a2012-06-23 22:30:04 +00003616 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003617 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3618 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3619 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003620 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003621 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003622
Bill Wendling44426052012-12-20 19:22:21 +00003623 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003624 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003625 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003626
3627 // They are allowed afterwards, though.
3628 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003629 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003630 while (Tok.is(tok::kw___declspec))
3631 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003632 }
Richard Smith7d137e32012-03-23 03:33:32 +00003633
John McCall6347b682012-05-07 06:16:58 +00003634 // C++11 [temp.explicit]p12:
3635 // The usual access controls do not apply to names used to specify
3636 // explicit instantiations.
3637 // We extend this to also cover explicit specializations. Note that
3638 // we don't suppress if this turns out to be an elaborated type
3639 // specifier.
3640 bool shouldDelayDiagsInTag =
3641 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3642 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3643 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003644
Richard Smithbfdb1082012-03-12 08:56:40 +00003645 // Enum definitions should not be parsed in a trailing-return-type.
3646 bool AllowDeclaration = DSC != DSC_trailing;
3647
3648 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003649 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003650 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003651
Abramo Bagnarad7548482010-05-19 21:37:53 +00003652 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003653 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003654 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3655 // if a fixed underlying type is allowed.
3656 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003657
3658 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003659 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003660 return;
3661
3662 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003663 Diag(Tok, diag::err_expected_ident);
3664 if (Tok.isNot(tok::l_brace)) {
3665 // Has no name and is not a definition.
3666 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003667 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003668 return;
3669 }
3670 }
3671 }
Mike Stump11289f42009-09-09 15:08:12 +00003672
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003673 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003674 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003675 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003676 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003677
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003678 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003679 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003680 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003681 }
Mike Stump11289f42009-09-09 15:08:12 +00003682
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003683 // If an identifier is present, consume and remember it.
3684 IdentifierInfo *Name = 0;
3685 SourceLocation NameLoc;
3686 if (Tok.is(tok::identifier)) {
3687 Name = Tok.getIdentifierInfo();
3688 NameLoc = ConsumeToken();
3689 }
Mike Stump11289f42009-09-09 15:08:12 +00003690
Richard Smith0f8ee222012-01-10 01:33:14 +00003691 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003692 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3693 // declaration of a scoped enumeration.
3694 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003695 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003696 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003697 }
3698
John McCall6347b682012-05-07 06:16:58 +00003699 // Okay, end the suppression area. We'll decide whether to emit the
3700 // diagnostics in a second.
3701 if (shouldDelayDiagsInTag)
3702 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003703
Douglas Gregor0bf31402010-10-08 23:50:27 +00003704 TypeResult BaseType;
3705
Douglas Gregord1f69f62010-12-01 17:42:47 +00003706 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003707 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003708 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003709 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003710 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003711 // If we're in class scope, this can either be an enum declaration with
3712 // an underlying type, or a declaration of a bitfield member. We try to
3713 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003714 // (integer literal, sizeof); if it's still ambiguous, we then consider
3715 // anything that's a simple-type-specifier followed by '(' as an
3716 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003717 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003718 EnterExpressionEvaluationContext Unevaluated(Actions,
3719 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003720 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003721 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003722 // bit-field. This is the common case.
3723 if (TPR == TPResult::True())
3724 PossibleBitfield = true;
3725 // If the next token starts a type-specifier-seq, it may be either a
3726 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003727 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003728 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003729 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003730 GetLookAheadToken(2).getKind() == tok::semi) {
3731 // Consume the ':'.
3732 ConsumeToken();
3733 } else {
3734 // We have the start of a type-specifier-seq, so we have to perform
3735 // tentative parsing to determine whether we have an expression or a
3736 // type.
3737 TentativeParsingAction TPA(*this);
3738
3739 // Consume the ':'.
3740 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003741
3742 // If we see a type specifier followed by an open-brace, we have an
3743 // ambiguity between an underlying type and a C++11 braced
3744 // function-style cast. Resolve this by always treating it as an
3745 // underlying type.
3746 // FIXME: The standard is not entirely clear on how to disambiguate in
3747 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003748 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003749 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003750 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003751 // We'll parse this as a bitfield later.
3752 PossibleBitfield = true;
3753 TPA.Revert();
3754 } else {
3755 // We have a type-specifier-seq.
3756 TPA.Commit();
3757 }
3758 }
3759 } else {
3760 // Consume the ':'.
3761 ConsumeToken();
3762 }
3763
3764 if (!PossibleBitfield) {
3765 SourceRange Range;
3766 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003767
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003768 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003769 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003770 } else if (!getLangOpts().ObjC2) {
3771 if (getLangOpts().CPlusPlus)
3772 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3773 else
3774 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3775 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003776 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003777 }
3778
Richard Smith0f8ee222012-01-10 01:33:14 +00003779 // There are four options here. If we have 'friend enum foo;' then this is a
3780 // friend declaration, and cannot have an accompanying definition. If we have
3781 // 'enum foo;', then this is a forward declaration. If we have
3782 // 'enum foo {...' then this is a definition. Otherwise we have something
3783 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003784 //
3785 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3786 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3787 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3788 //
John McCallfaf5fb42010-08-26 23:41:50 +00003789 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003790 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003791 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003792 } else if (Tok.is(tok::l_brace)) {
3793 if (DS.isFriendSpecified()) {
3794 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3795 << SourceRange(DS.getFriendSpecLoc());
3796 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003797 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003798 TUK = Sema::TUK_Friend;
3799 } else {
3800 TUK = Sema::TUK_Definition;
3801 }
Richard Smith369b9f92012-06-25 21:37:02 +00003802 } else if (DSC != DSC_type_specifier &&
3803 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003804 (Tok.isAtStartOfLine() &&
3805 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003806 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3807 if (Tok.isNot(tok::semi)) {
3808 // A semicolon was missing after this declaration. Diagnose and recover.
3809 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3810 "enum");
3811 PP.EnterToken(Tok);
3812 Tok.setKind(tok::semi);
3813 }
John McCall6347b682012-05-07 06:16:58 +00003814 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003815 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003816 }
3817
3818 // If this is an elaborated type specifier, and we delayed
3819 // diagnostics before, just merge them into the current pool.
3820 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3821 diagsFromTag.redelay();
3822 }
Richard Smith7d137e32012-03-23 03:33:32 +00003823
3824 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003825 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003826 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003827 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003828 // Skip the rest of this declarator, up until the comma or semicolon.
3829 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003830 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003831 return;
3832 }
3833
3834 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3835 // Enumerations can't be explicitly instantiated.
3836 DS.SetTypeSpecError();
3837 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3838 return;
3839 }
3840
3841 assert(TemplateInfo.TemplateParams && "no template parameters");
3842 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3843 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003844 }
Chad Rosierc1183952012-06-26 22:30:43 +00003845
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003846 if (TUK == Sema::TUK_Reference)
3847 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003848
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003849 if (!Name && TUK != Sema::TUK_Definition) {
3850 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003851
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003852 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003853 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003854 return;
3855 }
Richard Smith7d137e32012-03-23 03:33:32 +00003856
Douglas Gregord6ab8742009-05-28 23:31:59 +00003857 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003858 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003859 const char *PrevSpec = 0;
3860 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003861 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003862 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003863 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003864 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003865 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003866
Douglas Gregorba41d012010-04-24 16:38:41 +00003867 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003868 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003869 // dependent tag.
3870 if (!Name) {
3871 DS.SetTypeSpecError();
3872 Diag(Tok, diag::err_expected_type_name_after_typename);
3873 return;
3874 }
Chad Rosierc1183952012-06-26 22:30:43 +00003875
Douglas Gregor0be31a22010-07-02 17:43:08 +00003876 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003877 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003878 NameLoc);
3879 if (Type.isInvalid()) {
3880 DS.SetTypeSpecError();
3881 return;
3882 }
Chad Rosierc1183952012-06-26 22:30:43 +00003883
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003884 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3885 NameLoc.isValid() ? NameLoc : StartLoc,
3886 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003887 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003888
Douglas Gregorba41d012010-04-24 16:38:41 +00003889 return;
3890 }
Mike Stump11289f42009-09-09 15:08:12 +00003891
John McCall48871652010-08-21 09:40:31 +00003892 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003893 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003894 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003895 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003896 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003897 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003898 }
Chad Rosierc1183952012-06-26 22:30:43 +00003899
Douglas Gregorba41d012010-04-24 16:38:41 +00003900 DS.SetTypeSpecError();
3901 return;
3902 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003903
Richard Smith369b9f92012-06-25 21:37:02 +00003904 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003905 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003906
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003907 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3908 NameLoc.isValid() ? NameLoc : StartLoc,
3909 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003910 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003911}
3912
Chris Lattnerc1915e22007-01-25 07:29:02 +00003913/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3914/// enumerator-list:
3915/// enumerator
3916/// enumerator-list ',' enumerator
3917/// enumerator:
3918/// enumeration-constant
3919/// enumeration-constant '=' constant-expression
3920/// enumeration-constant:
3921/// identifier
3922///
John McCall48871652010-08-21 09:40:31 +00003923void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003924 // Enter the scope of the enum body and start the definition.
3925 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003926 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003927
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003928 BalancedDelimiterTracker T(*this, tok::l_brace);
3929 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003930
Chris Lattner37256fb2007-08-27 17:24:30 +00003931 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003932 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003933 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003934
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003935 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003936
John McCall48871652010-08-21 09:40:31 +00003937 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003938
Chris Lattnerc1915e22007-01-25 07:29:02 +00003939 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003940 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003941 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3942 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003943
John McCall811a0f52010-10-22 23:36:17 +00003944 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003945 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003946 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003947 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003948 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003949
Chris Lattnerc1915e22007-01-25 07:29:02 +00003950 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003951 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003952 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003953
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003954 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003955 AssignedVal = ParseConstantExpression();
3956 if (AssignedVal.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003957 SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003958 }
Mike Stump11289f42009-09-09 15:08:12 +00003959
Chris Lattnerc1915e22007-01-25 07:29:02 +00003960 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003961 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3962 LastEnumConstDecl,
3963 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003964 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003965 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003966 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003967
Chris Lattner4ef40012007-06-11 01:28:17 +00003968 EnumConstantDecls.push_back(EnumConstDecl);
3969 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003970
Douglas Gregorce66d022010-09-07 14:51:08 +00003971 if (Tok.is(tok::identifier)) {
3972 // We're missing a comma between enumerators.
3973 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003974 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003975 << FixItHint::CreateInsertion(Loc, ", ");
3976 continue;
3977 }
Chad Rosierc1183952012-06-26 22:30:43 +00003978
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003979 SourceLocation CommaLoc;
3980 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003981 break;
Mike Stump11289f42009-09-09 15:08:12 +00003982
Richard Smith5d164bc2011-10-15 05:09:34 +00003983 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003984 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003985 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3986 diag::ext_enumerator_list_comma_cxx :
3987 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003988 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003989 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003990 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3991 << FixItHint::CreateRemoval(CommaLoc);
3992 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003993 }
Mike Stump11289f42009-09-09 15:08:12 +00003994
Chris Lattnerc1915e22007-01-25 07:29:02 +00003995 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003996 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003997
Chris Lattnerc1915e22007-01-25 07:29:02 +00003998 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003999 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004000 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004001
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004002 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00004003 EnumDecl, EnumConstantDecls,
4004 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004005 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00004006
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004007 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004008 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
4009 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00004010
4011 // The next token must be valid after an enum definition. If not, a ';'
4012 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00004013 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
4014 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00004015 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
4016 // Push this token back into the preprocessor and change our current token
4017 // to ';' so that the rest of the code recovers as though there were an
4018 // ';' after the definition.
4019 PP.EnterToken(Tok);
4020 Tok.setKind(tok::semi);
4021 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004022}
Chris Lattner3b561a32006-08-13 00:12:11 +00004023
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004024/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004025/// start of a type-qualifier-list.
4026bool Parser::isTypeQualifier() const {
4027 switch (Tok.getKind()) {
4028 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00004029 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004030 case tok::kw_const:
4031 case tok::kw_volatile:
4032 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004033 case tok::kw___private:
4034 case tok::kw___local:
4035 case tok::kw___global:
4036 case tok::kw___constant:
4037 case tok::kw___read_only:
4038 case tok::kw___read_write:
4039 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004040 return true;
4041 }
4042}
4043
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004044/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4045/// is definitely a type-specifier. Return false if it isn't part of a type
4046/// specifier or if we're not sure.
4047bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4048 switch (Tok.getKind()) {
4049 default: return false;
4050 // type-specifiers
4051 case tok::kw_short:
4052 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004053 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004054 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004055 case tok::kw_signed:
4056 case tok::kw_unsigned:
4057 case tok::kw__Complex:
4058 case tok::kw__Imaginary:
4059 case tok::kw_void:
4060 case tok::kw_char:
4061 case tok::kw_wchar_t:
4062 case tok::kw_char16_t:
4063 case tok::kw_char32_t:
4064 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004065 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004066 case tok::kw_float:
4067 case tok::kw_double:
4068 case tok::kw_bool:
4069 case tok::kw__Bool:
4070 case tok::kw__Decimal32:
4071 case tok::kw__Decimal64:
4072 case tok::kw__Decimal128:
4073 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00004074
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004075 // OpenCL specific types:
4076 case tok::kw_image1d_t:
4077 case tok::kw_image1d_array_t:
4078 case tok::kw_image1d_buffer_t:
4079 case tok::kw_image2d_t:
4080 case tok::kw_image2d_array_t:
4081 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004082 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004083 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004084
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004085 // struct-or-union-specifier (C99) or class-specifier (C++)
4086 case tok::kw_class:
4087 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004088 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004089 case tok::kw_union:
4090 // enum-specifier
4091 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004092
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004093 // typedef-name
4094 case tok::annot_typename:
4095 return true;
4096 }
4097}
4098
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004099/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004100/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004101bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004102 switch (Tok.getKind()) {
4103 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004104
Chris Lattner020bab92009-01-04 23:41:41 +00004105 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004106 if (TryAltiVecVectorToken())
4107 return true;
4108 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004109 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004110 // Annotate typenames and C++ scope specifiers. If we get one, just
4111 // recurse to handle whatever we get.
4112 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004113 return true;
4114 if (Tok.is(tok::identifier))
4115 return false;
4116 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004117
Chris Lattner020bab92009-01-04 23:41:41 +00004118 case tok::coloncolon: // ::foo::bar
4119 if (NextToken().is(tok::kw_new) || // ::new
4120 NextToken().is(tok::kw_delete)) // ::delete
4121 return false;
4122
Chris Lattner020bab92009-01-04 23:41:41 +00004123 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004124 return true;
4125 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004126
Chris Lattnere37e2332006-08-15 04:50:22 +00004127 // GNU attributes support.
4128 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004129 // GNU typeof support.
4130 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004131
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004132 // type-specifiers
4133 case tok::kw_short:
4134 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004135 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004136 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004137 case tok::kw_signed:
4138 case tok::kw_unsigned:
4139 case tok::kw__Complex:
4140 case tok::kw__Imaginary:
4141 case tok::kw_void:
4142 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004143 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004144 case tok::kw_char16_t:
4145 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004146 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004147 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004148 case tok::kw_float:
4149 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004150 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004151 case tok::kw__Bool:
4152 case tok::kw__Decimal32:
4153 case tok::kw__Decimal64:
4154 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004155 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004156
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004157 // OpenCL specific types:
4158 case tok::kw_image1d_t:
4159 case tok::kw_image1d_array_t:
4160 case tok::kw_image1d_buffer_t:
4161 case tok::kw_image2d_t:
4162 case tok::kw_image2d_array_t:
4163 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004164 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004165 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004166
Chris Lattner861a2262008-04-13 18:59:07 +00004167 // struct-or-union-specifier (C99) or class-specifier (C++)
4168 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004169 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004170 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004171 case tok::kw_union:
4172 // enum-specifier
4173 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004174
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004175 // type-qualifier
4176 case tok::kw_const:
4177 case tok::kw_volatile:
4178 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004179
John McCallea0a39e2012-11-14 00:49:39 +00004180 // Debugger support.
4181 case tok::kw___unknown_anytype:
4182
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004183 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004184 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004185 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004186
Chris Lattner409bf7d2008-10-20 00:25:30 +00004187 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4188 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004189 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004190
Steve Naroff44ac7772008-12-25 14:16:32 +00004191 case tok::kw___cdecl:
4192 case tok::kw___stdcall:
4193 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004194 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004195 case tok::kw___w64:
4196 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004197 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004198 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004199 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004200
4201 case tok::kw___private:
4202 case tok::kw___local:
4203 case tok::kw___global:
4204 case tok::kw___constant:
4205 case tok::kw___read_only:
4206 case tok::kw___read_write:
4207 case tok::kw___write_only:
4208
Eli Friedman53339e02009-06-08 23:27:34 +00004209 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004210
Richard Smith8e1ac332013-03-28 01:55:44 +00004211 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004212 case tok::kw__Atomic:
4213 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004214 }
4215}
4216
Chris Lattneracd58a32006-08-06 17:24:14 +00004217/// isDeclarationSpecifier() - Return true if the current token is part of a
4218/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004219///
4220/// \param DisambiguatingWithExpression True to indicate that the purpose of
4221/// this check is to disambiguate between an expression and a declaration.
4222bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004223 switch (Tok.getKind()) {
4224 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004225
Chris Lattner020bab92009-01-04 23:41:41 +00004226 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004227 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004228 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004229 return false;
John Thompson22334602010-02-05 00:12:22 +00004230 if (TryAltiVecVectorToken())
4231 return true;
4232 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004233 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004234 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004235 // Annotate typenames and C++ scope specifiers. If we get one, just
4236 // recurse to handle whatever we get.
4237 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004238 return true;
4239 if (Tok.is(tok::identifier))
4240 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004241
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004242 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004243 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004244 // expression is permitted, then this is probably a class message send
4245 // missing the initial '['. In this case, we won't consider this to be
4246 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004247 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004248 isStartOfObjCClassMessageMissingOpenBracket())
4249 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004250
John McCall1f476a12010-02-26 08:45:28 +00004251 return isDeclarationSpecifier();
4252
Chris Lattner020bab92009-01-04 23:41:41 +00004253 case tok::coloncolon: // ::foo::bar
4254 if (NextToken().is(tok::kw_new) || // ::new
4255 NextToken().is(tok::kw_delete)) // ::delete
4256 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004257
Chris Lattner020bab92009-01-04 23:41:41 +00004258 // Annotate typenames and C++ scope specifiers. If we get one, just
4259 // recurse to handle whatever we get.
4260 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004261 return true;
4262 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004263
Chris Lattneracd58a32006-08-06 17:24:14 +00004264 // storage-class-specifier
4265 case tok::kw_typedef:
4266 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004267 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004268 case tok::kw_static:
4269 case tok::kw_auto:
4270 case tok::kw_register:
4271 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004272 case tok::kw_thread_local:
4273 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004274
Douglas Gregor26701a42011-09-09 02:06:17 +00004275 // Modules
4276 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004277
John McCallea0a39e2012-11-14 00:49:39 +00004278 // Debugger support
4279 case tok::kw___unknown_anytype:
4280
Chris Lattneracd58a32006-08-06 17:24:14 +00004281 // type-specifiers
4282 case tok::kw_short:
4283 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004284 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004285 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004286 case tok::kw_signed:
4287 case tok::kw_unsigned:
4288 case tok::kw__Complex:
4289 case tok::kw__Imaginary:
4290 case tok::kw_void:
4291 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004292 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004293 case tok::kw_char16_t:
4294 case tok::kw_char32_t:
4295
Chris Lattneracd58a32006-08-06 17:24:14 +00004296 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004297 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004298 case tok::kw_float:
4299 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004300 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004301 case tok::kw__Bool:
4302 case tok::kw__Decimal32:
4303 case tok::kw__Decimal64:
4304 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004305 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004306
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004307 // OpenCL specific types:
4308 case tok::kw_image1d_t:
4309 case tok::kw_image1d_array_t:
4310 case tok::kw_image1d_buffer_t:
4311 case tok::kw_image2d_t:
4312 case tok::kw_image2d_array_t:
4313 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004314 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004315 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004316
Chris Lattner861a2262008-04-13 18:59:07 +00004317 // struct-or-union-specifier (C99) or class-specifier (C++)
4318 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004319 case tok::kw_struct:
4320 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004321 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004322 // enum-specifier
4323 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004324
Chris Lattneracd58a32006-08-06 17:24:14 +00004325 // type-qualifier
4326 case tok::kw_const:
4327 case tok::kw_volatile:
4328 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004329
Chris Lattneracd58a32006-08-06 17:24:14 +00004330 // function-specifier
4331 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004332 case tok::kw_virtual:
4333 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004334 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004335
Richard Smith1dba27c2013-01-29 09:02:09 +00004336 // alignment-specifier
4337 case tok::kw__Alignas:
4338
Richard Smithd16fe122012-10-25 00:00:53 +00004339 // friend keyword.
4340 case tok::kw_friend:
4341
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004342 // static_assert-declaration
4343 case tok::kw__Static_assert:
4344
Chris Lattner599e47e2007-08-09 17:01:07 +00004345 // GNU typeof support.
4346 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004347
Chris Lattner599e47e2007-08-09 17:01:07 +00004348 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004349 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004350
Richard Smithd16fe122012-10-25 00:00:53 +00004351 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004352 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004353 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004354
Richard Smith8e1ac332013-03-28 01:55:44 +00004355 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004356 case tok::kw__Atomic:
4357 return true;
4358
Chris Lattner8b2ec162008-07-26 03:38:44 +00004359 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4360 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004361 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004362
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004363 // typedef-name
4364 case tok::annot_typename:
4365 return !DisambiguatingWithExpression ||
4366 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004367
Steve Narofff192fab2009-01-06 19:34:12 +00004368 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004369 case tok::kw___cdecl:
4370 case tok::kw___stdcall:
4371 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004372 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004373 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004374 case tok::kw___sptr:
4375 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004376 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004377 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004378 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004379 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004380 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004381
4382 case tok::kw___private:
4383 case tok::kw___local:
4384 case tok::kw___global:
4385 case tok::kw___constant:
4386 case tok::kw___read_only:
4387 case tok::kw___read_write:
4388 case tok::kw___write_only:
4389
Eli Friedman53339e02009-06-08 23:27:34 +00004390 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004391 }
4392}
4393
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004394bool Parser::isConstructorDeclarator() {
4395 TentativeParsingAction TPA(*this);
4396
4397 // Parse the C++ scope specifier.
4398 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004399 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004400 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004401 TPA.Revert();
4402 return false;
4403 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004404
4405 // Parse the constructor name.
4406 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4407 // We already know that we have a constructor name; just consume
4408 // the token.
4409 ConsumeToken();
4410 } else {
4411 TPA.Revert();
4412 return false;
4413 }
4414
Richard Smith43f340f2012-03-27 23:05:05 +00004415 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004416 if (Tok.isNot(tok::l_paren)) {
4417 TPA.Revert();
4418 return false;
4419 }
4420 ConsumeParen();
4421
Richard Smith43f340f2012-03-27 23:05:05 +00004422 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4423 // that we have a constructor.
4424 if (Tok.is(tok::r_paren) ||
4425 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004426 TPA.Revert();
4427 return true;
4428 }
4429
Richard Smithf2163662013-09-06 00:12:20 +00004430 // A C++11 attribute here signals that we have a constructor, and is an
4431 // attribute on the first constructor parameter.
4432 if (getLangOpts().CPlusPlus11 &&
4433 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4434 /*OuterMightBeMessageSend*/ true)) {
4435 TPA.Revert();
4436 return true;
4437 }
4438
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004439 // If we need to, enter the specified scope.
4440 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004441 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004442 DeclScopeObj.EnterDeclaratorScope();
4443
Francois Pichet79f3a872011-01-31 04:54:32 +00004444 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004445 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004446 MaybeParseMicrosoftAttributes(Attrs);
4447
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004448 // Check whether the next token(s) are part of a declaration
4449 // specifier, in which case we have the start of a parameter and,
4450 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004451 bool IsConstructor = false;
4452 if (isDeclarationSpecifier())
4453 IsConstructor = true;
4454 else if (Tok.is(tok::identifier) ||
4455 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4456 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4457 // This might be a parenthesized member name, but is more likely to
4458 // be a constructor declaration with an invalid argument type. Keep
4459 // looking.
4460 if (Tok.is(tok::annot_cxxscope))
4461 ConsumeToken();
4462 ConsumeToken();
4463
4464 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004465 // which must have one of the following syntactic forms (see the
4466 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004467 switch (Tok.getKind()) {
4468 case tok::l_paren:
4469 // C(X ( int));
4470 case tok::l_square:
4471 // C(X [ 5]);
4472 // C(X [ [attribute]]);
4473 case tok::coloncolon:
4474 // C(X :: Y);
4475 // C(X :: *p);
4476 case tok::r_paren:
4477 // C(X )
4478 // Assume this isn't a constructor, rather than assuming it's a
4479 // constructor with an unnamed parameter of an ill-formed type.
4480 break;
4481
4482 default:
4483 IsConstructor = true;
4484 break;
4485 }
4486 }
4487
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004488 TPA.Revert();
4489 return IsConstructor;
4490}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004491
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004492/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004493/// type-qualifier-list: [C99 6.7.5]
4494/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004495/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004496/// [ only if VendorAttributesAllowed=true ]
4497/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004498/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004499/// [ only if VendorAttributesAllowed=true ]
4500/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004501/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004502/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004503///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004504void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4505 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004506 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004507 bool AtomicAllowed,
4508 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004509 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004510 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004511 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004512 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004513 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004514 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004515
4516 SourceLocation EndLoc;
4517
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004518 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004519 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004520 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004521 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004522 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004523
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004524 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004525 case tok::code_completion:
4526 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004527 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004528
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004529 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004530 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004531 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004532 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004533 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004534 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004535 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004536 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004537 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004538 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004539 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004540 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004541 case tok::kw__Atomic:
4542 if (!AtomicAllowed)
4543 goto DoneWithTypeQuals;
4544 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4545 getLangOpts());
4546 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004547
4548 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004549 case tok::kw___private:
4550 case tok::kw___global:
4551 case tok::kw___local:
4552 case tok::kw___constant:
4553 case tok::kw___read_only:
4554 case tok::kw___write_only:
4555 case tok::kw___read_write:
4556 ParseOpenCLQualifiers(DS);
4557 break;
4558
Aaron Ballman317a77f2013-05-22 23:25:32 +00004559 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004560 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4561 // with the MS modifier keyword.
4562 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004563 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4564 if (TryKeywordIdentFallback(false))
4565 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004566 }
4567 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004568 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004569 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004570 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004571 case tok::kw___cdecl:
4572 case tok::kw___stdcall:
4573 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004574 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004575 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004576 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004577 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004578 continue;
4579 }
4580 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004581 case tok::kw___pascal:
4582 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004583 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004584 continue;
4585 }
4586 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004587 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004588 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004589 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004590 continue; // do *not* consume the next token!
4591 }
4592 // otherwise, FALL THROUGH!
4593 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004594 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004595 // If this is not a type-qualifier token, we're done reading type
4596 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004597 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004598 if (EndLoc.isValid())
4599 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004600 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004601 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004602
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004603 // If the specifier combination wasn't legal, issue a diagnostic.
4604 if (isInvalid) {
4605 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004606 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004607 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004608 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004609 }
4610}
4611
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004612
4613/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4614///
4615void Parser::ParseDeclarator(Declarator &D) {
4616 /// This implements the 'declarator' production in the C grammar, then checks
4617 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004618 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004619}
4620
Richard Smith0efa75c2012-03-29 01:16:42 +00004621static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4622 if (Kind == tok::star || Kind == tok::caret)
4623 return true;
4624
4625 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4626 if (!Lang.CPlusPlus)
4627 return false;
4628
4629 return Kind == tok::amp || Kind == tok::ampamp;
4630}
4631
Sebastian Redlbd150f42008-11-21 19:14:01 +00004632/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4633/// is parsed by the function passed to it. Pass null, and the direct-declarator
4634/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004635/// ptr-operator production.
4636///
Richard Smith09f76ee2011-10-19 21:33:05 +00004637/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004638/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4639/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004640///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004641/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4642/// [C] pointer[opt] direct-declarator
4643/// [C++] direct-declarator
4644/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004645///
4646/// pointer: [C99 6.7.5]
4647/// '*' type-qualifier-list[opt]
4648/// '*' type-qualifier-list[opt] pointer
4649///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004650/// ptr-operator:
4651/// '*' cv-qualifier-seq[opt]
4652/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004653/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004654/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004655/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004656/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004657void Parser::ParseDeclaratorInternal(Declarator &D,
4658 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004659 if (Diags.hasAllExtensionsSilenced())
4660 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004661
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004662 // C++ member pointers start with a '::' or a nested-name.
4663 // Member pointers get special handling, since there's no place for the
4664 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004665 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004666 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4667 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004668 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4669 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004670 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004671 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004672
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004673 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004674 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004675 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004676 if (D.mayHaveIdentifier())
4677 D.getCXXScopeSpec() = SS;
4678 else
4679 AnnotateScopeToken(SS, true);
4680
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004681 if (DirectDeclParser)
4682 (this->*DirectDeclParser)(D);
4683 return;
4684 }
4685
4686 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004687 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004688 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004689 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004690 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004691
4692 // Recurse to parse whatever is left.
4693 ParseDeclaratorInternal(D, DirectDeclParser);
4694
4695 // Sema will have to catch (syntactically invalid) pointers into global
4696 // scope. It has to catch pointers into namespace scope anyway.
4697 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004698 Loc),
4699 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004700 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004701 return;
4702 }
4703 }
4704
4705 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004706 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004707 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004708 if (DirectDeclParser)
4709 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004710 return;
4711 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004712
Sebastian Redled0f3b02009-03-15 22:02:01 +00004713 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4714 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004715 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004716 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004717
Chris Lattner9eac9312009-03-27 04:18:06 +00004718 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004719 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004720 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004721
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004722 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004723 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004724 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004725
Bill Wendling3708c182007-05-27 10:15:43 +00004726 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004727 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004728 if (Kind == tok::star)
4729 // Remember that we parsed a pointer type, and remember the type-quals.
4730 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004731 DS.getConstSpecLoc(),
4732 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004733 DS.getRestrictSpecLoc()),
4734 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004735 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004736 else
4737 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004738 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004739 Loc),
4740 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004741 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004742 } else {
4743 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004744 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004745
Sebastian Redl3b27be62009-03-23 00:00:23 +00004746 // Complain about rvalue references in C++03, but then go on and build
4747 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004748 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004749 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004750 diag::warn_cxx98_compat_rvalue_reference :
4751 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004752
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004753 // GNU-style and C++11 attributes are allowed here, as is restrict.
4754 ParseTypeQualifierListOpt(DS);
4755 D.ExtendWithDeclSpec(DS);
4756
Bill Wendling93efb222007-06-02 23:28:54 +00004757 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4758 // cv-qualifiers are introduced through the use of a typedef or of a
4759 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004760 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4761 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4762 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004763 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004764 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4765 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004766 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004767 // 'restrict' is permitted as an extension.
4768 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4769 Diag(DS.getAtomicSpecLoc(),
4770 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004771 }
Bill Wendling3708c182007-05-27 10:15:43 +00004772
4773 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004774 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004775
Douglas Gregor66583c52008-11-03 15:51:28 +00004776 if (D.getNumTypeObjects() > 0) {
4777 // C++ [dcl.ref]p4: There shall be no references to references.
4778 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4779 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004780 if (const IdentifierInfo *II = D.getIdentifier())
4781 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4782 << II;
4783 else
4784 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4785 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004786
Sebastian Redlbd150f42008-11-21 19:14:01 +00004787 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004788 // can go ahead and build the (technically ill-formed)
4789 // declarator: reference collapsing will take care of it.
4790 }
4791 }
4792
Richard Smith8e1ac332013-03-28 01:55:44 +00004793 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004794 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004795 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004796 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004797 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004798 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004799}
4800
Richard Smith0efa75c2012-03-29 01:16:42 +00004801static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4802 SourceLocation EllipsisLoc) {
4803 if (EllipsisLoc.isValid()) {
4804 FixItHint Insertion;
4805 if (!D.getEllipsisLoc().isValid()) {
4806 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4807 D.setEllipsisLoc(EllipsisLoc);
4808 }
4809 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4810 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4811 }
4812}
4813
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004814/// ParseDirectDeclarator
4815/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004816/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004817/// '(' declarator ')'
4818/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004819/// [C90] direct-declarator '[' constant-expression[opt] ']'
4820/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4821/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4822/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4823/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004824/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4825/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004826/// direct-declarator '(' parameter-type-list ')'
4827/// direct-declarator '(' identifier-list[opt] ')'
4828/// [GNU] direct-declarator '(' parameter-forward-declarations
4829/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004830/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4831/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004832/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4833/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4834/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004835/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004836/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004837///
4838/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004839/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004840/// '::'[opt] nested-name-specifier[opt] type-name
4841///
4842/// id-expression: [C++ 5.1]
4843/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004844/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004845///
4846/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004847/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004848/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004849/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004850/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004851/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004852///
Richard Smith1453e312012-03-27 01:42:32 +00004853/// Note, any additional constructs added here may need corresponding changes
4854/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004855void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004856 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004857
David Blaikiebbafb8a2012-03-11 07:00:24 +00004858 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004859 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004860 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004861 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4862 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004863 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004864 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004865 }
4866
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004867 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004868 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004869 // Change the declaration context for name lookup, until this function
4870 // is exited (and the declarator has been parsed).
4871 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004872 }
4873
Douglas Gregor27b4c162010-12-23 22:44:42 +00004874 // C++0x [dcl.fct]p14:
4875 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004876 // of a parameter-declaration-clause without a preceding comma. In
4877 // this case, the ellipsis is parsed as part of the
4878 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004879 // parameter pack that has not been expanded; otherwise, it is parsed
4880 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004881 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004882 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004883 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004884 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004885 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004886 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004887 !Actions.containsUnexpandedParameterPacks(D))) {
4888 SourceLocation EllipsisLoc = ConsumeToken();
4889 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4890 // The ellipsis was put in the wrong place. Recover, and explain to
4891 // the user what they should have done.
4892 ParseDeclarator(D);
4893 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4894 return;
4895 } else
4896 D.setEllipsisLoc(EllipsisLoc);
4897
4898 // The ellipsis can't be followed by a parenthesized declarator. We
4899 // check for that in ParseParenDeclarator, after we have disambiguated
4900 // the l_paren token.
4901 }
4902
Douglas Gregor7861a802009-11-03 01:35:08 +00004903 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4904 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4905 // We found something that indicates the start of an unqualified-id.
4906 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004907 bool AllowConstructorName;
4908 if (D.getDeclSpec().hasTypeSpecifier())
4909 AllowConstructorName = false;
4910 else if (D.getCXXScopeSpec().isSet())
4911 AllowConstructorName =
4912 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004913 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004914 else
4915 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4916
Abramo Bagnara7945c982012-01-27 09:46:47 +00004917 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004918 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4919 /*EnteringContext=*/true,
4920 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004921 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004922 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004923 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004924 D.getName()) ||
4925 // Once we're past the identifier, if the scope was bad, mark the
4926 // whole declarator bad.
4927 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004928 D.SetIdentifier(0, Tok.getLocation());
4929 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004930 } else {
4931 // Parsed the unqualified-id; update range information and move along.
4932 if (D.getSourceRange().getBegin().isInvalid())
4933 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4934 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004935 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004936 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004937 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004938 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004939 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004940 "There's a C++-specific check for tok::identifier above");
4941 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4942 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4943 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004944 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004945 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004946 // A virt-specifier isn't treated as an identifier if it appears after a
4947 // trailing-return-type.
4948 if (D.getContext() != Declarator::TrailingReturnContext ||
4949 !isCXX11VirtSpecifier(Tok)) {
4950 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4951 << FixItHint::CreateRemoval(Tok.getLocation());
4952 D.SetIdentifier(0, Tok.getLocation());
4953 ConsumeToken();
4954 goto PastIdentifier;
4955 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004956 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004957
Douglas Gregor7861a802009-11-03 01:35:08 +00004958 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004959 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004960 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004961 // Example: 'char (*X)' or 'int (*XX)(void)'
4962 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004963
4964 // If the declarator was parenthesized, we entered the declarator
4965 // scope when parsing the parenthesized declarator, then exited
4966 // the scope already. Re-enter the scope, if we need to.
4967 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004968 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004969 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004970 if (!D.isInvalidType() &&
4971 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004972 // Change the declaration context for name lookup, until this function
4973 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004974 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004975 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004976 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004977 // This could be something simple like "int" (in which case the declarator
4978 // portion is empty), if an abstract-declarator is allowed.
4979 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004980
4981 // The grammar for abstract-pack-declarator does not allow grouping parens.
4982 // FIXME: Revisit this once core issue 1488 is resolved.
4983 if (D.hasEllipsis() && D.hasGroupingParens())
4984 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4985 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004986 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004987 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004988 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004989 if (D.getContext() == Declarator::MemberContext)
4990 Diag(Tok, diag::err_expected_member_name_or_semi)
4991 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004992 else if (getLangOpts().CPlusPlus) {
4993 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4994 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004995 else {
4996 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4997 if (Tok.isAtStartOfLine() && Loc.isValid())
4998 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4999 << getLangOpts().CPlusPlus;
5000 else
5001 Diag(Tok, diag::err_expected_unqualified_id)
5002 << getLangOpts().CPlusPlus;
5003 }
Richard Trieu9c672672013-01-26 02:31:38 +00005004 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00005005 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00005006 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00005007 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00005008 }
Mike Stump11289f42009-09-09 15:08:12 +00005009
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005010 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00005011 assert(D.isPastIdentifier() &&
5012 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00005013
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005014 // Don't parse attributes unless we have parsed an unparenthesized name.
5015 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00005016 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005017
Chris Lattneracd58a32006-08-06 17:24:14 +00005018 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00005019 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00005020 // Enter function-declaration scope, limiting any declarators to the
5021 // function prototype scope, including parameter declarators.
5022 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005023 Scope::FunctionPrototypeScope|Scope::DeclScope|
5024 (D.isFunctionDeclaratorAFunctionDeclaration()
5025 ? Scope::FunctionDeclarationScope : 0));
5026
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005027 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
5028 // In such a case, check if we actually have a function declarator; if it
5029 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00005030 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00005031 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
5032 // The name of the declarator, if any, is tentatively declared within
5033 // a possible direct initializer.
5034 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
5035 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
5036 TentativelyDeclaredIdentifiers.pop_back();
5037 if (!IsFunctionDecl)
5038 break;
5039 }
John McCall084e83d2011-03-24 11:26:52 +00005040 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005041 BalancedDelimiterTracker T(*this, tok::l_paren);
5042 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00005043 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00005044 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00005045 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00005046 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00005047 } else {
5048 break;
5049 }
5050 }
Chad Rosierc1183952012-06-26 22:30:43 +00005051}
Chris Lattneracd58a32006-08-06 17:24:14 +00005052
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005053/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
5054/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00005055/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005056/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
5057///
5058/// direct-declarator:
5059/// '(' declarator ')'
5060/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005061/// direct-declarator '(' parameter-type-list ')'
5062/// direct-declarator '(' identifier-list[opt] ')'
5063/// [GNU] direct-declarator '(' parameter-forward-declarations
5064/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005065///
5066void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005067 BalancedDelimiterTracker T(*this, tok::l_paren);
5068 T.consumeOpen();
5069
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005070 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00005071
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005072 // Eat any attributes before we look at whether this is a grouping or function
5073 // declarator paren. If this is a grouping paren, the attribute applies to
5074 // the type being built up, for example:
5075 // int (__attribute__(()) *x)(long y)
5076 // If this ends up not being a grouping paren, the attribute applies to the
5077 // first argument, for example:
5078 // int (__attribute__(()) int x)
5079 // In either case, we need to eat any attributes to be able to determine what
5080 // sort of paren this is.
5081 //
John McCall084e83d2011-03-24 11:26:52 +00005082 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005083 bool RequiresArg = false;
5084 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005085 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005086
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005087 // We require that the argument list (if this is a non-grouping paren) be
5088 // present even if the attribute list was empty.
5089 RequiresArg = true;
5090 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005091
Steve Naroff44ac7772008-12-25 14:16:32 +00005092 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005093 ParseMicrosoftTypeAttributes(attrs);
5094
Dawn Perchik335e16b2010-09-03 01:29:35 +00005095 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005096 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005097 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005098
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005099 // If we haven't past the identifier yet (or where the identifier would be
5100 // stored, if this is an abstract declarator), then this is probably just
5101 // grouping parens. However, if this could be an abstract-declarator, then
5102 // this could also be the start of function arguments (consider 'void()').
5103 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005104
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005105 if (!D.mayOmitIdentifier()) {
5106 // If this can't be an abstract-declarator, this *must* be a grouping
5107 // paren, because we haven't seen the identifier yet.
5108 isGrouping = true;
5109 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005110 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5111 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005112 isDeclarationSpecifier() || // 'int(int)' is a function.
5113 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005114 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5115 // considered to be a type, not a K&R identifier-list.
5116 isGrouping = false;
5117 } else {
5118 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5119 isGrouping = true;
5120 }
Mike Stump11289f42009-09-09 15:08:12 +00005121
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005122 // If this is a grouping paren, handle:
5123 // direct-declarator: '(' declarator ')'
5124 // direct-declarator: '(' attributes declarator ')'
5125 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005126 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5127 D.setEllipsisLoc(SourceLocation());
5128
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005129 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005130 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005131 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005132 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005133 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005134 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005135 T.getCloseLocation()),
5136 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005137
5138 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005139
5140 // An ellipsis cannot be placed outside parentheses.
5141 if (EllipsisLoc.isValid())
5142 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5143
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005144 return;
5145 }
Mike Stump11289f42009-09-09 15:08:12 +00005146
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005147 // Okay, if this wasn't a grouping paren, it must be the start of a function
5148 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005149 // identifier (and remember where it would have been), then call into
5150 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005151 D.SetIdentifier(0, Tok.getLocation());
5152
David Blaikie15a430a2011-12-04 05:04:18 +00005153 // Enter function-declaration scope, limiting any declarators to the
5154 // function prototype scope, including parameter declarators.
5155 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005156 Scope::FunctionPrototypeScope | Scope::DeclScope |
5157 (D.isFunctionDeclaratorAFunctionDeclaration()
5158 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005159 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005160 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005161}
5162
5163/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5164/// declarator D up to a paren, which indicates that we are parsing function
5165/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005166///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005167/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5168/// immediately after the open paren - they should be considered to be the
5169/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005170///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005171/// If RequiresArg is true, then the first argument of the function is required
5172/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005173///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005174/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5175/// (C++11) ref-qualifier[opt], exception-specification[opt],
5176/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5177///
5178/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005179/// dynamic-exception-specification
5180/// noexcept-specification
5181///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005182void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005183 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005184 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005185 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005186 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005187 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005188 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005189 // lparen is already consumed!
5190 assert(D.isPastIdentifier() && "Should not call before identifier!");
5191
5192 // This should be true when the function has typed arguments.
5193 // Otherwise, it is treated as a K&R-style function.
5194 bool HasProto = false;
5195 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005196 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005197 // Remember where we see an ellipsis, if any.
5198 SourceLocation EllipsisLoc;
5199
5200 DeclSpec DS(AttrFactory);
5201 bool RefQualifierIsLValueRef = true;
5202 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005203 SourceLocation ConstQualifierLoc;
5204 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005205 ExceptionSpecificationType ESpecType = EST_None;
5206 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005207 SmallVector<ParsedType, 2> DynamicExceptions;
5208 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005209 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005210 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005211 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005212
James Molloy6f8780b2012-02-29 10:24:19 +00005213 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005214 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5215 EndLoc is the end location for the function declarator.
5216 They differ for trailing return types. */
5217 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005218 SourceLocation LParenLoc, RParenLoc;
5219 LParenLoc = Tracker.getOpenLocation();
5220 StartLoc = LParenLoc;
5221
Douglas Gregor9e66af42011-07-05 16:44:18 +00005222 if (isFunctionDeclaratorIdentifierList()) {
5223 if (RequiresArg)
5224 Diag(Tok, diag::err_argument_required_after_attribute);
5225
5226 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5227
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005228 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005229 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005230 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005231 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005232 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005233 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005234 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5235 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005236 else if (RequiresArg)
5237 Diag(Tok, diag::err_argument_required_after_attribute);
5238
David Blaikiebbafb8a2012-03-11 07:00:24 +00005239 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005240
5241 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005242 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005243 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005244 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005245 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005246
David Blaikiebbafb8a2012-03-11 07:00:24 +00005247 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005248 // FIXME: Accept these components in any order, and produce fixits to
5249 // correct the order if the user gets it wrong. Ideally we should deal
5250 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005251
5252 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005253 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5254 /*CXX11AttributesAllowed*/ false,
5255 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005256 if (!DS.getSourceRange().getEnd().isInvalid()) {
5257 EndLoc = DS.getSourceRange().getEnd();
5258 ConstQualifierLoc = DS.getConstSpecLoc();
5259 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5260 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005261
5262 // Parse ref-qualifier[opt].
5263 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005264 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005265 diag::warn_cxx98_compat_ref_qualifier :
5266 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005267
Douglas Gregor9e66af42011-07-05 16:44:18 +00005268 RefQualifierIsLValueRef = Tok.is(tok::amp);
5269 RefQualifierLoc = ConsumeToken();
5270 EndLoc = RefQualifierLoc;
5271 }
5272
Douglas Gregor3024f072012-04-16 07:05:22 +00005273 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005274 // If a declaration declares a member function or member function
5275 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005276 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005277 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005278 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005279 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005280 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005281 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005282 (D.getContext() == Declarator::MemberContext
5283 ? !D.getDeclSpec().isFriendSpecified()
5284 : D.getContext() == Declarator::FileContext &&
5285 D.getCXXScopeSpec().isValid() &&
5286 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005287 Sema::CXXThisScopeRAII ThisScope(Actions,
5288 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005289 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005290 (D.getDeclSpec().isConstexprSpecified() &&
5291 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005292 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005293 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005294
Douglas Gregor9e66af42011-07-05 16:44:18 +00005295 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005296 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005297 DynamicExceptions,
5298 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005299 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005300 if (ESpecType != EST_None)
5301 EndLoc = ESpecRange.getEnd();
5302
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005303 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5304 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005305 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005306
Douglas Gregor9e66af42011-07-05 16:44:18 +00005307 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005308 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005309 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005310 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005311 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5312 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005313 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005314 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005315 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005316 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005317 }
5318 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005319 }
5320
5321 // Remember that we parsed a function type, and remember the attributes.
5322 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005323 IsAmbiguous,
5324 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005325 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005326 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005327 DS.getTypeQualifiers(),
5328 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005329 RefQualifierLoc, ConstQualifierLoc,
5330 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005331 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005332 ESpecType, ESpecRange.getBegin(),
5333 DynamicExceptions.data(),
5334 DynamicExceptionRanges.data(),
5335 DynamicExceptions.size(),
5336 NoexceptExpr.isUsable() ?
5337 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005338 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005339 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005340 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005341
5342 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005343}
5344
5345/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5346/// identifier list form for a K&R-style function: void foo(a,b,c)
5347///
5348/// Note that identifier-lists are only allowed for normal declarators, not for
5349/// abstract-declarators.
5350bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005351 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005352 && Tok.is(tok::identifier)
5353 && !TryAltiVecVectorToken()
5354 // K&R identifier lists can't have typedefs as identifiers, per C99
5355 // 6.7.5.3p11.
5356 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5357 // Identifier lists follow a really simple grammar: the identifiers can
5358 // be followed *only* by a ", identifier" or ")". However, K&R
5359 // identifier lists are really rare in the brave new modern world, and
5360 // it is very common for someone to typo a type in a non-K&R style
5361 // list. If we are presented with something like: "void foo(intptr x,
5362 // float y)", we don't want to start parsing the function declarator as
5363 // though it is a K&R style declarator just because intptr is an
5364 // invalid type.
5365 //
5366 // To handle this, we check to see if the token after the first
5367 // identifier is a "," or ")". Only then do we parse it as an
5368 // identifier list.
5369 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5370}
5371
5372/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5373/// we found a K&R-style identifier list instead of a typed parameter list.
5374///
5375/// After returning, ParamInfo will hold the parsed parameters.
5376///
5377/// identifier-list: [C99 6.7.5]
5378/// identifier
5379/// identifier-list ',' identifier
5380///
5381void Parser::ParseFunctionDeclaratorIdentifierList(
5382 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005383 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005384 // If there was no identifier specified for the declarator, either we are in
5385 // an abstract-declarator, or we are in a parameter declarator which was found
5386 // to be abstract. In abstract-declarators, identifier lists are not valid:
5387 // diagnose this.
5388 if (!D.getIdentifier())
5389 Diag(Tok, diag::ext_ident_list_in_param);
5390
5391 // Maintain an efficient lookup of params we have seen so far.
5392 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5393
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005394 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005395 // If this isn't an identifier, report the error and skip until ')'.
5396 if (Tok.isNot(tok::identifier)) {
5397 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005398 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005399 // Forget we parsed anything.
5400 ParamInfo.clear();
5401 return;
5402 }
5403
5404 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5405
5406 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5407 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5408 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5409
5410 // Verify that the argument identifier has not already been mentioned.
5411 if (!ParamsSoFar.insert(ParmII)) {
5412 Diag(Tok, diag::err_param_redefinition) << ParmII;
5413 } else {
5414 // Remember this identifier in ParamInfo.
5415 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5416 Tok.getLocation(),
5417 0));
5418 }
5419
5420 // Eat the identifier.
5421 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005422 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005423 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005424}
5425
5426/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5427/// after the opening parenthesis. This function will not parse a K&R-style
5428/// identifier list.
5429///
Richard Smith2620cd92012-04-11 04:01:28 +00005430/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5431/// caller parsed those arguments immediately after the open paren - they should
5432/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005433///
5434/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5435/// be the location of the ellipsis, if any was parsed.
5436///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005437/// parameter-type-list: [C99 6.7.5]
5438/// parameter-list
5439/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005440/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005441///
5442/// parameter-list: [C99 6.7.5]
5443/// parameter-declaration
5444/// parameter-list ',' parameter-declaration
5445///
5446/// parameter-declaration: [C99 6.7.5]
5447/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005448/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005449/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005450/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005451/// declaration-specifiers abstract-declarator[opt]
5452/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005453/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005454/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005455/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005456///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005457void Parser::ParseParameterDeclarationClause(
5458 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005459 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005460 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005461 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005462 do {
5463 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5464 // before deciding this was a parameter-declaration-clause.
5465 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005466 break;
Mike Stump11289f42009-09-09 15:08:12 +00005467
Chris Lattner371ed4e2008-04-06 06:57:35 +00005468 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005469 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005470 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005471
Richard Smith2620cd92012-04-11 04:01:28 +00005472 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005473 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005474
John McCall53fa7142010-12-24 02:08:15 +00005475 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005476 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005477
5478 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005479
5480 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005481 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005482 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005483 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5484 // too much hassle.
5485 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005486
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005487 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005488
Faisal Vali2b391ab2013-09-26 19:54:12 +00005489
5490 // Parse the declarator. This is "PrototypeContext" or
5491 // "LambdaExprParameterContext", because we must accept either
5492 // 'declarator' or 'abstract-declarator' here.
5493 Declarator ParmDeclarator(DS,
5494 D.getContext() == Declarator::LambdaExprContext ?
5495 Declarator::LambdaExprParameterContext :
5496 Declarator::PrototypeContext);
5497 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005498
5499 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005500 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005501
Chris Lattner371ed4e2008-04-06 06:57:35 +00005502 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005503 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005504
Douglas Gregor4d87df52008-12-16 21:30:33 +00005505 // DefArgToks is used when the parsing of default arguments needs
5506 // to be delayed.
5507 CachedTokens *DefArgToks = 0;
5508
Chris Lattner371ed4e2008-04-06 06:57:35 +00005509 // If no parameter was specified, verify that *something* was specified,
5510 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005511 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5512 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005513 // Completely missing, emit error.
5514 Diag(DSStart, diag::err_missing_param);
5515 } else {
5516 // Otherwise, we have something. Add it and let semantic analysis try
5517 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005518
Chris Lattner371ed4e2008-04-06 06:57:35 +00005519 // Inform the actions module about the parameter declarator, so it gets
5520 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005521 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5522 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005523 // Parse the default argument, if any. We parse the default
5524 // arguments in all dialects; the semantic analysis in
5525 // ActOnParamDefaultArgument will reject the default argument in
5526 // C.
5527 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005528 SourceLocation EqualLoc = Tok.getLocation();
5529
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005530 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005531 if (D.getContext() == Declarator::MemberContext) {
5532 // If we're inside a class definition, cache the tokens
5533 // corresponding to the default argument. We'll actually parse
5534 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005535 // FIXME: Can we use a smart pointer for Toks?
5536 DefArgToks = new CachedTokens;
5537
Richard Smith1fff95c2013-09-12 23:28:08 +00005538 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005539 delete DefArgToks;
5540 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005541 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005542 } else {
5543 // Mark the end of the default argument so that we know when to
5544 // stop when we parse it later on.
5545 Token DefArgEnd;
5546 DefArgEnd.startToken();
5547 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5548 DefArgEnd.setLocation(Tok.getLocation());
5549 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005550 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005551 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005552 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005553 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005554 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005555 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005556
Chad Rosierc1183952012-06-26 22:30:43 +00005557 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005558 // used.
5559 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005560 Sema::PotentiallyEvaluatedIfUsed,
5561 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005562
Sebastian Redldb63af22012-03-14 15:54:00 +00005563 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005564 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005565 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005566 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005567 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005568 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005569 if (DefArgResult.isInvalid()) {
5570 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005571 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005572 } else {
5573 // Inform the actions module about the default argument
5574 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005575 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005576 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005577 }
5578 }
Mike Stump11289f42009-09-09 15:08:12 +00005579
5580 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005581 ParmDeclarator.getIdentifierLoc(),
5582 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005583 }
5584
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005585 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5586 !getLangOpts().CPlusPlus) {
5587 // We have ellipsis without a preceding ',', which is ill-formed
5588 // in C. Complain and provide the fix.
5589 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5590 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005591 break;
5592 }
Mike Stump11289f42009-09-09 15:08:12 +00005593
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005594 // If the next token is a comma, consume it and keep reading arguments.
5595 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005596}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005597
Chris Lattnere8074e62006-08-06 18:30:15 +00005598/// [C90] direct-declarator '[' constant-expression[opt] ']'
5599/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5600/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5601/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5602/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005603/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5604/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005605void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005606 if (CheckProhibitedCXX11Attribute())
5607 return;
5608
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005609 BalancedDelimiterTracker T(*this, tok::l_square);
5610 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005611
Chris Lattner84a11622008-12-18 07:27:21 +00005612 // C array syntax has many features, but by-far the most common is [] and [4].
5613 // This code does a fast path to handle some of the most obvious cases.
5614 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005615 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005616 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005617 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005618
Chris Lattner84a11622008-12-18 07:27:21 +00005619 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005620 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005621 T.getOpenLocation(),
5622 T.getCloseLocation()),
5623 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005624 return;
5625 } else if (Tok.getKind() == tok::numeric_constant &&
5626 GetLookAheadToken(1).is(tok::r_square)) {
5627 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005628 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005629 ConsumeToken();
5630
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005631 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005632 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005633 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005634
Chris Lattner84a11622008-12-18 07:27:21 +00005635 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005636 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005637 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005638 T.getOpenLocation(),
5639 T.getCloseLocation()),
5640 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005641 return;
5642 }
Mike Stump11289f42009-09-09 15:08:12 +00005643
Chris Lattnere8074e62006-08-06 18:30:15 +00005644 // If valid, this location is the position where we read the 'static' keyword.
5645 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005646 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005647
Chris Lattnere8074e62006-08-06 18:30:15 +00005648 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005649 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005650 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005651 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005652
Chris Lattnere8074e62006-08-06 18:30:15 +00005653 // If we haven't already read 'static', check to see if there is one after the
5654 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005655 if (!StaticLoc.isValid())
5656 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005657
Chris Lattnere8074e62006-08-06 18:30:15 +00005658 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005659 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005660 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005661
Chris Lattner521ff2b2008-04-06 05:26:30 +00005662 // Handle the case where we have '[*]' as the array size. However, a leading
5663 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005664 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005665 // infrequent, use of lookahead is not costly here.
5666 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005667 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005668
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005669 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005670 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005671 StaticLoc = SourceLocation(); // Drop the static.
5672 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005673 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005674 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005675 // Note, in C89, this production uses the constant-expr production instead
5676 // of assignment-expr. The only difference is that assignment-expr allows
5677 // things like '=' and '*='. Sema rejects these in C89 mode because they
5678 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005679
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005680 // Parse the constant-expression or assignment-expression now (depending
5681 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005682 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005683 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005684 } else {
5685 EnterExpressionEvaluationContext Unevaluated(Actions,
5686 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005687 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005688 }
Chris Lattner62591722006-08-12 18:40:58 +00005689 }
Mike Stump11289f42009-09-09 15:08:12 +00005690
Chris Lattner62591722006-08-12 18:40:58 +00005691 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005692 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005693 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005694 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005695 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005696 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005697 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005698
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005699 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005700
John McCall084e83d2011-03-24 11:26:52 +00005701 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005702 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005703
Chris Lattner84a11622008-12-18 07:27:21 +00005704 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005705 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005706 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005707 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005708 T.getOpenLocation(),
5709 T.getCloseLocation()),
5710 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005711}
5712
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005713/// [GNU] typeof-specifier:
5714/// typeof ( expressions )
5715/// typeof ( type-name )
5716/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005717///
5718void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005719 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005720 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005721 SourceLocation StartLoc = ConsumeToken();
5722
John McCalle8595032010-01-13 20:03:27 +00005723 const bool hasParens = Tok.is(tok::l_paren);
5724
Eli Friedman15681d62012-09-26 04:34:21 +00005725 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5726 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005727
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005728 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005729 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005730 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005731 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5732 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005733 if (hasParens)
5734 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005735
5736 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005737 // FIXME: Not accurate, the range gets one token more than it should.
5738 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005739 else
5740 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005741
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005742 if (isCastExpr) {
5743 if (!CastTy) {
5744 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005745 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005746 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005747
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005748 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005749 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005750 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5751 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005752 DiagID, CastTy))
5753 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005754 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005755 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005756
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005757 // If we get here, the operand to the typeof was an expresion.
5758 if (Operand.isInvalid()) {
5759 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005760 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005761 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005762
Eli Friedmane0afc982012-01-21 01:01:51 +00005763 // We might need to transform the operand if it is potentially evaluated.
5764 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5765 if (Operand.isInvalid()) {
5766 DS.SetTypeSpecError();
5767 return;
5768 }
5769
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005770 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005771 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005772 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5773 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005774 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005775 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005776}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005777
Benjamin Kramere56f3932011-12-23 17:00:35 +00005778/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005779/// _Atomic ( type-name )
5780///
5781void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005782 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5783 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005784
5785 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005786 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005787 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005788 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005789
5790 TypeResult Result = ParseTypeName();
5791 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005792 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005793 return;
5794 }
5795
5796 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005797 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005798
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005799 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005800 return;
5801
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005802 DS.setTypeofParensRange(T.getRange());
5803 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005804
5805 const char *PrevSpec = 0;
5806 unsigned DiagID;
5807 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5808 DiagID, Result.release()))
5809 Diag(StartLoc, DiagID) << PrevSpec;
5810}
5811
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005812
5813/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5814/// from TryAltiVecVectorToken.
5815bool Parser::TryAltiVecVectorTokenOutOfLine() {
5816 Token Next = NextToken();
5817 switch (Next.getKind()) {
5818 default: return false;
5819 case tok::kw_short:
5820 case tok::kw_long:
5821 case tok::kw_signed:
5822 case tok::kw_unsigned:
5823 case tok::kw_void:
5824 case tok::kw_char:
5825 case tok::kw_int:
5826 case tok::kw_float:
5827 case tok::kw_double:
5828 case tok::kw_bool:
5829 case tok::kw___pixel:
5830 Tok.setKind(tok::kw___vector);
5831 return true;
5832 case tok::identifier:
5833 if (Next.getIdentifierInfo() == Ident_pixel) {
5834 Tok.setKind(tok::kw___vector);
5835 return true;
5836 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005837 if (Next.getIdentifierInfo() == Ident_bool) {
5838 Tok.setKind(tok::kw___vector);
5839 return true;
5840 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005841 return false;
5842 }
5843}
5844
5845bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5846 const char *&PrevSpec, unsigned &DiagID,
5847 bool &isInvalid) {
5848 if (Tok.getIdentifierInfo() == Ident_vector) {
5849 Token Next = NextToken();
5850 switch (Next.getKind()) {
5851 case tok::kw_short:
5852 case tok::kw_long:
5853 case tok::kw_signed:
5854 case tok::kw_unsigned:
5855 case tok::kw_void:
5856 case tok::kw_char:
5857 case tok::kw_int:
5858 case tok::kw_float:
5859 case tok::kw_double:
5860 case tok::kw_bool:
5861 case tok::kw___pixel:
5862 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5863 return true;
5864 case tok::identifier:
5865 if (Next.getIdentifierInfo() == Ident_pixel) {
5866 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5867 return true;
5868 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005869 if (Next.getIdentifierInfo() == Ident_bool) {
5870 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5871 return true;
5872 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005873 break;
5874 default:
5875 break;
5876 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005877 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005878 DS.isTypeAltiVecVector()) {
5879 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5880 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005881 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5882 DS.isTypeAltiVecVector()) {
5883 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5884 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005885 }
5886 return false;
5887}