blob: b93979127a53d448f9c84382a0ed6feb29f3a028 [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:
1564 return;
1565
1566 default:
1567 break;
1568 }
1569
1570 ConsumeAnyToken();
1571 }
1572}
1573
John McCalld5a36322009-11-03 19:26:08 +00001574/// ParseDeclGroup - Having concluded that this is either a function
1575/// definition or a group of object declarations, actually parse the
1576/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001577Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1578 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001579 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001580 SourceLocation *DeclEnd,
1581 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001582 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001583 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001584 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001585
John McCalld5a36322009-11-03 19:26:08 +00001586 // Bail out if the first declarator didn't seem well-formed.
1587 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001588 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001589 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001590 }
Mike Stump11289f42009-09-09 15:08:12 +00001591
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001592 // Save late-parsed attributes for now; they need to be parsed in the
1593 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001594 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1595 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001596 if (D.isFunctionDeclarator())
1597 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1598
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001599 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001600 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001601 // Look at the next token to make sure that this isn't a function
1602 // declaration. We have to check this because __attribute__ might be the
1603 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001604 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001605
Douglas Gregor012efe22013-04-16 16:01:32 +00001606 if (AllowFunctionDefinitions) {
1607 if (isStartOfFunctionDefinition(D)) {
1608 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1609 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001610
Douglas Gregor012efe22013-04-16 16:01:32 +00001611 // Recover by treating the 'typedef' as spurious.
1612 DS.ClearStorageClassSpecs();
1613 }
1614
1615 Decl *TheDecl =
1616 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1617 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001618 }
1619
Douglas Gregor012efe22013-04-16 16:01:32 +00001620 if (isDeclarationSpecifier()) {
1621 // If there is an invalid declaration specifier right after the function
1622 // prototype, then we must be in a missing semicolon case where this isn't
1623 // actually a body. Just fall through into the code that handles it as a
1624 // prototype, and let the top-level code handle the erroneous declspec
1625 // where it would otherwise expect a comma or semicolon.
1626 } else {
1627 Diag(Tok, diag::err_expected_fn_body);
1628 SkipUntil(tok::semi);
1629 return DeclGroupPtrTy();
1630 }
John McCalld5a36322009-11-03 19:26:08 +00001631 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001632 if (Tok.is(tok::l_brace)) {
1633 Diag(Tok, diag::err_function_definition_not_allowed);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001634 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor012efe22013-04-16 16:01:32 +00001635 }
John McCalld5a36322009-11-03 19:26:08 +00001636 }
1637 }
1638
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001639 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001640 return DeclGroupPtrTy();
1641
1642 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1643 // must parse and analyze the for-range-initializer before the declaration is
1644 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001645 //
1646 // Handle the Objective-C for-in loop variable similarly, although we
1647 // don't need to parse the container in advance.
1648 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1649 bool IsForRangeLoop = false;
1650 if (Tok.is(tok::colon)) {
1651 IsForRangeLoop = true;
1652 FRI->ColonLoc = ConsumeToken();
1653 if (Tok.is(tok::l_brace))
1654 FRI->RangeExpr = ParseBraceInitializer();
1655 else
1656 FRI->RangeExpr = ParseExpression();
1657 }
1658
Richard Smith02e85f32011-04-14 22:09:26 +00001659 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001660 if (IsForRangeLoop)
1661 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001662 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001663 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001664 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001665 }
1666
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001667 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001668 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001669 if (LateParsedAttrs.size() > 0)
1670 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001671 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001672 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001673 DeclsInGroup.push_back(FirstDecl);
1674
Richard Smith09f76ee2011-10-19 21:33:05 +00001675 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001676
John McCalld5a36322009-11-03 19:26:08 +00001677 // If we don't have a comma, it is either the end of the list (a ';') or an
1678 // error, bail out.
1679 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001680 SourceLocation CommaLoc = ConsumeToken();
1681
1682 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1683 // This comma was followed by a line-break and something which can't be
1684 // the start of a declarator. The comma was probably a typo for a
1685 // semicolon.
1686 Diag(CommaLoc, diag::err_expected_semi_declaration)
1687 << FixItHint::CreateReplacement(CommaLoc, ";");
1688 ExpectSemi = false;
1689 break;
1690 }
John McCalld5a36322009-11-03 19:26:08 +00001691
1692 // Parse the next declarator.
1693 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001694 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001695
1696 // Accept attributes in an init-declarator. In the first declarator in a
1697 // declaration, these would be part of the declspec. In subsequent
1698 // declarators, they become part of the declarator itself, so that they
1699 // don't apply to declarators after *this* one. Examples:
1700 // short __attribute__((common)) var; -> declspec
1701 // short var __attribute__((common)); -> declarator
1702 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001703 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001704
1705 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001706 if (!D.isInvalidType()) {
1707 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1708 D.complete(ThisDecl);
1709 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001710 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001711 }
John McCalld5a36322009-11-03 19:26:08 +00001712 }
1713
1714 if (DeclEnd)
1715 *DeclEnd = Tok.getLocation();
1716
Richard Smith09f76ee2011-10-19 21:33:05 +00001717 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001718 ExpectAndConsumeSemi(Context == Declarator::FileContext
1719 ? diag::err_invalid_token_after_toplevel_declarator
1720 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001721 // Okay, there was no semicolon and one was expected. If we see a
1722 // declaration specifier, just assume it was missing and continue parsing.
1723 // Otherwise things are very confused and we skip to recover.
1724 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001725 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner13901342010-07-11 22:42:07 +00001726 if (Tok.is(tok::semi))
1727 ConsumeToken();
1728 }
John McCalld5a36322009-11-03 19:26:08 +00001729 }
1730
Rafael Espindolaab417692013-07-09 12:05:01 +00001731 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001732}
1733
Richard Smith02e85f32011-04-14 22:09:26 +00001734/// Parse an optional simple-asm-expr and attributes, and attach them to a
1735/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001736bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001737 // If a simple-asm-expr is present, parse it.
1738 if (Tok.is(tok::kw_asm)) {
1739 SourceLocation Loc;
1740 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1741 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001742 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001743 return true;
1744 }
1745
1746 D.setAsmLabel(AsmLabel.release());
1747 D.SetRangeEnd(Loc);
1748 }
1749
1750 MaybeParseGNUAttributes(D);
1751 return false;
1752}
1753
Douglas Gregor23996282009-05-12 21:31:51 +00001754/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1755/// declarator'. This method parses the remainder of the declaration
1756/// (including any attributes or initializer, among other things) and
1757/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001758///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001759/// init-declarator: [C99 6.7]
1760/// declarator
1761/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001762/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1763/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001764/// [C++] declarator initializer[opt]
1765///
1766/// [C++] initializer:
1767/// [C++] '=' initializer-clause
1768/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001769/// [C++0x] '=' 'default' [TODO]
1770/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001771/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001772///
1773/// According to the standard grammar, =default and =delete are function
1774/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001775///
John McCall48871652010-08-21 09:40:31 +00001776Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001777 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001778 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001779 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001780
Richard Smith02e85f32011-04-14 22:09:26 +00001781 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1782}
Mike Stump11289f42009-09-09 15:08:12 +00001783
Richard Smith02e85f32011-04-14 22:09:26 +00001784Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1785 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001786 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001787 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001788 switch (TemplateInfo.Kind) {
1789 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001790 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001791 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001792
Douglas Gregor450f00842009-09-25 18:43:00 +00001793 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001794 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001795 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001796 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001797 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001798 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001799 // Re-direct this decl to refer to the templated decl so that we can
1800 // initialize it.
1801 ThisDecl = VT->getTemplatedDecl();
1802 break;
1803 }
1804 case ParsedTemplateInfo::ExplicitInstantiation: {
1805 if (Tok.is(tok::semi)) {
1806 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1807 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1808 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001809 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001810 return 0;
1811 }
1812 ThisDecl = ThisRes.get();
1813 } else {
1814 // FIXME: This check should be for a variable template instantiation only.
1815
1816 // Check that this is a valid instantiation
1817 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1818 // If the declarator-id is not a template-id, issue a diagnostic and
1819 // recover by ignoring the 'template' keyword.
1820 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1821 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1822 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1823 } else {
1824 SourceLocation LAngleLoc =
1825 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1826 Diag(D.getIdentifierLoc(),
1827 diag::err_explicit_instantiation_with_definition)
1828 << SourceRange(TemplateInfo.TemplateLoc)
1829 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1830
1831 // Recover as if it were an explicit specialization.
1832 TemplateParameterLists FakedParamLists;
1833 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1834 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1835 LAngleLoc));
1836
1837 ThisDecl =
1838 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1839 }
1840 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001841 break;
1842 }
1843 }
Mike Stump11289f42009-09-09 15:08:12 +00001844
Richard Smith74aeef52013-04-26 16:15:35 +00001845 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001846
Douglas Gregor23996282009-05-12 21:31:51 +00001847 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001848 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001849 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001850 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001851
Anders Carlsson991285e2010-09-24 21:25:25 +00001852 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001853 if (D.isFunctionDeclarator())
1854 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1855 << 1 /* delete */;
1856 else
1857 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001858 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001859 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001860 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1861 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001862 else
1863 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001864 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001865 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001866 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001867 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001868 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001869
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001870 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001871 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001872 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001873 cutOffParsing();
1874 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001875 }
Chad Rosierc1183952012-06-26 22:30:43 +00001876
John McCalldadc5752010-08-24 06:29:42 +00001877 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001878
David Blaikiebbafb8a2012-03-11 07:00:24 +00001879 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001880 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001881 ExitScope();
1882 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001883
Douglas Gregor23996282009-05-12 21:31:51 +00001884 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001885 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001886 Actions.ActOnInitializerError(ThisDecl);
1887 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001888 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1889 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001890 }
1891 } else if (Tok.is(tok::l_paren)) {
1892 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001893 BalancedDelimiterTracker T(*this, tok::l_paren);
1894 T.consumeOpen();
1895
Benjamin Kramerf0623432012-08-23 22:51:59 +00001896 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001897 CommaLocsTy CommaLocs;
1898
David Blaikiebbafb8a2012-03-11 07:00:24 +00001899 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001900 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001901 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001902 }
1903
Douglas Gregor23996282009-05-12 21:31:51 +00001904 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001905 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001906 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001907
David Blaikiebbafb8a2012-03-11 07:00:24 +00001908 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001909 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001910 ExitScope();
1911 }
Douglas Gregor23996282009-05-12 21:31:51 +00001912 } else {
1913 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001914 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001915
1916 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1917 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001918
David Blaikiebbafb8a2012-03-11 07:00:24 +00001919 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001920 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001921 ExitScope();
1922 }
1923
Sebastian Redla9351792012-02-11 23:51:47 +00001924 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1925 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001926 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001927 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1928 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001929 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001930 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001931 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001932 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001933 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1934
Sebastian Redl3da34892011-06-05 12:23:16 +00001935 if (D.getCXXScopeSpec().isSet()) {
1936 EnterScope(0);
1937 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1938 }
1939
1940 ExprResult Init(ParseBraceInitializer());
1941
1942 if (D.getCXXScopeSpec().isSet()) {
1943 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1944 ExitScope();
1945 }
1946
1947 if (Init.isInvalid()) {
1948 Actions.ActOnInitializerError(ThisDecl);
1949 } else
1950 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1951 /*DirectInit=*/true, TypeContainsAuto);
1952
Douglas Gregor23996282009-05-12 21:31:51 +00001953 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001954 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001955 }
1956
Richard Smithb2bc2e62011-02-21 20:05:19 +00001957 Actions.FinalizeDeclaration(ThisDecl);
1958
Douglas Gregor23996282009-05-12 21:31:51 +00001959 return ThisDecl;
1960}
1961
Chris Lattner1890ac82006-08-13 01:16:23 +00001962/// ParseSpecifierQualifierList
1963/// specifier-qualifier-list:
1964/// type-specifier specifier-qualifier-list[opt]
1965/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001966/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001967///
Richard Smithc5b05522012-03-12 07:56:15 +00001968void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1969 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001970 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1971 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001972 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001973 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001974
Chris Lattner1890ac82006-08-13 01:16:23 +00001975 // Validate declspec for type-name.
1976 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001977 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1978 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001979 Diag(Tok, diag::err_expected_type);
1980 DS.SetTypeSpecError();
1981 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1982 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001983 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001984 if (!DS.hasTypeSpecifier())
1985 DS.SetTypeSpecError();
1986 }
Mike Stump11289f42009-09-09 15:08:12 +00001987
Chris Lattner1b22eed2006-11-28 05:12:07 +00001988 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001989 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001990 if (DS.getStorageClassSpecLoc().isValid())
1991 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1992 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001993 Diag(DS.getThreadStorageClassSpecLoc(),
1994 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001995 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001996 }
Mike Stump11289f42009-09-09 15:08:12 +00001997
Chris Lattner1b22eed2006-11-28 05:12:07 +00001998 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001999 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002000 if (DS.isInlineSpecified())
2001 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2002 if (DS.isVirtualSpecified())
2003 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2004 if (DS.isExplicitSpecified())
2005 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002006 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002007 }
Richard Smithc5b05522012-03-12 07:56:15 +00002008
2009 // Issue diagnostic and remove constexpr specfier if present.
2010 if (DS.isConstexprSpecified()) {
2011 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2012 DS.ClearConstexprSpec();
2013 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002014}
Chris Lattner53361ac2006-08-10 05:19:57 +00002015
Chris Lattner6cc055a2009-04-12 20:42:31 +00002016/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2017/// specified token is valid after the identifier in a declarator which
2018/// immediately follows the declspec. For example, these things are valid:
2019///
2020/// int x [ 4]; // direct-declarator
2021/// int x ( int y); // direct-declarator
2022/// int(int x ) // direct-declarator
2023/// int x ; // simple-declaration
2024/// int x = 17; // init-declarator-list
2025/// int x , y; // init-declarator-list
2026/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002027/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002028/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002029///
2030/// This is not, because 'x' does not immediately follow the declspec (though
2031/// ')' happens to be valid anyway).
2032/// int (x)
2033///
2034static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2035 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2036 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002037 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002038}
2039
Chris Lattner20a0c612009-04-14 21:34:55 +00002040
2041/// ParseImplicitInt - This method is called when we have an non-typename
2042/// identifier in a declspec (which normally terminates the decl spec) when
2043/// the declspec has no type specifier. In this case, the declspec is either
2044/// malformed or is "implicit int" (in K&R and C89).
2045///
2046/// This method handles diagnosing this prettily and returns false if the
2047/// declspec is done being processed. If it recovers and thinks there may be
2048/// other pieces of declspec after it, it returns true.
2049///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002050bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002051 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002052 AccessSpecifier AS, DeclSpecContext DSC,
2053 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002054 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002055
Chris Lattner20a0c612009-04-14 21:34:55 +00002056 SourceLocation Loc = Tok.getLocation();
2057 // If we see an identifier that is not a type name, we normally would
2058 // parse it as the identifer being declared. However, when a typename
2059 // is typo'd or the definition is not included, this will incorrectly
2060 // parse the typename as the identifier name and fall over misparsing
2061 // later parts of the diagnostic.
2062 //
2063 // As such, we try to do some look-ahead in cases where this would
2064 // otherwise be an "implicit-int" case to see if this is invalid. For
2065 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2066 // an identifier with implicit int, we'd get a parse error because the
2067 // next token is obviously invalid for a type. Parse these as a case
2068 // with an invalid type specifier.
2069 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002070
Chris Lattner20a0c612009-04-14 21:34:55 +00002071 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002072 // error, do lookahead to try to do better recovery. This never applies
2073 // within a type specifier. Outside of C++, we allow this even if the
2074 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002075 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002076 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002077 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002078 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002079 // If this token is valid for implicit int, e.g. "static x = 4", then
2080 // we just avoid eating the identifier, so it will be parsed as the
2081 // identifier in the declarator.
2082 return false;
2083 }
Mike Stump11289f42009-09-09 15:08:12 +00002084
Richard Smitha952ebb2012-05-15 21:01:51 +00002085 if (getLangOpts().CPlusPlus &&
2086 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2087 // Don't require a type specifier if we have the 'auto' storage class
2088 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002089 if (SS)
2090 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002091 return false;
2092 }
2093
Chris Lattner20a0c612009-04-14 21:34:55 +00002094 // Otherwise, if we don't consume this token, we are going to emit an
2095 // error anyway. Try to recover from various common problems. Check
2096 // to see if this was a reference to a tag name without a tag specified.
2097 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002098 //
2099 // C++ doesn't need this, and isTagName doesn't take SS.
2100 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002101 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002102 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002103
Douglas Gregor0be31a22010-07-02 17:43:08 +00002104 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002105 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002106 case DeclSpec::TST_enum:
2107 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2108 case DeclSpec::TST_union:
2109 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2110 case DeclSpec::TST_struct:
2111 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002112 case DeclSpec::TST_interface:
2113 TagName="__interface"; FixitTagName = "__interface ";
2114 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002115 case DeclSpec::TST_class:
2116 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002117 }
Mike Stump11289f42009-09-09 15:08:12 +00002118
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002119 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002120 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2121 LookupResult R(Actions, TokenName, SourceLocation(),
2122 Sema::LookupOrdinaryName);
2123
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002124 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002125 << TokenName << TagName << getLangOpts().CPlusPlus
2126 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2127
2128 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2129 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2130 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002131 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002132 << TokenName << TagName;
2133 }
Mike Stump11289f42009-09-09 15:08:12 +00002134
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002135 // Parse this as a tag as if the missing tag were present.
2136 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002137 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002138 else
Richard Smithc5b05522012-03-12 07:56:15 +00002139 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002140 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002141 return true;
2142 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002143 }
Mike Stump11289f42009-09-09 15:08:12 +00002144
Richard Smithfe904f02012-05-15 21:29:55 +00002145 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002146 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002147 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2148 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002149 // Look ahead to the next token to try to figure out what this declaration
2150 // was supposed to be.
2151 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002152 case tok::l_paren: {
2153 // static x(4); // 'x' is not a type
2154 // x(int n); // 'x' is not a type
2155 // x (*p)[]; // 'x' is a type
2156 //
2157 // Since we're in an error case (or the rare 'implicit int in C++' MS
2158 // extension), we can afford to perform a tentative parse to determine
2159 // which case we're in.
2160 TentativeParsingAction PA(*this);
2161 ConsumeToken();
2162 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2163 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002164
2165 if (TPR != TPResult::False()) {
2166 // The identifier is followed by a parenthesized declarator.
2167 // It's supposed to be a type.
2168 break;
2169 }
2170
2171 // If we're in a context where we could be declaring a constructor,
2172 // check whether this is a constructor declaration with a bogus name.
2173 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2174 IdentifierInfo *II = Tok.getIdentifierInfo();
2175 if (Actions.isCurrentClassNameTypo(II, SS)) {
2176 Diag(Loc, diag::err_constructor_bad_name)
2177 << Tok.getIdentifierInfo() << II
2178 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2179 Tok.setIdentifierInfo(II);
2180 }
2181 }
2182 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002183 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002184 case tok::comma:
2185 case tok::equal:
2186 case tok::kw_asm:
2187 case tok::l_brace:
2188 case tok::l_square:
2189 case tok::semi:
2190 // This looks like a variable or function declaration. The type is
2191 // probably missing. We're done parsing decl-specifiers.
2192 if (SS)
2193 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2194 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002195
2196 default:
2197 // This is probably supposed to be a type. This includes cases like:
2198 // int f(itn);
2199 // struct S { unsinged : 4; };
2200 break;
2201 }
2202 }
2203
Chad Rosierc1183952012-06-26 22:30:43 +00002204 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002205 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002206 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002207 IdentifierInfo *II = Tok.getIdentifierInfo();
2208 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002209 // The action emitted a diagnostic, so we don't have to.
2210 if (T) {
2211 // The action has suggested that the type T could be used. Set that as
2212 // the type in the declaration specifiers, consume the would-be type
2213 // name token, and we're done.
2214 const char *PrevSpec;
2215 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002216 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002217 DS.SetRangeEnd(Tok.getLocation());
2218 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002219 // There may be other declaration specifiers after this.
2220 return true;
2221 } else if (II != Tok.getIdentifierInfo()) {
2222 // If no type was suggested, the correction is to a keyword
2223 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002224 // There may be other declaration specifiers after this.
2225 return true;
2226 }
Chad Rosierc1183952012-06-26 22:30:43 +00002227
Douglas Gregor15e56022009-10-13 23:27:22 +00002228 // Fall through; the action had no suggestion for us.
2229 } else {
2230 // The action did not emit a diagnostic, so emit one now.
2231 SourceRange R;
2232 if (SS) R = SS->getRange();
2233 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2234 }
Mike Stump11289f42009-09-09 15:08:12 +00002235
Douglas Gregor15e56022009-10-13 23:27:22 +00002236 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002237 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002238 DS.SetRangeEnd(Tok.getLocation());
2239 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002240
Chris Lattner20a0c612009-04-14 21:34:55 +00002241 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2242 // avoid rippling error messages on subsequent uses of the same type,
2243 // could be useful if #include was forgotten.
2244 return false;
2245}
2246
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002247/// \brief Determine the declaration specifier context from the declarator
2248/// context.
2249///
2250/// \param Context the declarator context, which is one of the
2251/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002252Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002253Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2254 if (Context == Declarator::MemberContext)
2255 return DSC_class;
2256 if (Context == Declarator::FileContext)
2257 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002258 if (Context == Declarator::TrailingReturnContext)
2259 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002260 return DSC_normal;
2261}
2262
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002263/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2264///
2265/// FIXME: Simply returns an alignof() expression if the argument is a
2266/// type. Ideally, the type should be propagated directly into Sema.
2267///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002268/// [C11] type-id
2269/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002270/// [C++0x] type-id ...[opt]
2271/// [C++0x] assignment-expression ...[opt]
2272ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2273 SourceLocation &EllipsisLoc) {
2274 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002275 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002276 SourceLocation TypeLoc = Tok.getLocation();
2277 ParsedType Ty = ParseTypeName().get();
2278 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002279 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2280 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002281 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002282 ER = ParseConstantExpression();
2283
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002284 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002285 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002286
2287 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002288}
2289
2290/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2291/// attribute to Attrs.
2292///
2293/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002294/// [C11] '_Alignas' '(' type-id ')'
2295/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002296/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2297/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002298void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002299 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002300 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2301 "Not an alignment-specifier!");
2302
Richard Smithd11c7a12013-01-29 01:48:07 +00002303 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2304 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002305
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002306 BalancedDelimiterTracker T(*this, tok::l_paren);
2307 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002308 return;
2309
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002310 SourceLocation EllipsisLoc;
2311 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002312 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002313 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002314 return;
2315 }
2316
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002317 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002318 if (EndLoc)
2319 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002320
Aaron Ballman00e99962013-08-31 01:11:41 +00002321 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002322 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002323 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2324 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002325}
2326
Richard Smith404dfb42013-11-19 22:47:36 +00002327/// Determine whether we're looking at something that might be a declarator
2328/// in a simple-declaration. If it can't possibly be a declarator, maybe
2329/// diagnose a missing semicolon after a prior tag definition in the decl
2330/// specifier.
2331///
2332/// \return \c true if an error occurred and this can't be any kind of
2333/// declaration.
2334bool
2335Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2336 DeclSpecContext DSContext,
2337 LateParsedAttrList *LateAttrs) {
2338 assert(DS.hasTagDefinition() && "shouldn't call this");
2339
2340 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002341
2342 if (getLangOpts().CPlusPlus &&
2343 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2344 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2345 TryAnnotateCXXScopeToken(EnteringContext)) {
2346 SkipMalformedDecl();
2347 return true;
2348 }
2349
Richard Smith698875a2013-11-20 23:40:57 +00002350 bool HasScope = Tok.is(tok::annot_cxxscope);
2351 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2352 Token AfterScope = HasScope ? NextToken() : Tok;
2353
Richard Smith404dfb42013-11-19 22:47:36 +00002354 // Determine whether the following tokens could possibly be a
2355 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002356 bool MightBeDeclarator = true;
2357 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2358 // A declarator-id can't start with 'typename'.
2359 MightBeDeclarator = false;
2360 } else if (AfterScope.is(tok::annot_template_id)) {
2361 // If we have a type expressed as a template-id, this cannot be a
2362 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2363 TemplateIdAnnotation *Annot =
2364 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2365 if (Annot->Kind == TNK_Type_template)
2366 MightBeDeclarator = false;
2367 } else if (AfterScope.is(tok::identifier)) {
2368 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2369
Richard Smith404dfb42013-11-19 22:47:36 +00002370 // These tokens cannot come after the declarator-id in a
2371 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002372 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2373 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2374 Next.is(tok::coloncolon)) {
2375 // Missing a semicolon.
2376 MightBeDeclarator = false;
2377 } else if (HasScope) {
2378 // If the declarator-id has a scope specifier, it must redeclare a
2379 // previously-declared entity. If that's a type (and this is not a
2380 // typedef), that's an error.
2381 CXXScopeSpec SS;
2382 Actions.RestoreNestedNameSpecifierAnnotation(
2383 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2384 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2385 Sema::NameClassification Classification = Actions.ClassifyName(
2386 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2387 /*IsAddressOfOperand*/false);
2388 switch (Classification.getKind()) {
2389 case Sema::NC_Error:
2390 SkipMalformedDecl();
2391 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002392
Richard Smith698875a2013-11-20 23:40:57 +00002393 case Sema::NC_Keyword:
2394 case Sema::NC_NestedNameSpecifier:
2395 llvm_unreachable("typo correction and nested name specifiers not "
2396 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002397
Richard Smith698875a2013-11-20 23:40:57 +00002398 case Sema::NC_Type:
2399 case Sema::NC_TypeTemplate:
2400 // Not a previously-declared non-type entity.
2401 MightBeDeclarator = false;
2402 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002403
Richard Smith698875a2013-11-20 23:40:57 +00002404 case Sema::NC_Unknown:
2405 case Sema::NC_Expression:
2406 case Sema::NC_VarTemplate:
2407 case Sema::NC_FunctionTemplate:
2408 // Might be a redeclaration of a prior entity.
2409 break;
2410 }
Richard Smith404dfb42013-11-19 22:47:36 +00002411 }
Richard Smith404dfb42013-11-19 22:47:36 +00002412 }
2413
Richard Smith698875a2013-11-20 23:40:57 +00002414 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002415 return false;
2416
2417 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
2418 diag::err_expected_semi_after_tagdecl)
2419 << DeclSpec::getSpecifierName(DS.getTypeSpecType());
2420
2421 // Try to recover from the typo, by dropping the tag definition and parsing
2422 // the problematic tokens as a type.
2423 //
2424 // FIXME: Split the DeclSpec into pieces for the standalone
2425 // declaration and pieces for the following declaration, instead
2426 // of assuming that all the other pieces attach to new declaration,
2427 // and call ParsedFreeStandingDeclSpec as appropriate.
2428 DS.ClearTypeSpecType();
2429 ParsedTemplateInfo NotATemplate;
2430 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2431 return false;
2432}
2433
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002434/// ParseDeclarationSpecifiers
2435/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002436/// storage-class-specifier declaration-specifiers[opt]
2437/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002438/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002439/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002440/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002441/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002442///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002443/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002444/// 'typedef'
2445/// 'extern'
2446/// 'static'
2447/// 'auto'
2448/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002449/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002450/// [C++11] 'thread_local'
2451/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002452/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002453/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002454/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002455/// [C++] 'virtual'
2456/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002457/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002458/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002459/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002460
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002461///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002462void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002463 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002464 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002465 DeclSpecContext DSContext,
2466 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002467 if (DS.getSourceRange().isInvalid()) {
2468 DS.SetRangeStart(Tok.getLocation());
2469 DS.SetRangeEnd(Tok.getLocation());
2470 }
Chad Rosierc1183952012-06-26 22:30:43 +00002471
Douglas Gregordf593fb2011-11-07 17:33:42 +00002472 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002473 bool AttrsLastTime = false;
2474 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002475 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002476 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002477 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002478 unsigned DiagID = 0;
2479
Chris Lattner4d8f8732006-11-28 05:05:08 +00002480 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002481
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002482 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002483 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002484 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002485 if (!AttrsLastTime)
2486 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002487 else {
2488 // Reject C++11 attributes that appertain to decl specifiers as
2489 // we don't support any C++11 attributes that appertain to decl
2490 // specifiers. This also conforms to what g++ 4.8 is doing.
2491 ProhibitCXX11Attributes(attrs);
2492
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002493 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002494 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002495
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002496 // If this is not a declaration specifier token, we're done reading decl
2497 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002498 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002499 return;
Mike Stump11289f42009-09-09 15:08:12 +00002500
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002501 case tok::l_square:
2502 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002503 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002504 goto DoneWithDeclSpec;
2505
2506 ProhibitAttributes(attrs);
2507 // FIXME: It would be good to recover by accepting the attributes,
2508 // but attempting to do that now would cause serious
2509 // madness in terms of diagnostics.
2510 attrs.clear();
2511 attrs.Range = SourceRange();
2512
2513 ParseCXX11Attributes(attrs);
2514 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002515 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002516
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002517 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002518 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002519 if (DS.hasTypeSpecifier()) {
2520 bool AllowNonIdentifiers
2521 = (getCurScope()->getFlags() & (Scope::ControlScope |
2522 Scope::BlockScope |
2523 Scope::TemplateParamScope |
2524 Scope::FunctionPrototypeScope |
2525 Scope::AtCatchScope)) == 0;
2526 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002527 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002528 (DSContext == DSC_class && DS.isFriendSpecified());
2529
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002530 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002531 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002532 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002533 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002534 }
2535
Douglas Gregor80039242011-02-15 20:33:25 +00002536 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2537 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2538 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002539 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002540 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002541 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002542 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002543 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002544 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002545
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002546 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002547 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002548 }
2549
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002550 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002551 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002552 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002553 if (!DS.hasTypeSpecifier())
2554 DS.SetTypeSpecError();
2555 goto DoneWithDeclSpec;
2556 }
John McCall8bc2a702010-03-01 18:20:46 +00002557 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2558 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002559 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002560
2561 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002562 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002563 goto DoneWithDeclSpec;
2564
John McCall9dab4e62009-12-12 11:40:51 +00002565 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002566 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2567 Tok.getAnnotationRange(),
2568 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002569
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002570 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002571 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002572 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002573 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002574 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002575 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002576
2577 // C++ [class.qual]p2:
2578 // In a lookup in which the constructor is an acceptable lookup
2579 // result and the nested-name-specifier nominates a class C:
2580 //
2581 // - if the name specified after the
2582 // nested-name-specifier, when looked up in C, is the
2583 // injected-class-name of C (Clause 9), or
2584 //
2585 // - if the name specified after the nested-name-specifier
2586 // is the same as the identifier or the
2587 // simple-template-id's template-name in the last
2588 // component of the nested-name-specifier,
2589 //
2590 // the name is instead considered to name the constructor of
2591 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002592 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002593 // Thus, if the template-name is actually the constructor
2594 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002595 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002596 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002597 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002598 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002599 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002600 if (isConstructorDeclarator()) {
2601 // The user meant this to be an out-of-line constructor
2602 // definition, but template arguments are not allowed
2603 // there. Just allow this as a constructor; we'll
2604 // complain about it later.
2605 goto DoneWithDeclSpec;
2606 }
2607
2608 // The user meant this to name a type, but it actually names
2609 // a constructor with some extraneous template
2610 // arguments. Complain, then parse it as a type as the user
2611 // intended.
2612 Diag(TemplateId->TemplateNameLoc,
2613 diag::err_out_of_line_template_id_names_constructor)
2614 << TemplateId->Name;
2615 }
2616
John McCall9dab4e62009-12-12 11:40:51 +00002617 DS.getTypeSpecScope() = SS;
2618 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002619 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002620 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002621 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002622 continue;
2623 }
2624
Douglas Gregorc5790df2009-09-28 07:26:33 +00002625 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002626 DS.getTypeSpecScope() = SS;
2627 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002628 if (Tok.getAnnotationValue()) {
2629 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002630 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002631 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002632 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002633 if (isInvalid)
2634 break;
John McCallba7bf592010-08-24 05:47:05 +00002635 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002636 else
2637 DS.SetTypeSpecError();
2638 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2639 ConsumeToken(); // The typename
2640 }
2641
Douglas Gregor167fa622009-03-25 15:40:00 +00002642 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002643 goto DoneWithDeclSpec;
2644
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002645 // If we're in a context where the identifier could be a class name,
2646 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002647 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002648 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002649 &SS)) {
2650 if (isConstructorDeclarator())
2651 goto DoneWithDeclSpec;
2652
2653 // As noted in C++ [class.qual]p2 (cited above), when the name
2654 // of the class is qualified in a context where it could name
2655 // a constructor, its a constructor name. However, we've
2656 // looked at the declarator, and the user probably meant this
2657 // to be a type. Complain that it isn't supposed to be treated
2658 // as a type, then proceed to parse it as a type.
2659 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2660 << Next.getIdentifierInfo();
2661 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002662
John McCallba7bf592010-08-24 05:47:05 +00002663 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2664 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002665 getCurScope(), &SS,
2666 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002667 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002668 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002669
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002670 // If the referenced identifier is not a type, then this declspec is
2671 // erroneous: We already checked about that it has no type specifier, and
2672 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002673 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002674 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002675 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002676 ParsedAttributesWithRange Attrs(AttrFactory);
2677 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2678 if (!Attrs.empty()) {
2679 AttrsLastTime = true;
2680 attrs.takeAllFrom(Attrs);
2681 }
2682 continue;
2683 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002684 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002685 }
Mike Stump11289f42009-09-09 15:08:12 +00002686
John McCall9dab4e62009-12-12 11:40:51 +00002687 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002688 ConsumeToken(); // The C++ scope.
2689
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002691 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002692 if (isInvalid)
2693 break;
Mike Stump11289f42009-09-09 15:08:12 +00002694
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002695 DS.SetRangeEnd(Tok.getLocation());
2696 ConsumeToken(); // The typename.
2697
2698 continue;
2699 }
Mike Stump11289f42009-09-09 15:08:12 +00002700
Chris Lattnere387d9e2009-01-21 19:48:37 +00002701 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002702 // If we've previously seen a tag definition, we were almost surely
2703 // missing a semicolon after it.
2704 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2705 goto DoneWithDeclSpec;
2706
John McCallba7bf592010-08-24 05:47:05 +00002707 if (Tok.getAnnotationValue()) {
2708 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002710 DiagID, T);
2711 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002712 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002713
Chris Lattner005fc1b2010-04-05 18:18:31 +00002714 if (isInvalid)
2715 break;
2716
Chris Lattnere387d9e2009-01-21 19:48:37 +00002717 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2718 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002719
Chris Lattnere387d9e2009-01-21 19:48:37 +00002720 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2721 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002722 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002723 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002724 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002725
Chris Lattnere387d9e2009-01-21 19:48:37 +00002726 continue;
2727 }
Mike Stump11289f42009-09-09 15:08:12 +00002728
Douglas Gregor06873092011-04-28 15:48:45 +00002729 case tok::kw___is_signed:
2730 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2731 // typically treats it as a trait. If we see __is_signed as it appears
2732 // in libstdc++, e.g.,
2733 //
2734 // static const bool __is_signed;
2735 //
2736 // then treat __is_signed as an identifier rather than as a keyword.
2737 if (DS.getTypeSpecType() == TST_bool &&
2738 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2739 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2740 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2741 Tok.setKind(tok::identifier);
2742 }
2743
2744 // We're done with the declaration-specifiers.
2745 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002746
Chris Lattner16fac4f2008-07-26 01:18:38 +00002747 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002748 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002749 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002750 // In C++, check to see if this is a scope specifier like foo::bar::, if
2751 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002752 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002753 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002754 if (!DS.hasTypeSpecifier())
2755 DS.SetTypeSpecError();
2756 goto DoneWithDeclSpec;
2757 }
2758 if (!Tok.is(tok::identifier))
2759 continue;
2760 }
Mike Stump11289f42009-09-09 15:08:12 +00002761
Chris Lattner16fac4f2008-07-26 01:18:38 +00002762 // This identifier can only be a typedef name if we haven't already seen
2763 // a type-specifier. Without this check we misparse:
2764 // typedef int X; struct Y { short X; }; as 'short int'.
2765 if (DS.hasTypeSpecifier())
2766 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002767
John Thompson22334602010-02-05 00:12:22 +00002768 // Check for need to substitute AltiVec keyword tokens.
2769 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2770 break;
2771
Richard Smith3092a3b2012-05-09 18:56:43 +00002772 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2773 // allow the use of a typedef name as a type specifier.
2774 if (DS.isTypeAltiVecVector())
2775 goto DoneWithDeclSpec;
2776
John McCallba7bf592010-08-24 05:47:05 +00002777 ParsedType TypeRep =
2778 Actions.getTypeName(*Tok.getIdentifierInfo(),
2779 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002780
Chris Lattner6cc055a2009-04-12 20:42:31 +00002781 // If this is not a typedef name, don't parse it as part of the declspec,
2782 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002783 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002784 ParsedAttributesWithRange Attrs(AttrFactory);
2785 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2786 if (!Attrs.empty()) {
2787 AttrsLastTime = true;
2788 attrs.takeAllFrom(Attrs);
2789 }
2790 continue;
2791 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002792 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002793 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002794
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002795 // If we're in a context where the identifier could be a class name,
2796 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002797 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002798 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002799 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002800 goto DoneWithDeclSpec;
2801
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002802 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002803 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002804 if (isInvalid)
2805 break;
Mike Stump11289f42009-09-09 15:08:12 +00002806
Chris Lattner16fac4f2008-07-26 01:18:38 +00002807 DS.SetRangeEnd(Tok.getLocation());
2808 ConsumeToken(); // The identifier
2809
2810 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2811 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002812 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002813 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002814 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002815
Steve Naroffcd5e7822008-09-22 10:28:57 +00002816 // Need to support trailing type qualifiers (e.g. "id<p> const").
2817 // If a type specifier follows, it will be diagnosed elsewhere.
2818 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002819 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002820
2821 // type-name
2822 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002823 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002824 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002825 // This template-id does not refer to a type name, so we're
2826 // done with the type-specifiers.
2827 goto DoneWithDeclSpec;
2828 }
2829
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002830 // If we're in a context where the template-id could be a
2831 // constructor name or specialization, check whether this is a
2832 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002833 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002834 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002835 isConstructorDeclarator())
2836 goto DoneWithDeclSpec;
2837
Douglas Gregor7f741122009-02-25 19:37:18 +00002838 // Turn the template-id annotation token into a type annotation
2839 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002840 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002841 continue;
2842 }
2843
Chris Lattnere37e2332006-08-15 04:50:22 +00002844 // GNU attributes support.
2845 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002846 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002847 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002848
2849 // Microsoft declspec support.
2850 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002851 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002852 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002853
Steve Naroff44ac7772008-12-25 14:16:32 +00002854 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002855 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002856 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002857 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002858 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002859 // FIXME: This does not work correctly if it is set to be a declspec
2860 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002861 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2862 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002863 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002864 }
Eli Friedman53339e02009-06-08 23:27:34 +00002865
Aaron Ballman317a77f2013-05-22 23:25:32 +00002866 case tok::kw___sptr:
2867 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002868 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002869 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002870 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002871 case tok::kw___cdecl:
2872 case tok::kw___stdcall:
2873 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002874 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002875 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002876 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002877 continue;
2878
Dawn Perchik335e16b2010-09-03 01:29:35 +00002879 // Borland single token adornments.
2880 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002881 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002882 continue;
2883
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002884 // OpenCL single token adornments.
2885 case tok::kw___kernel:
2886 ParseOpenCLAttributes(DS.getAttributes());
2887 continue;
2888
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002889 // storage-class-specifier
2890 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002891 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2892 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002893 break;
2894 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002895 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002896 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002897 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2898 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002899 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002900 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002901 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2902 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002903 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002904 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002905 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002906 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002907 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2908 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002909 break;
2910 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002911 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002912 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002913 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2914 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002915 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002916 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002917 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002918 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2920 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002921 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002922 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2923 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002924 break;
2925 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002926 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2927 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002928 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002929 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002930 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2931 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002932 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002933 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002934 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2935 PrevSpec, DiagID);
2936 break;
2937 case tok::kw_thread_local:
2938 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2939 PrevSpec, DiagID);
2940 break;
2941 case tok::kw__Thread_local:
2942 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2943 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002944 break;
Mike Stump11289f42009-09-09 15:08:12 +00002945
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002946 // function-specifier
2947 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002948 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002949 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002950 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002951 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002952 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002953 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002954 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002955 break;
Richard Smith0015f092013-01-17 22:16:11 +00002956 case tok::kw__Noreturn:
2957 if (!getLangOpts().C11)
2958 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002959 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002960 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002961
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002962 // alignment-specifier
2963 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002964 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002965 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002966 ParseAlignmentSpecifier(DS.getAttributes());
2967 continue;
2968
Anders Carlssoncd8db412009-05-06 04:46:28 +00002969 // friend
2970 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002971 if (DSContext == DSC_class)
2972 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2973 else {
2974 PrevSpec = ""; // not actually used by the diagnostic
2975 DiagID = diag::err_friend_invalid_in_context;
2976 isInvalid = true;
2977 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002978 break;
Mike Stump11289f42009-09-09 15:08:12 +00002979
Douglas Gregor26701a42011-09-09 02:06:17 +00002980 // Modules
2981 case tok::kw___module_private__:
2982 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2983 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002984
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002985 // constexpr
2986 case tok::kw_constexpr:
2987 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2988 break;
2989
Chris Lattnere387d9e2009-01-21 19:48:37 +00002990 // type-specifier
2991 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002992 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2993 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002994 break;
2995 case tok::kw_long:
2996 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002997 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2998 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002999 else
John McCall49bfce42009-08-03 20:12:06 +00003000 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3001 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003002 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003003 case tok::kw___int64:
3004 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3005 DiagID);
3006 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003007 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003008 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3009 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003010 break;
3011 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003012 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3013 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003014 break;
3015 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003016 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3017 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003018 break;
3019 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003020 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3021 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003022 break;
3023 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003024 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3025 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003026 break;
3027 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003028 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3029 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003030 break;
3031 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003032 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3033 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003034 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003035 case tok::kw___int128:
3036 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3037 DiagID);
3038 break;
3039 case tok::kw_half:
3040 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3041 DiagID);
3042 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003043 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003044 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3045 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003046 break;
3047 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3049 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003050 break;
3051 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3053 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003054 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003055 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3057 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003058 break;
3059 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3061 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003062 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003063 case tok::kw_bool:
3064 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003065 if (Tok.is(tok::kw_bool) &&
3066 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3067 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3068 PrevSpec = ""; // Not used by the diagnostic.
3069 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003070 // For better error recovery.
3071 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003072 isInvalid = true;
3073 } else {
3074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3075 DiagID);
3076 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003077 break;
3078 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3080 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003081 break;
3082 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3084 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003085 break;
3086 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003087 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3088 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003089 break;
John Thompson22334602010-02-05 00:12:22 +00003090 case tok::kw___vector:
3091 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3092 break;
3093 case tok::kw___pixel:
3094 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3095 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003096 case tok::kw_image1d_t:
3097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
3098 PrevSpec, DiagID);
3099 break;
3100 case tok::kw_image1d_array_t:
3101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
3102 PrevSpec, DiagID);
3103 break;
3104 case tok::kw_image1d_buffer_t:
3105 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3106 PrevSpec, DiagID);
3107 break;
3108 case tok::kw_image2d_t:
3109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3110 PrevSpec, DiagID);
3111 break;
3112 case tok::kw_image2d_array_t:
3113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3114 PrevSpec, DiagID);
3115 break;
3116 case tok::kw_image3d_t:
3117 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3118 PrevSpec, DiagID);
3119 break;
Guy Benyei61054192013-02-07 10:55:47 +00003120 case tok::kw_sampler_t:
3121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3122 PrevSpec, DiagID);
3123 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003124 case tok::kw_event_t:
3125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3126 PrevSpec, DiagID);
3127 break;
John McCall39439732011-04-09 22:50:59 +00003128 case tok::kw___unknown_anytype:
3129 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3130 PrevSpec, DiagID);
3131 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003132
3133 // class-specifier:
3134 case tok::kw_class:
3135 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003136 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003137 case tok::kw_union: {
3138 tok::TokenKind Kind = Tok.getKind();
3139 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003140
3141 // These are attributes following class specifiers.
3142 // To produce better diagnostic, we parse them when
3143 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003144 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003145 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003146 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003147
3148 // If there are attributes following class specifier,
3149 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003150 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003151 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003152 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003153 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003154 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003155 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003156
3157 // enum-specifier:
3158 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003159 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003160 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003161 continue;
3162
3163 // cv-qualifier:
3164 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003165 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003166 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003167 break;
3168 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003169 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003170 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003171 break;
3172 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003173 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003174 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003175 break;
3176
Douglas Gregor333489b2009-03-27 23:10:48 +00003177 // C++ typename-specifier:
3178 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003179 if (TryAnnotateTypeOrScopeToken()) {
3180 DS.SetTypeSpecError();
3181 goto DoneWithDeclSpec;
3182 }
3183 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003184 continue;
3185 break;
3186
Chris Lattnere387d9e2009-01-21 19:48:37 +00003187 // GNU typeof support.
3188 case tok::kw_typeof:
3189 ParseTypeofSpecifier(DS);
3190 continue;
3191
David Blaikie15a430a2011-12-04 05:04:18 +00003192 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003193 ParseDecltypeSpecifier(DS);
3194 continue;
3195
Alexis Hunt4a257072011-05-19 05:37:45 +00003196 case tok::kw___underlying_type:
3197 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003198 continue;
3199
3200 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003201 // C11 6.7.2.4/4:
3202 // If the _Atomic keyword is immediately followed by a left parenthesis,
3203 // it is interpreted as a type specifier (with a type name), not as a
3204 // type qualifier.
3205 if (NextToken().is(tok::l_paren)) {
3206 ParseAtomicSpecifier(DS);
3207 continue;
3208 }
3209 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3210 getLangOpts());
3211 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003212
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003213 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003214 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003215 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003216 goto DoneWithDeclSpec;
3217 case tok::kw___private:
3218 case tok::kw___global:
3219 case tok::kw___local:
3220 case tok::kw___constant:
3221 case tok::kw___read_only:
3222 case tok::kw___write_only:
3223 case tok::kw___read_write:
3224 ParseOpenCLQualifiers(DS);
3225 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003226
Steve Naroffcfdf6162008-06-05 00:02:44 +00003227 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003228 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003229 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3230 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003231 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003232 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003233
Douglas Gregor3a001f42010-11-19 17:10:50 +00003234 if (!ParseObjCProtocolQualifiers(DS))
3235 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3236 << FixItHint::CreateInsertion(Loc, "id")
3237 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003238
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003239 // Need to support trailing type qualifiers (e.g. "id<p> const").
3240 // If a type specifier follows, it will be diagnosed elsewhere.
3241 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003242 }
John McCall49bfce42009-08-03 20:12:06 +00003243 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003244 if (isInvalid) {
3245 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003246 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003247
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003248 if (DiagID == diag::ext_duplicate_declspec)
3249 Diag(Tok, DiagID)
3250 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3251 else
3252 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003253 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003254
Chris Lattner2e232092008-03-13 06:29:04 +00003255 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003256 if (DiagID != diag::err_bool_redeclaration)
3257 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003258
3259 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003260 }
3261}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003262
Chris Lattner70ae4912007-10-29 04:42:53 +00003263/// ParseStructDeclaration - Parse a struct declaration without the terminating
3264/// semicolon.
3265///
Chris Lattner90a26b02007-01-23 04:38:16 +00003266/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003267/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003268/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003269/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003270/// struct-declarator-list:
3271/// struct-declarator
3272/// struct-declarator-list ',' struct-declarator
3273/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3274/// struct-declarator:
3275/// declarator
3276/// [GNU] declarator attributes[opt]
3277/// declarator[opt] ':' constant-expression
3278/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3279///
Chris Lattnera12405b2008-04-10 06:46:29 +00003280void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003281ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003282
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003283 if (Tok.is(tok::kw___extension__)) {
3284 // __extension__ silences extension warnings in the subexpression.
3285 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003286 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003287 return ParseStructDeclaration(DS, Fields);
3288 }
Mike Stump11289f42009-09-09 15:08:12 +00003289
Steve Naroff97170802007-08-20 22:28:22 +00003290 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003291 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003292
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003293 // If there are no declarators, this is a free-standing declaration
3294 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003295 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003296 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3297 DS);
3298 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003299 return;
3300 }
3301
3302 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003303 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003304 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003305 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003306 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003307 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003308
Bill Wendling44426052012-12-20 19:22:21 +00003309 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003310 if (!FirstDeclarator)
3311 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003312
Steve Naroff97170802007-08-20 22:28:22 +00003313 /// struct-declarator: declarator
3314 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003315 if (Tok.isNot(tok::colon)) {
3316 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3317 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003318 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003319 }
Mike Stump11289f42009-09-09 15:08:12 +00003320
Chris Lattner76c72282007-10-09 17:33:22 +00003321 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003322 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003323 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003324 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003325 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003326 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003327 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003328 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003329
Steve Naroff97170802007-08-20 22:28:22 +00003330 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003331 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003332
John McCallcfefb6d2009-11-03 02:38:08 +00003333 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003334 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003335
Steve Naroff97170802007-08-20 22:28:22 +00003336 // If we don't have a comma, it is either the end of the list (a ';')
3337 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003338 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003339 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003340
Steve Naroff97170802007-08-20 22:28:22 +00003341 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003342 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003343
John McCallcfefb6d2009-11-03 02:38:08 +00003344 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003345 }
Steve Naroff97170802007-08-20 22:28:22 +00003346}
3347
3348/// ParseStructUnionBody
3349/// struct-contents:
3350/// struct-declaration-list
3351/// [EXT] empty
3352/// [GNU] "struct-declaration-list" without terminatoring ';'
3353/// struct-declaration-list:
3354/// struct-declaration
3355/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003356/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003357///
Chris Lattner1300fb92007-01-23 23:42:53 +00003358void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003359 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003360 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3361 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003362 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003363
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003364 BalancedDelimiterTracker T(*this, tok::l_brace);
3365 if (T.consumeOpen())
3366 return;
Mike Stump11289f42009-09-09 15:08:12 +00003367
Douglas Gregor658b9552009-01-09 22:42:13 +00003368 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003369 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003370
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003371 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003372
Chris Lattner7b9ace62007-01-23 20:11:08 +00003373 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003374 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003375 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003376
Chris Lattner736ed5d2007-06-09 05:59:07 +00003377 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003378 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003379 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003380 continue;
3381 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003382
Andy Gibbsc804e082013-04-03 09:46:04 +00003383 // Parse _Static_assert declaration.
3384 if (Tok.is(tok::kw__Static_assert)) {
3385 SourceLocation DeclEnd;
3386 ParseStaticAssertDeclaration(DeclEnd);
3387 continue;
3388 }
3389
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003390 if (Tok.is(tok::annot_pragma_pack)) {
3391 HandlePragmaPack();
3392 continue;
3393 }
3394
3395 if (Tok.is(tok::annot_pragma_align)) {
3396 HandlePragmaAlign();
3397 continue;
3398 }
3399
John McCallcfefb6d2009-11-03 02:38:08 +00003400 if (!Tok.is(tok::at)) {
3401 struct CFieldCallback : FieldCallback {
3402 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003403 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003404 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003405
John McCall48871652010-08-21 09:40:31 +00003406 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003407 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003408 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3409
Eli Friedman934dbbf2012-08-08 23:53:27 +00003410 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003411 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003412 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003413 FD.D.getDeclSpec().getSourceRange().getBegin(),
3414 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003415 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003416 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003417 }
John McCallcfefb6d2009-11-03 02:38:08 +00003418 } Callback(*this, TagDecl, FieldDecls);
3419
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003420 // Parse all the comma separated declarators.
3421 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003422 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003423 } else { // Handle @defs
3424 ConsumeToken();
3425 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3426 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003427 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003428 continue;
3429 }
3430 ConsumeToken();
3431 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3432 if (!Tok.is(tok::identifier)) {
3433 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003434 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003435 continue;
3436 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003437 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003438 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003439 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003440 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3441 ConsumeToken();
3442 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003443 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003444
Chris Lattner76c72282007-10-09 17:33:22 +00003445 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003446 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003447 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003448 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003449 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003450 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003451 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3452 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003453 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner245c5332010-02-02 00:37:27 +00003454 // If we stopped at a ';', eat it.
3455 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003456 }
3457 }
Mike Stump11289f42009-09-09 15:08:12 +00003458
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003459 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003460
John McCall084e83d2011-03-24 11:26:52 +00003461 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003462 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003463 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003464
Douglas Gregor0be31a22010-07-02 17:43:08 +00003465 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003466 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003467 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003468 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003469 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003470 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3471 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003472}
3473
Chris Lattner3b561a32006-08-13 00:12:11 +00003474/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003475/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003476/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003477///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003478/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3479/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003480/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3481/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003482/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003483/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003484///
Richard Smith7d137e32012-03-23 03:33:32 +00003485/// [C++11] enum-head '{' enumerator-list[opt] '}'
3486/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003487///
Richard Smith7d137e32012-03-23 03:33:32 +00003488/// enum-head: [C++11]
3489/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3490/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3491/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003492///
Richard Smith7d137e32012-03-23 03:33:32 +00003493/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003494/// 'enum'
3495/// 'enum' 'class'
3496/// 'enum' 'struct'
3497///
Richard Smith7d137e32012-03-23 03:33:32 +00003498/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003499/// ':' type-specifier-seq
3500///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003501/// [C++] elaborated-type-specifier:
3502/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3503///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003504void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003505 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003506 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003507 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003508 if (Tok.is(tok::code_completion)) {
3509 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003510 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003511 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003512 }
John McCallcb432fa2011-07-06 05:58:41 +00003513
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003514 // If attributes exist after tag, parse them.
3515 ParsedAttributesWithRange attrs(AttrFactory);
3516 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003517 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003518
3519 // If declspecs exist after tag, parse them.
3520 while (Tok.is(tok::kw___declspec))
3521 ParseMicrosoftDeclSpec(attrs);
3522
Richard Smith0f8ee222012-01-10 01:33:14 +00003523 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003524 bool IsScopedUsingClassTag = false;
3525
John McCallbeae29a2012-06-23 22:30:04 +00003526 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003527 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3528 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3529 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003530 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003531 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003532
Bill Wendling44426052012-12-20 19:22:21 +00003533 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003534 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003535 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003536
3537 // They are allowed afterwards, though.
3538 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003539 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003540 while (Tok.is(tok::kw___declspec))
3541 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003542 }
Richard Smith7d137e32012-03-23 03:33:32 +00003543
John McCall6347b682012-05-07 06:16:58 +00003544 // C++11 [temp.explicit]p12:
3545 // The usual access controls do not apply to names used to specify
3546 // explicit instantiations.
3547 // We extend this to also cover explicit specializations. Note that
3548 // we don't suppress if this turns out to be an elaborated type
3549 // specifier.
3550 bool shouldDelayDiagsInTag =
3551 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3552 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3553 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003554
Richard Smithbfdb1082012-03-12 08:56:40 +00003555 // Enum definitions should not be parsed in a trailing-return-type.
3556 bool AllowDeclaration = DSC != DSC_trailing;
3557
3558 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003559 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003560 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003561
Abramo Bagnarad7548482010-05-19 21:37:53 +00003562 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003563 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003564 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3565 // if a fixed underlying type is allowed.
3566 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003567
3568 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003569 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003570 return;
3571
3572 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003573 Diag(Tok, diag::err_expected_ident);
3574 if (Tok.isNot(tok::l_brace)) {
3575 // Has no name and is not a definition.
3576 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003577 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003578 return;
3579 }
3580 }
3581 }
Mike Stump11289f42009-09-09 15:08:12 +00003582
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003583 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003584 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003585 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003586 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003587
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003588 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003589 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003590 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003591 }
Mike Stump11289f42009-09-09 15:08:12 +00003592
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003593 // If an identifier is present, consume and remember it.
3594 IdentifierInfo *Name = 0;
3595 SourceLocation NameLoc;
3596 if (Tok.is(tok::identifier)) {
3597 Name = Tok.getIdentifierInfo();
3598 NameLoc = ConsumeToken();
3599 }
Mike Stump11289f42009-09-09 15:08:12 +00003600
Richard Smith0f8ee222012-01-10 01:33:14 +00003601 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003602 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3603 // declaration of a scoped enumeration.
3604 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003605 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003606 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003607 }
3608
John McCall6347b682012-05-07 06:16:58 +00003609 // Okay, end the suppression area. We'll decide whether to emit the
3610 // diagnostics in a second.
3611 if (shouldDelayDiagsInTag)
3612 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003613
Douglas Gregor0bf31402010-10-08 23:50:27 +00003614 TypeResult BaseType;
3615
Douglas Gregord1f69f62010-12-01 17:42:47 +00003616 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003617 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003618 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003619 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003620 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003621 // If we're in class scope, this can either be an enum declaration with
3622 // an underlying type, or a declaration of a bitfield member. We try to
3623 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003624 // (integer literal, sizeof); if it's still ambiguous, we then consider
3625 // anything that's a simple-type-specifier followed by '(' as an
3626 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003627 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003628 EnterExpressionEvaluationContext Unevaluated(Actions,
3629 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003630 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003631 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003632 // bit-field. This is the common case.
3633 if (TPR == TPResult::True())
3634 PossibleBitfield = true;
3635 // If the next token starts a type-specifier-seq, it may be either a
3636 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003637 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003638 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003639 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003640 GetLookAheadToken(2).getKind() == tok::semi) {
3641 // Consume the ':'.
3642 ConsumeToken();
3643 } else {
3644 // We have the start of a type-specifier-seq, so we have to perform
3645 // tentative parsing to determine whether we have an expression or a
3646 // type.
3647 TentativeParsingAction TPA(*this);
3648
3649 // Consume the ':'.
3650 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003651
3652 // If we see a type specifier followed by an open-brace, we have an
3653 // ambiguity between an underlying type and a C++11 braced
3654 // function-style cast. Resolve this by always treating it as an
3655 // underlying type.
3656 // FIXME: The standard is not entirely clear on how to disambiguate in
3657 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003658 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003659 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003660 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003661 // We'll parse this as a bitfield later.
3662 PossibleBitfield = true;
3663 TPA.Revert();
3664 } else {
3665 // We have a type-specifier-seq.
3666 TPA.Commit();
3667 }
3668 }
3669 } else {
3670 // Consume the ':'.
3671 ConsumeToken();
3672 }
3673
3674 if (!PossibleBitfield) {
3675 SourceRange Range;
3676 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003677
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003678 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003679 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003680 } else if (!getLangOpts().ObjC2) {
3681 if (getLangOpts().CPlusPlus)
3682 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3683 else
3684 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3685 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003686 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003687 }
3688
Richard Smith0f8ee222012-01-10 01:33:14 +00003689 // There are four options here. If we have 'friend enum foo;' then this is a
3690 // friend declaration, and cannot have an accompanying definition. If we have
3691 // 'enum foo;', then this is a forward declaration. If we have
3692 // 'enum foo {...' then this is a definition. Otherwise we have something
3693 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003694 //
3695 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3696 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3697 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3698 //
John McCallfaf5fb42010-08-26 23:41:50 +00003699 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003700 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003701 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003702 } else if (Tok.is(tok::l_brace)) {
3703 if (DS.isFriendSpecified()) {
3704 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3705 << SourceRange(DS.getFriendSpecLoc());
3706 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003707 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003708 TUK = Sema::TUK_Friend;
3709 } else {
3710 TUK = Sema::TUK_Definition;
3711 }
Richard Smith369b9f92012-06-25 21:37:02 +00003712 } else if (DSC != DSC_type_specifier &&
3713 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003714 (Tok.isAtStartOfLine() &&
3715 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003716 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3717 if (Tok.isNot(tok::semi)) {
3718 // A semicolon was missing after this declaration. Diagnose and recover.
3719 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3720 "enum");
3721 PP.EnterToken(Tok);
3722 Tok.setKind(tok::semi);
3723 }
John McCall6347b682012-05-07 06:16:58 +00003724 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003725 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003726 }
3727
3728 // If this is an elaborated type specifier, and we delayed
3729 // diagnostics before, just merge them into the current pool.
3730 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3731 diagsFromTag.redelay();
3732 }
Richard Smith7d137e32012-03-23 03:33:32 +00003733
3734 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003735 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003736 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003737 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003738 // Skip the rest of this declarator, up until the comma or semicolon.
3739 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003740 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003741 return;
3742 }
3743
3744 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3745 // Enumerations can't be explicitly instantiated.
3746 DS.SetTypeSpecError();
3747 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3748 return;
3749 }
3750
3751 assert(TemplateInfo.TemplateParams && "no template parameters");
3752 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3753 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003754 }
Chad Rosierc1183952012-06-26 22:30:43 +00003755
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003756 if (TUK == Sema::TUK_Reference)
3757 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003758
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003759 if (!Name && TUK != Sema::TUK_Definition) {
3760 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003761
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003762 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003763 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003764 return;
3765 }
Richard Smith7d137e32012-03-23 03:33:32 +00003766
Douglas Gregord6ab8742009-05-28 23:31:59 +00003767 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003768 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003769 const char *PrevSpec = 0;
3770 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003771 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003772 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003773 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003774 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003775 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003776
Douglas Gregorba41d012010-04-24 16:38:41 +00003777 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003778 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003779 // dependent tag.
3780 if (!Name) {
3781 DS.SetTypeSpecError();
3782 Diag(Tok, diag::err_expected_type_name_after_typename);
3783 return;
3784 }
Chad Rosierc1183952012-06-26 22:30:43 +00003785
Douglas Gregor0be31a22010-07-02 17:43:08 +00003786 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003787 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003788 NameLoc);
3789 if (Type.isInvalid()) {
3790 DS.SetTypeSpecError();
3791 return;
3792 }
Chad Rosierc1183952012-06-26 22:30:43 +00003793
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003794 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3795 NameLoc.isValid() ? NameLoc : StartLoc,
3796 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003797 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003798
Douglas Gregorba41d012010-04-24 16:38:41 +00003799 return;
3800 }
Mike Stump11289f42009-09-09 15:08:12 +00003801
John McCall48871652010-08-21 09:40:31 +00003802 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003803 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003804 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003805 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003806 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003807 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003808 }
Chad Rosierc1183952012-06-26 22:30:43 +00003809
Douglas Gregorba41d012010-04-24 16:38:41 +00003810 DS.SetTypeSpecError();
3811 return;
3812 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003813
Richard Smith369b9f92012-06-25 21:37:02 +00003814 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003815 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003816
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003817 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3818 NameLoc.isValid() ? NameLoc : StartLoc,
3819 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003820 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003821}
3822
Chris Lattnerc1915e22007-01-25 07:29:02 +00003823/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3824/// enumerator-list:
3825/// enumerator
3826/// enumerator-list ',' enumerator
3827/// enumerator:
3828/// enumeration-constant
3829/// enumeration-constant '=' constant-expression
3830/// enumeration-constant:
3831/// identifier
3832///
John McCall48871652010-08-21 09:40:31 +00003833void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003834 // Enter the scope of the enum body and start the definition.
3835 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003836 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003837
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003838 BalancedDelimiterTracker T(*this, tok::l_brace);
3839 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003840
Chris Lattner37256fb2007-08-27 17:24:30 +00003841 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003842 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003843 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003844
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003845 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003846
John McCall48871652010-08-21 09:40:31 +00003847 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003848
Chris Lattnerc1915e22007-01-25 07:29:02 +00003849 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003850 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003851 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3852 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003853
John McCall811a0f52010-10-22 23:36:17 +00003854 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003855 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003856 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003857 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003858 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003859
Chris Lattnerc1915e22007-01-25 07:29:02 +00003860 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003861 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003862 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003863
Chris Lattner76c72282007-10-09 17:33:22 +00003864 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003865 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003866 AssignedVal = ParseConstantExpression();
3867 if (AssignedVal.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003868 SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003869 }
Mike Stump11289f42009-09-09 15:08:12 +00003870
Chris Lattnerc1915e22007-01-25 07:29:02 +00003871 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003872 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3873 LastEnumConstDecl,
3874 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003875 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003876 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003877 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003878
Chris Lattner4ef40012007-06-11 01:28:17 +00003879 EnumConstantDecls.push_back(EnumConstDecl);
3880 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003881
Douglas Gregorce66d022010-09-07 14:51:08 +00003882 if (Tok.is(tok::identifier)) {
3883 // We're missing a comma between enumerators.
3884 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003885 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003886 << FixItHint::CreateInsertion(Loc, ", ");
3887 continue;
3888 }
Chad Rosierc1183952012-06-26 22:30:43 +00003889
Chris Lattner76c72282007-10-09 17:33:22 +00003890 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003891 break;
3892 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003893
Richard Smith5d164bc2011-10-15 05:09:34 +00003894 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003895 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003896 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3897 diag::ext_enumerator_list_comma_cxx :
3898 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003899 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003900 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003901 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3902 << FixItHint::CreateRemoval(CommaLoc);
3903 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003904 }
Mike Stump11289f42009-09-09 15:08:12 +00003905
Chris Lattnerc1915e22007-01-25 07:29:02 +00003906 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003907 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003908
Chris Lattnerc1915e22007-01-25 07:29:02 +00003909 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003910 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003911 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003912
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003913 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003914 EnumDecl, EnumConstantDecls,
3915 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003916 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003917
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003918 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003919 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3920 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003921
3922 // The next token must be valid after an enum definition. If not, a ';'
3923 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003924 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3925 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003926 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3927 // Push this token back into the preprocessor and change our current token
3928 // to ';' so that the rest of the code recovers as though there were an
3929 // ';' after the definition.
3930 PP.EnterToken(Tok);
3931 Tok.setKind(tok::semi);
3932 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003933}
Chris Lattner3b561a32006-08-13 00:12:11 +00003934
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003935/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003936/// start of a type-qualifier-list.
3937bool Parser::isTypeQualifier() const {
3938 switch (Tok.getKind()) {
3939 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003940
3941 // type-qualifier only in OpenCL
3942 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003943 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003944
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003945 // type-qualifier
3946 case tok::kw_const:
3947 case tok::kw_volatile:
3948 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003949 case tok::kw___private:
3950 case tok::kw___local:
3951 case tok::kw___global:
3952 case tok::kw___constant:
3953 case tok::kw___read_only:
3954 case tok::kw___read_write:
3955 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003956 return true;
3957 }
3958}
3959
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003960/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3961/// is definitely a type-specifier. Return false if it isn't part of a type
3962/// specifier or if we're not sure.
3963bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3964 switch (Tok.getKind()) {
3965 default: return false;
3966 // type-specifiers
3967 case tok::kw_short:
3968 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003969 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003970 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003971 case tok::kw_signed:
3972 case tok::kw_unsigned:
3973 case tok::kw__Complex:
3974 case tok::kw__Imaginary:
3975 case tok::kw_void:
3976 case tok::kw_char:
3977 case tok::kw_wchar_t:
3978 case tok::kw_char16_t:
3979 case tok::kw_char32_t:
3980 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003981 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003982 case tok::kw_float:
3983 case tok::kw_double:
3984 case tok::kw_bool:
3985 case tok::kw__Bool:
3986 case tok::kw__Decimal32:
3987 case tok::kw__Decimal64:
3988 case tok::kw__Decimal128:
3989 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003990
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003991 // OpenCL specific types:
3992 case tok::kw_image1d_t:
3993 case tok::kw_image1d_array_t:
3994 case tok::kw_image1d_buffer_t:
3995 case tok::kw_image2d_t:
3996 case tok::kw_image2d_array_t:
3997 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003998 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003999 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004000
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004001 // struct-or-union-specifier (C99) or class-specifier (C++)
4002 case tok::kw_class:
4003 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004004 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004005 case tok::kw_union:
4006 // enum-specifier
4007 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004008
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004009 // typedef-name
4010 case tok::annot_typename:
4011 return true;
4012 }
4013}
4014
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004015/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004016/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004017bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004018 switch (Tok.getKind()) {
4019 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004020
Chris Lattner020bab92009-01-04 23:41:41 +00004021 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004022 if (TryAltiVecVectorToken())
4023 return true;
4024 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004025 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004026 // Annotate typenames and C++ scope specifiers. If we get one, just
4027 // recurse to handle whatever we get.
4028 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004029 return true;
4030 if (Tok.is(tok::identifier))
4031 return false;
4032 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004033
Chris Lattner020bab92009-01-04 23:41:41 +00004034 case tok::coloncolon: // ::foo::bar
4035 if (NextToken().is(tok::kw_new) || // ::new
4036 NextToken().is(tok::kw_delete)) // ::delete
4037 return false;
4038
Chris Lattner020bab92009-01-04 23:41:41 +00004039 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004040 return true;
4041 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004042
Chris Lattnere37e2332006-08-15 04:50:22 +00004043 // GNU attributes support.
4044 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004045 // GNU typeof support.
4046 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004047
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004048 // type-specifiers
4049 case tok::kw_short:
4050 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004051 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004052 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004053 case tok::kw_signed:
4054 case tok::kw_unsigned:
4055 case tok::kw__Complex:
4056 case tok::kw__Imaginary:
4057 case tok::kw_void:
4058 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004059 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004060 case tok::kw_char16_t:
4061 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004062 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004063 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004064 case tok::kw_float:
4065 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004066 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004067 case tok::kw__Bool:
4068 case tok::kw__Decimal32:
4069 case tok::kw__Decimal64:
4070 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004071 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004072
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004073 // OpenCL specific types:
4074 case tok::kw_image1d_t:
4075 case tok::kw_image1d_array_t:
4076 case tok::kw_image1d_buffer_t:
4077 case tok::kw_image2d_t:
4078 case tok::kw_image2d_array_t:
4079 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004080 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004081 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004082
Chris Lattner861a2262008-04-13 18:59:07 +00004083 // struct-or-union-specifier (C99) or class-specifier (C++)
4084 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004085 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004086 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004087 case tok::kw_union:
4088 // enum-specifier
4089 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004090
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004091 // type-qualifier
4092 case tok::kw_const:
4093 case tok::kw_volatile:
4094 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004095
John McCallea0a39e2012-11-14 00:49:39 +00004096 // Debugger support.
4097 case tok::kw___unknown_anytype:
4098
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004099 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004100 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004101 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004102
Chris Lattner409bf7d2008-10-20 00:25:30 +00004103 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4104 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004105 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004106
Steve Naroff44ac7772008-12-25 14:16:32 +00004107 case tok::kw___cdecl:
4108 case tok::kw___stdcall:
4109 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004110 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004111 case tok::kw___w64:
4112 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004113 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004114 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004115 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004116
4117 case tok::kw___private:
4118 case tok::kw___local:
4119 case tok::kw___global:
4120 case tok::kw___constant:
4121 case tok::kw___read_only:
4122 case tok::kw___read_write:
4123 case tok::kw___write_only:
4124
Eli Friedman53339e02009-06-08 23:27:34 +00004125 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004126
4127 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004128 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00004129
Richard Smith8e1ac332013-03-28 01:55:44 +00004130 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004131 case tok::kw__Atomic:
4132 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004133 }
4134}
4135
Chris Lattneracd58a32006-08-06 17:24:14 +00004136/// isDeclarationSpecifier() - Return true if the current token is part of a
4137/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004138///
4139/// \param DisambiguatingWithExpression True to indicate that the purpose of
4140/// this check is to disambiguate between an expression and a declaration.
4141bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004142 switch (Tok.getKind()) {
4143 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004144
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004145 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004146 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004147
Chris Lattner020bab92009-01-04 23:41:41 +00004148 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004149 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004150 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004151 return false;
John Thompson22334602010-02-05 00:12:22 +00004152 if (TryAltiVecVectorToken())
4153 return true;
4154 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004155 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004156 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004157 // Annotate typenames and C++ scope specifiers. If we get one, just
4158 // recurse to handle whatever we get.
4159 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004160 return true;
4161 if (Tok.is(tok::identifier))
4162 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004163
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004164 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004165 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004166 // expression is permitted, then this is probably a class message send
4167 // missing the initial '['. In this case, we won't consider this to be
4168 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004169 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004170 isStartOfObjCClassMessageMissingOpenBracket())
4171 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004172
John McCall1f476a12010-02-26 08:45:28 +00004173 return isDeclarationSpecifier();
4174
Chris Lattner020bab92009-01-04 23:41:41 +00004175 case tok::coloncolon: // ::foo::bar
4176 if (NextToken().is(tok::kw_new) || // ::new
4177 NextToken().is(tok::kw_delete)) // ::delete
4178 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004179
Chris Lattner020bab92009-01-04 23:41:41 +00004180 // Annotate typenames and C++ scope specifiers. If we get one, just
4181 // recurse to handle whatever we get.
4182 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004183 return true;
4184 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004185
Chris Lattneracd58a32006-08-06 17:24:14 +00004186 // storage-class-specifier
4187 case tok::kw_typedef:
4188 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004189 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004190 case tok::kw_static:
4191 case tok::kw_auto:
4192 case tok::kw_register:
4193 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004194 case tok::kw_thread_local:
4195 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004196
Douglas Gregor26701a42011-09-09 02:06:17 +00004197 // Modules
4198 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004199
John McCallea0a39e2012-11-14 00:49:39 +00004200 // Debugger support
4201 case tok::kw___unknown_anytype:
4202
Chris Lattneracd58a32006-08-06 17:24:14 +00004203 // type-specifiers
4204 case tok::kw_short:
4205 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004206 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004207 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004208 case tok::kw_signed:
4209 case tok::kw_unsigned:
4210 case tok::kw__Complex:
4211 case tok::kw__Imaginary:
4212 case tok::kw_void:
4213 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004214 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004215 case tok::kw_char16_t:
4216 case tok::kw_char32_t:
4217
Chris Lattneracd58a32006-08-06 17:24:14 +00004218 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004219 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004220 case tok::kw_float:
4221 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004222 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004223 case tok::kw__Bool:
4224 case tok::kw__Decimal32:
4225 case tok::kw__Decimal64:
4226 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004227 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004228
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004229 // OpenCL specific types:
4230 case tok::kw_image1d_t:
4231 case tok::kw_image1d_array_t:
4232 case tok::kw_image1d_buffer_t:
4233 case tok::kw_image2d_t:
4234 case tok::kw_image2d_array_t:
4235 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004236 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004237 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004238
Chris Lattner861a2262008-04-13 18:59:07 +00004239 // struct-or-union-specifier (C99) or class-specifier (C++)
4240 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004241 case tok::kw_struct:
4242 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004243 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004244 // enum-specifier
4245 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004246
Chris Lattneracd58a32006-08-06 17:24:14 +00004247 // type-qualifier
4248 case tok::kw_const:
4249 case tok::kw_volatile:
4250 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004251
Chris Lattneracd58a32006-08-06 17:24:14 +00004252 // function-specifier
4253 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004254 case tok::kw_virtual:
4255 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004256 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004257
Richard Smith1dba27c2013-01-29 09:02:09 +00004258 // alignment-specifier
4259 case tok::kw__Alignas:
4260
Richard Smithd16fe122012-10-25 00:00:53 +00004261 // friend keyword.
4262 case tok::kw_friend:
4263
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004264 // static_assert-declaration
4265 case tok::kw__Static_assert:
4266
Chris Lattner599e47e2007-08-09 17:01:07 +00004267 // GNU typeof support.
4268 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004269
Chris Lattner599e47e2007-08-09 17:01:07 +00004270 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004271 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004272
Richard Smithd16fe122012-10-25 00:00:53 +00004273 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004274 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004275 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004276
Richard Smith8e1ac332013-03-28 01:55:44 +00004277 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004278 case tok::kw__Atomic:
4279 return true;
4280
Chris Lattner8b2ec162008-07-26 03:38:44 +00004281 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4282 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004283 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004284
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004285 // typedef-name
4286 case tok::annot_typename:
4287 return !DisambiguatingWithExpression ||
4288 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004289
Steve Narofff192fab2009-01-06 19:34:12 +00004290 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004291 case tok::kw___cdecl:
4292 case tok::kw___stdcall:
4293 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004294 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004295 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004296 case tok::kw___sptr:
4297 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004298 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004299 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004300 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004301 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004302 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004303
4304 case tok::kw___private:
4305 case tok::kw___local:
4306 case tok::kw___global:
4307 case tok::kw___constant:
4308 case tok::kw___read_only:
4309 case tok::kw___read_write:
4310 case tok::kw___write_only:
4311
Eli Friedman53339e02009-06-08 23:27:34 +00004312 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004313 }
4314}
4315
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004316bool Parser::isConstructorDeclarator() {
4317 TentativeParsingAction TPA(*this);
4318
4319 // Parse the C++ scope specifier.
4320 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004321 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004322 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004323 TPA.Revert();
4324 return false;
4325 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004326
4327 // Parse the constructor name.
4328 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4329 // We already know that we have a constructor name; just consume
4330 // the token.
4331 ConsumeToken();
4332 } else {
4333 TPA.Revert();
4334 return false;
4335 }
4336
Richard Smith43f340f2012-03-27 23:05:05 +00004337 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004338 if (Tok.isNot(tok::l_paren)) {
4339 TPA.Revert();
4340 return false;
4341 }
4342 ConsumeParen();
4343
Richard Smith43f340f2012-03-27 23:05:05 +00004344 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4345 // that we have a constructor.
4346 if (Tok.is(tok::r_paren) ||
4347 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004348 TPA.Revert();
4349 return true;
4350 }
4351
Richard Smithf2163662013-09-06 00:12:20 +00004352 // A C++11 attribute here signals that we have a constructor, and is an
4353 // attribute on the first constructor parameter.
4354 if (getLangOpts().CPlusPlus11 &&
4355 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4356 /*OuterMightBeMessageSend*/ true)) {
4357 TPA.Revert();
4358 return true;
4359 }
4360
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004361 // If we need to, enter the specified scope.
4362 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004363 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004364 DeclScopeObj.EnterDeclaratorScope();
4365
Francois Pichet79f3a872011-01-31 04:54:32 +00004366 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004367 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004368 MaybeParseMicrosoftAttributes(Attrs);
4369
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004370 // Check whether the next token(s) are part of a declaration
4371 // specifier, in which case we have the start of a parameter and,
4372 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004373 bool IsConstructor = false;
4374 if (isDeclarationSpecifier())
4375 IsConstructor = true;
4376 else if (Tok.is(tok::identifier) ||
4377 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4378 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4379 // This might be a parenthesized member name, but is more likely to
4380 // be a constructor declaration with an invalid argument type. Keep
4381 // looking.
4382 if (Tok.is(tok::annot_cxxscope))
4383 ConsumeToken();
4384 ConsumeToken();
4385
4386 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004387 // which must have one of the following syntactic forms (see the
4388 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004389 switch (Tok.getKind()) {
4390 case tok::l_paren:
4391 // C(X ( int));
4392 case tok::l_square:
4393 // C(X [ 5]);
4394 // C(X [ [attribute]]);
4395 case tok::coloncolon:
4396 // C(X :: Y);
4397 // C(X :: *p);
4398 case tok::r_paren:
4399 // C(X )
4400 // Assume this isn't a constructor, rather than assuming it's a
4401 // constructor with an unnamed parameter of an ill-formed type.
4402 break;
4403
4404 default:
4405 IsConstructor = true;
4406 break;
4407 }
4408 }
4409
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004410 TPA.Revert();
4411 return IsConstructor;
4412}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004413
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004414/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004415/// type-qualifier-list: [C99 6.7.5]
4416/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004417/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004418/// [ only if VendorAttributesAllowed=true ]
4419/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004420/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004421/// [ only if VendorAttributesAllowed=true ]
4422/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004423/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004424/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004425///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004426void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4427 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004428 bool CXX11AttributesAllowed,
4429 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004430 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004431 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004432 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004433 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004434 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004435 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004436
4437 SourceLocation EndLoc;
4438
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004439 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004440 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004441 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004442 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004443 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004444
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004445 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004446 case tok::code_completion:
4447 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004448 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004449
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004450 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004451 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004452 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004453 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004454 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004455 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004456 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004457 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004458 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004459 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004460 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004461 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004462 case tok::kw__Atomic:
4463 if (!AtomicAllowed)
4464 goto DoneWithTypeQuals;
4465 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4466 getLangOpts());
4467 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004468
4469 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004470 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004471 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004472 goto DoneWithTypeQuals;
4473 case tok::kw___private:
4474 case tok::kw___global:
4475 case tok::kw___local:
4476 case tok::kw___constant:
4477 case tok::kw___read_only:
4478 case tok::kw___write_only:
4479 case tok::kw___read_write:
4480 ParseOpenCLQualifiers(DS);
4481 break;
4482
Aaron Ballman317a77f2013-05-22 23:25:32 +00004483 case tok::kw___sptr:
4484 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004485 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004486 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004487 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004488 case tok::kw___cdecl:
4489 case tok::kw___stdcall:
4490 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004491 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004492 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004493 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004494 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004495 continue;
4496 }
4497 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004498 case tok::kw___pascal:
4499 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004500 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004501 continue;
4502 }
4503 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004504 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004505 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004506 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004507 continue; // do *not* consume the next token!
4508 }
4509 // otherwise, FALL THROUGH!
4510 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004511 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004512 // If this is not a type-qualifier token, we're done reading type
4513 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004514 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004515 if (EndLoc.isValid())
4516 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004517 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004518 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004519
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004520 // If the specifier combination wasn't legal, issue a diagnostic.
4521 if (isInvalid) {
4522 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004523 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004524 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004525 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004526 }
4527}
4528
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004529
4530/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4531///
4532void Parser::ParseDeclarator(Declarator &D) {
4533 /// This implements the 'declarator' production in the C grammar, then checks
4534 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004535 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004536}
4537
Richard Smith0efa75c2012-03-29 01:16:42 +00004538static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4539 if (Kind == tok::star || Kind == tok::caret)
4540 return true;
4541
4542 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4543 if (!Lang.CPlusPlus)
4544 return false;
4545
4546 return Kind == tok::amp || Kind == tok::ampamp;
4547}
4548
Sebastian Redlbd150f42008-11-21 19:14:01 +00004549/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4550/// is parsed by the function passed to it. Pass null, and the direct-declarator
4551/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004552/// ptr-operator production.
4553///
Richard Smith09f76ee2011-10-19 21:33:05 +00004554/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004555/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4556/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004557///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004558/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4559/// [C] pointer[opt] direct-declarator
4560/// [C++] direct-declarator
4561/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004562///
4563/// pointer: [C99 6.7.5]
4564/// '*' type-qualifier-list[opt]
4565/// '*' type-qualifier-list[opt] pointer
4566///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004567/// ptr-operator:
4568/// '*' cv-qualifier-seq[opt]
4569/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004570/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004571/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004572/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004573/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004574void Parser::ParseDeclaratorInternal(Declarator &D,
4575 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004576 if (Diags.hasAllExtensionsSilenced())
4577 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004578
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004579 // C++ member pointers start with a '::' or a nested-name.
4580 // Member pointers get special handling, since there's no place for the
4581 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004582 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004583 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4584 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004585 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4586 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004587 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004588 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004589
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004590 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004591 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004592 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004593 if (D.mayHaveIdentifier())
4594 D.getCXXScopeSpec() = SS;
4595 else
4596 AnnotateScopeToken(SS, true);
4597
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004598 if (DirectDeclParser)
4599 (this->*DirectDeclParser)(D);
4600 return;
4601 }
4602
4603 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004604 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004605 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004606 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004607 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004608
4609 // Recurse to parse whatever is left.
4610 ParseDeclaratorInternal(D, DirectDeclParser);
4611
4612 // Sema will have to catch (syntactically invalid) pointers into global
4613 // scope. It has to catch pointers into namespace scope anyway.
4614 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004615 Loc),
4616 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004617 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004618 return;
4619 }
4620 }
4621
4622 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004623 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004624 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004625 if (DirectDeclParser)
4626 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004627 return;
4628 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004629
Sebastian Redled0f3b02009-03-15 22:02:01 +00004630 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4631 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004632 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004633 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004634
Chris Lattner9eac9312009-03-27 04:18:06 +00004635 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004636 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004637 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004638
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004639 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004640 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004641 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004642
Bill Wendling3708c182007-05-27 10:15:43 +00004643 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004644 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004645 if (Kind == tok::star)
4646 // Remember that we parsed a pointer type, and remember the type-quals.
4647 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004648 DS.getConstSpecLoc(),
4649 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004650 DS.getRestrictSpecLoc()),
4651 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004652 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004653 else
4654 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004655 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004656 Loc),
4657 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004658 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004659 } else {
4660 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004661 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004662
Sebastian Redl3b27be62009-03-23 00:00:23 +00004663 // Complain about rvalue references in C++03, but then go on and build
4664 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004665 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004666 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004667 diag::warn_cxx98_compat_rvalue_reference :
4668 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004669
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004670 // GNU-style and C++11 attributes are allowed here, as is restrict.
4671 ParseTypeQualifierListOpt(DS);
4672 D.ExtendWithDeclSpec(DS);
4673
Bill Wendling93efb222007-06-02 23:28:54 +00004674 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4675 // cv-qualifiers are introduced through the use of a typedef or of a
4676 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004677 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4678 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4679 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004680 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004681 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4682 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004683 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004684 // 'restrict' is permitted as an extension.
4685 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4686 Diag(DS.getAtomicSpecLoc(),
4687 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004688 }
Bill Wendling3708c182007-05-27 10:15:43 +00004689
4690 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004691 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004692
Douglas Gregor66583c52008-11-03 15:51:28 +00004693 if (D.getNumTypeObjects() > 0) {
4694 // C++ [dcl.ref]p4: There shall be no references to references.
4695 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4696 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004697 if (const IdentifierInfo *II = D.getIdentifier())
4698 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4699 << II;
4700 else
4701 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4702 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004703
Sebastian Redlbd150f42008-11-21 19:14:01 +00004704 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004705 // can go ahead and build the (technically ill-formed)
4706 // declarator: reference collapsing will take care of it.
4707 }
4708 }
4709
Richard Smith8e1ac332013-03-28 01:55:44 +00004710 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004711 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004712 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004713 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004714 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004715 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004716}
4717
Richard Smith0efa75c2012-03-29 01:16:42 +00004718static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4719 SourceLocation EllipsisLoc) {
4720 if (EllipsisLoc.isValid()) {
4721 FixItHint Insertion;
4722 if (!D.getEllipsisLoc().isValid()) {
4723 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4724 D.setEllipsisLoc(EllipsisLoc);
4725 }
4726 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4727 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4728 }
4729}
4730
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004731/// ParseDirectDeclarator
4732/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004733/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004734/// '(' declarator ')'
4735/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004736/// [C90] direct-declarator '[' constant-expression[opt] ']'
4737/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4738/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4739/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4740/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004741/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4742/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004743/// direct-declarator '(' parameter-type-list ')'
4744/// direct-declarator '(' identifier-list[opt] ')'
4745/// [GNU] direct-declarator '(' parameter-forward-declarations
4746/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004747/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4748/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004749/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4750/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4751/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004752/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004753/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004754///
4755/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004756/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004757/// '::'[opt] nested-name-specifier[opt] type-name
4758///
4759/// id-expression: [C++ 5.1]
4760/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004761/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004762///
4763/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004764/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004765/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004766/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004767/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004768/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004769///
Richard Smith1453e312012-03-27 01:42:32 +00004770/// Note, any additional constructs added here may need corresponding changes
4771/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004772void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004773 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004774
David Blaikiebbafb8a2012-03-11 07:00:24 +00004775 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004776 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004777 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004778 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4779 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004780 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004781 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004782 }
4783
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004784 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004785 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004786 // Change the declaration context for name lookup, until this function
4787 // is exited (and the declarator has been parsed).
4788 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004789 }
4790
Douglas Gregor27b4c162010-12-23 22:44:42 +00004791 // C++0x [dcl.fct]p14:
4792 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004793 // of a parameter-declaration-clause without a preceding comma. In
4794 // this case, the ellipsis is parsed as part of the
4795 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004796 // parameter pack that has not been expanded; otherwise, it is parsed
4797 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004798 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004799 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004800 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004801 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004802 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004803 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004804 !Actions.containsUnexpandedParameterPacks(D))) {
4805 SourceLocation EllipsisLoc = ConsumeToken();
4806 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4807 // The ellipsis was put in the wrong place. Recover, and explain to
4808 // the user what they should have done.
4809 ParseDeclarator(D);
4810 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4811 return;
4812 } else
4813 D.setEllipsisLoc(EllipsisLoc);
4814
4815 // The ellipsis can't be followed by a parenthesized declarator. We
4816 // check for that in ParseParenDeclarator, after we have disambiguated
4817 // the l_paren token.
4818 }
4819
Douglas Gregor7861a802009-11-03 01:35:08 +00004820 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4821 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4822 // We found something that indicates the start of an unqualified-id.
4823 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004824 bool AllowConstructorName;
4825 if (D.getDeclSpec().hasTypeSpecifier())
4826 AllowConstructorName = false;
4827 else if (D.getCXXScopeSpec().isSet())
4828 AllowConstructorName =
4829 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004830 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004831 else
4832 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4833
Abramo Bagnara7945c982012-01-27 09:46:47 +00004834 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004835 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4836 /*EnteringContext=*/true,
4837 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004838 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004839 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004840 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004841 D.getName()) ||
4842 // Once we're past the identifier, if the scope was bad, mark the
4843 // whole declarator bad.
4844 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004845 D.SetIdentifier(0, Tok.getLocation());
4846 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004847 } else {
4848 // Parsed the unqualified-id; update range information and move along.
4849 if (D.getSourceRange().getBegin().isInvalid())
4850 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4851 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004852 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004853 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004854 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004855 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004856 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004857 "There's a C++-specific check for tok::identifier above");
4858 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4859 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4860 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004861 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004862 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004863 // A virt-specifier isn't treated as an identifier if it appears after a
4864 // trailing-return-type.
4865 if (D.getContext() != Declarator::TrailingReturnContext ||
4866 !isCXX11VirtSpecifier(Tok)) {
4867 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4868 << FixItHint::CreateRemoval(Tok.getLocation());
4869 D.SetIdentifier(0, Tok.getLocation());
4870 ConsumeToken();
4871 goto PastIdentifier;
4872 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004873 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004874
Douglas Gregor7861a802009-11-03 01:35:08 +00004875 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004876 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004877 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004878 // Example: 'char (*X)' or 'int (*XX)(void)'
4879 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004880
4881 // If the declarator was parenthesized, we entered the declarator
4882 // scope when parsing the parenthesized declarator, then exited
4883 // the scope already. Re-enter the scope, if we need to.
4884 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004885 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004886 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004887 if (!D.isInvalidType() &&
4888 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004889 // Change the declaration context for name lookup, until this function
4890 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004891 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004892 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004893 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004894 // This could be something simple like "int" (in which case the declarator
4895 // portion is empty), if an abstract-declarator is allowed.
4896 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004897
4898 // The grammar for abstract-pack-declarator does not allow grouping parens.
4899 // FIXME: Revisit this once core issue 1488 is resolved.
4900 if (D.hasEllipsis() && D.hasGroupingParens())
4901 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4902 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004903 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004904 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004905 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004906 if (D.getContext() == Declarator::MemberContext)
4907 Diag(Tok, diag::err_expected_member_name_or_semi)
4908 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004909 else if (getLangOpts().CPlusPlus) {
4910 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4911 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004912 else {
4913 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4914 if (Tok.isAtStartOfLine() && Loc.isValid())
4915 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4916 << getLangOpts().CPlusPlus;
4917 else
4918 Diag(Tok, diag::err_expected_unqualified_id)
4919 << getLangOpts().CPlusPlus;
4920 }
Richard Trieu9c672672013-01-26 02:31:38 +00004921 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004922 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004923 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004924 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004925 }
Mike Stump11289f42009-09-09 15:08:12 +00004926
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004927 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004928 assert(D.isPastIdentifier() &&
4929 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004930
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004931 // Don't parse attributes unless we have parsed an unparenthesized name.
4932 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004933 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004934
Chris Lattneracd58a32006-08-06 17:24:14 +00004935 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004936 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004937 // Enter function-declaration scope, limiting any declarators to the
4938 // function prototype scope, including parameter declarators.
4939 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004940 Scope::FunctionPrototypeScope|Scope::DeclScope|
4941 (D.isFunctionDeclaratorAFunctionDeclaration()
4942 ? Scope::FunctionDeclarationScope : 0));
4943
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004944 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4945 // In such a case, check if we actually have a function declarator; if it
4946 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004947 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004948 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4949 // The name of the declarator, if any, is tentatively declared within
4950 // a possible direct initializer.
4951 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4952 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4953 TentativelyDeclaredIdentifiers.pop_back();
4954 if (!IsFunctionDecl)
4955 break;
4956 }
John McCall084e83d2011-03-24 11:26:52 +00004957 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004958 BalancedDelimiterTracker T(*this, tok::l_paren);
4959 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004960 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004961 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004962 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004963 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004964 } else {
4965 break;
4966 }
4967 }
Chad Rosierc1183952012-06-26 22:30:43 +00004968}
Chris Lattneracd58a32006-08-06 17:24:14 +00004969
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004970/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4971/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004972/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004973/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4974///
4975/// direct-declarator:
4976/// '(' declarator ')'
4977/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004978/// direct-declarator '(' parameter-type-list ')'
4979/// direct-declarator '(' identifier-list[opt] ')'
4980/// [GNU] direct-declarator '(' parameter-forward-declarations
4981/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004982///
4983void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004984 BalancedDelimiterTracker T(*this, tok::l_paren);
4985 T.consumeOpen();
4986
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004987 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004988
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004989 // Eat any attributes before we look at whether this is a grouping or function
4990 // declarator paren. If this is a grouping paren, the attribute applies to
4991 // the type being built up, for example:
4992 // int (__attribute__(()) *x)(long y)
4993 // If this ends up not being a grouping paren, the attribute applies to the
4994 // first argument, for example:
4995 // int (__attribute__(()) int x)
4996 // In either case, we need to eat any attributes to be able to determine what
4997 // sort of paren this is.
4998 //
John McCall084e83d2011-03-24 11:26:52 +00004999 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005000 bool RequiresArg = false;
5001 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005002 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005003
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005004 // We require that the argument list (if this is a non-grouping paren) be
5005 // present even if the attribute list was empty.
5006 RequiresArg = true;
5007 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005008
Steve Naroff44ac7772008-12-25 14:16:32 +00005009 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005010 ParseMicrosoftTypeAttributes(attrs);
5011
Dawn Perchik335e16b2010-09-03 01:29:35 +00005012 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005013 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005014 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005015
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005016 // If we haven't past the identifier yet (or where the identifier would be
5017 // stored, if this is an abstract declarator), then this is probably just
5018 // grouping parens. However, if this could be an abstract-declarator, then
5019 // this could also be the start of function arguments (consider 'void()').
5020 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005021
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005022 if (!D.mayOmitIdentifier()) {
5023 // If this can't be an abstract-declarator, this *must* be a grouping
5024 // paren, because we haven't seen the identifier yet.
5025 isGrouping = true;
5026 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005027 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5028 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005029 isDeclarationSpecifier() || // 'int(int)' is a function.
5030 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005031 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5032 // considered to be a type, not a K&R identifier-list.
5033 isGrouping = false;
5034 } else {
5035 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5036 isGrouping = true;
5037 }
Mike Stump11289f42009-09-09 15:08:12 +00005038
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005039 // If this is a grouping paren, handle:
5040 // direct-declarator: '(' declarator ')'
5041 // direct-declarator: '(' attributes declarator ')'
5042 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005043 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5044 D.setEllipsisLoc(SourceLocation());
5045
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005046 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005047 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005048 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005049 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005050 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005051 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005052 T.getCloseLocation()),
5053 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005054
5055 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005056
5057 // An ellipsis cannot be placed outside parentheses.
5058 if (EllipsisLoc.isValid())
5059 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5060
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005061 return;
5062 }
Mike Stump11289f42009-09-09 15:08:12 +00005063
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005064 // Okay, if this wasn't a grouping paren, it must be the start of a function
5065 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005066 // identifier (and remember where it would have been), then call into
5067 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005068 D.SetIdentifier(0, Tok.getLocation());
5069
David Blaikie15a430a2011-12-04 05:04:18 +00005070 // Enter function-declaration scope, limiting any declarators to the
5071 // function prototype scope, including parameter declarators.
5072 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005073 Scope::FunctionPrototypeScope | Scope::DeclScope |
5074 (D.isFunctionDeclaratorAFunctionDeclaration()
5075 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005076 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005077 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005078}
5079
5080/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5081/// declarator D up to a paren, which indicates that we are parsing function
5082/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005083///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005084/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5085/// immediately after the open paren - they should be considered to be the
5086/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005087///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005088/// If RequiresArg is true, then the first argument of the function is required
5089/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005090///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005091/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5092/// (C++11) ref-qualifier[opt], exception-specification[opt],
5093/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5094///
5095/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005096/// dynamic-exception-specification
5097/// noexcept-specification
5098///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005099void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005100 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005101 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005102 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005103 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005104 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005105 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005106 // lparen is already consumed!
5107 assert(D.isPastIdentifier() && "Should not call before identifier!");
5108
5109 // This should be true when the function has typed arguments.
5110 // Otherwise, it is treated as a K&R-style function.
5111 bool HasProto = false;
5112 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005113 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005114 // Remember where we see an ellipsis, if any.
5115 SourceLocation EllipsisLoc;
5116
5117 DeclSpec DS(AttrFactory);
5118 bool RefQualifierIsLValueRef = true;
5119 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005120 SourceLocation ConstQualifierLoc;
5121 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005122 ExceptionSpecificationType ESpecType = EST_None;
5123 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005124 SmallVector<ParsedType, 2> DynamicExceptions;
5125 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005126 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005127 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005128 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005129
James Molloy6f8780b2012-02-29 10:24:19 +00005130 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005131 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5132 EndLoc is the end location for the function declarator.
5133 They differ for trailing return types. */
5134 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005135 SourceLocation LParenLoc, RParenLoc;
5136 LParenLoc = Tracker.getOpenLocation();
5137 StartLoc = LParenLoc;
5138
Douglas Gregor9e66af42011-07-05 16:44:18 +00005139 if (isFunctionDeclaratorIdentifierList()) {
5140 if (RequiresArg)
5141 Diag(Tok, diag::err_argument_required_after_attribute);
5142
5143 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5144
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005145 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005146 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005147 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005148 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005149 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005150 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005151 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5152 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005153 else if (RequiresArg)
5154 Diag(Tok, diag::err_argument_required_after_attribute);
5155
David Blaikiebbafb8a2012-03-11 07:00:24 +00005156 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005157
5158 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005159 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005160 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005161 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005162 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005163
David Blaikiebbafb8a2012-03-11 07:00:24 +00005164 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005165 // FIXME: Accept these components in any order, and produce fixits to
5166 // correct the order if the user gets it wrong. Ideally we should deal
5167 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005168
5169 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005170 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5171 /*CXX11AttributesAllowed*/ false,
5172 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005173 if (!DS.getSourceRange().getEnd().isInvalid()) {
5174 EndLoc = DS.getSourceRange().getEnd();
5175 ConstQualifierLoc = DS.getConstSpecLoc();
5176 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5177 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005178
5179 // Parse ref-qualifier[opt].
5180 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005181 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005182 diag::warn_cxx98_compat_ref_qualifier :
5183 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005184
Douglas Gregor9e66af42011-07-05 16:44:18 +00005185 RefQualifierIsLValueRef = Tok.is(tok::amp);
5186 RefQualifierLoc = ConsumeToken();
5187 EndLoc = RefQualifierLoc;
5188 }
5189
Douglas Gregor3024f072012-04-16 07:05:22 +00005190 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005191 // If a declaration declares a member function or member function
5192 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005193 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005194 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005195 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005196 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005197 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005198 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005199 (D.getContext() == Declarator::MemberContext
5200 ? !D.getDeclSpec().isFriendSpecified()
5201 : D.getContext() == Declarator::FileContext &&
5202 D.getCXXScopeSpec().isValid() &&
5203 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005204 Sema::CXXThisScopeRAII ThisScope(Actions,
5205 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005206 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005207 (D.getDeclSpec().isConstexprSpecified() &&
5208 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005209 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005210 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005211
Douglas Gregor9e66af42011-07-05 16:44:18 +00005212 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005213 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005214 DynamicExceptions,
5215 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005216 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005217 if (ESpecType != EST_None)
5218 EndLoc = ESpecRange.getEnd();
5219
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005220 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5221 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005222 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005223
Douglas Gregor9e66af42011-07-05 16:44:18 +00005224 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005225 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005226 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005227 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005228 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5229 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005230 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005231 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005232 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005233 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005234 }
5235 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005236 }
5237
5238 // Remember that we parsed a function type, and remember the attributes.
5239 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005240 IsAmbiguous,
5241 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005242 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005243 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005244 DS.getTypeQualifiers(),
5245 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005246 RefQualifierLoc, ConstQualifierLoc,
5247 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005248 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005249 ESpecType, ESpecRange.getBegin(),
5250 DynamicExceptions.data(),
5251 DynamicExceptionRanges.data(),
5252 DynamicExceptions.size(),
5253 NoexceptExpr.isUsable() ?
5254 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005255 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005256 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005257 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005258
5259 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005260}
5261
5262/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5263/// identifier list form for a K&R-style function: void foo(a,b,c)
5264///
5265/// Note that identifier-lists are only allowed for normal declarators, not for
5266/// abstract-declarators.
5267bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005268 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005269 && Tok.is(tok::identifier)
5270 && !TryAltiVecVectorToken()
5271 // K&R identifier lists can't have typedefs as identifiers, per C99
5272 // 6.7.5.3p11.
5273 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5274 // Identifier lists follow a really simple grammar: the identifiers can
5275 // be followed *only* by a ", identifier" or ")". However, K&R
5276 // identifier lists are really rare in the brave new modern world, and
5277 // it is very common for someone to typo a type in a non-K&R style
5278 // list. If we are presented with something like: "void foo(intptr x,
5279 // float y)", we don't want to start parsing the function declarator as
5280 // though it is a K&R style declarator just because intptr is an
5281 // invalid type.
5282 //
5283 // To handle this, we check to see if the token after the first
5284 // identifier is a "," or ")". Only then do we parse it as an
5285 // identifier list.
5286 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5287}
5288
5289/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5290/// we found a K&R-style identifier list instead of a typed parameter list.
5291///
5292/// After returning, ParamInfo will hold the parsed parameters.
5293///
5294/// identifier-list: [C99 6.7.5]
5295/// identifier
5296/// identifier-list ',' identifier
5297///
5298void Parser::ParseFunctionDeclaratorIdentifierList(
5299 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005300 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005301 // If there was no identifier specified for the declarator, either we are in
5302 // an abstract-declarator, or we are in a parameter declarator which was found
5303 // to be abstract. In abstract-declarators, identifier lists are not valid:
5304 // diagnose this.
5305 if (!D.getIdentifier())
5306 Diag(Tok, diag::ext_ident_list_in_param);
5307
5308 // Maintain an efficient lookup of params we have seen so far.
5309 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5310
5311 while (1) {
5312 // If this isn't an identifier, report the error and skip until ')'.
5313 if (Tok.isNot(tok::identifier)) {
5314 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005315 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005316 // Forget we parsed anything.
5317 ParamInfo.clear();
5318 return;
5319 }
5320
5321 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5322
5323 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5324 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5325 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5326
5327 // Verify that the argument identifier has not already been mentioned.
5328 if (!ParamsSoFar.insert(ParmII)) {
5329 Diag(Tok, diag::err_param_redefinition) << ParmII;
5330 } else {
5331 // Remember this identifier in ParamInfo.
5332 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5333 Tok.getLocation(),
5334 0));
5335 }
5336
5337 // Eat the identifier.
5338 ConsumeToken();
5339
5340 // The list continues if we see a comma.
5341 if (Tok.isNot(tok::comma))
5342 break;
5343 ConsumeToken();
5344 }
5345}
5346
5347/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5348/// after the opening parenthesis. This function will not parse a K&R-style
5349/// identifier list.
5350///
Richard Smith2620cd92012-04-11 04:01:28 +00005351/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5352/// caller parsed those arguments immediately after the open paren - they should
5353/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005354///
5355/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5356/// be the location of the ellipsis, if any was parsed.
5357///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005358/// parameter-type-list: [C99 6.7.5]
5359/// parameter-list
5360/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005361/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005362///
5363/// parameter-list: [C99 6.7.5]
5364/// parameter-declaration
5365/// parameter-list ',' parameter-declaration
5366///
5367/// parameter-declaration: [C99 6.7.5]
5368/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005369/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005370/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005371/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005372/// declaration-specifiers abstract-declarator[opt]
5373/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005374/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005375/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005376/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005377///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005378void Parser::ParseParameterDeclarationClause(
5379 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005380 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005381 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005382 SourceLocation &EllipsisLoc) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005383 while (1) {
5384 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005385 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5386 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005387 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005388 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005389 }
Mike Stump11289f42009-09-09 15:08:12 +00005390
Chris Lattner371ed4e2008-04-06 06:57:35 +00005391 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005392 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005393 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005394
Richard Smith2620cd92012-04-11 04:01:28 +00005395 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005396 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005397
John McCall53fa7142010-12-24 02:08:15 +00005398 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005399 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005400
5401 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005402
5403 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005404 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005405 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005406 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5407 // too much hassle.
5408 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005409
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005410 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005411
Faisal Vali2b391ab2013-09-26 19:54:12 +00005412
5413 // Parse the declarator. This is "PrototypeContext" or
5414 // "LambdaExprParameterContext", because we must accept either
5415 // 'declarator' or 'abstract-declarator' here.
5416 Declarator ParmDeclarator(DS,
5417 D.getContext() == Declarator::LambdaExprContext ?
5418 Declarator::LambdaExprParameterContext :
5419 Declarator::PrototypeContext);
5420 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005421
5422 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005423 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005424
Chris Lattner371ed4e2008-04-06 06:57:35 +00005425 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005426 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005427
Douglas Gregor4d87df52008-12-16 21:30:33 +00005428 // DefArgToks is used when the parsing of default arguments needs
5429 // to be delayed.
5430 CachedTokens *DefArgToks = 0;
5431
Chris Lattner371ed4e2008-04-06 06:57:35 +00005432 // If no parameter was specified, verify that *something* was specified,
5433 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005434 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5435 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005436 // Completely missing, emit error.
5437 Diag(DSStart, diag::err_missing_param);
5438 } else {
5439 // Otherwise, we have something. Add it and let semantic analysis try
5440 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005441
Chris Lattner371ed4e2008-04-06 06:57:35 +00005442 // Inform the actions module about the parameter declarator, so it gets
5443 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005444 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5445 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005446 // Parse the default argument, if any. We parse the default
5447 // arguments in all dialects; the semantic analysis in
5448 // ActOnParamDefaultArgument will reject the default argument in
5449 // C.
5450 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005451 SourceLocation EqualLoc = Tok.getLocation();
5452
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005453 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005454 if (D.getContext() == Declarator::MemberContext) {
5455 // If we're inside a class definition, cache the tokens
5456 // corresponding to the default argument. We'll actually parse
5457 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005458 // FIXME: Can we use a smart pointer for Toks?
5459 DefArgToks = new CachedTokens;
5460
Richard Smith1fff95c2013-09-12 23:28:08 +00005461 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005462 delete DefArgToks;
5463 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005464 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005465 } else {
5466 // Mark the end of the default argument so that we know when to
5467 // stop when we parse it later on.
5468 Token DefArgEnd;
5469 DefArgEnd.startToken();
5470 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5471 DefArgEnd.setLocation(Tok.getLocation());
5472 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005473 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005474 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005475 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005476 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005477 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005478 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005479
Chad Rosierc1183952012-06-26 22:30:43 +00005480 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005481 // used.
5482 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005483 Sema::PotentiallyEvaluatedIfUsed,
5484 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005485
Sebastian Redldb63af22012-03-14 15:54:00 +00005486 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005487 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005488 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005489 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005490 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005491 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005492 if (DefArgResult.isInvalid()) {
5493 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005494 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005495 } else {
5496 // Inform the actions module about the default argument
5497 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005498 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005499 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005500 }
5501 }
Mike Stump11289f42009-09-09 15:08:12 +00005502
5503 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005504 ParmDeclarator.getIdentifierLoc(),
5505 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005506 }
5507
5508 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005509 if (Tok.isNot(tok::comma)) {
5510 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005511 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005512
David Blaikiebbafb8a2012-03-11 07:00:24 +00005513 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005514 // We have ellipsis without a preceding ',', which is ill-formed
5515 // in C. Complain and provide the fix.
5516 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005517 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005518 }
5519 }
Chad Rosierc1183952012-06-26 22:30:43 +00005520
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005521 break;
5522 }
Mike Stump11289f42009-09-09 15:08:12 +00005523
Chris Lattner371ed4e2008-04-06 06:57:35 +00005524 // Consume the comma.
5525 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005526 }
Mike Stump11289f42009-09-09 15:08:12 +00005527
Chris Lattner6c940e62008-04-06 06:34:08 +00005528}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005529
Chris Lattnere8074e62006-08-06 18:30:15 +00005530/// [C90] direct-declarator '[' constant-expression[opt] ']'
5531/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5532/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5533/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5534/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005535/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5536/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005537void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005538 if (CheckProhibitedCXX11Attribute())
5539 return;
5540
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005541 BalancedDelimiterTracker T(*this, tok::l_square);
5542 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005543
Chris Lattner84a11622008-12-18 07:27:21 +00005544 // C array syntax has many features, but by-far the most common is [] and [4].
5545 // This code does a fast path to handle some of the most obvious cases.
5546 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005547 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005548 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005549 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005550
Chris Lattner84a11622008-12-18 07:27:21 +00005551 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005552 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005553 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005554 T.getOpenLocation(),
5555 T.getCloseLocation()),
5556 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005557 return;
5558 } else if (Tok.getKind() == tok::numeric_constant &&
5559 GetLookAheadToken(1).is(tok::r_square)) {
5560 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005561 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005562 ConsumeToken();
5563
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005564 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005565 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005566 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005567
Chris Lattner84a11622008-12-18 07:27:21 +00005568 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005569 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005570 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005571 T.getOpenLocation(),
5572 T.getCloseLocation()),
5573 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005574 return;
5575 }
Mike Stump11289f42009-09-09 15:08:12 +00005576
Chris Lattnere8074e62006-08-06 18:30:15 +00005577 // If valid, this location is the position where we read the 'static' keyword.
5578 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005579 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005580 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005581
Chris Lattnere8074e62006-08-06 18:30:15 +00005582 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005583 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005584 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005585 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005586
Chris Lattnere8074e62006-08-06 18:30:15 +00005587 // If we haven't already read 'static', check to see if there is one after the
5588 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005589 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005590 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005591
Chris Lattnere8074e62006-08-06 18:30:15 +00005592 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005593 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005594 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005595
Chris Lattner521ff2b2008-04-06 05:26:30 +00005596 // Handle the case where we have '[*]' as the array size. However, a leading
5597 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005598 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005599 // infrequent, use of lookahead is not costly here.
5600 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005601 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005602
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005603 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005604 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005605 StaticLoc = SourceLocation(); // Drop the static.
5606 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005607 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005608 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005609 // Note, in C89, this production uses the constant-expr production instead
5610 // of assignment-expr. The only difference is that assignment-expr allows
5611 // things like '=' and '*='. Sema rejects these in C89 mode because they
5612 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005613
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005614 // Parse the constant-expression or assignment-expression now (depending
5615 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005616 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005617 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005618 } else {
5619 EnterExpressionEvaluationContext Unevaluated(Actions,
5620 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005621 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005622 }
Chris Lattner62591722006-08-12 18:40:58 +00005623 }
Mike Stump11289f42009-09-09 15:08:12 +00005624
Chris Lattner62591722006-08-12 18:40:58 +00005625 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005626 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005627 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005628 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005629 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005630 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005631 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005632
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005633 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005634
John McCall084e83d2011-03-24 11:26:52 +00005635 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005636 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005637
Chris Lattner84a11622008-12-18 07:27:21 +00005638 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005639 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005640 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005641 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005642 T.getOpenLocation(),
5643 T.getCloseLocation()),
5644 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005645}
5646
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005647/// [GNU] typeof-specifier:
5648/// typeof ( expressions )
5649/// typeof ( type-name )
5650/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005651///
5652void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005653 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005654 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005655 SourceLocation StartLoc = ConsumeToken();
5656
John McCalle8595032010-01-13 20:03:27 +00005657 const bool hasParens = Tok.is(tok::l_paren);
5658
Eli Friedman15681d62012-09-26 04:34:21 +00005659 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5660 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005661
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005662 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005663 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005664 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005665 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5666 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005667 if (hasParens)
5668 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005669
5670 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005671 // FIXME: Not accurate, the range gets one token more than it should.
5672 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005673 else
5674 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005675
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005676 if (isCastExpr) {
5677 if (!CastTy) {
5678 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005679 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005680 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005681
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005682 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005683 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005684 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5685 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005686 DiagID, CastTy))
5687 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005688 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005689 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005690
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005691 // If we get here, the operand to the typeof was an expresion.
5692 if (Operand.isInvalid()) {
5693 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005694 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005695 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005696
Eli Friedmane0afc982012-01-21 01:01:51 +00005697 // We might need to transform the operand if it is potentially evaluated.
5698 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5699 if (Operand.isInvalid()) {
5700 DS.SetTypeSpecError();
5701 return;
5702 }
5703
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005704 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005705 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005706 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5707 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005708 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005709 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005710}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005711
Benjamin Kramere56f3932011-12-23 17:00:35 +00005712/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005713/// _Atomic ( type-name )
5714///
5715void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005716 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5717 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005718
5719 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005720 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005721 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005722 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005723
5724 TypeResult Result = ParseTypeName();
5725 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005726 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005727 return;
5728 }
5729
5730 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005731 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005732
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005733 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005734 return;
5735
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005736 DS.setTypeofParensRange(T.getRange());
5737 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005738
5739 const char *PrevSpec = 0;
5740 unsigned DiagID;
5741 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5742 DiagID, Result.release()))
5743 Diag(StartLoc, DiagID) << PrevSpec;
5744}
5745
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005746
5747/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5748/// from TryAltiVecVectorToken.
5749bool Parser::TryAltiVecVectorTokenOutOfLine() {
5750 Token Next = NextToken();
5751 switch (Next.getKind()) {
5752 default: return false;
5753 case tok::kw_short:
5754 case tok::kw_long:
5755 case tok::kw_signed:
5756 case tok::kw_unsigned:
5757 case tok::kw_void:
5758 case tok::kw_char:
5759 case tok::kw_int:
5760 case tok::kw_float:
5761 case tok::kw_double:
5762 case tok::kw_bool:
5763 case tok::kw___pixel:
5764 Tok.setKind(tok::kw___vector);
5765 return true;
5766 case tok::identifier:
5767 if (Next.getIdentifierInfo() == Ident_pixel) {
5768 Tok.setKind(tok::kw___vector);
5769 return true;
5770 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005771 if (Next.getIdentifierInfo() == Ident_bool) {
5772 Tok.setKind(tok::kw___vector);
5773 return true;
5774 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005775 return false;
5776 }
5777}
5778
5779bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5780 const char *&PrevSpec, unsigned &DiagID,
5781 bool &isInvalid) {
5782 if (Tok.getIdentifierInfo() == Ident_vector) {
5783 Token Next = NextToken();
5784 switch (Next.getKind()) {
5785 case tok::kw_short:
5786 case tok::kw_long:
5787 case tok::kw_signed:
5788 case tok::kw_unsigned:
5789 case tok::kw_void:
5790 case tok::kw_char:
5791 case tok::kw_int:
5792 case tok::kw_float:
5793 case tok::kw_double:
5794 case tok::kw_bool:
5795 case tok::kw___pixel:
5796 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5797 return true;
5798 case tok::identifier:
5799 if (Next.getIdentifierInfo() == Ident_pixel) {
5800 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5801 return true;
5802 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005803 if (Next.getIdentifierInfo() == Ident_bool) {
5804 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5805 return true;
5806 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005807 break;
5808 default:
5809 break;
5810 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005811 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005812 DS.isTypeAltiVecVector()) {
5813 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5814 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005815 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5816 DS.isTypeAltiVecVector()) {
5817 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5818 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005819 }
5820 return false;
5821}