blob: 4fce10fc32fc235ebb1c8e0fed78050debc8a296 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Larisse Voufo725de3e2013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Alexis Hunt96d5c762009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000102///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000103/// Whether an attribute takes an 'identifier' is determined by the
104/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000106/// * In C mode, if the attribute argument list starts with an identifier
107/// followed by a ',' or an ')', and the identifier doesn't resolve to
108/// a type, it is parsed as an identifier. If the attribute actually
109/// wanted an expression, it's out of luck (but it turns out that no
110/// attributes work that way, because C constant expressions are very
111/// limited).
112/// * In C++ mode, if the attribute argument list starts with an identifier,
113/// and the attribute *wants* an identifier, it is parsed as an identifier.
114/// At block scope, any additional tokens between the identifier and the
115/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000116///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
120 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000121 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000122
Chris Lattner76c72282007-10-09 17:33:22 +0000123 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 ConsumeToken();
125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
126 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000127 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000128 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 }
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000131 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000132 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 }
134 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000135 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
136 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000137 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
139 ConsumeToken();
140 continue;
141 }
142 // we have an identifier or declaration specifier (const, int, etc.)
143 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
144 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 if (Tok.is(tok::l_paren)) {
147 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000149 LateParsedAttribute *LA =
150 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
151 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000152
Bill Wendling44426052012-12-20 19:22:21 +0000153 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000154 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000155 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000156 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000158 // consume everything up to and including the matching right parens
159 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000161 Token Eof;
162 Eof.startToken();
163 Eof.setLocation(Tok.getLocation());
164 LA->Toks.push_back(Eof);
165 } else {
Michael Han23214e52012-10-03 01:56:22 +0000166 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000167 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 }
169 } else {
Aaron Ballman00e99962013-08-31 01:11:41 +0000170 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
171 AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000172 }
173 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000175 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Richard Smith66e71682013-10-24 01:07:54 +0000177 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000178 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000179 if (endLoc)
180 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000181 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000183
Aaron Ballman4768b312013-11-04 12:55:56 +0000184/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
185static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000186 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
187 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000188 return Name;
189}
190
191/// \brief Determine whether the given attribute has an identifier argument.
192static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
193 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Richard Smith66e71682013-10-24 01:07:54 +0000194#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000195 .Default(false);
196}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000197
Aaron Ballman4768b312013-11-04 12:55:56 +0000198/// \brief Determine whether the given attribute parses a type argument.
199static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
200 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
201#include "clang/Parse/AttrTypeArg.inc"
202 .Default(false);
203}
204
Richard Smithfeefaf52013-09-03 18:01:40 +0000205IdentifierLoc *Parser::ParseIdentifierLoc() {
206 assert(Tok.is(tok::identifier) && "expected an identifier");
207 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
208 Tok.getLocation(),
209 Tok.getIdentifierInfo());
210 ConsumeToken();
211 return IL;
212}
213
Richard Smithb1f9a282013-10-31 01:56:18 +0000214void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
215 SourceLocation AttrNameLoc,
216 ParsedAttributes &Attrs,
217 SourceLocation *EndLoc) {
218 BalancedDelimiterTracker Parens(*this, tok::l_paren);
219 Parens.consumeOpen();
220
221 TypeResult T;
222 if (Tok.isNot(tok::r_paren))
223 T = ParseTypeName();
224
225 if (Parens.consumeClose())
226 return;
227
228 if (T.isInvalid())
229 return;
230
231 if (T.isUsable())
232 Attrs.addNewTypeAttr(&AttrName,
233 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
234 AttrNameLoc, T.get(), AttributeList::AS_GNU);
235 else
236 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
237 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
238}
239
Michael Han23214e52012-10-03 01:56:22 +0000240/// Parse the arguments to a parameterized GNU attribute or
241/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000242void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
243 SourceLocation AttrNameLoc,
244 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000245 SourceLocation *EndLoc,
246 IdentifierInfo *ScopeName,
247 SourceLocation ScopeLoc,
248 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000249
250 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
251
Richard Smith66e71682013-10-24 01:07:54 +0000252 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000253 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000254
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000255 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000256 // FIXME: All these cases fail to pass in the syntax and scope, and might be
257 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000258 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000259 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
260 return;
261 }
Richard Smithb1f9a282013-10-31 01:56:18 +0000262 // Thread safety attributes are parsed in an unevaluated context.
263 // FIXME: Share the bulk of the parsing code here and just pull out
264 // the unevaluated context.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000265 if (IsThreadSafetyAttribute(AttrName->getName())) {
266 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
267 return;
268 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000269 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000270 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000271 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
272 return;
273 }
Aaron Ballman4768b312013-11-04 12:55:56 +0000274 // Some attributes expect solely a type parameter.
275 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000276 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
277 return;
278 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000279
Richard Smith66e71682013-10-24 01:07:54 +0000280 // Ignore the left paren location for now.
281 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000282
Aaron Ballman00e99962013-08-31 01:11:41 +0000283 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000284
Richard Smithb1f9a282013-10-31 01:56:18 +0000285 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000286 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000287 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000288
289 // If we don't know how to parse this attribute, but this is the only
290 // token in this argument, assume it's meant to be an identifier.
291 if (AttrKind == AttributeList::UnknownAttribute) {
292 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000293 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000294 }
Richard Smithb12bf692011-10-17 21:20:17 +0000295
Richard Smithb1f9a282013-10-31 01:56:18 +0000296 if (IsIdentifierArg)
297 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000298 }
299
Richard Smithb1f9a282013-10-31 01:56:18 +0000300 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000301 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000302 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000303 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000304
Richard Smithb12bf692011-10-17 21:20:17 +0000305 // Parse the non-empty comma-separated list of expressions.
306 while (1) {
307 ExprResult ArgExpr(ParseAssignmentExpression());
308 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000309 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000310 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000311 }
Richard Smithb12bf692011-10-17 21:20:17 +0000312 ArgExprs.push_back(ArgExpr.release());
313 if (Tok.isNot(tok::comma))
314 break;
315 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000316 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000317 }
Richard Smithb12bf692011-10-17 21:20:17 +0000318
319 SourceLocation RParen = Tok.getLocation();
Richard Smithb1f9a282013-10-31 01:56:18 +0000320 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000321 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000322 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
323 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000324 }
325}
326
Chad Rosierc1183952012-06-26 22:30:43 +0000327/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000328/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000329void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000330 SourceLocation AttrNameLoc,
331 ParsedAttributes &Attrs)
332{
333 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000334 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000335 AttrName->getNameStart(), tok::r_paren))
336 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000337
Aaron Ballman478faed2012-06-19 22:09:27 +0000338 ExprResult ArgExpr(ParseConstantExpression());
339 if (ArgExpr.isInvalid()) {
340 T.skipToEnd();
341 return;
342 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000343 ArgsUnion ExprList = ArgExpr.take();
344 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
345 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000346
347 T.consumeClose();
348}
349
Chad Rosierc1183952012-06-26 22:30:43 +0000350/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000351/// arguments.
352bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
353 return llvm::StringSwitch<bool>(Ident->getName())
354 .Case("dllimport", true)
355 .Case("dllexport", true)
356 .Case("noreturn", true)
357 .Case("nothrow", true)
358 .Case("noinline", true)
359 .Case("naked", true)
360 .Case("appdomain", true)
361 .Case("process", true)
362 .Case("jitintrinsic", true)
363 .Case("noalias", true)
364 .Case("restrict", true)
365 .Case("novtable", true)
366 .Case("selectany", true)
367 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000368 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000369 .Default(false);
370}
371
Chad Rosierc1183952012-06-26 22:30:43 +0000372/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000373/// parameters). Will return false if we properly handled the declspec, or
374/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000375void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000376 SourceLocation Loc,
377 ParsedAttributes &Attrs) {
378 // Try to handle the easy case first -- these declspecs all take a single
379 // parameter as their argument.
380 if (llvm::StringSwitch<bool>(Ident->getName())
381 .Case("uuid", true)
382 .Case("align", true)
383 .Case("allocate", true)
384 .Default(false)) {
385 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
386 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000387 // The deprecated declspec has an optional single argument, so we will
388 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000389 // not.
390 if (Tok.getKind() == tok::l_paren)
391 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
392 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000393 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000394 } else if (Ident->getName() == "property") {
395 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000396 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000397 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000398 if (Tok.isNot(tok::l_paren)) {
399 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
400 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000401 return;
John McCall5e77d762013-04-16 07:28:30 +0000402 }
403 BalancedDelimiterTracker T(*this, tok::l_paren);
404 T.expectAndConsume(diag::err_expected_lparen_after,
405 Ident->getNameStart(), tok::r_paren);
406
407 enum AccessorKind {
408 AK_Invalid = -1,
409 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
410 };
411 IdentifierInfo *AccessorNames[] = { 0, 0 };
412 bool HasInvalidAccessor = false;
413
414 // Parse the accessor specifications.
415 while (true) {
416 // Stop if this doesn't look like an accessor spec.
417 if (!Tok.is(tok::identifier)) {
418 // If the user wrote a completely empty list, use a special diagnostic.
419 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
420 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
421 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
422 break;
423 }
424
425 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
426 break;
427 }
428
429 AccessorKind Kind;
430 SourceLocation KindLoc = Tok.getLocation();
431 StringRef KindStr = Tok.getIdentifierInfo()->getName();
432 if (KindStr == "get") {
433 Kind = AK_Get;
434 } else if (KindStr == "put") {
435 Kind = AK_Put;
436
437 // Recover from the common mistake of using 'set' instead of 'put'.
438 } else if (KindStr == "set") {
439 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
440 << FixItHint::CreateReplacement(KindLoc, "put");
441 Kind = AK_Put;
442
443 // Handle the mistake of forgetting the accessor kind by skipping
444 // this accessor.
445 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
446 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
447 ConsumeToken();
448 HasInvalidAccessor = true;
449 goto next_property_accessor;
450
451 // Otherwise, complain about the unknown accessor kind.
452 } else {
453 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
454 HasInvalidAccessor = true;
455 Kind = AK_Invalid;
456
457 // Try to keep parsing unless it doesn't look like an accessor spec.
458 if (!NextToken().is(tok::equal)) break;
459 }
460
461 // Consume the identifier.
462 ConsumeToken();
463
464 // Consume the '='.
465 if (Tok.is(tok::equal)) {
466 ConsumeToken();
467 } else {
468 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
469 << KindStr;
470 break;
471 }
472
473 // Expect the method name.
474 if (!Tok.is(tok::identifier)) {
475 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
476 break;
477 }
478
479 if (Kind == AK_Invalid) {
480 // Just drop invalid accessors.
481 } else if (AccessorNames[Kind] != NULL) {
482 // Complain about the repeated accessor, ignore it, and keep parsing.
483 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
484 } else {
485 AccessorNames[Kind] = Tok.getIdentifierInfo();
486 }
487 ConsumeToken();
488
489 next_property_accessor:
490 // Keep processing accessors until we run out.
491 if (Tok.is(tok::comma)) {
492 ConsumeAnyToken();
493 continue;
494
495 // If we run into the ')', stop without consuming it.
496 } else if (Tok.is(tok::r_paren)) {
497 break;
498 } else {
499 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
500 break;
501 }
502 }
503
504 // Only add the property attribute if it was well-formed.
505 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000506 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000507 AccessorNames[AK_Get], AccessorNames[AK_Put],
508 AttributeList::AS_Declspec);
509 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000510 T.skipToEnd();
511 } else {
512 // We don't recognize this as a valid declspec, but instead of creating the
513 // attribute and allowing sema to warn about it, we will warn here instead.
514 // This is because some attributes have multiple spellings, but we need to
515 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000516 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000517 // both locations.
518 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
519
520 // If there's an open paren, we should eat the open and close parens under
521 // the assumption that this unknown declspec has parameters.
522 BalancedDelimiterTracker T(*this, tok::l_paren);
523 if (!T.consumeOpen())
524 T.skipToEnd();
525 }
526}
527
Eli Friedman06de2b52009-06-08 07:21:15 +0000528/// [MS] decl-specifier:
529/// __declspec ( extended-decl-modifier-seq )
530///
531/// [MS] extended-decl-modifier-seq:
532/// extended-decl-modifier[opt]
533/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000534void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000535 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000536
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000537 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000538 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000539 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000540 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000541 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000542
Chad Rosierc1183952012-06-26 22:30:43 +0000543 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000544 // you can specify multiple attributes per declspec.
545 while (Tok.getKind() != tok::r_paren) {
546 // We expect either a well-known identifier or a generic string. Anything
547 // else is a malformed declspec.
548 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000549 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000550 Tok.getKind() != tok::kw_restrict) {
551 Diag(Tok, diag::err_ms_declspec_type);
552 T.skipToEnd();
553 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000554 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000555
556 IdentifierInfo *AttrName;
557 SourceLocation AttrNameLoc;
558 if (IsString) {
559 SmallString<8> StrBuffer;
560 bool Invalid = false;
561 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
562 if (Invalid) {
563 T.skipToEnd();
564 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000565 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000566 AttrName = PP.getIdentifierInfo(Str);
567 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000568 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000569 AttrName = Tok.getIdentifierInfo();
570 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000571 }
Chad Rosierc1183952012-06-26 22:30:43 +0000572
Aaron Ballman478faed2012-06-19 22:09:27 +0000573 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000574 // If we have a generic string, we will allow it because there is no
575 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000576 // (for instance, SAL declspecs in older versions of MSVC).
577 //
Chad Rosierc1183952012-06-26 22:30:43 +0000578 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000579 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000580 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
581 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000582 else
583 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000584 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000585 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000586}
587
John McCall53fa7142010-12-24 02:08:15 +0000588void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000589 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000590 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000591 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000592 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000593 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
594 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000595 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
596 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000597 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
598 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000599 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000600}
601
John McCall53fa7142010-12-24 02:08:15 +0000602void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000603 // Treat these like attributes
604 while (Tok.is(tok::kw___pascal)) {
605 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
606 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000607 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
608 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000609 }
John McCall53fa7142010-12-24 02:08:15 +0000610}
611
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000612void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
613 // Treat these like attributes
614 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000615 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000616 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000617 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
618 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000619 }
620}
621
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000622void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000623 // FIXME: The mapping from attribute spelling to semantics should be
624 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000625 SourceLocation Loc = Tok.getLocation();
626 switch(Tok.getKind()) {
627 // OpenCL qualifiers:
628 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000629 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000630 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000631 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000632 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000633 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000634
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000635 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000636 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000637 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000638 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000639 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000640
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000641 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000642 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000643 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000644 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000645 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000646
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000647 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000648 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000649 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000650 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000651 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000652
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000653 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000654 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000655 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000656 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000658
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000660 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000661 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000662 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000663 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000664
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000665 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000666 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000667 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000668 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000669 break;
670 default: break;
671 }
672}
673
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000674/// \brief Parse a version number.
675///
676/// version:
677/// simple-integer
678/// simple-integer ',' simple-integer
679/// simple-integer ',' simple-integer ',' simple-integer
680VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
681 Range = Tok.getLocation();
682
683 if (!Tok.is(tok::numeric_constant)) {
684 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000685 SkipUntil(tok::comma, tok::r_paren,
686 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000687 return VersionTuple();
688 }
689
690 // Parse the major (and possibly minor and subminor) versions, which
691 // are stored in the numeric constant. We utilize a quirk of the
692 // lexer, which is that it handles something like 1.2.3 as a single
693 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000694 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000695 Buffer.resize(Tok.getLength()+1);
696 const char *ThisTokBegin = &Buffer[0];
697
698 // Get the spelling of the token, which eliminates trigraphs, etc.
699 bool Invalid = false;
700 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
701 if (Invalid)
702 return VersionTuple();
703
704 // Parse the major version.
705 unsigned AfterMajor = 0;
706 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000707 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000708 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
709 ++AfterMajor;
710 }
711
712 if (AfterMajor == 0) {
713 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000714 SkipUntil(tok::comma, tok::r_paren,
715 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000716 return VersionTuple();
717 }
718
719 if (AfterMajor == ActualLength) {
720 ConsumeToken();
721
722 // We only had a single version component.
723 if (Major == 0) {
724 Diag(Tok, diag::err_zero_version);
725 return VersionTuple();
726 }
727
728 return VersionTuple(Major);
729 }
730
731 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
732 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000733 SkipUntil(tok::comma, tok::r_paren,
734 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000735 return VersionTuple();
736 }
737
738 // Parse the minor version.
739 unsigned AfterMinor = AfterMajor + 1;
740 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000741 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000742 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
743 ++AfterMinor;
744 }
745
746 if (AfterMinor == ActualLength) {
747 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000748
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000749 // We had major.minor.
750 if (Major == 0 && Minor == 0) {
751 Diag(Tok, diag::err_zero_version);
752 return VersionTuple();
753 }
754
Chad Rosierc1183952012-06-26 22:30:43 +0000755 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000756 }
757
758 // If what follows is not a '.', we have a problem.
759 if (ThisTokBegin[AfterMinor] != '.') {
760 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000761 SkipUntil(tok::comma, tok::r_paren,
762 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000763 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000764 }
765
766 // Parse the subminor version.
767 unsigned AfterSubminor = AfterMinor + 1;
768 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000769 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000770 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
771 ++AfterSubminor;
772 }
773
774 if (AfterSubminor != ActualLength) {
775 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000776 SkipUntil(tok::comma, tok::r_paren,
777 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000778 return VersionTuple();
779 }
780 ConsumeToken();
781 return VersionTuple(Major, Minor, Subminor);
782}
783
784/// \brief Parse the contents of the "availability" attribute.
785///
786/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000787/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000788///
789/// platform:
790/// identifier
791///
792/// version-arg-list:
793/// version-arg
794/// version-arg ',' version-arg-list
795///
796/// version-arg:
797/// 'introduced' '=' version
798/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000799/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000800/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000801/// opt-message:
802/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000803void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
804 SourceLocation AvailabilityLoc,
805 ParsedAttributes &attrs,
806 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000807 enum { Introduced, Deprecated, Obsoleted, Unknown };
808 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000809 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000810
811 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000812 BalancedDelimiterTracker T(*this, tok::l_paren);
813 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000814 Diag(Tok, diag::err_expected_lparen);
815 return;
816 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000817
818 // Parse the platform name,
819 if (Tok.isNot(tok::identifier)) {
820 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000821 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000822 return;
823 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000824 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000825
826 // Parse the ',' following the platform name.
827 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
828 return;
829
830 // If we haven't grabbed the pointers for the identifiers
831 // "introduced", "deprecated", and "obsoleted", do so now.
832 if (!Ident_introduced) {
833 Ident_introduced = PP.getIdentifierInfo("introduced");
834 Ident_deprecated = PP.getIdentifierInfo("deprecated");
835 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000836 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000837 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000838 }
839
840 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000841 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000842 do {
843 if (Tok.isNot(tok::identifier)) {
844 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000845 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000846 return;
847 }
848 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
849 SourceLocation KeywordLoc = ConsumeToken();
850
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000851 if (Keyword == Ident_unavailable) {
852 if (UnavailableLoc.isValid()) {
853 Diag(KeywordLoc, diag::err_availability_redundant)
854 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000855 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000856 UnavailableLoc = KeywordLoc;
857
858 if (Tok.isNot(tok::comma))
859 break;
860
861 ConsumeToken();
862 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000863 }
864
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000865 if (Tok.isNot(tok::equal)) {
866 Diag(Tok, diag::err_expected_equal_after)
867 << Keyword;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000868 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000869 return;
870 }
871 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000872 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000873 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000874 Diag(Tok, diag::err_expected_string_literal)
875 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000876 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000877 return;
878 }
879 MessageExpr = ParseStringLiteralExpression();
880 break;
881 }
Chad Rosierc1183952012-06-26 22:30:43 +0000882
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000883 SourceRange VersionRange;
884 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000885
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000886 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000887 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000888 return;
889 }
890
891 unsigned Index;
892 if (Keyword == Ident_introduced)
893 Index = Introduced;
894 else if (Keyword == Ident_deprecated)
895 Index = Deprecated;
896 else if (Keyword == Ident_obsoleted)
897 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000898 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000899 Index = Unknown;
900
901 if (Index < Unknown) {
902 if (!Changes[Index].KeywordLoc.isInvalid()) {
903 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000904 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000905 << SourceRange(Changes[Index].KeywordLoc,
906 Changes[Index].VersionRange.getEnd());
907 }
908
909 Changes[Index].KeywordLoc = KeywordLoc;
910 Changes[Index].Version = Version;
911 Changes[Index].VersionRange = VersionRange;
912 } else {
913 Diag(KeywordLoc, diag::err_availability_unknown_change)
914 << Keyword << VersionRange;
915 }
916
917 if (Tok.isNot(tok::comma))
918 break;
919
920 ConsumeToken();
921 } while (true);
922
923 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000924 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000925 return;
926
927 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000928 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000929
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000930 // The 'unavailable' availability cannot be combined with any other
931 // availability changes. Make sure that hasn't happened.
932 if (UnavailableLoc.isValid()) {
933 bool Complained = false;
934 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
935 if (Changes[Index].KeywordLoc.isValid()) {
936 if (!Complained) {
937 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
938 << SourceRange(Changes[Index].KeywordLoc,
939 Changes[Index].VersionRange.getEnd());
940 Complained = true;
941 }
942
943 // Clear out the availability.
944 Changes[Index] = AvailabilityChange();
945 }
946 }
947 }
948
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000949 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000950 attrs.addNew(&Availability,
951 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000952 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000953 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000954 Changes[Introduced],
955 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000956 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000957 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000958 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000959}
960
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000961
Bill Wendling44426052012-12-20 19:22:21 +0000962// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000963// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
964
965void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
966
967void Parser::LateParsedClass::ParseLexedAttributes() {
968 Self->ParseLexedAttributes(*Class);
969}
970
971void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000972 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000973}
974
975/// Wrapper class which calls ParseLexedAttribute, after setting up the
976/// scope appropriately.
977void Parser::ParseLexedAttributes(ParsingClass &Class) {
978 // Deal with templates
979 // FIXME: Test cases to make sure this does the right thing for templates.
980 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
981 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
982 HasTemplateScope);
983 if (HasTemplateScope)
984 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
985
Douglas Gregor3024f072012-04-16 07:05:22 +0000986 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000987 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000988 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000989 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
990 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
991
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000992 // Enter the scope of nested classes
993 if (!AlreadyHasClassScope)
994 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
995 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000996 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000997 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
998 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
999 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001000 }
Chad Rosierc1183952012-06-26 22:30:43 +00001001
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001002 if (!AlreadyHasClassScope)
1003 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1004 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001005}
1006
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001007
1008/// \brief Parse all attributes in LAs, and attach them to Decl D.
1009void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1010 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001011 assert(LAs.parseSoon() &&
1012 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001013 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001014 if (D)
1015 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001016 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001017 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001018 }
1019 LAs.clear();
1020}
1021
1022
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001023/// \brief Finish parsing an attribute for which parsing was delayed.
1024/// This will be called at the end of parsing a class declaration
1025/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001026/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001027/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001028void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1029 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001030 // Save the current token position.
1031 SourceLocation OrigLoc = Tok.getLocation();
1032
1033 // Append the current token at the end of the new token stream so that it
1034 // doesn't get lost.
1035 LA.Toks.push_back(Tok);
1036 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1037 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001038 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001039
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001040 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001041 // FIXME: Do not warn on C++11 attributes, once we start supporting
1042 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001043 Diag(Tok, diag::warn_attribute_on_function_definition)
1044 << LA.AttrName.getName();
1045 }
1046
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001047 ParsedAttributes Attrs(AttrFactory);
1048 SourceLocation endLoc;
1049
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001050 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001051 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001052 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1053 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001054
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001055 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001056 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1057 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001058
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001059 if (LA.Decls.size() == 1) {
1060 // If the Decl is templatized, add template parameters to scope.
1061 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1062 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1063 if (HasTemplateScope)
1064 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001065
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001066 // If the Decl is on a function, add function parameters to the scope.
1067 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1068 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1069 if (HasFunScope)
1070 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001071
Michael Han23214e52012-10-03 01:56:22 +00001072 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001073 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001074
1075 if (HasFunScope) {
1076 Actions.ActOnExitFunctionContext();
1077 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1078 }
1079 if (HasTemplateScope) {
1080 TempScope.Exit();
1081 }
1082 } else {
1083 // If there are multiple decls, then the decl cannot be within the
1084 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001085 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001086 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001087 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001088 } else {
1089 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001090 }
1091
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001092 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1093 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1094 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001095
1096 if (Tok.getLocation() != OrigLoc) {
1097 // Due to a parsing error, we either went over the cached tokens or
1098 // there are still cached tokens left, so we skip the leftover tokens.
1099 // Since this is an uncommon situation that should be avoided, use the
1100 // expensive isBeforeInTranslationUnit call.
1101 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1102 OrigLoc))
1103 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001104 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001105 }
1106}
1107
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001108/// \brief Wrapper around a case statement checking if AttrName is
1109/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001110bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001111 return llvm::StringSwitch<bool>(AttrName)
1112 .Case("guarded_by", true)
1113 .Case("guarded_var", true)
1114 .Case("pt_guarded_by", true)
1115 .Case("pt_guarded_var", true)
1116 .Case("lockable", true)
1117 .Case("scoped_lockable", true)
1118 .Case("no_thread_safety_analysis", true)
1119 .Case("acquired_after", true)
1120 .Case("acquired_before", true)
1121 .Case("exclusive_lock_function", true)
1122 .Case("shared_lock_function", true)
1123 .Case("exclusive_trylock_function", true)
1124 .Case("shared_trylock_function", true)
1125 .Case("unlock_function", true)
1126 .Case("lock_returned", true)
1127 .Case("locks_excluded", true)
1128 .Case("exclusive_locks_required", true)
1129 .Case("shared_locks_required", true)
1130 .Default(false);
1131}
1132
1133/// \brief Parse the contents of thread safety attributes. These
1134/// should always be parsed as an expression list.
1135///
1136/// We need to special case the parsing due to the fact that if the first token
1137/// of the first argument is an identifier, the main parse loop will store
1138/// that token as a "parameter" and the rest of
1139/// the arguments will be added to a list of "arguments". However,
1140/// subsequent tokens in the first argument are lost. We instead parse each
1141/// argument as an expression and add all arguments to the list of "arguments".
1142/// In future, we will take advantage of this special case to also
1143/// deal with some argument scoping issues here (for example, referring to a
1144/// function parameter in the attribute on that function).
1145void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1146 SourceLocation AttrNameLoc,
1147 ParsedAttributes &Attrs,
1148 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001149 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001150
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001151 BalancedDelimiterTracker T(*this, tok::l_paren);
1152 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001153
Aaron Ballman00e99962013-08-31 01:11:41 +00001154 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001155 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001156
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001157 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001158 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001159 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001160 ExprResult ArgExpr(ParseAssignmentExpression());
1161 if (ArgExpr.isInvalid()) {
1162 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001163 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001164 break;
1165 } else {
1166 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001167 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001168 if (Tok.isNot(tok::comma))
1169 break;
1170 ConsumeToken(); // Eat the comma, move to the next argument
1171 }
1172 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001173 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001174 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1175 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001176 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001177 if (EndLoc)
1178 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001179}
1180
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001181void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1182 SourceLocation AttrNameLoc,
1183 ParsedAttributes &Attrs,
1184 SourceLocation *EndLoc) {
1185 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1186
1187 BalancedDelimiterTracker T(*this, tok::l_paren);
1188 T.consumeOpen();
1189
1190 if (Tok.isNot(tok::identifier)) {
1191 Diag(Tok, diag::err_expected_ident);
1192 T.skipToEnd();
1193 return;
1194 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001195 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001196
1197 if (Tok.isNot(tok::comma)) {
1198 Diag(Tok, diag::err_expected_comma);
1199 T.skipToEnd();
1200 return;
1201 }
1202 ConsumeToken();
1203
1204 SourceRange MatchingCTypeRange;
1205 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1206 if (MatchingCType.isInvalid()) {
1207 T.skipToEnd();
1208 return;
1209 }
1210
1211 bool LayoutCompatible = false;
1212 bool MustBeNull = false;
1213 while (Tok.is(tok::comma)) {
1214 ConsumeToken();
1215 if (Tok.isNot(tok::identifier)) {
1216 Diag(Tok, diag::err_expected_ident);
1217 T.skipToEnd();
1218 return;
1219 }
1220 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1221 if (Flag->isStr("layout_compatible"))
1222 LayoutCompatible = true;
1223 else if (Flag->isStr("must_be_null"))
1224 MustBeNull = true;
1225 else {
1226 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1227 T.skipToEnd();
1228 return;
1229 }
1230 ConsumeToken(); // consume flag
1231 }
1232
1233 if (!T.consumeClose()) {
1234 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001235 ArgumentKind, MatchingCType.release(),
1236 LayoutCompatible, MustBeNull,
1237 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001238 }
1239
1240 if (EndLoc)
1241 *EndLoc = T.getCloseLocation();
1242}
1243
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001244/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1245/// of a C++11 attribute-specifier in a location where an attribute is not
1246/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1247/// situation.
1248///
1249/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1250/// this doesn't appear to actually be an attribute-specifier, and the caller
1251/// should try to parse it.
1252bool Parser::DiagnoseProhibitedCXX11Attribute() {
1253 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1254
1255 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1256 case CAK_NotAttributeSpecifier:
1257 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1258 return false;
1259
1260 case CAK_InvalidAttributeSpecifier:
1261 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1262 return false;
1263
1264 case CAK_AttributeSpecifier:
1265 // Parse and discard the attributes.
1266 SourceLocation BeginLoc = ConsumeBracket();
1267 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001268 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001269 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1270 SourceLocation EndLoc = ConsumeBracket();
1271 Diag(BeginLoc, diag::err_attributes_not_allowed)
1272 << SourceRange(BeginLoc, EndLoc);
1273 return true;
1274 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001275 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001276}
1277
Richard Smith98155ad2013-02-20 01:17:14 +00001278/// \brief We have found the opening square brackets of a C++11
1279/// attribute-specifier in a location where an attribute is not permitted, but
1280/// we know where the attributes ought to be written. Parse them anyway, and
1281/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001282void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1283 SourceLocation CorrectLocation) {
1284 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1285 Tok.is(tok::kw_alignas));
1286
1287 // Consume the attributes.
1288 SourceLocation Loc = Tok.getLocation();
1289 ParseCXX11Attributes(Attrs);
1290 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1291
1292 Diag(Loc, diag::err_attributes_not_allowed)
1293 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1294 << FixItHint::CreateRemoval(AttrRange);
1295}
1296
John McCall53fa7142010-12-24 02:08:15 +00001297void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1298 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1299 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001300}
1301
Michael Han64536a62012-11-06 19:34:54 +00001302void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1303 AttributeList *AttrList = attrs.getList();
1304 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001305 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001306 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001307 << AttrList->getName();
1308 AttrList->setInvalid();
1309 }
1310 AttrList = AttrList->getNext();
1311 }
1312}
1313
Chris Lattner53361ac2006-08-10 05:19:57 +00001314/// ParseDeclaration - Parse a full 'declaration', which consists of
1315/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001316/// 'Context' should be a Declarator::TheContext value. This returns the
1317/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001318///
1319/// declaration: [C99 6.7]
1320/// block-declaration ->
1321/// simple-declaration
1322/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001323/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001324/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001325/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001326/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001327/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001328/// others... [FIXME]
1329///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001330Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1331 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001332 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001333 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001334 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001335 // Must temporarily exit the objective-c container scope for
1336 // parsing c none objective-c decls.
1337 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001338
John McCall48871652010-08-21 09:40:31 +00001339 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001340 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001341 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001342 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001343 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001344 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001345 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001346 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001347 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001348 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001349 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001350 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001351 SourceLocation InlineLoc = ConsumeToken();
1352 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1353 break;
1354 }
Chad Rosierc1183952012-06-26 22:30:43 +00001355 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001356 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001357 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001358 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001359 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001360 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001361 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001362 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001363 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001364 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001365 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001366 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001367 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001369 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001370 default:
John McCall53fa7142010-12-24 02:08:15 +00001371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001372 }
Chad Rosierc1183952012-06-26 22:30:43 +00001373
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001374 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001375 // single decl, convert it now. Alias declarations can also declare a type;
1376 // include that too if it is present.
1377 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001378}
1379
1380/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1381/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001382/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1383/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001384///[C90/C++]init-declarator-list ';' [TODO]
1385/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001386///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001387/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001388/// attribute-specifier-seq[opt] type-specifier-seq declarator
1389///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001390/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001391/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001392///
1393/// If FRI is non-null, we might be parsing a for-range-declaration instead
1394/// of a simple-declaration. If we find that we are, we also parse the
1395/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001396Parser::DeclGroupPtrTy
1397Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1398 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001399 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001400 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001401 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001402 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001403
Richard Smith404dfb42013-11-19 22:47:36 +00001404 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1405 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1406
1407 // If we had a free-standing type definition with a missing semicolon, we
1408 // may get this far before the problem becomes obvious.
1409 if (DS.hasTagDefinition() &&
1410 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1411 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001412
Chris Lattner0e894622006-08-13 19:58:17 +00001413 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1414 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001415 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001416 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001417 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001418 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001419 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001420 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001421 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001422 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001423 }
Chad Rosierc1183952012-06-26 22:30:43 +00001424
Richard Smith2386c8b2013-02-22 09:06:26 +00001425 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001426 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001427}
Mike Stump11289f42009-09-09 15:08:12 +00001428
Richard Smith09f76ee2011-10-19 21:33:05 +00001429/// Returns true if this might be the start of a declarator, or a common typo
1430/// for a declarator.
1431bool Parser::MightBeDeclarator(unsigned Context) {
1432 switch (Tok.getKind()) {
1433 case tok::annot_cxxscope:
1434 case tok::annot_template_id:
1435 case tok::caret:
1436 case tok::code_completion:
1437 case tok::coloncolon:
1438 case tok::ellipsis:
1439 case tok::kw___attribute:
1440 case tok::kw_operator:
1441 case tok::l_paren:
1442 case tok::star:
1443 return true;
1444
1445 case tok::amp:
1446 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001447 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001448
Richard Smithc8a79032012-01-09 22:31:44 +00001449 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001450 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001451 NextToken().is(tok::l_square);
1452
1453 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001454 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001455
Richard Smith09f76ee2011-10-19 21:33:05 +00001456 case tok::identifier:
1457 switch (NextToken().getKind()) {
1458 case tok::code_completion:
1459 case tok::coloncolon:
1460 case tok::comma:
1461 case tok::equal:
1462 case tok::equalequal: // Might be a typo for '='.
1463 case tok::kw_alignas:
1464 case tok::kw_asm:
1465 case tok::kw___attribute:
1466 case tok::l_brace:
1467 case tok::l_paren:
1468 case tok::l_square:
1469 case tok::less:
1470 case tok::r_brace:
1471 case tok::r_paren:
1472 case tok::r_square:
1473 case tok::semi:
1474 return true;
1475
1476 case tok::colon:
1477 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001478 // and in block scope it's probably a label. Inside a class definition,
1479 // this is a bit-field.
1480 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001481 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001482
1483 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001484 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001485
1486 default:
1487 return false;
1488 }
1489
1490 default:
1491 return false;
1492 }
1493}
1494
Richard Smithb8caac82012-04-11 20:59:20 +00001495/// Skip until we reach something which seems like a sensible place to pick
1496/// up parsing after a malformed declaration. This will sometimes stop sooner
1497/// than SkipUntil(tok::r_brace) would, but will never stop later.
1498void Parser::SkipMalformedDecl() {
1499 while (true) {
1500 switch (Tok.getKind()) {
1501 case tok::l_brace:
1502 // Skip until matching }, then stop. We've probably skipped over
1503 // a malformed class or function definition or similar.
1504 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001505 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001506 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1507 // This declaration isn't over yet. Keep skipping.
1508 continue;
1509 }
1510 if (Tok.is(tok::semi))
1511 ConsumeToken();
1512 return;
1513
1514 case tok::l_square:
1515 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001516 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001517 continue;
1518
1519 case tok::l_paren:
1520 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001521 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001522 continue;
1523
1524 case tok::r_brace:
1525 return;
1526
1527 case tok::semi:
1528 ConsumeToken();
1529 return;
1530
1531 case tok::kw_inline:
1532 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001533 // a good place to pick back up parsing, except in an Objective-C
1534 // @interface context.
1535 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1536 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001537 return;
1538 break;
1539
1540 case tok::kw_namespace:
1541 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001542 // place to pick back up parsing, except in an Objective-C
1543 // @interface context.
1544 if (Tok.isAtStartOfLine() &&
1545 (!ParsingInObjCContainer || CurParsedObjCImpl))
1546 return;
1547 break;
1548
1549 case tok::at:
1550 // @end is very much like } in Objective-C contexts.
1551 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1552 ParsingInObjCContainer)
1553 return;
1554 break;
1555
1556 case tok::minus:
1557 case tok::plus:
1558 // - and + probably start new method declarations in Objective-C contexts.
1559 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001560 return;
1561 break;
1562
1563 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001564 case tok::annot_module_begin:
1565 case tok::annot_module_end:
1566 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001567 return;
1568
1569 default:
1570 break;
1571 }
1572
1573 ConsumeAnyToken();
1574 }
1575}
1576
John McCalld5a36322009-11-03 19:26:08 +00001577/// ParseDeclGroup - Having concluded that this is either a function
1578/// definition or a group of object declarations, actually parse the
1579/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001580Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1581 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001582 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001583 SourceLocation *DeclEnd,
1584 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001585 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001586 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001587 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001588
John McCalld5a36322009-11-03 19:26:08 +00001589 // Bail out if the first declarator didn't seem well-formed.
1590 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001591 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001592 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001593 }
Mike Stump11289f42009-09-09 15:08:12 +00001594
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001595 // Save late-parsed attributes for now; they need to be parsed in the
1596 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001597 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1598 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001599 if (D.isFunctionDeclarator())
1600 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1601
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001602 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001603 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001604 // Look at the next token to make sure that this isn't a function
1605 // declaration. We have to check this because __attribute__ might be the
1606 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001607 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001608
Douglas Gregor012efe22013-04-16 16:01:32 +00001609 if (AllowFunctionDefinitions) {
1610 if (isStartOfFunctionDefinition(D)) {
1611 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1612 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001613
Douglas Gregor012efe22013-04-16 16:01:32 +00001614 // Recover by treating the 'typedef' as spurious.
1615 DS.ClearStorageClassSpecs();
1616 }
1617
1618 Decl *TheDecl =
1619 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1620 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001621 }
1622
Douglas Gregor012efe22013-04-16 16:01:32 +00001623 if (isDeclarationSpecifier()) {
1624 // If there is an invalid declaration specifier right after the function
1625 // prototype, then we must be in a missing semicolon case where this isn't
1626 // actually a body. Just fall through into the code that handles it as a
1627 // prototype, and let the top-level code handle the erroneous declspec
1628 // where it would otherwise expect a comma or semicolon.
1629 } else {
1630 Diag(Tok, diag::err_expected_fn_body);
1631 SkipUntil(tok::semi);
1632 return DeclGroupPtrTy();
1633 }
John McCalld5a36322009-11-03 19:26:08 +00001634 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001635 if (Tok.is(tok::l_brace)) {
1636 Diag(Tok, diag::err_function_definition_not_allowed);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001637 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor012efe22013-04-16 16:01:32 +00001638 }
John McCalld5a36322009-11-03 19:26:08 +00001639 }
1640 }
1641
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001642 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001643 return DeclGroupPtrTy();
1644
1645 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1646 // must parse and analyze the for-range-initializer before the declaration is
1647 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001648 //
1649 // Handle the Objective-C for-in loop variable similarly, although we
1650 // don't need to parse the container in advance.
1651 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1652 bool IsForRangeLoop = false;
1653 if (Tok.is(tok::colon)) {
1654 IsForRangeLoop = true;
1655 FRI->ColonLoc = ConsumeToken();
1656 if (Tok.is(tok::l_brace))
1657 FRI->RangeExpr = ParseBraceInitializer();
1658 else
1659 FRI->RangeExpr = ParseExpression();
1660 }
1661
Richard Smith02e85f32011-04-14 22:09:26 +00001662 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001663 if (IsForRangeLoop)
1664 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001665 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001666 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001667 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001668 }
1669
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001670 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001671 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001672 if (LateParsedAttrs.size() > 0)
1673 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001674 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001675 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001676 DeclsInGroup.push_back(FirstDecl);
1677
Richard Smith09f76ee2011-10-19 21:33:05 +00001678 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001679
John McCalld5a36322009-11-03 19:26:08 +00001680 // If we don't have a comma, it is either the end of the list (a ';') or an
1681 // error, bail out.
1682 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001683 SourceLocation CommaLoc = ConsumeToken();
1684
1685 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1686 // This comma was followed by a line-break and something which can't be
1687 // the start of a declarator. The comma was probably a typo for a
1688 // semicolon.
1689 Diag(CommaLoc, diag::err_expected_semi_declaration)
1690 << FixItHint::CreateReplacement(CommaLoc, ";");
1691 ExpectSemi = false;
1692 break;
1693 }
John McCalld5a36322009-11-03 19:26:08 +00001694
1695 // Parse the next declarator.
1696 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001697 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001698
1699 // Accept attributes in an init-declarator. In the first declarator in a
1700 // declaration, these would be part of the declspec. In subsequent
1701 // declarators, they become part of the declarator itself, so that they
1702 // don't apply to declarators after *this* one. Examples:
1703 // short __attribute__((common)) var; -> declspec
1704 // short var __attribute__((common)); -> declarator
1705 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001706 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001707
1708 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001709 if (!D.isInvalidType()) {
1710 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1711 D.complete(ThisDecl);
1712 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001713 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001714 }
John McCalld5a36322009-11-03 19:26:08 +00001715 }
1716
1717 if (DeclEnd)
1718 *DeclEnd = Tok.getLocation();
1719
Richard Smith09f76ee2011-10-19 21:33:05 +00001720 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001721 ExpectAndConsumeSemi(Context == Declarator::FileContext
1722 ? diag::err_invalid_token_after_toplevel_declarator
1723 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001724 // Okay, there was no semicolon and one was expected. If we see a
1725 // declaration specifier, just assume it was missing and continue parsing.
1726 // Otherwise things are very confused and we skip to recover.
1727 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001728 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner13901342010-07-11 22:42:07 +00001729 if (Tok.is(tok::semi))
1730 ConsumeToken();
1731 }
John McCalld5a36322009-11-03 19:26:08 +00001732 }
1733
Rafael Espindolaab417692013-07-09 12:05:01 +00001734 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001735}
1736
Richard Smith02e85f32011-04-14 22:09:26 +00001737/// Parse an optional simple-asm-expr and attributes, and attach them to a
1738/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001739bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001740 // If a simple-asm-expr is present, parse it.
1741 if (Tok.is(tok::kw_asm)) {
1742 SourceLocation Loc;
1743 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1744 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001745 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001746 return true;
1747 }
1748
1749 D.setAsmLabel(AsmLabel.release());
1750 D.SetRangeEnd(Loc);
1751 }
1752
1753 MaybeParseGNUAttributes(D);
1754 return false;
1755}
1756
Douglas Gregor23996282009-05-12 21:31:51 +00001757/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1758/// declarator'. This method parses the remainder of the declaration
1759/// (including any attributes or initializer, among other things) and
1760/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001761///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001762/// init-declarator: [C99 6.7]
1763/// declarator
1764/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001765/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1766/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001767/// [C++] declarator initializer[opt]
1768///
1769/// [C++] initializer:
1770/// [C++] '=' initializer-clause
1771/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001772/// [C++0x] '=' 'default' [TODO]
1773/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001774/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001775///
1776/// According to the standard grammar, =default and =delete are function
1777/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001778///
John McCall48871652010-08-21 09:40:31 +00001779Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001780 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001781 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001782 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001783
Richard Smith02e85f32011-04-14 22:09:26 +00001784 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1785}
Mike Stump11289f42009-09-09 15:08:12 +00001786
Richard Smith02e85f32011-04-14 22:09:26 +00001787Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1788 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001789 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001790 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001791 switch (TemplateInfo.Kind) {
1792 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001793 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001794 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001795
Douglas Gregor450f00842009-09-25 18:43:00 +00001796 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001797 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001798 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001799 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001800 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001801 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001802 // Re-direct this decl to refer to the templated decl so that we can
1803 // initialize it.
1804 ThisDecl = VT->getTemplatedDecl();
1805 break;
1806 }
1807 case ParsedTemplateInfo::ExplicitInstantiation: {
1808 if (Tok.is(tok::semi)) {
1809 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1810 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1811 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001812 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001813 return 0;
1814 }
1815 ThisDecl = ThisRes.get();
1816 } else {
1817 // FIXME: This check should be for a variable template instantiation only.
1818
1819 // Check that this is a valid instantiation
1820 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1821 // If the declarator-id is not a template-id, issue a diagnostic and
1822 // recover by ignoring the 'template' keyword.
1823 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1824 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1825 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1826 } else {
1827 SourceLocation LAngleLoc =
1828 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1829 Diag(D.getIdentifierLoc(),
1830 diag::err_explicit_instantiation_with_definition)
1831 << SourceRange(TemplateInfo.TemplateLoc)
1832 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1833
1834 // Recover as if it were an explicit specialization.
1835 TemplateParameterLists FakedParamLists;
1836 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1837 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1838 LAngleLoc));
1839
1840 ThisDecl =
1841 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1842 }
1843 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001844 break;
1845 }
1846 }
Mike Stump11289f42009-09-09 15:08:12 +00001847
Richard Smith74aeef52013-04-26 16:15:35 +00001848 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001849
Douglas Gregor23996282009-05-12 21:31:51 +00001850 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001851 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001852 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001853 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001854
Anders Carlsson991285e2010-09-24 21:25:25 +00001855 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001856 if (D.isFunctionDeclarator())
1857 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1858 << 1 /* delete */;
1859 else
1860 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001861 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001862 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001863 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1864 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001865 else
1866 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001867 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001868 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001869 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001870 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001871 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001872
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001873 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001874 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001875 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001876 cutOffParsing();
1877 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001878 }
Chad Rosierc1183952012-06-26 22:30:43 +00001879
John McCalldadc5752010-08-24 06:29:42 +00001880 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001881
David Blaikiebbafb8a2012-03-11 07:00:24 +00001882 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001883 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001884 ExitScope();
1885 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001886
Douglas Gregor23996282009-05-12 21:31:51 +00001887 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001888 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001889 Actions.ActOnInitializerError(ThisDecl);
1890 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001891 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1892 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001893 }
1894 } else if (Tok.is(tok::l_paren)) {
1895 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001896 BalancedDelimiterTracker T(*this, tok::l_paren);
1897 T.consumeOpen();
1898
Benjamin Kramerf0623432012-08-23 22:51:59 +00001899 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001900 CommaLocsTy CommaLocs;
1901
David Blaikiebbafb8a2012-03-11 07:00:24 +00001902 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001903 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001904 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001905 }
1906
Douglas Gregor23996282009-05-12 21:31:51 +00001907 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001908 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001909 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001910
David Blaikiebbafb8a2012-03-11 07:00:24 +00001911 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001912 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001913 ExitScope();
1914 }
Douglas Gregor23996282009-05-12 21:31:51 +00001915 } else {
1916 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001917 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001918
1919 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1920 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001921
David Blaikiebbafb8a2012-03-11 07:00:24 +00001922 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001923 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001924 ExitScope();
1925 }
1926
Sebastian Redla9351792012-02-11 23:51:47 +00001927 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1928 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001929 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001930 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1931 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001932 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001933 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001934 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001935 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001936 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1937
Sebastian Redl3da34892011-06-05 12:23:16 +00001938 if (D.getCXXScopeSpec().isSet()) {
1939 EnterScope(0);
1940 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1941 }
1942
1943 ExprResult Init(ParseBraceInitializer());
1944
1945 if (D.getCXXScopeSpec().isSet()) {
1946 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1947 ExitScope();
1948 }
1949
1950 if (Init.isInvalid()) {
1951 Actions.ActOnInitializerError(ThisDecl);
1952 } else
1953 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1954 /*DirectInit=*/true, TypeContainsAuto);
1955
Douglas Gregor23996282009-05-12 21:31:51 +00001956 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001957 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001958 }
1959
Richard Smithb2bc2e62011-02-21 20:05:19 +00001960 Actions.FinalizeDeclaration(ThisDecl);
1961
Douglas Gregor23996282009-05-12 21:31:51 +00001962 return ThisDecl;
1963}
1964
Chris Lattner1890ac82006-08-13 01:16:23 +00001965/// ParseSpecifierQualifierList
1966/// specifier-qualifier-list:
1967/// type-specifier specifier-qualifier-list[opt]
1968/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001969/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001970///
Richard Smithc5b05522012-03-12 07:56:15 +00001971void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1972 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001973 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1974 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001975 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001976 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001977
Chris Lattner1890ac82006-08-13 01:16:23 +00001978 // Validate declspec for type-name.
1979 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001980 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1981 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001982 Diag(Tok, diag::err_expected_type);
1983 DS.SetTypeSpecError();
1984 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1985 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001986 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001987 if (!DS.hasTypeSpecifier())
1988 DS.SetTypeSpecError();
1989 }
Mike Stump11289f42009-09-09 15:08:12 +00001990
Chris Lattner1b22eed2006-11-28 05:12:07 +00001991 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001992 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001993 if (DS.getStorageClassSpecLoc().isValid())
1994 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1995 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001996 Diag(DS.getThreadStorageClassSpecLoc(),
1997 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001998 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001999 }
Mike Stump11289f42009-09-09 15:08:12 +00002000
Chris Lattner1b22eed2006-11-28 05:12:07 +00002001 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002002 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002003 if (DS.isInlineSpecified())
2004 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2005 if (DS.isVirtualSpecified())
2006 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2007 if (DS.isExplicitSpecified())
2008 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002009 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002010 }
Richard Smithc5b05522012-03-12 07:56:15 +00002011
2012 // Issue diagnostic and remove constexpr specfier if present.
2013 if (DS.isConstexprSpecified()) {
2014 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2015 DS.ClearConstexprSpec();
2016 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002017}
Chris Lattner53361ac2006-08-10 05:19:57 +00002018
Chris Lattner6cc055a2009-04-12 20:42:31 +00002019/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2020/// specified token is valid after the identifier in a declarator which
2021/// immediately follows the declspec. For example, these things are valid:
2022///
2023/// int x [ 4]; // direct-declarator
2024/// int x ( int y); // direct-declarator
2025/// int(int x ) // direct-declarator
2026/// int x ; // simple-declaration
2027/// int x = 17; // init-declarator-list
2028/// int x , y; // init-declarator-list
2029/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002030/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002031/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002032///
2033/// This is not, because 'x' does not immediately follow the declspec (though
2034/// ')' happens to be valid anyway).
2035/// int (x)
2036///
2037static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2038 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2039 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002040 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002041}
2042
Chris Lattner20a0c612009-04-14 21:34:55 +00002043
2044/// ParseImplicitInt - This method is called when we have an non-typename
2045/// identifier in a declspec (which normally terminates the decl spec) when
2046/// the declspec has no type specifier. In this case, the declspec is either
2047/// malformed or is "implicit int" (in K&R and C89).
2048///
2049/// This method handles diagnosing this prettily and returns false if the
2050/// declspec is done being processed. If it recovers and thinks there may be
2051/// other pieces of declspec after it, it returns true.
2052///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002053bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002054 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002055 AccessSpecifier AS, DeclSpecContext DSC,
2056 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002057 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattner20a0c612009-04-14 21:34:55 +00002059 SourceLocation Loc = Tok.getLocation();
2060 // If we see an identifier that is not a type name, we normally would
2061 // parse it as the identifer being declared. However, when a typename
2062 // is typo'd or the definition is not included, this will incorrectly
2063 // parse the typename as the identifier name and fall over misparsing
2064 // later parts of the diagnostic.
2065 //
2066 // As such, we try to do some look-ahead in cases where this would
2067 // otherwise be an "implicit-int" case to see if this is invalid. For
2068 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2069 // an identifier with implicit int, we'd get a parse error because the
2070 // next token is obviously invalid for a type. Parse these as a case
2071 // with an invalid type specifier.
2072 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002073
Chris Lattner20a0c612009-04-14 21:34:55 +00002074 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002075 // error, do lookahead to try to do better recovery. This never applies
2076 // within a type specifier. Outside of C++, we allow this even if the
2077 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002078 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002079 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002080 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002081 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002082 // If this token is valid for implicit int, e.g. "static x = 4", then
2083 // we just avoid eating the identifier, so it will be parsed as the
2084 // identifier in the declarator.
2085 return false;
2086 }
Mike Stump11289f42009-09-09 15:08:12 +00002087
Richard Smitha952ebb2012-05-15 21:01:51 +00002088 if (getLangOpts().CPlusPlus &&
2089 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2090 // Don't require a type specifier if we have the 'auto' storage class
2091 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002092 if (SS)
2093 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002094 return false;
2095 }
2096
Chris Lattner20a0c612009-04-14 21:34:55 +00002097 // Otherwise, if we don't consume this token, we are going to emit an
2098 // error anyway. Try to recover from various common problems. Check
2099 // to see if this was a reference to a tag name without a tag specified.
2100 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002101 //
2102 // C++ doesn't need this, and isTagName doesn't take SS.
2103 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002104 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002105 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002106
Douglas Gregor0be31a22010-07-02 17:43:08 +00002107 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002108 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002109 case DeclSpec::TST_enum:
2110 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2111 case DeclSpec::TST_union:
2112 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2113 case DeclSpec::TST_struct:
2114 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002115 case DeclSpec::TST_interface:
2116 TagName="__interface"; FixitTagName = "__interface ";
2117 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002118 case DeclSpec::TST_class:
2119 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002120 }
Mike Stump11289f42009-09-09 15:08:12 +00002121
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002122 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002123 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2124 LookupResult R(Actions, TokenName, SourceLocation(),
2125 Sema::LookupOrdinaryName);
2126
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002127 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002128 << TokenName << TagName << getLangOpts().CPlusPlus
2129 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2130
2131 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2132 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2133 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002134 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002135 << TokenName << TagName;
2136 }
Mike Stump11289f42009-09-09 15:08:12 +00002137
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002138 // Parse this as a tag as if the missing tag were present.
2139 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002140 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002141 else
Richard Smithc5b05522012-03-12 07:56:15 +00002142 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002143 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002144 return true;
2145 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002146 }
Mike Stump11289f42009-09-09 15:08:12 +00002147
Richard Smithfe904f02012-05-15 21:29:55 +00002148 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002149 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002150 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2151 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002152 // Look ahead to the next token to try to figure out what this declaration
2153 // was supposed to be.
2154 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002155 case tok::l_paren: {
2156 // static x(4); // 'x' is not a type
2157 // x(int n); // 'x' is not a type
2158 // x (*p)[]; // 'x' is a type
2159 //
2160 // Since we're in an error case (or the rare 'implicit int in C++' MS
2161 // extension), we can afford to perform a tentative parse to determine
2162 // which case we're in.
2163 TentativeParsingAction PA(*this);
2164 ConsumeToken();
2165 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2166 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002167
2168 if (TPR != TPResult::False()) {
2169 // The identifier is followed by a parenthesized declarator.
2170 // It's supposed to be a type.
2171 break;
2172 }
2173
2174 // If we're in a context where we could be declaring a constructor,
2175 // check whether this is a constructor declaration with a bogus name.
2176 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2177 IdentifierInfo *II = Tok.getIdentifierInfo();
2178 if (Actions.isCurrentClassNameTypo(II, SS)) {
2179 Diag(Loc, diag::err_constructor_bad_name)
2180 << Tok.getIdentifierInfo() << II
2181 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2182 Tok.setIdentifierInfo(II);
2183 }
2184 }
2185 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002186 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002187 case tok::comma:
2188 case tok::equal:
2189 case tok::kw_asm:
2190 case tok::l_brace:
2191 case tok::l_square:
2192 case tok::semi:
2193 // This looks like a variable or function declaration. The type is
2194 // probably missing. We're done parsing decl-specifiers.
2195 if (SS)
2196 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2197 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002198
2199 default:
2200 // This is probably supposed to be a type. This includes cases like:
2201 // int f(itn);
2202 // struct S { unsinged : 4; };
2203 break;
2204 }
2205 }
2206
Chad Rosierc1183952012-06-26 22:30:43 +00002207 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002208 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002209 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002210 IdentifierInfo *II = Tok.getIdentifierInfo();
2211 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002212 // The action emitted a diagnostic, so we don't have to.
2213 if (T) {
2214 // The action has suggested that the type T could be used. Set that as
2215 // the type in the declaration specifiers, consume the would-be type
2216 // name token, and we're done.
2217 const char *PrevSpec;
2218 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002219 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002220 DS.SetRangeEnd(Tok.getLocation());
2221 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002222 // There may be other declaration specifiers after this.
2223 return true;
2224 } else if (II != Tok.getIdentifierInfo()) {
2225 // If no type was suggested, the correction is to a keyword
2226 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002227 // There may be other declaration specifiers after this.
2228 return true;
2229 }
Chad Rosierc1183952012-06-26 22:30:43 +00002230
Douglas Gregor15e56022009-10-13 23:27:22 +00002231 // Fall through; the action had no suggestion for us.
2232 } else {
2233 // The action did not emit a diagnostic, so emit one now.
2234 SourceRange R;
2235 if (SS) R = SS->getRange();
2236 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2237 }
Mike Stump11289f42009-09-09 15:08:12 +00002238
Douglas Gregor15e56022009-10-13 23:27:22 +00002239 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002240 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002241 DS.SetRangeEnd(Tok.getLocation());
2242 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002243
Chris Lattner20a0c612009-04-14 21:34:55 +00002244 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2245 // avoid rippling error messages on subsequent uses of the same type,
2246 // could be useful if #include was forgotten.
2247 return false;
2248}
2249
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002250/// \brief Determine the declaration specifier context from the declarator
2251/// context.
2252///
2253/// \param Context the declarator context, which is one of the
2254/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002255Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002256Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2257 if (Context == Declarator::MemberContext)
2258 return DSC_class;
2259 if (Context == Declarator::FileContext)
2260 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002261 if (Context == Declarator::TrailingReturnContext)
2262 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002263 return DSC_normal;
2264}
2265
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002266/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2267///
2268/// FIXME: Simply returns an alignof() expression if the argument is a
2269/// type. Ideally, the type should be propagated directly into Sema.
2270///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002271/// [C11] type-id
2272/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002273/// [C++0x] type-id ...[opt]
2274/// [C++0x] assignment-expression ...[opt]
2275ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2276 SourceLocation &EllipsisLoc) {
2277 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002278 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002279 SourceLocation TypeLoc = Tok.getLocation();
2280 ParsedType Ty = ParseTypeName().get();
2281 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002282 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2283 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002284 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002285 ER = ParseConstantExpression();
2286
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002287 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002288 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002289
2290 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002291}
2292
2293/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2294/// attribute to Attrs.
2295///
2296/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002297/// [C11] '_Alignas' '(' type-id ')'
2298/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002299/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2300/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002301void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002302 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002303 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2304 "Not an alignment-specifier!");
2305
Richard Smithd11c7a12013-01-29 01:48:07 +00002306 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2307 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002308
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002309 BalancedDelimiterTracker T(*this, tok::l_paren);
2310 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002311 return;
2312
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002313 SourceLocation EllipsisLoc;
2314 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002315 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002316 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002317 return;
2318 }
2319
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002320 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002321 if (EndLoc)
2322 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002323
Aaron Ballman00e99962013-08-31 01:11:41 +00002324 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002325 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002326 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2327 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002328}
2329
Richard Smith404dfb42013-11-19 22:47:36 +00002330/// Determine whether we're looking at something that might be a declarator
2331/// in a simple-declaration. If it can't possibly be a declarator, maybe
2332/// diagnose a missing semicolon after a prior tag definition in the decl
2333/// specifier.
2334///
2335/// \return \c true if an error occurred and this can't be any kind of
2336/// declaration.
2337bool
2338Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2339 DeclSpecContext DSContext,
2340 LateParsedAttrList *LateAttrs) {
2341 assert(DS.hasTagDefinition() && "shouldn't call this");
2342
2343 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002344
2345 if (getLangOpts().CPlusPlus &&
2346 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2347 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2348 TryAnnotateCXXScopeToken(EnteringContext)) {
2349 SkipMalformedDecl();
2350 return true;
2351 }
2352
Richard Smith698875a2013-11-20 23:40:57 +00002353 bool HasScope = Tok.is(tok::annot_cxxscope);
2354 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2355 Token AfterScope = HasScope ? NextToken() : Tok;
2356
Richard Smith404dfb42013-11-19 22:47:36 +00002357 // Determine whether the following tokens could possibly be a
2358 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002359 bool MightBeDeclarator = true;
2360 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2361 // A declarator-id can't start with 'typename'.
2362 MightBeDeclarator = false;
2363 } else if (AfterScope.is(tok::annot_template_id)) {
2364 // If we have a type expressed as a template-id, this cannot be a
2365 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2366 TemplateIdAnnotation *Annot =
2367 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2368 if (Annot->Kind == TNK_Type_template)
2369 MightBeDeclarator = false;
2370 } else if (AfterScope.is(tok::identifier)) {
2371 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2372
Richard Smith404dfb42013-11-19 22:47:36 +00002373 // These tokens cannot come after the declarator-id in a
2374 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002375 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2376 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2377 Next.is(tok::coloncolon)) {
2378 // Missing a semicolon.
2379 MightBeDeclarator = false;
2380 } else if (HasScope) {
2381 // If the declarator-id has a scope specifier, it must redeclare a
2382 // previously-declared entity. If that's a type (and this is not a
2383 // typedef), that's an error.
2384 CXXScopeSpec SS;
2385 Actions.RestoreNestedNameSpecifierAnnotation(
2386 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2387 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2388 Sema::NameClassification Classification = Actions.ClassifyName(
2389 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2390 /*IsAddressOfOperand*/false);
2391 switch (Classification.getKind()) {
2392 case Sema::NC_Error:
2393 SkipMalformedDecl();
2394 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002395
Richard Smith698875a2013-11-20 23:40:57 +00002396 case Sema::NC_Keyword:
2397 case Sema::NC_NestedNameSpecifier:
2398 llvm_unreachable("typo correction and nested name specifiers not "
2399 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002400
Richard Smith698875a2013-11-20 23:40:57 +00002401 case Sema::NC_Type:
2402 case Sema::NC_TypeTemplate:
2403 // Not a previously-declared non-type entity.
2404 MightBeDeclarator = false;
2405 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002406
Richard Smith698875a2013-11-20 23:40:57 +00002407 case Sema::NC_Unknown:
2408 case Sema::NC_Expression:
2409 case Sema::NC_VarTemplate:
2410 case Sema::NC_FunctionTemplate:
2411 // Might be a redeclaration of a prior entity.
2412 break;
2413 }
Richard Smith404dfb42013-11-19 22:47:36 +00002414 }
Richard Smith404dfb42013-11-19 22:47:36 +00002415 }
2416
Richard Smith698875a2013-11-20 23:40:57 +00002417 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002418 return false;
2419
2420 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
2421 diag::err_expected_semi_after_tagdecl)
2422 << DeclSpec::getSpecifierName(DS.getTypeSpecType());
2423
2424 // Try to recover from the typo, by dropping the tag definition and parsing
2425 // the problematic tokens as a type.
2426 //
2427 // FIXME: Split the DeclSpec into pieces for the standalone
2428 // declaration and pieces for the following declaration, instead
2429 // of assuming that all the other pieces attach to new declaration,
2430 // and call ParsedFreeStandingDeclSpec as appropriate.
2431 DS.ClearTypeSpecType();
2432 ParsedTemplateInfo NotATemplate;
2433 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2434 return false;
2435}
2436
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002437/// ParseDeclarationSpecifiers
2438/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002439/// storage-class-specifier declaration-specifiers[opt]
2440/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002441/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002442/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002443/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002444/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002445///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002446/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002447/// 'typedef'
2448/// 'extern'
2449/// 'static'
2450/// 'auto'
2451/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002452/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002453/// [C++11] 'thread_local'
2454/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002455/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002456/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002457/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002458/// [C++] 'virtual'
2459/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002460/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002461/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002462/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002463
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002464///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002465void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002466 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002467 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002468 DeclSpecContext DSContext,
2469 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002470 if (DS.getSourceRange().isInvalid()) {
2471 DS.SetRangeStart(Tok.getLocation());
2472 DS.SetRangeEnd(Tok.getLocation());
2473 }
Chad Rosierc1183952012-06-26 22:30:43 +00002474
Douglas Gregordf593fb2011-11-07 17:33:42 +00002475 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002476 bool AttrsLastTime = false;
2477 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002478 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002479 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002480 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002481 unsigned DiagID = 0;
2482
Chris Lattner4d8f8732006-11-28 05:05:08 +00002483 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002484
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002485 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002486 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002487 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002488 if (!AttrsLastTime)
2489 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002490 else {
2491 // Reject C++11 attributes that appertain to decl specifiers as
2492 // we don't support any C++11 attributes that appertain to decl
2493 // specifiers. This also conforms to what g++ 4.8 is doing.
2494 ProhibitCXX11Attributes(attrs);
2495
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002496 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002497 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002498
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002499 // If this is not a declaration specifier token, we're done reading decl
2500 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002501 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002502 return;
Mike Stump11289f42009-09-09 15:08:12 +00002503
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002504 case tok::l_square:
2505 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002506 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002507 goto DoneWithDeclSpec;
2508
2509 ProhibitAttributes(attrs);
2510 // FIXME: It would be good to recover by accepting the attributes,
2511 // but attempting to do that now would cause serious
2512 // madness in terms of diagnostics.
2513 attrs.clear();
2514 attrs.Range = SourceRange();
2515
2516 ParseCXX11Attributes(attrs);
2517 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002518 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002519
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002520 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002521 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002522 if (DS.hasTypeSpecifier()) {
2523 bool AllowNonIdentifiers
2524 = (getCurScope()->getFlags() & (Scope::ControlScope |
2525 Scope::BlockScope |
2526 Scope::TemplateParamScope |
2527 Scope::FunctionPrototypeScope |
2528 Scope::AtCatchScope)) == 0;
2529 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002530 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002531 (DSContext == DSC_class && DS.isFriendSpecified());
2532
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002533 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002534 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002535 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002536 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002537 }
2538
Douglas Gregor80039242011-02-15 20:33:25 +00002539 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2540 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2541 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002542 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002543 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002544 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002545 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002546 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002547 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002548
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002549 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002550 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002551 }
2552
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002553 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002554 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002555 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002556 if (!DS.hasTypeSpecifier())
2557 DS.SetTypeSpecError();
2558 goto DoneWithDeclSpec;
2559 }
John McCall8bc2a702010-03-01 18:20:46 +00002560 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2561 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002562 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002563
2564 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002565 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002566 goto DoneWithDeclSpec;
2567
John McCall9dab4e62009-12-12 11:40:51 +00002568 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002569 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2570 Tok.getAnnotationRange(),
2571 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002572
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002573 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002574 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002575 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002576 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002577 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002578 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002579
2580 // C++ [class.qual]p2:
2581 // In a lookup in which the constructor is an acceptable lookup
2582 // result and the nested-name-specifier nominates a class C:
2583 //
2584 // - if the name specified after the
2585 // nested-name-specifier, when looked up in C, is the
2586 // injected-class-name of C (Clause 9), or
2587 //
2588 // - if the name specified after the nested-name-specifier
2589 // is the same as the identifier or the
2590 // simple-template-id's template-name in the last
2591 // component of the nested-name-specifier,
2592 //
2593 // the name is instead considered to name the constructor of
2594 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002595 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002596 // Thus, if the template-name is actually the constructor
2597 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002598 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002599 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002600 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002601 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002602 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002603 if (isConstructorDeclarator()) {
2604 // The user meant this to be an out-of-line constructor
2605 // definition, but template arguments are not allowed
2606 // there. Just allow this as a constructor; we'll
2607 // complain about it later.
2608 goto DoneWithDeclSpec;
2609 }
2610
2611 // The user meant this to name a type, but it actually names
2612 // a constructor with some extraneous template
2613 // arguments. Complain, then parse it as a type as the user
2614 // intended.
2615 Diag(TemplateId->TemplateNameLoc,
2616 diag::err_out_of_line_template_id_names_constructor)
2617 << TemplateId->Name;
2618 }
2619
John McCall9dab4e62009-12-12 11:40:51 +00002620 DS.getTypeSpecScope() = SS;
2621 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002622 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002623 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002624 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002625 continue;
2626 }
2627
Douglas Gregorc5790df2009-09-28 07:26:33 +00002628 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002629 DS.getTypeSpecScope() = SS;
2630 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002631 if (Tok.getAnnotationValue()) {
2632 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002633 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002634 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002635 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002636 if (isInvalid)
2637 break;
John McCallba7bf592010-08-24 05:47:05 +00002638 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002639 else
2640 DS.SetTypeSpecError();
2641 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2642 ConsumeToken(); // The typename
2643 }
2644
Douglas Gregor167fa622009-03-25 15:40:00 +00002645 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002646 goto DoneWithDeclSpec;
2647
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002648 // If we're in a context where the identifier could be a class name,
2649 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002650 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002651 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002652 &SS)) {
2653 if (isConstructorDeclarator())
2654 goto DoneWithDeclSpec;
2655
2656 // As noted in C++ [class.qual]p2 (cited above), when the name
2657 // of the class is qualified in a context where it could name
2658 // a constructor, its a constructor name. However, we've
2659 // looked at the declarator, and the user probably meant this
2660 // to be a type. Complain that it isn't supposed to be treated
2661 // as a type, then proceed to parse it as a type.
2662 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2663 << Next.getIdentifierInfo();
2664 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002665
John McCallba7bf592010-08-24 05:47:05 +00002666 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2667 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002668 getCurScope(), &SS,
2669 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002670 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002671 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002672
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002673 // If the referenced identifier is not a type, then this declspec is
2674 // erroneous: We already checked about that it has no type specifier, and
2675 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002676 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002677 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002678 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002679 ParsedAttributesWithRange Attrs(AttrFactory);
2680 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2681 if (!Attrs.empty()) {
2682 AttrsLastTime = true;
2683 attrs.takeAllFrom(Attrs);
2684 }
2685 continue;
2686 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002687 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002688 }
Mike Stump11289f42009-09-09 15:08:12 +00002689
John McCall9dab4e62009-12-12 11:40:51 +00002690 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002691 ConsumeToken(); // The C++ scope.
2692
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002694 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002695 if (isInvalid)
2696 break;
Mike Stump11289f42009-09-09 15:08:12 +00002697
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002698 DS.SetRangeEnd(Tok.getLocation());
2699 ConsumeToken(); // The typename.
2700
2701 continue;
2702 }
Mike Stump11289f42009-09-09 15:08:12 +00002703
Chris Lattnere387d9e2009-01-21 19:48:37 +00002704 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002705 // If we've previously seen a tag definition, we were almost surely
2706 // missing a semicolon after it.
2707 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2708 goto DoneWithDeclSpec;
2709
John McCallba7bf592010-08-24 05:47:05 +00002710 if (Tok.getAnnotationValue()) {
2711 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002713 DiagID, T);
2714 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002715 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002716
Chris Lattner005fc1b2010-04-05 18:18:31 +00002717 if (isInvalid)
2718 break;
2719
Chris Lattnere387d9e2009-01-21 19:48:37 +00002720 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2721 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002722
Chris Lattnere387d9e2009-01-21 19:48:37 +00002723 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2724 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002725 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002726 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002727 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002728
Chris Lattnere387d9e2009-01-21 19:48:37 +00002729 continue;
2730 }
Mike Stump11289f42009-09-09 15:08:12 +00002731
Douglas Gregor06873092011-04-28 15:48:45 +00002732 case tok::kw___is_signed:
2733 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2734 // typically treats it as a trait. If we see __is_signed as it appears
2735 // in libstdc++, e.g.,
2736 //
2737 // static const bool __is_signed;
2738 //
2739 // then treat __is_signed as an identifier rather than as a keyword.
2740 if (DS.getTypeSpecType() == TST_bool &&
2741 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002742 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2743 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002744
2745 // We're done with the declaration-specifiers.
2746 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002747
Chris Lattner16fac4f2008-07-26 01:18:38 +00002748 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002749 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002750 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002751 // In C++, check to see if this is a scope specifier like foo::bar::, if
2752 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002753 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002754 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002755 if (!DS.hasTypeSpecifier())
2756 DS.SetTypeSpecError();
2757 goto DoneWithDeclSpec;
2758 }
2759 if (!Tok.is(tok::identifier))
2760 continue;
2761 }
Mike Stump11289f42009-09-09 15:08:12 +00002762
Chris Lattner16fac4f2008-07-26 01:18:38 +00002763 // This identifier can only be a typedef name if we haven't already seen
2764 // a type-specifier. Without this check we misparse:
2765 // typedef int X; struct Y { short X; }; as 'short int'.
2766 if (DS.hasTypeSpecifier())
2767 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002768
John Thompson22334602010-02-05 00:12:22 +00002769 // Check for need to substitute AltiVec keyword tokens.
2770 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2771 break;
2772
Richard Smith3092a3b2012-05-09 18:56:43 +00002773 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2774 // allow the use of a typedef name as a type specifier.
2775 if (DS.isTypeAltiVecVector())
2776 goto DoneWithDeclSpec;
2777
John McCallba7bf592010-08-24 05:47:05 +00002778 ParsedType TypeRep =
2779 Actions.getTypeName(*Tok.getIdentifierInfo(),
2780 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002781
Chris Lattner6cc055a2009-04-12 20:42:31 +00002782 // If this is not a typedef name, don't parse it as part of the declspec,
2783 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002784 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002785 ParsedAttributesWithRange Attrs(AttrFactory);
2786 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2787 if (!Attrs.empty()) {
2788 AttrsLastTime = true;
2789 attrs.takeAllFrom(Attrs);
2790 }
2791 continue;
2792 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002793 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002794 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002795
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002796 // If we're in a context where the identifier could be a class name,
2797 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002798 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002799 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002800 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002801 goto DoneWithDeclSpec;
2802
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002803 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002804 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002805 if (isInvalid)
2806 break;
Mike Stump11289f42009-09-09 15:08:12 +00002807
Chris Lattner16fac4f2008-07-26 01:18:38 +00002808 DS.SetRangeEnd(Tok.getLocation());
2809 ConsumeToken(); // The identifier
2810
2811 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2812 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002813 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002814 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002815 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002816
Steve Naroffcd5e7822008-09-22 10:28:57 +00002817 // Need to support trailing type qualifiers (e.g. "id<p> const").
2818 // If a type specifier follows, it will be diagnosed elsewhere.
2819 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002820 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002821
2822 // type-name
2823 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002824 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002825 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002826 // This template-id does not refer to a type name, so we're
2827 // done with the type-specifiers.
2828 goto DoneWithDeclSpec;
2829 }
2830
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002831 // If we're in a context where the template-id could be a
2832 // constructor name or specialization, check whether this is a
2833 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002834 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002835 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002836 isConstructorDeclarator())
2837 goto DoneWithDeclSpec;
2838
Douglas Gregor7f741122009-02-25 19:37:18 +00002839 // Turn the template-id annotation token into a type annotation
2840 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002841 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002842 continue;
2843 }
2844
Chris Lattnere37e2332006-08-15 04:50:22 +00002845 // GNU attributes support.
2846 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002847 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002848 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002849
2850 // Microsoft declspec support.
2851 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002852 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002853 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002854
Steve Naroff44ac7772008-12-25 14:16:32 +00002855 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002856 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002857 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002858 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002859 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002860 // FIXME: This does not work correctly if it is set to be a declspec
2861 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002862 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2863 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002864 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002865 }
Eli Friedman53339e02009-06-08 23:27:34 +00002866
Aaron Ballman317a77f2013-05-22 23:25:32 +00002867 case tok::kw___sptr:
2868 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002869 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002870 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002871 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002872 case tok::kw___cdecl:
2873 case tok::kw___stdcall:
2874 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002875 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002876 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002877 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002878 continue;
2879
Dawn Perchik335e16b2010-09-03 01:29:35 +00002880 // Borland single token adornments.
2881 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002882 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002883 continue;
2884
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002885 // OpenCL single token adornments.
2886 case tok::kw___kernel:
2887 ParseOpenCLAttributes(DS.getAttributes());
2888 continue;
2889
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002890 // storage-class-specifier
2891 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002892 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2893 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002894 break;
2895 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002896 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002897 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002898 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2899 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002900 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002901 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002902 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2903 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002904 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002905 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002906 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002907 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002908 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2909 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002910 break;
2911 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002912 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002913 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002914 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2915 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002916 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002917 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002918 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002919 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2921 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002922 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002923 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2924 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002925 break;
2926 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002927 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2928 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002929 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002930 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002931 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2932 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002933 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002934 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002935 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2936 PrevSpec, DiagID);
2937 break;
2938 case tok::kw_thread_local:
2939 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2940 PrevSpec, DiagID);
2941 break;
2942 case tok::kw__Thread_local:
2943 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2944 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002945 break;
Mike Stump11289f42009-09-09 15:08:12 +00002946
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002947 // function-specifier
2948 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002949 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002950 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002951 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002952 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002953 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002954 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002955 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002956 break;
Richard Smith0015f092013-01-17 22:16:11 +00002957 case tok::kw__Noreturn:
2958 if (!getLangOpts().C11)
2959 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002960 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002961 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002962
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002963 // alignment-specifier
2964 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002965 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002966 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002967 ParseAlignmentSpecifier(DS.getAttributes());
2968 continue;
2969
Anders Carlssoncd8db412009-05-06 04:46:28 +00002970 // friend
2971 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002972 if (DSContext == DSC_class)
2973 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2974 else {
2975 PrevSpec = ""; // not actually used by the diagnostic
2976 DiagID = diag::err_friend_invalid_in_context;
2977 isInvalid = true;
2978 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002979 break;
Mike Stump11289f42009-09-09 15:08:12 +00002980
Douglas Gregor26701a42011-09-09 02:06:17 +00002981 // Modules
2982 case tok::kw___module_private__:
2983 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2984 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002985
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002986 // constexpr
2987 case tok::kw_constexpr:
2988 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2989 break;
2990
Chris Lattnere387d9e2009-01-21 19:48:37 +00002991 // type-specifier
2992 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002993 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2994 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002995 break;
2996 case tok::kw_long:
2997 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002998 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2999 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003000 else
John McCall49bfce42009-08-03 20:12:06 +00003001 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3002 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003003 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003004 case tok::kw___int64:
3005 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3006 DiagID);
3007 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003008 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003009 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3010 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003011 break;
3012 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003013 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3014 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003015 break;
3016 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003017 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3018 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003019 break;
3020 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003021 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3022 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003023 break;
3024 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3026 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003027 break;
3028 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3030 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003031 break;
3032 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003033 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3034 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003035 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003036 case tok::kw___int128:
3037 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3038 DiagID);
3039 break;
3040 case tok::kw_half:
3041 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3042 DiagID);
3043 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003044 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3046 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003047 break;
3048 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3050 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003051 break;
3052 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003053 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3054 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003055 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003056 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3058 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003059 break;
3060 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003061 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3062 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003063 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003064 case tok::kw_bool:
3065 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003066 if (Tok.is(tok::kw_bool) &&
3067 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3068 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3069 PrevSpec = ""; // Not used by the diagnostic.
3070 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003071 // For better error recovery.
3072 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003073 isInvalid = true;
3074 } else {
3075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3076 DiagID);
3077 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003078 break;
3079 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3081 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003082 break;
3083 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003084 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3085 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003086 break;
3087 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3089 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003090 break;
John Thompson22334602010-02-05 00:12:22 +00003091 case tok::kw___vector:
3092 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3093 break;
3094 case tok::kw___pixel:
3095 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3096 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003097 case tok::kw_image1d_t:
3098 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
3099 PrevSpec, DiagID);
3100 break;
3101 case tok::kw_image1d_array_t:
3102 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
3103 PrevSpec, DiagID);
3104 break;
3105 case tok::kw_image1d_buffer_t:
3106 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3107 PrevSpec, DiagID);
3108 break;
3109 case tok::kw_image2d_t:
3110 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3111 PrevSpec, DiagID);
3112 break;
3113 case tok::kw_image2d_array_t:
3114 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3115 PrevSpec, DiagID);
3116 break;
3117 case tok::kw_image3d_t:
3118 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3119 PrevSpec, DiagID);
3120 break;
Guy Benyei61054192013-02-07 10:55:47 +00003121 case tok::kw_sampler_t:
3122 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3123 PrevSpec, DiagID);
3124 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003125 case tok::kw_event_t:
3126 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3127 PrevSpec, DiagID);
3128 break;
John McCall39439732011-04-09 22:50:59 +00003129 case tok::kw___unknown_anytype:
3130 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3131 PrevSpec, DiagID);
3132 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003133
3134 // class-specifier:
3135 case tok::kw_class:
3136 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003137 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003138 case tok::kw_union: {
3139 tok::TokenKind Kind = Tok.getKind();
3140 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003141
3142 // These are attributes following class specifiers.
3143 // To produce better diagnostic, we parse them when
3144 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003145 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003146 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003147 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003148
3149 // If there are attributes following class specifier,
3150 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003151 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003152 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003153 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003154 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003155 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003156 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003157
3158 // enum-specifier:
3159 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003160 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003161 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003162 continue;
3163
3164 // cv-qualifier:
3165 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003166 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003167 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003168 break;
3169 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003170 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003171 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003172 break;
3173 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003174 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003175 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003176 break;
3177
Douglas Gregor333489b2009-03-27 23:10:48 +00003178 // C++ typename-specifier:
3179 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003180 if (TryAnnotateTypeOrScopeToken()) {
3181 DS.SetTypeSpecError();
3182 goto DoneWithDeclSpec;
3183 }
3184 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003185 continue;
3186 break;
3187
Chris Lattnere387d9e2009-01-21 19:48:37 +00003188 // GNU typeof support.
3189 case tok::kw_typeof:
3190 ParseTypeofSpecifier(DS);
3191 continue;
3192
David Blaikie15a430a2011-12-04 05:04:18 +00003193 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003194 ParseDecltypeSpecifier(DS);
3195 continue;
3196
Alexis Hunt4a257072011-05-19 05:37:45 +00003197 case tok::kw___underlying_type:
3198 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003199 continue;
3200
3201 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003202 // C11 6.7.2.4/4:
3203 // If the _Atomic keyword is immediately followed by a left parenthesis,
3204 // it is interpreted as a type specifier (with a type name), not as a
3205 // type qualifier.
3206 if (NextToken().is(tok::l_paren)) {
3207 ParseAtomicSpecifier(DS);
3208 continue;
3209 }
3210 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3211 getLangOpts());
3212 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003213
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003214 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003215 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003216 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003217 goto DoneWithDeclSpec;
3218 case tok::kw___private:
3219 case tok::kw___global:
3220 case tok::kw___local:
3221 case tok::kw___constant:
3222 case tok::kw___read_only:
3223 case tok::kw___write_only:
3224 case tok::kw___read_write:
3225 ParseOpenCLQualifiers(DS);
3226 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003227
Steve Naroffcfdf6162008-06-05 00:02:44 +00003228 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003229 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003230 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3231 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003232 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003233 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003234
Douglas Gregor3a001f42010-11-19 17:10:50 +00003235 if (!ParseObjCProtocolQualifiers(DS))
3236 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3237 << FixItHint::CreateInsertion(Loc, "id")
3238 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003239
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003240 // Need to support trailing type qualifiers (e.g. "id<p> const").
3241 // If a type specifier follows, it will be diagnosed elsewhere.
3242 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003243 }
John McCall49bfce42009-08-03 20:12:06 +00003244 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003245 if (isInvalid) {
3246 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003247 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003248
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003249 if (DiagID == diag::ext_duplicate_declspec)
3250 Diag(Tok, DiagID)
3251 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3252 else
3253 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003254 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003255
Chris Lattner2e232092008-03-13 06:29:04 +00003256 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003257 if (DiagID != diag::err_bool_redeclaration)
3258 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003259
3260 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003261 }
3262}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003263
Chris Lattner70ae4912007-10-29 04:42:53 +00003264/// ParseStructDeclaration - Parse a struct declaration without the terminating
3265/// semicolon.
3266///
Chris Lattner90a26b02007-01-23 04:38:16 +00003267/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003268/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003269/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003270/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003271/// struct-declarator-list:
3272/// struct-declarator
3273/// struct-declarator-list ',' struct-declarator
3274/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3275/// struct-declarator:
3276/// declarator
3277/// [GNU] declarator attributes[opt]
3278/// declarator[opt] ':' constant-expression
3279/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3280///
Chris Lattnera12405b2008-04-10 06:46:29 +00003281void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003282ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003283
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003284 if (Tok.is(tok::kw___extension__)) {
3285 // __extension__ silences extension warnings in the subexpression.
3286 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003287 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003288 return ParseStructDeclaration(DS, Fields);
3289 }
Mike Stump11289f42009-09-09 15:08:12 +00003290
Steve Naroff97170802007-08-20 22:28:22 +00003291 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003292 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003293
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003294 // If there are no declarators, this is a free-standing declaration
3295 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003296 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003297 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3298 DS);
3299 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003300 return;
3301 }
3302
3303 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003304 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003305 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003306 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003307 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003308 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003309
Bill Wendling44426052012-12-20 19:22:21 +00003310 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003311 if (!FirstDeclarator)
3312 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003313
Steve Naroff97170802007-08-20 22:28:22 +00003314 /// struct-declarator: declarator
3315 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003316 if (Tok.isNot(tok::colon)) {
3317 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3318 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003319 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003320 }
Mike Stump11289f42009-09-09 15:08:12 +00003321
Chris Lattner76c72282007-10-09 17:33:22 +00003322 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003323 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003324 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003325 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003326 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003327 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003328 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003329 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003330
Steve Naroff97170802007-08-20 22:28:22 +00003331 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003332 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003333
John McCallcfefb6d2009-11-03 02:38:08 +00003334 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003335 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003336
Steve Naroff97170802007-08-20 22:28:22 +00003337 // If we don't have a comma, it is either the end of the list (a ';')
3338 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003339 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003340 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003341
Steve Naroff97170802007-08-20 22:28:22 +00003342 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003343 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003344
John McCallcfefb6d2009-11-03 02:38:08 +00003345 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003346 }
Steve Naroff97170802007-08-20 22:28:22 +00003347}
3348
3349/// ParseStructUnionBody
3350/// struct-contents:
3351/// struct-declaration-list
3352/// [EXT] empty
3353/// [GNU] "struct-declaration-list" without terminatoring ';'
3354/// struct-declaration-list:
3355/// struct-declaration
3356/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003357/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003358///
Chris Lattner1300fb92007-01-23 23:42:53 +00003359void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003360 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003361 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3362 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003363 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003364
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003365 BalancedDelimiterTracker T(*this, tok::l_brace);
3366 if (T.consumeOpen())
3367 return;
Mike Stump11289f42009-09-09 15:08:12 +00003368
Douglas Gregor658b9552009-01-09 22:42:13 +00003369 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003370 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003371
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003372 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003373
Chris Lattner7b9ace62007-01-23 20:11:08 +00003374 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003375 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003376 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003377
Chris Lattner736ed5d2007-06-09 05:59:07 +00003378 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003379 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003380 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003381 continue;
3382 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003383
Andy Gibbsc804e082013-04-03 09:46:04 +00003384 // Parse _Static_assert declaration.
3385 if (Tok.is(tok::kw__Static_assert)) {
3386 SourceLocation DeclEnd;
3387 ParseStaticAssertDeclaration(DeclEnd);
3388 continue;
3389 }
3390
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003391 if (Tok.is(tok::annot_pragma_pack)) {
3392 HandlePragmaPack();
3393 continue;
3394 }
3395
3396 if (Tok.is(tok::annot_pragma_align)) {
3397 HandlePragmaAlign();
3398 continue;
3399 }
3400
John McCallcfefb6d2009-11-03 02:38:08 +00003401 if (!Tok.is(tok::at)) {
3402 struct CFieldCallback : FieldCallback {
3403 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003404 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003405 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003406
John McCall48871652010-08-21 09:40:31 +00003407 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003408 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003409 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3410
Eli Friedman934dbbf2012-08-08 23:53:27 +00003411 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003412 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003413 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003414 FD.D.getDeclSpec().getSourceRange().getBegin(),
3415 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003416 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003417 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003418 }
John McCallcfefb6d2009-11-03 02:38:08 +00003419 } Callback(*this, TagDecl, FieldDecls);
3420
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003421 // Parse all the comma separated declarators.
3422 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003423 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003424 } else { // Handle @defs
3425 ConsumeToken();
3426 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3427 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003428 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003429 continue;
3430 }
3431 ConsumeToken();
3432 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3433 if (!Tok.is(tok::identifier)) {
3434 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003435 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003436 continue;
3437 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003438 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003439 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003440 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003441 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3442 ConsumeToken();
3443 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003444 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003445
Chris Lattner76c72282007-10-09 17:33:22 +00003446 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003447 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003448 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003449 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003450 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003451 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003452 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3453 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003454 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner245c5332010-02-02 00:37:27 +00003455 // If we stopped at a ';', eat it.
3456 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003457 }
3458 }
Mike Stump11289f42009-09-09 15:08:12 +00003459
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003460 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003461
John McCall084e83d2011-03-24 11:26:52 +00003462 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003463 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003464 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003465
Douglas Gregor0be31a22010-07-02 17:43:08 +00003466 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003467 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003468 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003469 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003470 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003471 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3472 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003473}
3474
Chris Lattner3b561a32006-08-13 00:12:11 +00003475/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003476/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003477/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003478///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003479/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3480/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003481/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3482/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003483/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003484/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003485///
Richard Smith7d137e32012-03-23 03:33:32 +00003486/// [C++11] enum-head '{' enumerator-list[opt] '}'
3487/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003488///
Richard Smith7d137e32012-03-23 03:33:32 +00003489/// enum-head: [C++11]
3490/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3491/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3492/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003493///
Richard Smith7d137e32012-03-23 03:33:32 +00003494/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003495/// 'enum'
3496/// 'enum' 'class'
3497/// 'enum' 'struct'
3498///
Richard Smith7d137e32012-03-23 03:33:32 +00003499/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003500/// ':' type-specifier-seq
3501///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003502/// [C++] elaborated-type-specifier:
3503/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3504///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003505void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003506 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003507 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003508 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003509 if (Tok.is(tok::code_completion)) {
3510 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003511 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003512 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003513 }
John McCallcb432fa2011-07-06 05:58:41 +00003514
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003515 // If attributes exist after tag, parse them.
3516 ParsedAttributesWithRange attrs(AttrFactory);
3517 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003518 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003519
3520 // If declspecs exist after tag, parse them.
3521 while (Tok.is(tok::kw___declspec))
3522 ParseMicrosoftDeclSpec(attrs);
3523
Richard Smith0f8ee222012-01-10 01:33:14 +00003524 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003525 bool IsScopedUsingClassTag = false;
3526
John McCallbeae29a2012-06-23 22:30:04 +00003527 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003528 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3529 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3530 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003531 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003532 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003533
Bill Wendling44426052012-12-20 19:22:21 +00003534 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003535 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003536 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003537
3538 // They are allowed afterwards, though.
3539 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003540 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003541 while (Tok.is(tok::kw___declspec))
3542 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003543 }
Richard Smith7d137e32012-03-23 03:33:32 +00003544
John McCall6347b682012-05-07 06:16:58 +00003545 // C++11 [temp.explicit]p12:
3546 // The usual access controls do not apply to names used to specify
3547 // explicit instantiations.
3548 // We extend this to also cover explicit specializations. Note that
3549 // we don't suppress if this turns out to be an elaborated type
3550 // specifier.
3551 bool shouldDelayDiagsInTag =
3552 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3553 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3554 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003555
Richard Smithbfdb1082012-03-12 08:56:40 +00003556 // Enum definitions should not be parsed in a trailing-return-type.
3557 bool AllowDeclaration = DSC != DSC_trailing;
3558
3559 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003560 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003561 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003562
Abramo Bagnarad7548482010-05-19 21:37:53 +00003563 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003564 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003565 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3566 // if a fixed underlying type is allowed.
3567 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003568
3569 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003570 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003571 return;
3572
3573 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003574 Diag(Tok, diag::err_expected_ident);
3575 if (Tok.isNot(tok::l_brace)) {
3576 // Has no name and is not a definition.
3577 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003578 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003579 return;
3580 }
3581 }
3582 }
Mike Stump11289f42009-09-09 15:08:12 +00003583
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003584 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003585 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003586 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003587 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003588
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003589 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003590 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003591 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003592 }
Mike Stump11289f42009-09-09 15:08:12 +00003593
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003594 // If an identifier is present, consume and remember it.
3595 IdentifierInfo *Name = 0;
3596 SourceLocation NameLoc;
3597 if (Tok.is(tok::identifier)) {
3598 Name = Tok.getIdentifierInfo();
3599 NameLoc = ConsumeToken();
3600 }
Mike Stump11289f42009-09-09 15:08:12 +00003601
Richard Smith0f8ee222012-01-10 01:33:14 +00003602 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003603 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3604 // declaration of a scoped enumeration.
3605 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003606 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003607 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003608 }
3609
John McCall6347b682012-05-07 06:16:58 +00003610 // Okay, end the suppression area. We'll decide whether to emit the
3611 // diagnostics in a second.
3612 if (shouldDelayDiagsInTag)
3613 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003614
Douglas Gregor0bf31402010-10-08 23:50:27 +00003615 TypeResult BaseType;
3616
Douglas Gregord1f69f62010-12-01 17:42:47 +00003617 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003618 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003619 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003620 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003621 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003622 // If we're in class scope, this can either be an enum declaration with
3623 // an underlying type, or a declaration of a bitfield member. We try to
3624 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003625 // (integer literal, sizeof); if it's still ambiguous, we then consider
3626 // anything that's a simple-type-specifier followed by '(' as an
3627 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003628 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003629 EnterExpressionEvaluationContext Unevaluated(Actions,
3630 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003631 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003632 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003633 // bit-field. This is the common case.
3634 if (TPR == TPResult::True())
3635 PossibleBitfield = true;
3636 // If the next token starts a type-specifier-seq, it may be either a
3637 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003638 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003639 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003640 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003641 GetLookAheadToken(2).getKind() == tok::semi) {
3642 // Consume the ':'.
3643 ConsumeToken();
3644 } else {
3645 // We have the start of a type-specifier-seq, so we have to perform
3646 // tentative parsing to determine whether we have an expression or a
3647 // type.
3648 TentativeParsingAction TPA(*this);
3649
3650 // Consume the ':'.
3651 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003652
3653 // If we see a type specifier followed by an open-brace, we have an
3654 // ambiguity between an underlying type and a C++11 braced
3655 // function-style cast. Resolve this by always treating it as an
3656 // underlying type.
3657 // FIXME: The standard is not entirely clear on how to disambiguate in
3658 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003659 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003660 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003661 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003662 // We'll parse this as a bitfield later.
3663 PossibleBitfield = true;
3664 TPA.Revert();
3665 } else {
3666 // We have a type-specifier-seq.
3667 TPA.Commit();
3668 }
3669 }
3670 } else {
3671 // Consume the ':'.
3672 ConsumeToken();
3673 }
3674
3675 if (!PossibleBitfield) {
3676 SourceRange Range;
3677 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003678
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003679 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003680 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003681 } else if (!getLangOpts().ObjC2) {
3682 if (getLangOpts().CPlusPlus)
3683 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3684 else
3685 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3686 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003687 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003688 }
3689
Richard Smith0f8ee222012-01-10 01:33:14 +00003690 // There are four options here. If we have 'friend enum foo;' then this is a
3691 // friend declaration, and cannot have an accompanying definition. If we have
3692 // 'enum foo;', then this is a forward declaration. If we have
3693 // 'enum foo {...' then this is a definition. Otherwise we have something
3694 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003695 //
3696 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3697 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3698 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3699 //
John McCallfaf5fb42010-08-26 23:41:50 +00003700 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003701 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003702 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003703 } else if (Tok.is(tok::l_brace)) {
3704 if (DS.isFriendSpecified()) {
3705 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3706 << SourceRange(DS.getFriendSpecLoc());
3707 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003708 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003709 TUK = Sema::TUK_Friend;
3710 } else {
3711 TUK = Sema::TUK_Definition;
3712 }
Richard Smith369b9f92012-06-25 21:37:02 +00003713 } else if (DSC != DSC_type_specifier &&
3714 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003715 (Tok.isAtStartOfLine() &&
3716 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003717 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3718 if (Tok.isNot(tok::semi)) {
3719 // A semicolon was missing after this declaration. Diagnose and recover.
3720 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3721 "enum");
3722 PP.EnterToken(Tok);
3723 Tok.setKind(tok::semi);
3724 }
John McCall6347b682012-05-07 06:16:58 +00003725 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003726 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003727 }
3728
3729 // If this is an elaborated type specifier, and we delayed
3730 // diagnostics before, just merge them into the current pool.
3731 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3732 diagsFromTag.redelay();
3733 }
Richard Smith7d137e32012-03-23 03:33:32 +00003734
3735 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003736 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003737 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003738 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003739 // Skip the rest of this declarator, up until the comma or semicolon.
3740 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003741 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003742 return;
3743 }
3744
3745 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3746 // Enumerations can't be explicitly instantiated.
3747 DS.SetTypeSpecError();
3748 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3749 return;
3750 }
3751
3752 assert(TemplateInfo.TemplateParams && "no template parameters");
3753 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3754 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003755 }
Chad Rosierc1183952012-06-26 22:30:43 +00003756
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003757 if (TUK == Sema::TUK_Reference)
3758 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003759
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003760 if (!Name && TUK != Sema::TUK_Definition) {
3761 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003762
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003763 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003764 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003765 return;
3766 }
Richard Smith7d137e32012-03-23 03:33:32 +00003767
Douglas Gregord6ab8742009-05-28 23:31:59 +00003768 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003769 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003770 const char *PrevSpec = 0;
3771 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003772 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003773 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003774 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003775 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003776 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003777
Douglas Gregorba41d012010-04-24 16:38:41 +00003778 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003779 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003780 // dependent tag.
3781 if (!Name) {
3782 DS.SetTypeSpecError();
3783 Diag(Tok, diag::err_expected_type_name_after_typename);
3784 return;
3785 }
Chad Rosierc1183952012-06-26 22:30:43 +00003786
Douglas Gregor0be31a22010-07-02 17:43:08 +00003787 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003788 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003789 NameLoc);
3790 if (Type.isInvalid()) {
3791 DS.SetTypeSpecError();
3792 return;
3793 }
Chad Rosierc1183952012-06-26 22:30:43 +00003794
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003795 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3796 NameLoc.isValid() ? NameLoc : StartLoc,
3797 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003798 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003799
Douglas Gregorba41d012010-04-24 16:38:41 +00003800 return;
3801 }
Mike Stump11289f42009-09-09 15:08:12 +00003802
John McCall48871652010-08-21 09:40:31 +00003803 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003804 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003805 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003806 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003807 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003808 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003809 }
Chad Rosierc1183952012-06-26 22:30:43 +00003810
Douglas Gregorba41d012010-04-24 16:38:41 +00003811 DS.SetTypeSpecError();
3812 return;
3813 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003814
Richard Smith369b9f92012-06-25 21:37:02 +00003815 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003816 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003817
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003818 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3819 NameLoc.isValid() ? NameLoc : StartLoc,
3820 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003821 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003822}
3823
Chris Lattnerc1915e22007-01-25 07:29:02 +00003824/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3825/// enumerator-list:
3826/// enumerator
3827/// enumerator-list ',' enumerator
3828/// enumerator:
3829/// enumeration-constant
3830/// enumeration-constant '=' constant-expression
3831/// enumeration-constant:
3832/// identifier
3833///
John McCall48871652010-08-21 09:40:31 +00003834void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003835 // Enter the scope of the enum body and start the definition.
3836 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003837 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003838
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003839 BalancedDelimiterTracker T(*this, tok::l_brace);
3840 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003841
Chris Lattner37256fb2007-08-27 17:24:30 +00003842 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003843 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003844 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003845
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003846 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003847
John McCall48871652010-08-21 09:40:31 +00003848 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003849
Chris Lattnerc1915e22007-01-25 07:29:02 +00003850 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003851 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003852 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3853 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003854
John McCall811a0f52010-10-22 23:36:17 +00003855 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003856 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003857 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003858 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003859 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003860
Chris Lattnerc1915e22007-01-25 07:29:02 +00003861 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003862 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003863 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003864
Chris Lattner76c72282007-10-09 17:33:22 +00003865 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003866 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003867 AssignedVal = ParseConstantExpression();
3868 if (AssignedVal.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003869 SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003870 }
Mike Stump11289f42009-09-09 15:08:12 +00003871
Chris Lattnerc1915e22007-01-25 07:29:02 +00003872 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003873 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3874 LastEnumConstDecl,
3875 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003876 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003877 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003878 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003879
Chris Lattner4ef40012007-06-11 01:28:17 +00003880 EnumConstantDecls.push_back(EnumConstDecl);
3881 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003882
Douglas Gregorce66d022010-09-07 14:51:08 +00003883 if (Tok.is(tok::identifier)) {
3884 // We're missing a comma between enumerators.
3885 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003886 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003887 << FixItHint::CreateInsertion(Loc, ", ");
3888 continue;
3889 }
Chad Rosierc1183952012-06-26 22:30:43 +00003890
Chris Lattner76c72282007-10-09 17:33:22 +00003891 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003892 break;
3893 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003894
Richard Smith5d164bc2011-10-15 05:09:34 +00003895 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003896 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003897 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3898 diag::ext_enumerator_list_comma_cxx :
3899 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003900 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003901 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003902 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3903 << FixItHint::CreateRemoval(CommaLoc);
3904 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003905 }
Mike Stump11289f42009-09-09 15:08:12 +00003906
Chris Lattnerc1915e22007-01-25 07:29:02 +00003907 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003908 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003909
Chris Lattnerc1915e22007-01-25 07:29:02 +00003910 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003911 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003912 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003913
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003914 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003915 EnumDecl, EnumConstantDecls,
3916 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003917 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003918
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003919 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003920 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3921 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003922
3923 // The next token must be valid after an enum definition. If not, a ';'
3924 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003925 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3926 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003927 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3928 // Push this token back into the preprocessor and change our current token
3929 // to ';' so that the rest of the code recovers as though there were an
3930 // ';' after the definition.
3931 PP.EnterToken(Tok);
3932 Tok.setKind(tok::semi);
3933 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003934}
Chris Lattner3b561a32006-08-13 00:12:11 +00003935
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003936/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003937/// start of a type-qualifier-list.
3938bool Parser::isTypeQualifier() const {
3939 switch (Tok.getKind()) {
3940 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003941
3942 // type-qualifier only in OpenCL
3943 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003944 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003945
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003946 // type-qualifier
3947 case tok::kw_const:
3948 case tok::kw_volatile:
3949 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003950 case tok::kw___private:
3951 case tok::kw___local:
3952 case tok::kw___global:
3953 case tok::kw___constant:
3954 case tok::kw___read_only:
3955 case tok::kw___read_write:
3956 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003957 return true;
3958 }
3959}
3960
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003961/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3962/// is definitely a type-specifier. Return false if it isn't part of a type
3963/// specifier or if we're not sure.
3964bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3965 switch (Tok.getKind()) {
3966 default: return false;
3967 // type-specifiers
3968 case tok::kw_short:
3969 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003970 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003971 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003972 case tok::kw_signed:
3973 case tok::kw_unsigned:
3974 case tok::kw__Complex:
3975 case tok::kw__Imaginary:
3976 case tok::kw_void:
3977 case tok::kw_char:
3978 case tok::kw_wchar_t:
3979 case tok::kw_char16_t:
3980 case tok::kw_char32_t:
3981 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003982 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003983 case tok::kw_float:
3984 case tok::kw_double:
3985 case tok::kw_bool:
3986 case tok::kw__Bool:
3987 case tok::kw__Decimal32:
3988 case tok::kw__Decimal64:
3989 case tok::kw__Decimal128:
3990 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003991
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003992 // OpenCL specific types:
3993 case tok::kw_image1d_t:
3994 case tok::kw_image1d_array_t:
3995 case tok::kw_image1d_buffer_t:
3996 case tok::kw_image2d_t:
3997 case tok::kw_image2d_array_t:
3998 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003999 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004000 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004001
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004002 // struct-or-union-specifier (C99) or class-specifier (C++)
4003 case tok::kw_class:
4004 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004005 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004006 case tok::kw_union:
4007 // enum-specifier
4008 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004009
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004010 // typedef-name
4011 case tok::annot_typename:
4012 return true;
4013 }
4014}
4015
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004016/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004017/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004018bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004019 switch (Tok.getKind()) {
4020 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004021
Chris Lattner020bab92009-01-04 23:41:41 +00004022 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004023 if (TryAltiVecVectorToken())
4024 return true;
4025 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004026 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004027 // Annotate typenames and C++ scope specifiers. If we get one, just
4028 // recurse to handle whatever we get.
4029 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004030 return true;
4031 if (Tok.is(tok::identifier))
4032 return false;
4033 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004034
Chris Lattner020bab92009-01-04 23:41:41 +00004035 case tok::coloncolon: // ::foo::bar
4036 if (NextToken().is(tok::kw_new) || // ::new
4037 NextToken().is(tok::kw_delete)) // ::delete
4038 return false;
4039
Chris Lattner020bab92009-01-04 23:41:41 +00004040 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004041 return true;
4042 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004043
Chris Lattnere37e2332006-08-15 04:50:22 +00004044 // GNU attributes support.
4045 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004046 // GNU typeof support.
4047 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004048
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004049 // type-specifiers
4050 case tok::kw_short:
4051 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004052 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004053 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004054 case tok::kw_signed:
4055 case tok::kw_unsigned:
4056 case tok::kw__Complex:
4057 case tok::kw__Imaginary:
4058 case tok::kw_void:
4059 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004060 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004061 case tok::kw_char16_t:
4062 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004063 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004064 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004065 case tok::kw_float:
4066 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004067 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004068 case tok::kw__Bool:
4069 case tok::kw__Decimal32:
4070 case tok::kw__Decimal64:
4071 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004072 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004073
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004074 // OpenCL specific types:
4075 case tok::kw_image1d_t:
4076 case tok::kw_image1d_array_t:
4077 case tok::kw_image1d_buffer_t:
4078 case tok::kw_image2d_t:
4079 case tok::kw_image2d_array_t:
4080 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004081 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004082 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004083
Chris Lattner861a2262008-04-13 18:59:07 +00004084 // struct-or-union-specifier (C99) or class-specifier (C++)
4085 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004086 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004087 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004088 case tok::kw_union:
4089 // enum-specifier
4090 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004091
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004092 // type-qualifier
4093 case tok::kw_const:
4094 case tok::kw_volatile:
4095 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004096
John McCallea0a39e2012-11-14 00:49:39 +00004097 // Debugger support.
4098 case tok::kw___unknown_anytype:
4099
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004100 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004101 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004102 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004103
Chris Lattner409bf7d2008-10-20 00:25:30 +00004104 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4105 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004106 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004107
Steve Naroff44ac7772008-12-25 14:16:32 +00004108 case tok::kw___cdecl:
4109 case tok::kw___stdcall:
4110 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004111 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004112 case tok::kw___w64:
4113 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004114 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004115 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004116 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004117
4118 case tok::kw___private:
4119 case tok::kw___local:
4120 case tok::kw___global:
4121 case tok::kw___constant:
4122 case tok::kw___read_only:
4123 case tok::kw___read_write:
4124 case tok::kw___write_only:
4125
Eli Friedman53339e02009-06-08 23:27:34 +00004126 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004127
4128 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004129 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00004130
Richard Smith8e1ac332013-03-28 01:55:44 +00004131 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004132 case tok::kw__Atomic:
4133 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004134 }
4135}
4136
Chris Lattneracd58a32006-08-06 17:24:14 +00004137/// isDeclarationSpecifier() - Return true if the current token is part of a
4138/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004139///
4140/// \param DisambiguatingWithExpression True to indicate that the purpose of
4141/// this check is to disambiguate between an expression and a declaration.
4142bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004143 switch (Tok.getKind()) {
4144 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004145
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004146 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004147 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004148
Chris Lattner020bab92009-01-04 23:41:41 +00004149 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004150 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004151 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004152 return false;
John Thompson22334602010-02-05 00:12:22 +00004153 if (TryAltiVecVectorToken())
4154 return true;
4155 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004156 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004157 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004158 // Annotate typenames and C++ scope specifiers. If we get one, just
4159 // recurse to handle whatever we get.
4160 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004161 return true;
4162 if (Tok.is(tok::identifier))
4163 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004164
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004165 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004166 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004167 // expression is permitted, then this is probably a class message send
4168 // missing the initial '['. In this case, we won't consider this to be
4169 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004170 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004171 isStartOfObjCClassMessageMissingOpenBracket())
4172 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004173
John McCall1f476a12010-02-26 08:45:28 +00004174 return isDeclarationSpecifier();
4175
Chris Lattner020bab92009-01-04 23:41:41 +00004176 case tok::coloncolon: // ::foo::bar
4177 if (NextToken().is(tok::kw_new) || // ::new
4178 NextToken().is(tok::kw_delete)) // ::delete
4179 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004180
Chris Lattner020bab92009-01-04 23:41:41 +00004181 // Annotate typenames and C++ scope specifiers. If we get one, just
4182 // recurse to handle whatever we get.
4183 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004184 return true;
4185 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004186
Chris Lattneracd58a32006-08-06 17:24:14 +00004187 // storage-class-specifier
4188 case tok::kw_typedef:
4189 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004190 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004191 case tok::kw_static:
4192 case tok::kw_auto:
4193 case tok::kw_register:
4194 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004195 case tok::kw_thread_local:
4196 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004197
Douglas Gregor26701a42011-09-09 02:06:17 +00004198 // Modules
4199 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004200
John McCallea0a39e2012-11-14 00:49:39 +00004201 // Debugger support
4202 case tok::kw___unknown_anytype:
4203
Chris Lattneracd58a32006-08-06 17:24:14 +00004204 // type-specifiers
4205 case tok::kw_short:
4206 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004207 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004208 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004209 case tok::kw_signed:
4210 case tok::kw_unsigned:
4211 case tok::kw__Complex:
4212 case tok::kw__Imaginary:
4213 case tok::kw_void:
4214 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004215 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004216 case tok::kw_char16_t:
4217 case tok::kw_char32_t:
4218
Chris Lattneracd58a32006-08-06 17:24:14 +00004219 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004220 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004221 case tok::kw_float:
4222 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004223 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004224 case tok::kw__Bool:
4225 case tok::kw__Decimal32:
4226 case tok::kw__Decimal64:
4227 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004228 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004229
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004230 // OpenCL specific types:
4231 case tok::kw_image1d_t:
4232 case tok::kw_image1d_array_t:
4233 case tok::kw_image1d_buffer_t:
4234 case tok::kw_image2d_t:
4235 case tok::kw_image2d_array_t:
4236 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004237 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004238 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004239
Chris Lattner861a2262008-04-13 18:59:07 +00004240 // struct-or-union-specifier (C99) or class-specifier (C++)
4241 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004242 case tok::kw_struct:
4243 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004244 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004245 // enum-specifier
4246 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004247
Chris Lattneracd58a32006-08-06 17:24:14 +00004248 // type-qualifier
4249 case tok::kw_const:
4250 case tok::kw_volatile:
4251 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004252
Chris Lattneracd58a32006-08-06 17:24:14 +00004253 // function-specifier
4254 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004255 case tok::kw_virtual:
4256 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004257 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004258
Richard Smith1dba27c2013-01-29 09:02:09 +00004259 // alignment-specifier
4260 case tok::kw__Alignas:
4261
Richard Smithd16fe122012-10-25 00:00:53 +00004262 // friend keyword.
4263 case tok::kw_friend:
4264
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004265 // static_assert-declaration
4266 case tok::kw__Static_assert:
4267
Chris Lattner599e47e2007-08-09 17:01:07 +00004268 // GNU typeof support.
4269 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004270
Chris Lattner599e47e2007-08-09 17:01:07 +00004271 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004272 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004273
Richard Smithd16fe122012-10-25 00:00:53 +00004274 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004275 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004276 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004277
Richard Smith8e1ac332013-03-28 01:55:44 +00004278 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004279 case tok::kw__Atomic:
4280 return true;
4281
Chris Lattner8b2ec162008-07-26 03:38:44 +00004282 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4283 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004284 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004285
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004286 // typedef-name
4287 case tok::annot_typename:
4288 return !DisambiguatingWithExpression ||
4289 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004290
Steve Narofff192fab2009-01-06 19:34:12 +00004291 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004292 case tok::kw___cdecl:
4293 case tok::kw___stdcall:
4294 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004295 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004296 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004297 case tok::kw___sptr:
4298 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004299 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004300 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004301 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004302 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004303 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004304
4305 case tok::kw___private:
4306 case tok::kw___local:
4307 case tok::kw___global:
4308 case tok::kw___constant:
4309 case tok::kw___read_only:
4310 case tok::kw___read_write:
4311 case tok::kw___write_only:
4312
Eli Friedman53339e02009-06-08 23:27:34 +00004313 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004314 }
4315}
4316
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004317bool Parser::isConstructorDeclarator() {
4318 TentativeParsingAction TPA(*this);
4319
4320 // Parse the C++ scope specifier.
4321 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004322 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004323 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004324 TPA.Revert();
4325 return false;
4326 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004327
4328 // Parse the constructor name.
4329 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4330 // We already know that we have a constructor name; just consume
4331 // the token.
4332 ConsumeToken();
4333 } else {
4334 TPA.Revert();
4335 return false;
4336 }
4337
Richard Smith43f340f2012-03-27 23:05:05 +00004338 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004339 if (Tok.isNot(tok::l_paren)) {
4340 TPA.Revert();
4341 return false;
4342 }
4343 ConsumeParen();
4344
Richard Smith43f340f2012-03-27 23:05:05 +00004345 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4346 // that we have a constructor.
4347 if (Tok.is(tok::r_paren) ||
4348 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004349 TPA.Revert();
4350 return true;
4351 }
4352
Richard Smithf2163662013-09-06 00:12:20 +00004353 // A C++11 attribute here signals that we have a constructor, and is an
4354 // attribute on the first constructor parameter.
4355 if (getLangOpts().CPlusPlus11 &&
4356 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4357 /*OuterMightBeMessageSend*/ true)) {
4358 TPA.Revert();
4359 return true;
4360 }
4361
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004362 // If we need to, enter the specified scope.
4363 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004364 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004365 DeclScopeObj.EnterDeclaratorScope();
4366
Francois Pichet79f3a872011-01-31 04:54:32 +00004367 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004368 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004369 MaybeParseMicrosoftAttributes(Attrs);
4370
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004371 // Check whether the next token(s) are part of a declaration
4372 // specifier, in which case we have the start of a parameter and,
4373 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004374 bool IsConstructor = false;
4375 if (isDeclarationSpecifier())
4376 IsConstructor = true;
4377 else if (Tok.is(tok::identifier) ||
4378 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4379 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4380 // This might be a parenthesized member name, but is more likely to
4381 // be a constructor declaration with an invalid argument type. Keep
4382 // looking.
4383 if (Tok.is(tok::annot_cxxscope))
4384 ConsumeToken();
4385 ConsumeToken();
4386
4387 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004388 // which must have one of the following syntactic forms (see the
4389 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004390 switch (Tok.getKind()) {
4391 case tok::l_paren:
4392 // C(X ( int));
4393 case tok::l_square:
4394 // C(X [ 5]);
4395 // C(X [ [attribute]]);
4396 case tok::coloncolon:
4397 // C(X :: Y);
4398 // C(X :: *p);
4399 case tok::r_paren:
4400 // C(X )
4401 // Assume this isn't a constructor, rather than assuming it's a
4402 // constructor with an unnamed parameter of an ill-formed type.
4403 break;
4404
4405 default:
4406 IsConstructor = true;
4407 break;
4408 }
4409 }
4410
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004411 TPA.Revert();
4412 return IsConstructor;
4413}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004414
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004415/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004416/// type-qualifier-list: [C99 6.7.5]
4417/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004418/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004419/// [ only if VendorAttributesAllowed=true ]
4420/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004421/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004422/// [ only if VendorAttributesAllowed=true ]
4423/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004424/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004425/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004426///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004427void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4428 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004429 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004430 bool AtomicAllowed,
4431 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004432 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004433 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004434 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004435 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004436 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004437 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004438
4439 SourceLocation EndLoc;
4440
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004441 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004442 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004443 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004444 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004445 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004446
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004447 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004448 case tok::code_completion:
4449 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004450 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004451
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004452 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004453 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004454 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004455 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004456 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004457 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004458 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004459 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004460 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004461 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004462 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004463 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004464 case tok::kw__Atomic:
4465 if (!AtomicAllowed)
4466 goto DoneWithTypeQuals;
4467 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4468 getLangOpts());
4469 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004470
4471 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004472 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004473 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004474 goto DoneWithTypeQuals;
4475 case tok::kw___private:
4476 case tok::kw___global:
4477 case tok::kw___local:
4478 case tok::kw___constant:
4479 case tok::kw___read_only:
4480 case tok::kw___write_only:
4481 case tok::kw___read_write:
4482 ParseOpenCLQualifiers(DS);
4483 break;
4484
Aaron Ballman317a77f2013-05-22 23:25:32 +00004485 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004486 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4487 // with the MS modifier keyword.
4488 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004489 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4490 if (TryKeywordIdentFallback(false))
4491 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004492 }
4493 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004494 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004495 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004496 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004497 case tok::kw___cdecl:
4498 case tok::kw___stdcall:
4499 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004500 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004501 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004502 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004503 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004504 continue;
4505 }
4506 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004507 case tok::kw___pascal:
4508 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004509 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004510 continue;
4511 }
4512 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004513 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004514 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004515 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004516 continue; // do *not* consume the next token!
4517 }
4518 // otherwise, FALL THROUGH!
4519 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004520 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004521 // If this is not a type-qualifier token, we're done reading type
4522 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004523 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004524 if (EndLoc.isValid())
4525 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004526 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004527 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004528
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004529 // If the specifier combination wasn't legal, issue a diagnostic.
4530 if (isInvalid) {
4531 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004532 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004533 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004534 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004535 }
4536}
4537
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004538
4539/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4540///
4541void Parser::ParseDeclarator(Declarator &D) {
4542 /// This implements the 'declarator' production in the C grammar, then checks
4543 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004544 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004545}
4546
Richard Smith0efa75c2012-03-29 01:16:42 +00004547static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4548 if (Kind == tok::star || Kind == tok::caret)
4549 return true;
4550
4551 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4552 if (!Lang.CPlusPlus)
4553 return false;
4554
4555 return Kind == tok::amp || Kind == tok::ampamp;
4556}
4557
Sebastian Redlbd150f42008-11-21 19:14:01 +00004558/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4559/// is parsed by the function passed to it. Pass null, and the direct-declarator
4560/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004561/// ptr-operator production.
4562///
Richard Smith09f76ee2011-10-19 21:33:05 +00004563/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004564/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4565/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004566///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004567/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4568/// [C] pointer[opt] direct-declarator
4569/// [C++] direct-declarator
4570/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004571///
4572/// pointer: [C99 6.7.5]
4573/// '*' type-qualifier-list[opt]
4574/// '*' type-qualifier-list[opt] pointer
4575///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004576/// ptr-operator:
4577/// '*' cv-qualifier-seq[opt]
4578/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004579/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004580/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004581/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004582/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004583void Parser::ParseDeclaratorInternal(Declarator &D,
4584 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004585 if (Diags.hasAllExtensionsSilenced())
4586 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004587
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004588 // C++ member pointers start with a '::' or a nested-name.
4589 // Member pointers get special handling, since there's no place for the
4590 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004591 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004592 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4593 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004594 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4595 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004596 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004597 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004598
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004599 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004600 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004601 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004602 if (D.mayHaveIdentifier())
4603 D.getCXXScopeSpec() = SS;
4604 else
4605 AnnotateScopeToken(SS, true);
4606
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004607 if (DirectDeclParser)
4608 (this->*DirectDeclParser)(D);
4609 return;
4610 }
4611
4612 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004613 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004614 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004615 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004616 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004617
4618 // Recurse to parse whatever is left.
4619 ParseDeclaratorInternal(D, DirectDeclParser);
4620
4621 // Sema will have to catch (syntactically invalid) pointers into global
4622 // scope. It has to catch pointers into namespace scope anyway.
4623 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004624 Loc),
4625 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004626 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004627 return;
4628 }
4629 }
4630
4631 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004632 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004633 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004634 if (DirectDeclParser)
4635 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004636 return;
4637 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004638
Sebastian Redled0f3b02009-03-15 22:02:01 +00004639 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4640 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004641 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004642 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004643
Chris Lattner9eac9312009-03-27 04:18:06 +00004644 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004645 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004646 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004647
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004648 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004649 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004650 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004651
Bill Wendling3708c182007-05-27 10:15:43 +00004652 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004653 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004654 if (Kind == tok::star)
4655 // Remember that we parsed a pointer type, and remember the type-quals.
4656 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004657 DS.getConstSpecLoc(),
4658 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004659 DS.getRestrictSpecLoc()),
4660 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004661 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004662 else
4663 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004664 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004665 Loc),
4666 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004667 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004668 } else {
4669 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004670 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004671
Sebastian Redl3b27be62009-03-23 00:00:23 +00004672 // Complain about rvalue references in C++03, but then go on and build
4673 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004674 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004675 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004676 diag::warn_cxx98_compat_rvalue_reference :
4677 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004678
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004679 // GNU-style and C++11 attributes are allowed here, as is restrict.
4680 ParseTypeQualifierListOpt(DS);
4681 D.ExtendWithDeclSpec(DS);
4682
Bill Wendling93efb222007-06-02 23:28:54 +00004683 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4684 // cv-qualifiers are introduced through the use of a typedef or of a
4685 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004686 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4687 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4688 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004689 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004690 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4691 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004692 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004693 // 'restrict' is permitted as an extension.
4694 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4695 Diag(DS.getAtomicSpecLoc(),
4696 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004697 }
Bill Wendling3708c182007-05-27 10:15:43 +00004698
4699 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004700 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004701
Douglas Gregor66583c52008-11-03 15:51:28 +00004702 if (D.getNumTypeObjects() > 0) {
4703 // C++ [dcl.ref]p4: There shall be no references to references.
4704 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4705 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004706 if (const IdentifierInfo *II = D.getIdentifier())
4707 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4708 << II;
4709 else
4710 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4711 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004712
Sebastian Redlbd150f42008-11-21 19:14:01 +00004713 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004714 // can go ahead and build the (technically ill-formed)
4715 // declarator: reference collapsing will take care of it.
4716 }
4717 }
4718
Richard Smith8e1ac332013-03-28 01:55:44 +00004719 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004720 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004721 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004722 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004723 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004724 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004725}
4726
Richard Smith0efa75c2012-03-29 01:16:42 +00004727static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4728 SourceLocation EllipsisLoc) {
4729 if (EllipsisLoc.isValid()) {
4730 FixItHint Insertion;
4731 if (!D.getEllipsisLoc().isValid()) {
4732 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4733 D.setEllipsisLoc(EllipsisLoc);
4734 }
4735 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4736 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4737 }
4738}
4739
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004740/// ParseDirectDeclarator
4741/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004742/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004743/// '(' declarator ')'
4744/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004745/// [C90] direct-declarator '[' constant-expression[opt] ']'
4746/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4747/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4748/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4749/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004750/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4751/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004752/// direct-declarator '(' parameter-type-list ')'
4753/// direct-declarator '(' identifier-list[opt] ')'
4754/// [GNU] direct-declarator '(' parameter-forward-declarations
4755/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004756/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4757/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004758/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4759/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4760/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004761/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004762/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004763///
4764/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004765/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004766/// '::'[opt] nested-name-specifier[opt] type-name
4767///
4768/// id-expression: [C++ 5.1]
4769/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004770/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004771///
4772/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004773/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004774/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004775/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004776/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004777/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004778///
Richard Smith1453e312012-03-27 01:42:32 +00004779/// Note, any additional constructs added here may need corresponding changes
4780/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004781void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004782 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004783
David Blaikiebbafb8a2012-03-11 07:00:24 +00004784 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004785 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004786 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004787 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4788 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004789 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004790 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004791 }
4792
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004793 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004794 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004795 // Change the declaration context for name lookup, until this function
4796 // is exited (and the declarator has been parsed).
4797 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004798 }
4799
Douglas Gregor27b4c162010-12-23 22:44:42 +00004800 // C++0x [dcl.fct]p14:
4801 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004802 // of a parameter-declaration-clause without a preceding comma. In
4803 // this case, the ellipsis is parsed as part of the
4804 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004805 // parameter pack that has not been expanded; otherwise, it is parsed
4806 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004807 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004808 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004809 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004810 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004811 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004812 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004813 !Actions.containsUnexpandedParameterPacks(D))) {
4814 SourceLocation EllipsisLoc = ConsumeToken();
4815 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4816 // The ellipsis was put in the wrong place. Recover, and explain to
4817 // the user what they should have done.
4818 ParseDeclarator(D);
4819 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4820 return;
4821 } else
4822 D.setEllipsisLoc(EllipsisLoc);
4823
4824 // The ellipsis can't be followed by a parenthesized declarator. We
4825 // check for that in ParseParenDeclarator, after we have disambiguated
4826 // the l_paren token.
4827 }
4828
Douglas Gregor7861a802009-11-03 01:35:08 +00004829 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4830 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4831 // We found something that indicates the start of an unqualified-id.
4832 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004833 bool AllowConstructorName;
4834 if (D.getDeclSpec().hasTypeSpecifier())
4835 AllowConstructorName = false;
4836 else if (D.getCXXScopeSpec().isSet())
4837 AllowConstructorName =
4838 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004839 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004840 else
4841 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4842
Abramo Bagnara7945c982012-01-27 09:46:47 +00004843 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004844 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4845 /*EnteringContext=*/true,
4846 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004847 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004848 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004849 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004850 D.getName()) ||
4851 // Once we're past the identifier, if the scope was bad, mark the
4852 // whole declarator bad.
4853 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004854 D.SetIdentifier(0, Tok.getLocation());
4855 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004856 } else {
4857 // Parsed the unqualified-id; update range information and move along.
4858 if (D.getSourceRange().getBegin().isInvalid())
4859 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4860 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004861 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004862 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004863 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004864 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004865 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004866 "There's a C++-specific check for tok::identifier above");
4867 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4868 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4869 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004870 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004871 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004872 // A virt-specifier isn't treated as an identifier if it appears after a
4873 // trailing-return-type.
4874 if (D.getContext() != Declarator::TrailingReturnContext ||
4875 !isCXX11VirtSpecifier(Tok)) {
4876 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4877 << FixItHint::CreateRemoval(Tok.getLocation());
4878 D.SetIdentifier(0, Tok.getLocation());
4879 ConsumeToken();
4880 goto PastIdentifier;
4881 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004882 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004883
Douglas Gregor7861a802009-11-03 01:35:08 +00004884 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004885 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004886 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004887 // Example: 'char (*X)' or 'int (*XX)(void)'
4888 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004889
4890 // If the declarator was parenthesized, we entered the declarator
4891 // scope when parsing the parenthesized declarator, then exited
4892 // the scope already. Re-enter the scope, if we need to.
4893 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004894 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004895 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004896 if (!D.isInvalidType() &&
4897 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004898 // Change the declaration context for name lookup, until this function
4899 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004900 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004901 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004902 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004903 // This could be something simple like "int" (in which case the declarator
4904 // portion is empty), if an abstract-declarator is allowed.
4905 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004906
4907 // The grammar for abstract-pack-declarator does not allow grouping parens.
4908 // FIXME: Revisit this once core issue 1488 is resolved.
4909 if (D.hasEllipsis() && D.hasGroupingParens())
4910 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4911 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004912 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004913 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004914 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004915 if (D.getContext() == Declarator::MemberContext)
4916 Diag(Tok, diag::err_expected_member_name_or_semi)
4917 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004918 else if (getLangOpts().CPlusPlus) {
4919 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4920 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004921 else {
4922 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4923 if (Tok.isAtStartOfLine() && Loc.isValid())
4924 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4925 << getLangOpts().CPlusPlus;
4926 else
4927 Diag(Tok, diag::err_expected_unqualified_id)
4928 << getLangOpts().CPlusPlus;
4929 }
Richard Trieu9c672672013-01-26 02:31:38 +00004930 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004931 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004932 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004933 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004934 }
Mike Stump11289f42009-09-09 15:08:12 +00004935
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004936 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004937 assert(D.isPastIdentifier() &&
4938 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004939
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004940 // Don't parse attributes unless we have parsed an unparenthesized name.
4941 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004942 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004943
Chris Lattneracd58a32006-08-06 17:24:14 +00004944 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004945 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004946 // Enter function-declaration scope, limiting any declarators to the
4947 // function prototype scope, including parameter declarators.
4948 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004949 Scope::FunctionPrototypeScope|Scope::DeclScope|
4950 (D.isFunctionDeclaratorAFunctionDeclaration()
4951 ? Scope::FunctionDeclarationScope : 0));
4952
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004953 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4954 // In such a case, check if we actually have a function declarator; if it
4955 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004956 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004957 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4958 // The name of the declarator, if any, is tentatively declared within
4959 // a possible direct initializer.
4960 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4961 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4962 TentativelyDeclaredIdentifiers.pop_back();
4963 if (!IsFunctionDecl)
4964 break;
4965 }
John McCall084e83d2011-03-24 11:26:52 +00004966 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004967 BalancedDelimiterTracker T(*this, tok::l_paren);
4968 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004969 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004970 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004971 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004972 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004973 } else {
4974 break;
4975 }
4976 }
Chad Rosierc1183952012-06-26 22:30:43 +00004977}
Chris Lattneracd58a32006-08-06 17:24:14 +00004978
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004979/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4980/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004981/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004982/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4983///
4984/// direct-declarator:
4985/// '(' declarator ')'
4986/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004987/// direct-declarator '(' parameter-type-list ')'
4988/// direct-declarator '(' identifier-list[opt] ')'
4989/// [GNU] direct-declarator '(' parameter-forward-declarations
4990/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004991///
4992void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004993 BalancedDelimiterTracker T(*this, tok::l_paren);
4994 T.consumeOpen();
4995
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004996 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004997
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004998 // Eat any attributes before we look at whether this is a grouping or function
4999 // declarator paren. If this is a grouping paren, the attribute applies to
5000 // the type being built up, for example:
5001 // int (__attribute__(()) *x)(long y)
5002 // If this ends up not being a grouping paren, the attribute applies to the
5003 // first argument, for example:
5004 // int (__attribute__(()) int x)
5005 // In either case, we need to eat any attributes to be able to determine what
5006 // sort of paren this is.
5007 //
John McCall084e83d2011-03-24 11:26:52 +00005008 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005009 bool RequiresArg = false;
5010 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005011 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005012
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005013 // We require that the argument list (if this is a non-grouping paren) be
5014 // present even if the attribute list was empty.
5015 RequiresArg = true;
5016 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005017
Steve Naroff44ac7772008-12-25 14:16:32 +00005018 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005019 ParseMicrosoftTypeAttributes(attrs);
5020
Dawn Perchik335e16b2010-09-03 01:29:35 +00005021 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005022 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005023 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005024
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005025 // If we haven't past the identifier yet (or where the identifier would be
5026 // stored, if this is an abstract declarator), then this is probably just
5027 // grouping parens. However, if this could be an abstract-declarator, then
5028 // this could also be the start of function arguments (consider 'void()').
5029 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005030
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005031 if (!D.mayOmitIdentifier()) {
5032 // If this can't be an abstract-declarator, this *must* be a grouping
5033 // paren, because we haven't seen the identifier yet.
5034 isGrouping = true;
5035 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005036 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5037 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005038 isDeclarationSpecifier() || // 'int(int)' is a function.
5039 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005040 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5041 // considered to be a type, not a K&R identifier-list.
5042 isGrouping = false;
5043 } else {
5044 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5045 isGrouping = true;
5046 }
Mike Stump11289f42009-09-09 15:08:12 +00005047
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005048 // If this is a grouping paren, handle:
5049 // direct-declarator: '(' declarator ')'
5050 // direct-declarator: '(' attributes declarator ')'
5051 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005052 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5053 D.setEllipsisLoc(SourceLocation());
5054
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005055 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005056 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005057 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005058 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005059 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005060 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005061 T.getCloseLocation()),
5062 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005063
5064 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005065
5066 // An ellipsis cannot be placed outside parentheses.
5067 if (EllipsisLoc.isValid())
5068 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5069
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005070 return;
5071 }
Mike Stump11289f42009-09-09 15:08:12 +00005072
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005073 // Okay, if this wasn't a grouping paren, it must be the start of a function
5074 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005075 // identifier (and remember where it would have been), then call into
5076 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005077 D.SetIdentifier(0, Tok.getLocation());
5078
David Blaikie15a430a2011-12-04 05:04:18 +00005079 // Enter function-declaration scope, limiting any declarators to the
5080 // function prototype scope, including parameter declarators.
5081 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005082 Scope::FunctionPrototypeScope | Scope::DeclScope |
5083 (D.isFunctionDeclaratorAFunctionDeclaration()
5084 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005085 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005086 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005087}
5088
5089/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5090/// declarator D up to a paren, which indicates that we are parsing function
5091/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005092///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005093/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5094/// immediately after the open paren - they should be considered to be the
5095/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005096///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005097/// If RequiresArg is true, then the first argument of the function is required
5098/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005099///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005100/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5101/// (C++11) ref-qualifier[opt], exception-specification[opt],
5102/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5103///
5104/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005105/// dynamic-exception-specification
5106/// noexcept-specification
5107///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005108void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005109 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005110 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005111 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005112 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005113 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005114 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005115 // lparen is already consumed!
5116 assert(D.isPastIdentifier() && "Should not call before identifier!");
5117
5118 // This should be true when the function has typed arguments.
5119 // Otherwise, it is treated as a K&R-style function.
5120 bool HasProto = false;
5121 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005122 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005123 // Remember where we see an ellipsis, if any.
5124 SourceLocation EllipsisLoc;
5125
5126 DeclSpec DS(AttrFactory);
5127 bool RefQualifierIsLValueRef = true;
5128 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005129 SourceLocation ConstQualifierLoc;
5130 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005131 ExceptionSpecificationType ESpecType = EST_None;
5132 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005133 SmallVector<ParsedType, 2> DynamicExceptions;
5134 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005135 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005136 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005137 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005138
James Molloy6f8780b2012-02-29 10:24:19 +00005139 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005140 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5141 EndLoc is the end location for the function declarator.
5142 They differ for trailing return types. */
5143 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005144 SourceLocation LParenLoc, RParenLoc;
5145 LParenLoc = Tracker.getOpenLocation();
5146 StartLoc = LParenLoc;
5147
Douglas Gregor9e66af42011-07-05 16:44:18 +00005148 if (isFunctionDeclaratorIdentifierList()) {
5149 if (RequiresArg)
5150 Diag(Tok, diag::err_argument_required_after_attribute);
5151
5152 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5153
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005154 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005155 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005156 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005157 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005158 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005159 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005160 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5161 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005162 else if (RequiresArg)
5163 Diag(Tok, diag::err_argument_required_after_attribute);
5164
David Blaikiebbafb8a2012-03-11 07:00:24 +00005165 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005166
5167 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005168 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005169 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005170 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005171 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005172
David Blaikiebbafb8a2012-03-11 07:00:24 +00005173 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005174 // FIXME: Accept these components in any order, and produce fixits to
5175 // correct the order if the user gets it wrong. Ideally we should deal
5176 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005177
5178 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005179 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5180 /*CXX11AttributesAllowed*/ false,
5181 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005182 if (!DS.getSourceRange().getEnd().isInvalid()) {
5183 EndLoc = DS.getSourceRange().getEnd();
5184 ConstQualifierLoc = DS.getConstSpecLoc();
5185 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5186 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005187
5188 // Parse ref-qualifier[opt].
5189 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005190 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005191 diag::warn_cxx98_compat_ref_qualifier :
5192 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005193
Douglas Gregor9e66af42011-07-05 16:44:18 +00005194 RefQualifierIsLValueRef = Tok.is(tok::amp);
5195 RefQualifierLoc = ConsumeToken();
5196 EndLoc = RefQualifierLoc;
5197 }
5198
Douglas Gregor3024f072012-04-16 07:05:22 +00005199 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005200 // If a declaration declares a member function or member function
5201 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005202 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005203 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005204 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005205 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005206 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005207 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005208 (D.getContext() == Declarator::MemberContext
5209 ? !D.getDeclSpec().isFriendSpecified()
5210 : D.getContext() == Declarator::FileContext &&
5211 D.getCXXScopeSpec().isValid() &&
5212 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005213 Sema::CXXThisScopeRAII ThisScope(Actions,
5214 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005215 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005216 (D.getDeclSpec().isConstexprSpecified() &&
5217 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005218 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005219 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005220
Douglas Gregor9e66af42011-07-05 16:44:18 +00005221 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005222 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005223 DynamicExceptions,
5224 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005225 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005226 if (ESpecType != EST_None)
5227 EndLoc = ESpecRange.getEnd();
5228
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005229 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5230 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005231 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005232
Douglas Gregor9e66af42011-07-05 16:44:18 +00005233 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005234 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005235 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005236 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005237 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5238 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005239 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005240 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005241 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005242 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005243 }
5244 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005245 }
5246
5247 // Remember that we parsed a function type, and remember the attributes.
5248 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005249 IsAmbiguous,
5250 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005251 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005252 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005253 DS.getTypeQualifiers(),
5254 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005255 RefQualifierLoc, ConstQualifierLoc,
5256 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005257 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005258 ESpecType, ESpecRange.getBegin(),
5259 DynamicExceptions.data(),
5260 DynamicExceptionRanges.data(),
5261 DynamicExceptions.size(),
5262 NoexceptExpr.isUsable() ?
5263 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005264 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005265 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005266 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005267
5268 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005269}
5270
5271/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5272/// identifier list form for a K&R-style function: void foo(a,b,c)
5273///
5274/// Note that identifier-lists are only allowed for normal declarators, not for
5275/// abstract-declarators.
5276bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005277 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005278 && Tok.is(tok::identifier)
5279 && !TryAltiVecVectorToken()
5280 // K&R identifier lists can't have typedefs as identifiers, per C99
5281 // 6.7.5.3p11.
5282 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5283 // Identifier lists follow a really simple grammar: the identifiers can
5284 // be followed *only* by a ", identifier" or ")". However, K&R
5285 // identifier lists are really rare in the brave new modern world, and
5286 // it is very common for someone to typo a type in a non-K&R style
5287 // list. If we are presented with something like: "void foo(intptr x,
5288 // float y)", we don't want to start parsing the function declarator as
5289 // though it is a K&R style declarator just because intptr is an
5290 // invalid type.
5291 //
5292 // To handle this, we check to see if the token after the first
5293 // identifier is a "," or ")". Only then do we parse it as an
5294 // identifier list.
5295 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5296}
5297
5298/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5299/// we found a K&R-style identifier list instead of a typed parameter list.
5300///
5301/// After returning, ParamInfo will hold the parsed parameters.
5302///
5303/// identifier-list: [C99 6.7.5]
5304/// identifier
5305/// identifier-list ',' identifier
5306///
5307void Parser::ParseFunctionDeclaratorIdentifierList(
5308 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005309 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005310 // If there was no identifier specified for the declarator, either we are in
5311 // an abstract-declarator, or we are in a parameter declarator which was found
5312 // to be abstract. In abstract-declarators, identifier lists are not valid:
5313 // diagnose this.
5314 if (!D.getIdentifier())
5315 Diag(Tok, diag::ext_ident_list_in_param);
5316
5317 // Maintain an efficient lookup of params we have seen so far.
5318 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5319
5320 while (1) {
5321 // If this isn't an identifier, report the error and skip until ')'.
5322 if (Tok.isNot(tok::identifier)) {
5323 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005324 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005325 // Forget we parsed anything.
5326 ParamInfo.clear();
5327 return;
5328 }
5329
5330 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5331
5332 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5333 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5334 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5335
5336 // Verify that the argument identifier has not already been mentioned.
5337 if (!ParamsSoFar.insert(ParmII)) {
5338 Diag(Tok, diag::err_param_redefinition) << ParmII;
5339 } else {
5340 // Remember this identifier in ParamInfo.
5341 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5342 Tok.getLocation(),
5343 0));
5344 }
5345
5346 // Eat the identifier.
5347 ConsumeToken();
5348
5349 // The list continues if we see a comma.
5350 if (Tok.isNot(tok::comma))
5351 break;
5352 ConsumeToken();
5353 }
5354}
5355
5356/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5357/// after the opening parenthesis. This function will not parse a K&R-style
5358/// identifier list.
5359///
Richard Smith2620cd92012-04-11 04:01:28 +00005360/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5361/// caller parsed those arguments immediately after the open paren - they should
5362/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005363///
5364/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5365/// be the location of the ellipsis, if any was parsed.
5366///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005367/// parameter-type-list: [C99 6.7.5]
5368/// parameter-list
5369/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005370/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005371///
5372/// parameter-list: [C99 6.7.5]
5373/// parameter-declaration
5374/// parameter-list ',' parameter-declaration
5375///
5376/// parameter-declaration: [C99 6.7.5]
5377/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005378/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005379/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005380/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005381/// declaration-specifiers abstract-declarator[opt]
5382/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005383/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005384/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005385/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005386///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005387void Parser::ParseParameterDeclarationClause(
5388 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005389 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005390 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005391 SourceLocation &EllipsisLoc) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005392 while (1) {
5393 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005394 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5395 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005396 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005397 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005398 }
Mike Stump11289f42009-09-09 15:08:12 +00005399
Chris Lattner371ed4e2008-04-06 06:57:35 +00005400 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005401 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005402 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005403
Richard Smith2620cd92012-04-11 04:01:28 +00005404 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005405 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005406
John McCall53fa7142010-12-24 02:08:15 +00005407 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005408 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005409
5410 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005411
5412 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005413 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005414 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005415 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5416 // too much hassle.
5417 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005418
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005419 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005420
Faisal Vali2b391ab2013-09-26 19:54:12 +00005421
5422 // Parse the declarator. This is "PrototypeContext" or
5423 // "LambdaExprParameterContext", because we must accept either
5424 // 'declarator' or 'abstract-declarator' here.
5425 Declarator ParmDeclarator(DS,
5426 D.getContext() == Declarator::LambdaExprContext ?
5427 Declarator::LambdaExprParameterContext :
5428 Declarator::PrototypeContext);
5429 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005430
5431 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005432 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005433
Chris Lattner371ed4e2008-04-06 06:57:35 +00005434 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005435 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005436
Douglas Gregor4d87df52008-12-16 21:30:33 +00005437 // DefArgToks is used when the parsing of default arguments needs
5438 // to be delayed.
5439 CachedTokens *DefArgToks = 0;
5440
Chris Lattner371ed4e2008-04-06 06:57:35 +00005441 // If no parameter was specified, verify that *something* was specified,
5442 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005443 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5444 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005445 // Completely missing, emit error.
5446 Diag(DSStart, diag::err_missing_param);
5447 } else {
5448 // Otherwise, we have something. Add it and let semantic analysis try
5449 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005450
Chris Lattner371ed4e2008-04-06 06:57:35 +00005451 // Inform the actions module about the parameter declarator, so it gets
5452 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005453 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5454 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005455 // Parse the default argument, if any. We parse the default
5456 // arguments in all dialects; the semantic analysis in
5457 // ActOnParamDefaultArgument will reject the default argument in
5458 // C.
5459 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005460 SourceLocation EqualLoc = Tok.getLocation();
5461
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005462 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005463 if (D.getContext() == Declarator::MemberContext) {
5464 // If we're inside a class definition, cache the tokens
5465 // corresponding to the default argument. We'll actually parse
5466 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005467 // FIXME: Can we use a smart pointer for Toks?
5468 DefArgToks = new CachedTokens;
5469
Richard Smith1fff95c2013-09-12 23:28:08 +00005470 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005471 delete DefArgToks;
5472 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005473 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005474 } else {
5475 // Mark the end of the default argument so that we know when to
5476 // stop when we parse it later on.
5477 Token DefArgEnd;
5478 DefArgEnd.startToken();
5479 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5480 DefArgEnd.setLocation(Tok.getLocation());
5481 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005482 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005483 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005484 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005485 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005486 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005487 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005488
Chad Rosierc1183952012-06-26 22:30:43 +00005489 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005490 // used.
5491 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005492 Sema::PotentiallyEvaluatedIfUsed,
5493 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005494
Sebastian Redldb63af22012-03-14 15:54:00 +00005495 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005496 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005497 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005498 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005499 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005500 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005501 if (DefArgResult.isInvalid()) {
5502 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005503 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005504 } else {
5505 // Inform the actions module about the default argument
5506 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005507 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005508 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005509 }
5510 }
Mike Stump11289f42009-09-09 15:08:12 +00005511
5512 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005513 ParmDeclarator.getIdentifierLoc(),
5514 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005515 }
5516
5517 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005518 if (Tok.isNot(tok::comma)) {
5519 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005520 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005521
David Blaikiebbafb8a2012-03-11 07:00:24 +00005522 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005523 // We have ellipsis without a preceding ',', which is ill-formed
5524 // in C. Complain and provide the fix.
5525 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005526 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005527 }
5528 }
Chad Rosierc1183952012-06-26 22:30:43 +00005529
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005530 break;
5531 }
Mike Stump11289f42009-09-09 15:08:12 +00005532
Chris Lattner371ed4e2008-04-06 06:57:35 +00005533 // Consume the comma.
5534 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005535 }
Mike Stump11289f42009-09-09 15:08:12 +00005536
Chris Lattner6c940e62008-04-06 06:34:08 +00005537}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005538
Chris Lattnere8074e62006-08-06 18:30:15 +00005539/// [C90] direct-declarator '[' constant-expression[opt] ']'
5540/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5541/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5542/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5543/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005544/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5545/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005546void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005547 if (CheckProhibitedCXX11Attribute())
5548 return;
5549
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005550 BalancedDelimiterTracker T(*this, tok::l_square);
5551 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005552
Chris Lattner84a11622008-12-18 07:27:21 +00005553 // C array syntax has many features, but by-far the most common is [] and [4].
5554 // This code does a fast path to handle some of the most obvious cases.
5555 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005556 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005557 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005558 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005559
Chris Lattner84a11622008-12-18 07:27:21 +00005560 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005561 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005562 T.getOpenLocation(),
5563 T.getCloseLocation()),
5564 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005565 return;
5566 } else if (Tok.getKind() == tok::numeric_constant &&
5567 GetLookAheadToken(1).is(tok::r_square)) {
5568 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005569 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005570 ConsumeToken();
5571
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005572 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005573 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005574 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005575
Chris Lattner84a11622008-12-18 07:27:21 +00005576 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005577 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005578 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005579 T.getOpenLocation(),
5580 T.getCloseLocation()),
5581 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005582 return;
5583 }
Mike Stump11289f42009-09-09 15:08:12 +00005584
Chris Lattnere8074e62006-08-06 18:30:15 +00005585 // If valid, this location is the position where we read the 'static' keyword.
5586 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005587 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005588 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005589
Chris Lattnere8074e62006-08-06 18:30:15 +00005590 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005591 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005592 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005593 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005594
Chris Lattnere8074e62006-08-06 18:30:15 +00005595 // If we haven't already read 'static', check to see if there is one after the
5596 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005597 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005598 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005599
Chris Lattnere8074e62006-08-06 18:30:15 +00005600 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005601 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005602 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005603
Chris Lattner521ff2b2008-04-06 05:26:30 +00005604 // Handle the case where we have '[*]' as the array size. However, a leading
5605 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005606 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005607 // infrequent, use of lookahead is not costly here.
5608 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005609 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005610
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005611 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005612 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005613 StaticLoc = SourceLocation(); // Drop the static.
5614 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005615 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005616 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005617 // Note, in C89, this production uses the constant-expr production instead
5618 // of assignment-expr. The only difference is that assignment-expr allows
5619 // things like '=' and '*='. Sema rejects these in C89 mode because they
5620 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005621
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005622 // Parse the constant-expression or assignment-expression now (depending
5623 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005624 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005625 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005626 } else {
5627 EnterExpressionEvaluationContext Unevaluated(Actions,
5628 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005629 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005630 }
Chris Lattner62591722006-08-12 18:40:58 +00005631 }
Mike Stump11289f42009-09-09 15:08:12 +00005632
Chris Lattner62591722006-08-12 18:40:58 +00005633 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005634 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005635 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005636 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005637 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005638 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005639 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005640
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005641 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005642
John McCall084e83d2011-03-24 11:26:52 +00005643 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005644 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005645
Chris Lattner84a11622008-12-18 07:27:21 +00005646 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005647 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005648 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005649 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005650 T.getOpenLocation(),
5651 T.getCloseLocation()),
5652 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005653}
5654
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005655/// [GNU] typeof-specifier:
5656/// typeof ( expressions )
5657/// typeof ( type-name )
5658/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005659///
5660void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005661 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005662 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005663 SourceLocation StartLoc = ConsumeToken();
5664
John McCalle8595032010-01-13 20:03:27 +00005665 const bool hasParens = Tok.is(tok::l_paren);
5666
Eli Friedman15681d62012-09-26 04:34:21 +00005667 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5668 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005669
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005670 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005671 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005672 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005673 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5674 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005675 if (hasParens)
5676 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005677
5678 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005679 // FIXME: Not accurate, the range gets one token more than it should.
5680 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005681 else
5682 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005683
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005684 if (isCastExpr) {
5685 if (!CastTy) {
5686 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005687 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005688 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005689
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005690 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005691 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005692 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5693 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005694 DiagID, CastTy))
5695 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005696 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005697 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005698
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005699 // If we get here, the operand to the typeof was an expresion.
5700 if (Operand.isInvalid()) {
5701 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005702 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005703 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005704
Eli Friedmane0afc982012-01-21 01:01:51 +00005705 // We might need to transform the operand if it is potentially evaluated.
5706 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5707 if (Operand.isInvalid()) {
5708 DS.SetTypeSpecError();
5709 return;
5710 }
5711
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005712 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005713 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005714 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5715 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005716 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005717 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005718}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005719
Benjamin Kramere56f3932011-12-23 17:00:35 +00005720/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005721/// _Atomic ( type-name )
5722///
5723void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005724 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5725 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005726
5727 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005728 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005729 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005730 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005731
5732 TypeResult Result = ParseTypeName();
5733 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005734 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005735 return;
5736 }
5737
5738 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005739 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005740
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005741 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005742 return;
5743
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005744 DS.setTypeofParensRange(T.getRange());
5745 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005746
5747 const char *PrevSpec = 0;
5748 unsigned DiagID;
5749 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5750 DiagID, Result.release()))
5751 Diag(StartLoc, DiagID) << PrevSpec;
5752}
5753
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005754
5755/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5756/// from TryAltiVecVectorToken.
5757bool Parser::TryAltiVecVectorTokenOutOfLine() {
5758 Token Next = NextToken();
5759 switch (Next.getKind()) {
5760 default: return false;
5761 case tok::kw_short:
5762 case tok::kw_long:
5763 case tok::kw_signed:
5764 case tok::kw_unsigned:
5765 case tok::kw_void:
5766 case tok::kw_char:
5767 case tok::kw_int:
5768 case tok::kw_float:
5769 case tok::kw_double:
5770 case tok::kw_bool:
5771 case tok::kw___pixel:
5772 Tok.setKind(tok::kw___vector);
5773 return true;
5774 case tok::identifier:
5775 if (Next.getIdentifierInfo() == Ident_pixel) {
5776 Tok.setKind(tok::kw___vector);
5777 return true;
5778 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005779 if (Next.getIdentifierInfo() == Ident_bool) {
5780 Tok.setKind(tok::kw___vector);
5781 return true;
5782 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005783 return false;
5784 }
5785}
5786
5787bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5788 const char *&PrevSpec, unsigned &DiagID,
5789 bool &isInvalid) {
5790 if (Tok.getIdentifierInfo() == Ident_vector) {
5791 Token Next = NextToken();
5792 switch (Next.getKind()) {
5793 case tok::kw_short:
5794 case tok::kw_long:
5795 case tok::kw_signed:
5796 case tok::kw_unsigned:
5797 case tok::kw_void:
5798 case tok::kw_char:
5799 case tok::kw_int:
5800 case tok::kw_float:
5801 case tok::kw_double:
5802 case tok::kw_bool:
5803 case tok::kw___pixel:
5804 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5805 return true;
5806 case tok::identifier:
5807 if (Next.getIdentifierInfo() == Ident_pixel) {
5808 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5809 return true;
5810 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005811 if (Next.getIdentifierInfo() == Ident_bool) {
5812 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5813 return true;
5814 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005815 break;
5816 default:
5817 break;
5818 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005819 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005820 DS.isTypeAltiVecVector()) {
5821 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5822 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005823 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5824 DS.isTypeAltiVecVector()) {
5825 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5826 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005827 }
5828 return false;
5829}