blob: 944942658ab2e9c7cf20512586184de2b0bfe480 [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.
Aaron Ballman66037472013-12-04 15:32:26 +0000291 if (AttrKind == AttributeList::UnknownAttribute ||
292 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000293 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000294 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000295 }
Richard Smithb12bf692011-10-17 21:20:17 +0000296
Richard Smithb1f9a282013-10-31 01:56:18 +0000297 if (IsIdentifierArg)
298 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000299 }
300
Richard Smithb1f9a282013-10-31 01:56:18 +0000301 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000302 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000303 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000304 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000305
Richard Smithb12bf692011-10-17 21:20:17 +0000306 // Parse the non-empty comma-separated list of expressions.
307 while (1) {
308 ExprResult ArgExpr(ParseAssignmentExpression());
309 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000310 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000311 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000312 }
Richard Smithb12bf692011-10-17 21:20:17 +0000313 ArgExprs.push_back(ArgExpr.release());
314 if (Tok.isNot(tok::comma))
315 break;
316 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000317 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000318 }
Richard Smithb12bf692011-10-17 21:20:17 +0000319
320 SourceLocation RParen = Tok.getLocation();
Richard Smithb1f9a282013-10-31 01:56:18 +0000321 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000322 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000323 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
324 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000325 }
326}
327
Chad Rosierc1183952012-06-26 22:30:43 +0000328/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000329/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000330void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000331 SourceLocation AttrNameLoc,
332 ParsedAttributes &Attrs)
333{
334 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000335 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000336 AttrName->getNameStart(), tok::r_paren))
337 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000338
Aaron Ballman478faed2012-06-19 22:09:27 +0000339 ExprResult ArgExpr(ParseConstantExpression());
340 if (ArgExpr.isInvalid()) {
341 T.skipToEnd();
342 return;
343 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000344 ArgsUnion ExprList = ArgExpr.take();
345 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
346 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000347
348 T.consumeClose();
349}
350
Chad Rosierc1183952012-06-26 22:30:43 +0000351/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000352/// arguments.
353bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
354 return llvm::StringSwitch<bool>(Ident->getName())
355 .Case("dllimport", true)
356 .Case("dllexport", true)
357 .Case("noreturn", true)
358 .Case("nothrow", true)
359 .Case("noinline", true)
360 .Case("naked", true)
361 .Case("appdomain", true)
362 .Case("process", true)
363 .Case("jitintrinsic", true)
364 .Case("noalias", true)
365 .Case("restrict", true)
366 .Case("novtable", true)
367 .Case("selectany", true)
368 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000369 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000370 .Default(false);
371}
372
Chad Rosierc1183952012-06-26 22:30:43 +0000373/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000374/// parameters). Will return false if we properly handled the declspec, or
375/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000376void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000377 SourceLocation Loc,
378 ParsedAttributes &Attrs) {
379 // Try to handle the easy case first -- these declspecs all take a single
380 // parameter as their argument.
381 if (llvm::StringSwitch<bool>(Ident->getName())
382 .Case("uuid", true)
383 .Case("align", true)
384 .Case("allocate", true)
385 .Default(false)) {
386 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
387 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000388 // The deprecated declspec has an optional single argument, so we will
389 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000390 // not.
391 if (Tok.getKind() == tok::l_paren)
392 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
393 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000394 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000395 } else if (Ident->getName() == "property") {
396 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000397 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000398 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000399 if (Tok.isNot(tok::l_paren)) {
400 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
401 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000402 return;
John McCall5e77d762013-04-16 07:28:30 +0000403 }
404 BalancedDelimiterTracker T(*this, tok::l_paren);
405 T.expectAndConsume(diag::err_expected_lparen_after,
406 Ident->getNameStart(), tok::r_paren);
407
408 enum AccessorKind {
409 AK_Invalid = -1,
410 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
411 };
412 IdentifierInfo *AccessorNames[] = { 0, 0 };
413 bool HasInvalidAccessor = false;
414
415 // Parse the accessor specifications.
416 while (true) {
417 // Stop if this doesn't look like an accessor spec.
418 if (!Tok.is(tok::identifier)) {
419 // If the user wrote a completely empty list, use a special diagnostic.
420 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
421 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
422 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
423 break;
424 }
425
426 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
427 break;
428 }
429
430 AccessorKind Kind;
431 SourceLocation KindLoc = Tok.getLocation();
432 StringRef KindStr = Tok.getIdentifierInfo()->getName();
433 if (KindStr == "get") {
434 Kind = AK_Get;
435 } else if (KindStr == "put") {
436 Kind = AK_Put;
437
438 // Recover from the common mistake of using 'set' instead of 'put'.
439 } else if (KindStr == "set") {
440 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
441 << FixItHint::CreateReplacement(KindLoc, "put");
442 Kind = AK_Put;
443
444 // Handle the mistake of forgetting the accessor kind by skipping
445 // this accessor.
446 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
447 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
448 ConsumeToken();
449 HasInvalidAccessor = true;
450 goto next_property_accessor;
451
452 // Otherwise, complain about the unknown accessor kind.
453 } else {
454 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
455 HasInvalidAccessor = true;
456 Kind = AK_Invalid;
457
458 // Try to keep parsing unless it doesn't look like an accessor spec.
459 if (!NextToken().is(tok::equal)) break;
460 }
461
462 // Consume the identifier.
463 ConsumeToken();
464
465 // Consume the '='.
466 if (Tok.is(tok::equal)) {
467 ConsumeToken();
468 } else {
469 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
470 << KindStr;
471 break;
472 }
473
474 // Expect the method name.
475 if (!Tok.is(tok::identifier)) {
476 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
477 break;
478 }
479
480 if (Kind == AK_Invalid) {
481 // Just drop invalid accessors.
482 } else if (AccessorNames[Kind] != NULL) {
483 // Complain about the repeated accessor, ignore it, and keep parsing.
484 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
485 } else {
486 AccessorNames[Kind] = Tok.getIdentifierInfo();
487 }
488 ConsumeToken();
489
490 next_property_accessor:
491 // Keep processing accessors until we run out.
492 if (Tok.is(tok::comma)) {
493 ConsumeAnyToken();
494 continue;
495
496 // If we run into the ')', stop without consuming it.
497 } else if (Tok.is(tok::r_paren)) {
498 break;
499 } else {
500 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
501 break;
502 }
503 }
504
505 // Only add the property attribute if it was well-formed.
506 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000507 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000508 AccessorNames[AK_Get], AccessorNames[AK_Put],
509 AttributeList::AS_Declspec);
510 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000511 T.skipToEnd();
512 } else {
513 // We don't recognize this as a valid declspec, but instead of creating the
514 // attribute and allowing sema to warn about it, we will warn here instead.
515 // This is because some attributes have multiple spellings, but we need to
516 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000517 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000518 // both locations.
519 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
520
521 // If there's an open paren, we should eat the open and close parens under
522 // the assumption that this unknown declspec has parameters.
523 BalancedDelimiterTracker T(*this, tok::l_paren);
524 if (!T.consumeOpen())
525 T.skipToEnd();
526 }
527}
528
Eli Friedman06de2b52009-06-08 07:21:15 +0000529/// [MS] decl-specifier:
530/// __declspec ( extended-decl-modifier-seq )
531///
532/// [MS] extended-decl-modifier-seq:
533/// extended-decl-modifier[opt]
534/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000535void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000536 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000537
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000538 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000539 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000540 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000541 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000542 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000543
Chad Rosierc1183952012-06-26 22:30:43 +0000544 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000545 // you can specify multiple attributes per declspec.
546 while (Tok.getKind() != tok::r_paren) {
547 // We expect either a well-known identifier or a generic string. Anything
548 // else is a malformed declspec.
549 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000550 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000551 Tok.getKind() != tok::kw_restrict) {
552 Diag(Tok, diag::err_ms_declspec_type);
553 T.skipToEnd();
554 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000555 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000556
557 IdentifierInfo *AttrName;
558 SourceLocation AttrNameLoc;
559 if (IsString) {
560 SmallString<8> StrBuffer;
561 bool Invalid = false;
562 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
563 if (Invalid) {
564 T.skipToEnd();
565 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000566 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000567 AttrName = PP.getIdentifierInfo(Str);
568 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000569 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000570 AttrName = Tok.getIdentifierInfo();
571 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000572 }
Chad Rosierc1183952012-06-26 22:30:43 +0000573
Aaron Ballman478faed2012-06-19 22:09:27 +0000574 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000575 // If we have a generic string, we will allow it because there is no
576 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000577 // (for instance, SAL declspecs in older versions of MSVC).
578 //
Chad Rosierc1183952012-06-26 22:30:43 +0000579 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000580 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000581 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
582 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000583 else
584 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000585 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000586 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000587}
588
John McCall53fa7142010-12-24 02:08:15 +0000589void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000590 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000591 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000592 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000593 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000594 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
595 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000596 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
597 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000598 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
599 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000600 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000601}
602
John McCall53fa7142010-12-24 02:08:15 +0000603void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000604 // Treat these like attributes
605 while (Tok.is(tok::kw___pascal)) {
606 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
607 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000608 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
609 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000610 }
John McCall53fa7142010-12-24 02:08:15 +0000611}
612
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000613void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
614 // Treat these like attributes
615 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000616 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000617 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000618 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
619 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000620 }
621}
622
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000623void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000624 // FIXME: The mapping from attribute spelling to semantics should be
625 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000626 SourceLocation Loc = Tok.getLocation();
627 switch(Tok.getKind()) {
628 // OpenCL qualifiers:
629 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000630 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000631 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000632 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000633 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000634 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000635
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000636 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000637 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000638 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000639 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000640 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000641
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000642 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000643 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000644 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000645 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000646 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000647
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000648 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000649 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000650 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000651 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000652 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000653
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000654 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000655 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000656 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000657 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000658 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000659
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000660 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000661 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000662 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000663 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000664 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000665
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000666 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000667 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000668 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000669 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000670 break;
671 default: break;
672 }
673}
674
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000675/// \brief Parse a version number.
676///
677/// version:
678/// simple-integer
679/// simple-integer ',' simple-integer
680/// simple-integer ',' simple-integer ',' simple-integer
681VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
682 Range = Tok.getLocation();
683
684 if (!Tok.is(tok::numeric_constant)) {
685 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000686 SkipUntil(tok::comma, tok::r_paren,
687 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000688 return VersionTuple();
689 }
690
691 // Parse the major (and possibly minor and subminor) versions, which
692 // are stored in the numeric constant. We utilize a quirk of the
693 // lexer, which is that it handles something like 1.2.3 as a single
694 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000695 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000696 Buffer.resize(Tok.getLength()+1);
697 const char *ThisTokBegin = &Buffer[0];
698
699 // Get the spelling of the token, which eliminates trigraphs, etc.
700 bool Invalid = false;
701 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
702 if (Invalid)
703 return VersionTuple();
704
705 // Parse the major version.
706 unsigned AfterMajor = 0;
707 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000708 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000709 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
710 ++AfterMajor;
711 }
712
713 if (AfterMajor == 0) {
714 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000715 SkipUntil(tok::comma, tok::r_paren,
716 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000717 return VersionTuple();
718 }
719
720 if (AfterMajor == ActualLength) {
721 ConsumeToken();
722
723 // We only had a single version component.
724 if (Major == 0) {
725 Diag(Tok, diag::err_zero_version);
726 return VersionTuple();
727 }
728
729 return VersionTuple(Major);
730 }
731
732 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
733 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000734 SkipUntil(tok::comma, tok::r_paren,
735 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000736 return VersionTuple();
737 }
738
739 // Parse the minor version.
740 unsigned AfterMinor = AfterMajor + 1;
741 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000742 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000743 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
744 ++AfterMinor;
745 }
746
747 if (AfterMinor == ActualLength) {
748 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000749
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000750 // We had major.minor.
751 if (Major == 0 && Minor == 0) {
752 Diag(Tok, diag::err_zero_version);
753 return VersionTuple();
754 }
755
Chad Rosierc1183952012-06-26 22:30:43 +0000756 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000757 }
758
759 // If what follows is not a '.', we have a problem.
760 if (ThisTokBegin[AfterMinor] != '.') {
761 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000762 SkipUntil(tok::comma, tok::r_paren,
763 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000764 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000765 }
766
767 // Parse the subminor version.
768 unsigned AfterSubminor = AfterMinor + 1;
769 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000770 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000771 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
772 ++AfterSubminor;
773 }
774
775 if (AfterSubminor != ActualLength) {
776 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000777 SkipUntil(tok::comma, tok::r_paren,
778 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000779 return VersionTuple();
780 }
781 ConsumeToken();
782 return VersionTuple(Major, Minor, Subminor);
783}
784
785/// \brief Parse the contents of the "availability" attribute.
786///
787/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000788/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000789///
790/// platform:
791/// identifier
792///
793/// version-arg-list:
794/// version-arg
795/// version-arg ',' version-arg-list
796///
797/// version-arg:
798/// 'introduced' '=' version
799/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000800/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000801/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000802/// opt-message:
803/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000804void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
805 SourceLocation AvailabilityLoc,
806 ParsedAttributes &attrs,
807 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000808 enum { Introduced, Deprecated, Obsoleted, Unknown };
809 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000810 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000811
812 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000813 BalancedDelimiterTracker T(*this, tok::l_paren);
814 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000815 Diag(Tok, diag::err_expected_lparen);
816 return;
817 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000818
819 // Parse the platform name,
820 if (Tok.isNot(tok::identifier)) {
821 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000822 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000823 return;
824 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000825 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000826
827 // Parse the ',' following the platform name.
828 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
829 return;
830
831 // If we haven't grabbed the pointers for the identifiers
832 // "introduced", "deprecated", and "obsoleted", do so now.
833 if (!Ident_introduced) {
834 Ident_introduced = PP.getIdentifierInfo("introduced");
835 Ident_deprecated = PP.getIdentifierInfo("deprecated");
836 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000837 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000838 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000839 }
840
841 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000842 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000843 do {
844 if (Tok.isNot(tok::identifier)) {
845 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000846 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000847 return;
848 }
849 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
850 SourceLocation KeywordLoc = ConsumeToken();
851
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000852 if (Keyword == Ident_unavailable) {
853 if (UnavailableLoc.isValid()) {
854 Diag(KeywordLoc, diag::err_availability_redundant)
855 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000856 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000857 UnavailableLoc = KeywordLoc;
858
859 if (Tok.isNot(tok::comma))
860 break;
861
862 ConsumeToken();
863 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000864 }
865
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000866 if (Tok.isNot(tok::equal)) {
867 Diag(Tok, diag::err_expected_equal_after)
868 << Keyword;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000869 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000870 return;
871 }
872 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000873 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000874 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000875 Diag(Tok, diag::err_expected_string_literal)
876 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000877 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000878 return;
879 }
880 MessageExpr = ParseStringLiteralExpression();
881 break;
882 }
Chad Rosierc1183952012-06-26 22:30:43 +0000883
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000884 SourceRange VersionRange;
885 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000886
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000887 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000888 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000889 return;
890 }
891
892 unsigned Index;
893 if (Keyword == Ident_introduced)
894 Index = Introduced;
895 else if (Keyword == Ident_deprecated)
896 Index = Deprecated;
897 else if (Keyword == Ident_obsoleted)
898 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000899 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000900 Index = Unknown;
901
902 if (Index < Unknown) {
903 if (!Changes[Index].KeywordLoc.isInvalid()) {
904 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000905 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000906 << SourceRange(Changes[Index].KeywordLoc,
907 Changes[Index].VersionRange.getEnd());
908 }
909
910 Changes[Index].KeywordLoc = KeywordLoc;
911 Changes[Index].Version = Version;
912 Changes[Index].VersionRange = VersionRange;
913 } else {
914 Diag(KeywordLoc, diag::err_availability_unknown_change)
915 << Keyword << VersionRange;
916 }
917
918 if (Tok.isNot(tok::comma))
919 break;
920
921 ConsumeToken();
922 } while (true);
923
924 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000925 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000926 return;
927
928 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000929 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000930
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000931 // The 'unavailable' availability cannot be combined with any other
932 // availability changes. Make sure that hasn't happened.
933 if (UnavailableLoc.isValid()) {
934 bool Complained = false;
935 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
936 if (Changes[Index].KeywordLoc.isValid()) {
937 if (!Complained) {
938 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
939 << SourceRange(Changes[Index].KeywordLoc,
940 Changes[Index].VersionRange.getEnd());
941 Complained = true;
942 }
943
944 // Clear out the availability.
945 Changes[Index] = AvailabilityChange();
946 }
947 }
948 }
949
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000950 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000951 attrs.addNew(&Availability,
952 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000953 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000954 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000955 Changes[Introduced],
956 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000957 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000958 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000959 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000960}
961
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000962
Bill Wendling44426052012-12-20 19:22:21 +0000963// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000964// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
965
966void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
967
968void Parser::LateParsedClass::ParseLexedAttributes() {
969 Self->ParseLexedAttributes(*Class);
970}
971
972void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000973 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000974}
975
976/// Wrapper class which calls ParseLexedAttribute, after setting up the
977/// scope appropriately.
978void Parser::ParseLexedAttributes(ParsingClass &Class) {
979 // Deal with templates
980 // FIXME: Test cases to make sure this does the right thing for templates.
981 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
982 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
983 HasTemplateScope);
984 if (HasTemplateScope)
985 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
986
Douglas Gregor3024f072012-04-16 07:05:22 +0000987 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000988 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000989 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000990 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
991 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
992
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000993 // Enter the scope of nested classes
994 if (!AlreadyHasClassScope)
995 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
996 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000997 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000998 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
999 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1000 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001001 }
Chad Rosierc1183952012-06-26 22:30:43 +00001002
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001003 if (!AlreadyHasClassScope)
1004 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1005 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001006}
1007
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001008
1009/// \brief Parse all attributes in LAs, and attach them to Decl D.
1010void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1011 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001012 assert(LAs.parseSoon() &&
1013 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001014 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001015 if (D)
1016 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001017 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001018 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001019 }
1020 LAs.clear();
1021}
1022
1023
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001024/// \brief Finish parsing an attribute for which parsing was delayed.
1025/// This will be called at the end of parsing a class declaration
1026/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001027/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001028/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001029void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1030 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001031 // Save the current token position.
1032 SourceLocation OrigLoc = Tok.getLocation();
1033
1034 // Append the current token at the end of the new token stream so that it
1035 // doesn't get lost.
1036 LA.Toks.push_back(Tok);
1037 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1038 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001039 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001040
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001041 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001042 // FIXME: Do not warn on C++11 attributes, once we start supporting
1043 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001044 Diag(Tok, diag::warn_attribute_on_function_definition)
1045 << LA.AttrName.getName();
1046 }
1047
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001048 ParsedAttributes Attrs(AttrFactory);
1049 SourceLocation endLoc;
1050
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001051 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001052 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001053 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1054 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001055
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001056 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001057 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1058 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001059
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001060 if (LA.Decls.size() == 1) {
1061 // If the Decl is templatized, add template parameters to scope.
1062 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1063 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1064 if (HasTemplateScope)
1065 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001066
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001067 // If the Decl is on a function, add function parameters to the scope.
1068 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1069 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1070 if (HasFunScope)
1071 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001072
Michael Han23214e52012-10-03 01:56:22 +00001073 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001074 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001075
1076 if (HasFunScope) {
1077 Actions.ActOnExitFunctionContext();
1078 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1079 }
1080 if (HasTemplateScope) {
1081 TempScope.Exit();
1082 }
1083 } else {
1084 // If there are multiple decls, then the decl cannot be within the
1085 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001086 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001087 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001088 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001089 } else {
1090 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001091 }
1092
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001093 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1094 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1095 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001096
1097 if (Tok.getLocation() != OrigLoc) {
1098 // Due to a parsing error, we either went over the cached tokens or
1099 // there are still cached tokens left, so we skip the leftover tokens.
1100 // Since this is an uncommon situation that should be avoided, use the
1101 // expensive isBeforeInTranslationUnit call.
1102 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1103 OrigLoc))
1104 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001105 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001106 }
1107}
1108
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001109/// \brief Wrapper around a case statement checking if AttrName is
1110/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001111bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001112 return llvm::StringSwitch<bool>(AttrName)
1113 .Case("guarded_by", true)
1114 .Case("guarded_var", true)
1115 .Case("pt_guarded_by", true)
1116 .Case("pt_guarded_var", true)
1117 .Case("lockable", true)
1118 .Case("scoped_lockable", true)
1119 .Case("no_thread_safety_analysis", true)
1120 .Case("acquired_after", true)
1121 .Case("acquired_before", true)
1122 .Case("exclusive_lock_function", true)
1123 .Case("shared_lock_function", true)
1124 .Case("exclusive_trylock_function", true)
1125 .Case("shared_trylock_function", true)
1126 .Case("unlock_function", true)
1127 .Case("lock_returned", true)
1128 .Case("locks_excluded", true)
1129 .Case("exclusive_locks_required", true)
1130 .Case("shared_locks_required", true)
1131 .Default(false);
1132}
1133
1134/// \brief Parse the contents of thread safety attributes. These
1135/// should always be parsed as an expression list.
1136///
1137/// We need to special case the parsing due to the fact that if the first token
1138/// of the first argument is an identifier, the main parse loop will store
1139/// that token as a "parameter" and the rest of
1140/// the arguments will be added to a list of "arguments". However,
1141/// subsequent tokens in the first argument are lost. We instead parse each
1142/// argument as an expression and add all arguments to the list of "arguments".
1143/// In future, we will take advantage of this special case to also
1144/// deal with some argument scoping issues here (for example, referring to a
1145/// function parameter in the attribute on that function).
1146void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1147 SourceLocation AttrNameLoc,
1148 ParsedAttributes &Attrs,
1149 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001150 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001151
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001152 BalancedDelimiterTracker T(*this, tok::l_paren);
1153 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001154
Aaron Ballman00e99962013-08-31 01:11:41 +00001155 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001156 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001158 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001159 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001160 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001161 ExprResult ArgExpr(ParseAssignmentExpression());
1162 if (ArgExpr.isInvalid()) {
1163 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001164 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001165 break;
1166 } else {
1167 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001168 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001169 if (Tok.isNot(tok::comma))
1170 break;
1171 ConsumeToken(); // Eat the comma, move to the next argument
1172 }
1173 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001174 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001175 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1176 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001177 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001178 if (EndLoc)
1179 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001180}
1181
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001182void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1183 SourceLocation AttrNameLoc,
1184 ParsedAttributes &Attrs,
1185 SourceLocation *EndLoc) {
1186 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1187
1188 BalancedDelimiterTracker T(*this, tok::l_paren);
1189 T.consumeOpen();
1190
1191 if (Tok.isNot(tok::identifier)) {
1192 Diag(Tok, diag::err_expected_ident);
1193 T.skipToEnd();
1194 return;
1195 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001196 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001197
1198 if (Tok.isNot(tok::comma)) {
1199 Diag(Tok, diag::err_expected_comma);
1200 T.skipToEnd();
1201 return;
1202 }
1203 ConsumeToken();
1204
1205 SourceRange MatchingCTypeRange;
1206 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1207 if (MatchingCType.isInvalid()) {
1208 T.skipToEnd();
1209 return;
1210 }
1211
1212 bool LayoutCompatible = false;
1213 bool MustBeNull = false;
1214 while (Tok.is(tok::comma)) {
1215 ConsumeToken();
1216 if (Tok.isNot(tok::identifier)) {
1217 Diag(Tok, diag::err_expected_ident);
1218 T.skipToEnd();
1219 return;
1220 }
1221 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1222 if (Flag->isStr("layout_compatible"))
1223 LayoutCompatible = true;
1224 else if (Flag->isStr("must_be_null"))
1225 MustBeNull = true;
1226 else {
1227 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1228 T.skipToEnd();
1229 return;
1230 }
1231 ConsumeToken(); // consume flag
1232 }
1233
1234 if (!T.consumeClose()) {
1235 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001236 ArgumentKind, MatchingCType.release(),
1237 LayoutCompatible, MustBeNull,
1238 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001239 }
1240
1241 if (EndLoc)
1242 *EndLoc = T.getCloseLocation();
1243}
1244
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001245/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1246/// of a C++11 attribute-specifier in a location where an attribute is not
1247/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1248/// situation.
1249///
1250/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1251/// this doesn't appear to actually be an attribute-specifier, and the caller
1252/// should try to parse it.
1253bool Parser::DiagnoseProhibitedCXX11Attribute() {
1254 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1255
1256 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1257 case CAK_NotAttributeSpecifier:
1258 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1259 return false;
1260
1261 case CAK_InvalidAttributeSpecifier:
1262 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1263 return false;
1264
1265 case CAK_AttributeSpecifier:
1266 // Parse and discard the attributes.
1267 SourceLocation BeginLoc = ConsumeBracket();
1268 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001269 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001270 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1271 SourceLocation EndLoc = ConsumeBracket();
1272 Diag(BeginLoc, diag::err_attributes_not_allowed)
1273 << SourceRange(BeginLoc, EndLoc);
1274 return true;
1275 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001276 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001277}
1278
Richard Smith98155ad2013-02-20 01:17:14 +00001279/// \brief We have found the opening square brackets of a C++11
1280/// attribute-specifier in a location where an attribute is not permitted, but
1281/// we know where the attributes ought to be written. Parse them anyway, and
1282/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001283void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1284 SourceLocation CorrectLocation) {
1285 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1286 Tok.is(tok::kw_alignas));
1287
1288 // Consume the attributes.
1289 SourceLocation Loc = Tok.getLocation();
1290 ParseCXX11Attributes(Attrs);
1291 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1292
1293 Diag(Loc, diag::err_attributes_not_allowed)
1294 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1295 << FixItHint::CreateRemoval(AttrRange);
1296}
1297
John McCall53fa7142010-12-24 02:08:15 +00001298void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1299 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1300 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001301}
1302
Michael Han64536a62012-11-06 19:34:54 +00001303void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1304 AttributeList *AttrList = attrs.getList();
1305 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001306 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001307 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001308 << AttrList->getName();
1309 AttrList->setInvalid();
1310 }
1311 AttrList = AttrList->getNext();
1312 }
1313}
1314
Chris Lattner53361ac2006-08-10 05:19:57 +00001315/// ParseDeclaration - Parse a full 'declaration', which consists of
1316/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001317/// 'Context' should be a Declarator::TheContext value. This returns the
1318/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001319///
1320/// declaration: [C99 6.7]
1321/// block-declaration ->
1322/// simple-declaration
1323/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001324/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001325/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001326/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001327/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001328/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001329/// others... [FIXME]
1330///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001331Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1332 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001333 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001334 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001335 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001336 // Must temporarily exit the objective-c container scope for
1337 // parsing c none objective-c decls.
1338 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001339
John McCall48871652010-08-21 09:40:31 +00001340 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001341 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001342 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001343 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001344 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001345 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001346 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001347 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001348 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001349 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001350 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001351 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001352 SourceLocation InlineLoc = ConsumeToken();
1353 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1354 break;
1355 }
Chad Rosierc1183952012-06-26 22:30:43 +00001356 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001357 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001358 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001359 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001360 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001361 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001362 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001363 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001364 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001365 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001366 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001367 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001368 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001369 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001370 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001371 default:
John McCall53fa7142010-12-24 02:08:15 +00001372 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001373 }
Chad Rosierc1183952012-06-26 22:30:43 +00001374
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001375 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001376 // single decl, convert it now. Alias declarations can also declare a type;
1377 // include that too if it is present.
1378 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001379}
1380
1381/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1382/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001383/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1384/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001385///[C90/C++]init-declarator-list ';' [TODO]
1386/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001387///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001388/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001389/// attribute-specifier-seq[opt] type-specifier-seq declarator
1390///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001391/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001392/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001393///
1394/// If FRI is non-null, we might be parsing a for-range-declaration instead
1395/// of a simple-declaration. If we find that we are, we also parse the
1396/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001397Parser::DeclGroupPtrTy
1398Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1399 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001400 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001401 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001402 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001403 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001404
Richard Smith404dfb42013-11-19 22:47:36 +00001405 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1406 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1407
1408 // If we had a free-standing type definition with a missing semicolon, we
1409 // may get this far before the problem becomes obvious.
1410 if (DS.hasTagDefinition() &&
1411 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1412 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001413
Chris Lattner0e894622006-08-13 19:58:17 +00001414 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1415 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001416 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001417 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001418 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001419 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001420 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001421 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001422 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001423 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001424 }
Chad Rosierc1183952012-06-26 22:30:43 +00001425
Richard Smith2386c8b2013-02-22 09:06:26 +00001426 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001427 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001428}
Mike Stump11289f42009-09-09 15:08:12 +00001429
Richard Smith09f76ee2011-10-19 21:33:05 +00001430/// Returns true if this might be the start of a declarator, or a common typo
1431/// for a declarator.
1432bool Parser::MightBeDeclarator(unsigned Context) {
1433 switch (Tok.getKind()) {
1434 case tok::annot_cxxscope:
1435 case tok::annot_template_id:
1436 case tok::caret:
1437 case tok::code_completion:
1438 case tok::coloncolon:
1439 case tok::ellipsis:
1440 case tok::kw___attribute:
1441 case tok::kw_operator:
1442 case tok::l_paren:
1443 case tok::star:
1444 return true;
1445
1446 case tok::amp:
1447 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001448 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001449
Richard Smithc8a79032012-01-09 22:31:44 +00001450 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001451 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001452 NextToken().is(tok::l_square);
1453
1454 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001455 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001456
Richard Smith09f76ee2011-10-19 21:33:05 +00001457 case tok::identifier:
1458 switch (NextToken().getKind()) {
1459 case tok::code_completion:
1460 case tok::coloncolon:
1461 case tok::comma:
1462 case tok::equal:
1463 case tok::equalequal: // Might be a typo for '='.
1464 case tok::kw_alignas:
1465 case tok::kw_asm:
1466 case tok::kw___attribute:
1467 case tok::l_brace:
1468 case tok::l_paren:
1469 case tok::l_square:
1470 case tok::less:
1471 case tok::r_brace:
1472 case tok::r_paren:
1473 case tok::r_square:
1474 case tok::semi:
1475 return true;
1476
1477 case tok::colon:
1478 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001479 // and in block scope it's probably a label. Inside a class definition,
1480 // this is a bit-field.
1481 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001482 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001483
1484 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001485 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001486
1487 default:
1488 return false;
1489 }
1490
1491 default:
1492 return false;
1493 }
1494}
1495
Richard Smithb8caac82012-04-11 20:59:20 +00001496/// Skip until we reach something which seems like a sensible place to pick
1497/// up parsing after a malformed declaration. This will sometimes stop sooner
1498/// than SkipUntil(tok::r_brace) would, but will never stop later.
1499void Parser::SkipMalformedDecl() {
1500 while (true) {
1501 switch (Tok.getKind()) {
1502 case tok::l_brace:
1503 // Skip until matching }, then stop. We've probably skipped over
1504 // a malformed class or function definition or similar.
1505 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001506 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001507 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1508 // This declaration isn't over yet. Keep skipping.
1509 continue;
1510 }
1511 if (Tok.is(tok::semi))
1512 ConsumeToken();
1513 return;
1514
1515 case tok::l_square:
1516 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001517 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001518 continue;
1519
1520 case tok::l_paren:
1521 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001522 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001523 continue;
1524
1525 case tok::r_brace:
1526 return;
1527
1528 case tok::semi:
1529 ConsumeToken();
1530 return;
1531
1532 case tok::kw_inline:
1533 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001534 // a good place to pick back up parsing, except in an Objective-C
1535 // @interface context.
1536 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1537 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001538 return;
1539 break;
1540
1541 case tok::kw_namespace:
1542 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001543 // place to pick back up parsing, except in an Objective-C
1544 // @interface context.
1545 if (Tok.isAtStartOfLine() &&
1546 (!ParsingInObjCContainer || CurParsedObjCImpl))
1547 return;
1548 break;
1549
1550 case tok::at:
1551 // @end is very much like } in Objective-C contexts.
1552 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1553 ParsingInObjCContainer)
1554 return;
1555 break;
1556
1557 case tok::minus:
1558 case tok::plus:
1559 // - and + probably start new method declarations in Objective-C contexts.
1560 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001561 return;
1562 break;
1563
1564 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001565 case tok::annot_module_begin:
1566 case tok::annot_module_end:
1567 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001568 return;
1569
1570 default:
1571 break;
1572 }
1573
1574 ConsumeAnyToken();
1575 }
1576}
1577
John McCalld5a36322009-11-03 19:26:08 +00001578/// ParseDeclGroup - Having concluded that this is either a function
1579/// definition or a group of object declarations, actually parse the
1580/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001581Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1582 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001583 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001584 SourceLocation *DeclEnd,
1585 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001586 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001587 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001588 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001589
John McCalld5a36322009-11-03 19:26:08 +00001590 // Bail out if the first declarator didn't seem well-formed.
1591 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001592 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001593 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001594 }
Mike Stump11289f42009-09-09 15:08:12 +00001595
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001596 // Save late-parsed attributes for now; they need to be parsed in the
1597 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001598 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1599 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001600 if (D.isFunctionDeclarator())
1601 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1602
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001603 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001604 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001605 // Look at the next token to make sure that this isn't a function
1606 // declaration. We have to check this because __attribute__ might be the
1607 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001608 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001609
Douglas Gregor012efe22013-04-16 16:01:32 +00001610 if (AllowFunctionDefinitions) {
1611 if (isStartOfFunctionDefinition(D)) {
1612 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1613 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001614
Douglas Gregor012efe22013-04-16 16:01:32 +00001615 // Recover by treating the 'typedef' as spurious.
1616 DS.ClearStorageClassSpecs();
1617 }
1618
1619 Decl *TheDecl =
1620 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1621 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001622 }
1623
Douglas Gregor012efe22013-04-16 16:01:32 +00001624 if (isDeclarationSpecifier()) {
1625 // If there is an invalid declaration specifier right after the function
1626 // prototype, then we must be in a missing semicolon case where this isn't
1627 // actually a body. Just fall through into the code that handles it as a
1628 // prototype, and let the top-level code handle the erroneous declspec
1629 // where it would otherwise expect a comma or semicolon.
1630 } else {
1631 Diag(Tok, diag::err_expected_fn_body);
1632 SkipUntil(tok::semi);
1633 return DeclGroupPtrTy();
1634 }
John McCalld5a36322009-11-03 19:26:08 +00001635 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001636 if (Tok.is(tok::l_brace)) {
1637 Diag(Tok, diag::err_function_definition_not_allowed);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001638 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor012efe22013-04-16 16:01:32 +00001639 }
John McCalld5a36322009-11-03 19:26:08 +00001640 }
1641 }
1642
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001643 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001644 return DeclGroupPtrTy();
1645
1646 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1647 // must parse and analyze the for-range-initializer before the declaration is
1648 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001649 //
1650 // Handle the Objective-C for-in loop variable similarly, although we
1651 // don't need to parse the container in advance.
1652 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1653 bool IsForRangeLoop = false;
1654 if (Tok.is(tok::colon)) {
1655 IsForRangeLoop = true;
1656 FRI->ColonLoc = ConsumeToken();
1657 if (Tok.is(tok::l_brace))
1658 FRI->RangeExpr = ParseBraceInitializer();
1659 else
1660 FRI->RangeExpr = ParseExpression();
1661 }
1662
Richard Smith02e85f32011-04-14 22:09:26 +00001663 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001664 if (IsForRangeLoop)
1665 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001666 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001667 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001668 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001669 }
1670
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001671 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001672 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001673 if (LateParsedAttrs.size() > 0)
1674 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001675 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001676 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001677 DeclsInGroup.push_back(FirstDecl);
1678
Richard Smith09f76ee2011-10-19 21:33:05 +00001679 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001680
John McCalld5a36322009-11-03 19:26:08 +00001681 // If we don't have a comma, it is either the end of the list (a ';') or an
1682 // error, bail out.
1683 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001684 SourceLocation CommaLoc = ConsumeToken();
1685
1686 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1687 // This comma was followed by a line-break and something which can't be
1688 // the start of a declarator. The comma was probably a typo for a
1689 // semicolon.
1690 Diag(CommaLoc, diag::err_expected_semi_declaration)
1691 << FixItHint::CreateReplacement(CommaLoc, ";");
1692 ExpectSemi = false;
1693 break;
1694 }
John McCalld5a36322009-11-03 19:26:08 +00001695
1696 // Parse the next declarator.
1697 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001698 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001699
1700 // Accept attributes in an init-declarator. In the first declarator in a
1701 // declaration, these would be part of the declspec. In subsequent
1702 // declarators, they become part of the declarator itself, so that they
1703 // don't apply to declarators after *this* one. Examples:
1704 // short __attribute__((common)) var; -> declspec
1705 // short var __attribute__((common)); -> declarator
1706 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001707 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001708
1709 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001710 if (!D.isInvalidType()) {
1711 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1712 D.complete(ThisDecl);
1713 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001714 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001715 }
John McCalld5a36322009-11-03 19:26:08 +00001716 }
1717
1718 if (DeclEnd)
1719 *DeclEnd = Tok.getLocation();
1720
Richard Smith09f76ee2011-10-19 21:33:05 +00001721 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001722 ExpectAndConsumeSemi(Context == Declarator::FileContext
1723 ? diag::err_invalid_token_after_toplevel_declarator
1724 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001725 // Okay, there was no semicolon and one was expected. If we see a
1726 // declaration specifier, just assume it was missing and continue parsing.
1727 // Otherwise things are very confused and we skip to recover.
1728 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001729 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner13901342010-07-11 22:42:07 +00001730 if (Tok.is(tok::semi))
1731 ConsumeToken();
1732 }
John McCalld5a36322009-11-03 19:26:08 +00001733 }
1734
Rafael Espindolaab417692013-07-09 12:05:01 +00001735 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001736}
1737
Richard Smith02e85f32011-04-14 22:09:26 +00001738/// Parse an optional simple-asm-expr and attributes, and attach them to a
1739/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001740bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001741 // If a simple-asm-expr is present, parse it.
1742 if (Tok.is(tok::kw_asm)) {
1743 SourceLocation Loc;
1744 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1745 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001746 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001747 return true;
1748 }
1749
1750 D.setAsmLabel(AsmLabel.release());
1751 D.SetRangeEnd(Loc);
1752 }
1753
1754 MaybeParseGNUAttributes(D);
1755 return false;
1756}
1757
Douglas Gregor23996282009-05-12 21:31:51 +00001758/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1759/// declarator'. This method parses the remainder of the declaration
1760/// (including any attributes or initializer, among other things) and
1761/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001762///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001763/// init-declarator: [C99 6.7]
1764/// declarator
1765/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001766/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1767/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001768/// [C++] declarator initializer[opt]
1769///
1770/// [C++] initializer:
1771/// [C++] '=' initializer-clause
1772/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001773/// [C++0x] '=' 'default' [TODO]
1774/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001775/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001776///
1777/// According to the standard grammar, =default and =delete are function
1778/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001779///
John McCall48871652010-08-21 09:40:31 +00001780Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001781 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001782 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001783 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001784
Richard Smith02e85f32011-04-14 22:09:26 +00001785 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1786}
Mike Stump11289f42009-09-09 15:08:12 +00001787
Richard Smith02e85f32011-04-14 22:09:26 +00001788Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1789 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001790 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001791 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001792 switch (TemplateInfo.Kind) {
1793 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001794 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001795 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001796
Douglas Gregor450f00842009-09-25 18:43:00 +00001797 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001798 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001799 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001800 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001801 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001802 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001803 // Re-direct this decl to refer to the templated decl so that we can
1804 // initialize it.
1805 ThisDecl = VT->getTemplatedDecl();
1806 break;
1807 }
1808 case ParsedTemplateInfo::ExplicitInstantiation: {
1809 if (Tok.is(tok::semi)) {
1810 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1811 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1812 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001813 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001814 return 0;
1815 }
1816 ThisDecl = ThisRes.get();
1817 } else {
1818 // FIXME: This check should be for a variable template instantiation only.
1819
1820 // Check that this is a valid instantiation
1821 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1822 // If the declarator-id is not a template-id, issue a diagnostic and
1823 // recover by ignoring the 'template' keyword.
1824 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1825 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1826 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1827 } else {
1828 SourceLocation LAngleLoc =
1829 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1830 Diag(D.getIdentifierLoc(),
1831 diag::err_explicit_instantiation_with_definition)
1832 << SourceRange(TemplateInfo.TemplateLoc)
1833 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1834
1835 // Recover as if it were an explicit specialization.
1836 TemplateParameterLists FakedParamLists;
1837 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1838 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1839 LAngleLoc));
1840
1841 ThisDecl =
1842 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1843 }
1844 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001845 break;
1846 }
1847 }
Mike Stump11289f42009-09-09 15:08:12 +00001848
Richard Smith74aeef52013-04-26 16:15:35 +00001849 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001850
Douglas Gregor23996282009-05-12 21:31:51 +00001851 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001852 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001853 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001854 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001855
Anders Carlsson991285e2010-09-24 21:25:25 +00001856 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001857 if (D.isFunctionDeclarator())
1858 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1859 << 1 /* delete */;
1860 else
1861 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001862 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001863 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001864 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1865 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001866 else
1867 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001868 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001869 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001870 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001871 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001872 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001873
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001874 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001875 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001876 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001877 cutOffParsing();
1878 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001879 }
Chad Rosierc1183952012-06-26 22:30:43 +00001880
John McCalldadc5752010-08-24 06:29:42 +00001881 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001882
David Blaikiebbafb8a2012-03-11 07:00:24 +00001883 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001884 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001885 ExitScope();
1886 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001887
Douglas Gregor23996282009-05-12 21:31:51 +00001888 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001889 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001890 Actions.ActOnInitializerError(ThisDecl);
1891 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001892 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1893 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001894 }
1895 } else if (Tok.is(tok::l_paren)) {
1896 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001897 BalancedDelimiterTracker T(*this, tok::l_paren);
1898 T.consumeOpen();
1899
Benjamin Kramerf0623432012-08-23 22:51:59 +00001900 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001901 CommaLocsTy CommaLocs;
1902
David Blaikiebbafb8a2012-03-11 07:00:24 +00001903 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001904 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001905 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001906 }
1907
Douglas Gregor23996282009-05-12 21:31:51 +00001908 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001909 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001910 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001911
David Blaikiebbafb8a2012-03-11 07:00:24 +00001912 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001913 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001914 ExitScope();
1915 }
Douglas Gregor23996282009-05-12 21:31:51 +00001916 } else {
1917 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001918 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001919
1920 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1921 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001922
David Blaikiebbafb8a2012-03-11 07:00:24 +00001923 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001924 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001925 ExitScope();
1926 }
1927
Sebastian Redla9351792012-02-11 23:51:47 +00001928 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1929 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001930 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001931 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1932 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001933 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001934 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001935 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001936 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001937 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1938
Sebastian Redl3da34892011-06-05 12:23:16 +00001939 if (D.getCXXScopeSpec().isSet()) {
1940 EnterScope(0);
1941 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1942 }
1943
1944 ExprResult Init(ParseBraceInitializer());
1945
1946 if (D.getCXXScopeSpec().isSet()) {
1947 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1948 ExitScope();
1949 }
1950
1951 if (Init.isInvalid()) {
1952 Actions.ActOnInitializerError(ThisDecl);
1953 } else
1954 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1955 /*DirectInit=*/true, TypeContainsAuto);
1956
Douglas Gregor23996282009-05-12 21:31:51 +00001957 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001958 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001959 }
1960
Richard Smithb2bc2e62011-02-21 20:05:19 +00001961 Actions.FinalizeDeclaration(ThisDecl);
1962
Douglas Gregor23996282009-05-12 21:31:51 +00001963 return ThisDecl;
1964}
1965
Chris Lattner1890ac82006-08-13 01:16:23 +00001966/// ParseSpecifierQualifierList
1967/// specifier-qualifier-list:
1968/// type-specifier specifier-qualifier-list[opt]
1969/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001970/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001971///
Richard Smithc5b05522012-03-12 07:56:15 +00001972void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1973 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001974 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1975 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001976 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001977 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001978
Chris Lattner1890ac82006-08-13 01:16:23 +00001979 // Validate declspec for type-name.
1980 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001981 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1982 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001983 Diag(Tok, diag::err_expected_type);
1984 DS.SetTypeSpecError();
1985 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1986 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001987 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001988 if (!DS.hasTypeSpecifier())
1989 DS.SetTypeSpecError();
1990 }
Mike Stump11289f42009-09-09 15:08:12 +00001991
Chris Lattner1b22eed2006-11-28 05:12:07 +00001992 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001993 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001994 if (DS.getStorageClassSpecLoc().isValid())
1995 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1996 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001997 Diag(DS.getThreadStorageClassSpecLoc(),
1998 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001999 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002000 }
Mike Stump11289f42009-09-09 15:08:12 +00002001
Chris Lattner1b22eed2006-11-28 05:12:07 +00002002 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002003 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002004 if (DS.isInlineSpecified())
2005 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2006 if (DS.isVirtualSpecified())
2007 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2008 if (DS.isExplicitSpecified())
2009 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002010 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002011 }
Richard Smithc5b05522012-03-12 07:56:15 +00002012
2013 // Issue diagnostic and remove constexpr specfier if present.
2014 if (DS.isConstexprSpecified()) {
2015 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2016 DS.ClearConstexprSpec();
2017 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002018}
Chris Lattner53361ac2006-08-10 05:19:57 +00002019
Chris Lattner6cc055a2009-04-12 20:42:31 +00002020/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2021/// specified token is valid after the identifier in a declarator which
2022/// immediately follows the declspec. For example, these things are valid:
2023///
2024/// int x [ 4]; // direct-declarator
2025/// int x ( int y); // direct-declarator
2026/// int(int x ) // direct-declarator
2027/// int x ; // simple-declaration
2028/// int x = 17; // init-declarator-list
2029/// int x , y; // init-declarator-list
2030/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002031/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002032/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002033///
2034/// This is not, because 'x' does not immediately follow the declspec (though
2035/// ')' happens to be valid anyway).
2036/// int (x)
2037///
2038static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2039 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2040 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002041 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002042}
2043
Chris Lattner20a0c612009-04-14 21:34:55 +00002044
2045/// ParseImplicitInt - This method is called when we have an non-typename
2046/// identifier in a declspec (which normally terminates the decl spec) when
2047/// the declspec has no type specifier. In this case, the declspec is either
2048/// malformed or is "implicit int" (in K&R and C89).
2049///
2050/// This method handles diagnosing this prettily and returns false if the
2051/// declspec is done being processed. If it recovers and thinks there may be
2052/// other pieces of declspec after it, it returns true.
2053///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002054bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002055 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002056 AccessSpecifier AS, DeclSpecContext DSC,
2057 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002058 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002059
Chris Lattner20a0c612009-04-14 21:34:55 +00002060 SourceLocation Loc = Tok.getLocation();
2061 // If we see an identifier that is not a type name, we normally would
2062 // parse it as the identifer being declared. However, when a typename
2063 // is typo'd or the definition is not included, this will incorrectly
2064 // parse the typename as the identifier name and fall over misparsing
2065 // later parts of the diagnostic.
2066 //
2067 // As such, we try to do some look-ahead in cases where this would
2068 // otherwise be an "implicit-int" case to see if this is invalid. For
2069 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2070 // an identifier with implicit int, we'd get a parse error because the
2071 // next token is obviously invalid for a type. Parse these as a case
2072 // with an invalid type specifier.
2073 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002074
Chris Lattner20a0c612009-04-14 21:34:55 +00002075 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002076 // error, do lookahead to try to do better recovery. This never applies
2077 // within a type specifier. Outside of C++, we allow this even if the
2078 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002079 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002080 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002081 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002082 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002083 // If this token is valid for implicit int, e.g. "static x = 4", then
2084 // we just avoid eating the identifier, so it will be parsed as the
2085 // identifier in the declarator.
2086 return false;
2087 }
Mike Stump11289f42009-09-09 15:08:12 +00002088
Richard Smitha952ebb2012-05-15 21:01:51 +00002089 if (getLangOpts().CPlusPlus &&
2090 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2091 // Don't require a type specifier if we have the 'auto' storage class
2092 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002093 if (SS)
2094 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002095 return false;
2096 }
2097
Chris Lattner20a0c612009-04-14 21:34:55 +00002098 // Otherwise, if we don't consume this token, we are going to emit an
2099 // error anyway. Try to recover from various common problems. Check
2100 // to see if this was a reference to a tag name without a tag specified.
2101 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002102 //
2103 // C++ doesn't need this, and isTagName doesn't take SS.
2104 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002105 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002106 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002107
Douglas Gregor0be31a22010-07-02 17:43:08 +00002108 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002109 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002110 case DeclSpec::TST_enum:
2111 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2112 case DeclSpec::TST_union:
2113 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2114 case DeclSpec::TST_struct:
2115 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002116 case DeclSpec::TST_interface:
2117 TagName="__interface"; FixitTagName = "__interface ";
2118 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002119 case DeclSpec::TST_class:
2120 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002121 }
Mike Stump11289f42009-09-09 15:08:12 +00002122
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002123 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002124 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2125 LookupResult R(Actions, TokenName, SourceLocation(),
2126 Sema::LookupOrdinaryName);
2127
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002128 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002129 << TokenName << TagName << getLangOpts().CPlusPlus
2130 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2131
2132 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2133 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2134 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002135 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002136 << TokenName << TagName;
2137 }
Mike Stump11289f42009-09-09 15:08:12 +00002138
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002139 // Parse this as a tag as if the missing tag were present.
2140 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002141 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002142 else
Richard Smithc5b05522012-03-12 07:56:15 +00002143 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002144 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002145 return true;
2146 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002147 }
Mike Stump11289f42009-09-09 15:08:12 +00002148
Richard Smithfe904f02012-05-15 21:29:55 +00002149 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002150 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002151 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2152 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002153 // Look ahead to the next token to try to figure out what this declaration
2154 // was supposed to be.
2155 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002156 case tok::l_paren: {
2157 // static x(4); // 'x' is not a type
2158 // x(int n); // 'x' is not a type
2159 // x (*p)[]; // 'x' is a type
2160 //
2161 // Since we're in an error case (or the rare 'implicit int in C++' MS
2162 // extension), we can afford to perform a tentative parse to determine
2163 // which case we're in.
2164 TentativeParsingAction PA(*this);
2165 ConsumeToken();
2166 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2167 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002168
2169 if (TPR != TPResult::False()) {
2170 // The identifier is followed by a parenthesized declarator.
2171 // It's supposed to be a type.
2172 break;
2173 }
2174
2175 // If we're in a context where we could be declaring a constructor,
2176 // check whether this is a constructor declaration with a bogus name.
2177 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2178 IdentifierInfo *II = Tok.getIdentifierInfo();
2179 if (Actions.isCurrentClassNameTypo(II, SS)) {
2180 Diag(Loc, diag::err_constructor_bad_name)
2181 << Tok.getIdentifierInfo() << II
2182 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2183 Tok.setIdentifierInfo(II);
2184 }
2185 }
2186 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002187 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002188 case tok::comma:
2189 case tok::equal:
2190 case tok::kw_asm:
2191 case tok::l_brace:
2192 case tok::l_square:
2193 case tok::semi:
2194 // This looks like a variable or function declaration. The type is
2195 // probably missing. We're done parsing decl-specifiers.
2196 if (SS)
2197 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2198 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002199
2200 default:
2201 // This is probably supposed to be a type. This includes cases like:
2202 // int f(itn);
2203 // struct S { unsinged : 4; };
2204 break;
2205 }
2206 }
2207
Chad Rosierc1183952012-06-26 22:30:43 +00002208 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002209 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002210 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002211 IdentifierInfo *II = Tok.getIdentifierInfo();
2212 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002213 // The action emitted a diagnostic, so we don't have to.
2214 if (T) {
2215 // The action has suggested that the type T could be used. Set that as
2216 // the type in the declaration specifiers, consume the would-be type
2217 // name token, and we're done.
2218 const char *PrevSpec;
2219 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002220 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002221 DS.SetRangeEnd(Tok.getLocation());
2222 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002223 // There may be other declaration specifiers after this.
2224 return true;
2225 } else if (II != Tok.getIdentifierInfo()) {
2226 // If no type was suggested, the correction is to a keyword
2227 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002228 // There may be other declaration specifiers after this.
2229 return true;
2230 }
Chad Rosierc1183952012-06-26 22:30:43 +00002231
Douglas Gregor15e56022009-10-13 23:27:22 +00002232 // Fall through; the action had no suggestion for us.
2233 } else {
2234 // The action did not emit a diagnostic, so emit one now.
2235 SourceRange R;
2236 if (SS) R = SS->getRange();
2237 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2238 }
Mike Stump11289f42009-09-09 15:08:12 +00002239
Douglas Gregor15e56022009-10-13 23:27:22 +00002240 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002241 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002242 DS.SetRangeEnd(Tok.getLocation());
2243 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002244
Chris Lattner20a0c612009-04-14 21:34:55 +00002245 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2246 // avoid rippling error messages on subsequent uses of the same type,
2247 // could be useful if #include was forgotten.
2248 return false;
2249}
2250
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002251/// \brief Determine the declaration specifier context from the declarator
2252/// context.
2253///
2254/// \param Context the declarator context, which is one of the
2255/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002256Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002257Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2258 if (Context == Declarator::MemberContext)
2259 return DSC_class;
2260 if (Context == Declarator::FileContext)
2261 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002262 if (Context == Declarator::TrailingReturnContext)
2263 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002264 return DSC_normal;
2265}
2266
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002267/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2268///
2269/// FIXME: Simply returns an alignof() expression if the argument is a
2270/// type. Ideally, the type should be propagated directly into Sema.
2271///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002272/// [C11] type-id
2273/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002274/// [C++0x] type-id ...[opt]
2275/// [C++0x] assignment-expression ...[opt]
2276ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2277 SourceLocation &EllipsisLoc) {
2278 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002279 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002280 SourceLocation TypeLoc = Tok.getLocation();
2281 ParsedType Ty = ParseTypeName().get();
2282 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002283 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2284 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002285 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002286 ER = ParseConstantExpression();
2287
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002288 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002289 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002290
2291 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002292}
2293
2294/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2295/// attribute to Attrs.
2296///
2297/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002298/// [C11] '_Alignas' '(' type-id ')'
2299/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002300/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2301/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002302void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002303 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002304 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2305 "Not an alignment-specifier!");
2306
Richard Smithd11c7a12013-01-29 01:48:07 +00002307 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2308 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002309
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002310 BalancedDelimiterTracker T(*this, tok::l_paren);
2311 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002312 return;
2313
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002314 SourceLocation EllipsisLoc;
2315 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002316 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002317 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002318 return;
2319 }
2320
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002321 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002322 if (EndLoc)
2323 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002324
Aaron Ballman00e99962013-08-31 01:11:41 +00002325 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002326 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002327 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2328 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002329}
2330
Richard Smith404dfb42013-11-19 22:47:36 +00002331/// Determine whether we're looking at something that might be a declarator
2332/// in a simple-declaration. If it can't possibly be a declarator, maybe
2333/// diagnose a missing semicolon after a prior tag definition in the decl
2334/// specifier.
2335///
2336/// \return \c true if an error occurred and this can't be any kind of
2337/// declaration.
2338bool
2339Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2340 DeclSpecContext DSContext,
2341 LateParsedAttrList *LateAttrs) {
2342 assert(DS.hasTagDefinition() && "shouldn't call this");
2343
2344 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002345
2346 if (getLangOpts().CPlusPlus &&
2347 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2348 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2349 TryAnnotateCXXScopeToken(EnteringContext)) {
2350 SkipMalformedDecl();
2351 return true;
2352 }
2353
Richard Smith698875a2013-11-20 23:40:57 +00002354 bool HasScope = Tok.is(tok::annot_cxxscope);
2355 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2356 Token AfterScope = HasScope ? NextToken() : Tok;
2357
Richard Smith404dfb42013-11-19 22:47:36 +00002358 // Determine whether the following tokens could possibly be a
2359 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002360 bool MightBeDeclarator = true;
2361 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2362 // A declarator-id can't start with 'typename'.
2363 MightBeDeclarator = false;
2364 } else if (AfterScope.is(tok::annot_template_id)) {
2365 // If we have a type expressed as a template-id, this cannot be a
2366 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2367 TemplateIdAnnotation *Annot =
2368 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2369 if (Annot->Kind == TNK_Type_template)
2370 MightBeDeclarator = false;
2371 } else if (AfterScope.is(tok::identifier)) {
2372 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2373
Richard Smith404dfb42013-11-19 22:47:36 +00002374 // These tokens cannot come after the declarator-id in a
2375 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002376 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2377 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2378 Next.is(tok::coloncolon)) {
2379 // Missing a semicolon.
2380 MightBeDeclarator = false;
2381 } else if (HasScope) {
2382 // If the declarator-id has a scope specifier, it must redeclare a
2383 // previously-declared entity. If that's a type (and this is not a
2384 // typedef), that's an error.
2385 CXXScopeSpec SS;
2386 Actions.RestoreNestedNameSpecifierAnnotation(
2387 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2388 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2389 Sema::NameClassification Classification = Actions.ClassifyName(
2390 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2391 /*IsAddressOfOperand*/false);
2392 switch (Classification.getKind()) {
2393 case Sema::NC_Error:
2394 SkipMalformedDecl();
2395 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002396
Richard Smith698875a2013-11-20 23:40:57 +00002397 case Sema::NC_Keyword:
2398 case Sema::NC_NestedNameSpecifier:
2399 llvm_unreachable("typo correction and nested name specifiers not "
2400 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002401
Richard Smith698875a2013-11-20 23:40:57 +00002402 case Sema::NC_Type:
2403 case Sema::NC_TypeTemplate:
2404 // Not a previously-declared non-type entity.
2405 MightBeDeclarator = false;
2406 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002407
Richard Smith698875a2013-11-20 23:40:57 +00002408 case Sema::NC_Unknown:
2409 case Sema::NC_Expression:
2410 case Sema::NC_VarTemplate:
2411 case Sema::NC_FunctionTemplate:
2412 // Might be a redeclaration of a prior entity.
2413 break;
2414 }
Richard Smith404dfb42013-11-19 22:47:36 +00002415 }
Richard Smith404dfb42013-11-19 22:47:36 +00002416 }
2417
Richard Smith698875a2013-11-20 23:40:57 +00002418 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002419 return false;
2420
2421 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
2422 diag::err_expected_semi_after_tagdecl)
2423 << DeclSpec::getSpecifierName(DS.getTypeSpecType());
2424
2425 // Try to recover from the typo, by dropping the tag definition and parsing
2426 // the problematic tokens as a type.
2427 //
2428 // FIXME: Split the DeclSpec into pieces for the standalone
2429 // declaration and pieces for the following declaration, instead
2430 // of assuming that all the other pieces attach to new declaration,
2431 // and call ParsedFreeStandingDeclSpec as appropriate.
2432 DS.ClearTypeSpecType();
2433 ParsedTemplateInfo NotATemplate;
2434 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2435 return false;
2436}
2437
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002438/// ParseDeclarationSpecifiers
2439/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002440/// storage-class-specifier declaration-specifiers[opt]
2441/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002442/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002443/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002444/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002445/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002446///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002447/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002448/// 'typedef'
2449/// 'extern'
2450/// 'static'
2451/// 'auto'
2452/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002453/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002454/// [C++11] 'thread_local'
2455/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002456/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002457/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002458/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002459/// [C++] 'virtual'
2460/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002461/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002462/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002463/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002464
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002465///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002466void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002467 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002468 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002469 DeclSpecContext DSContext,
2470 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002471 if (DS.getSourceRange().isInvalid()) {
2472 DS.SetRangeStart(Tok.getLocation());
2473 DS.SetRangeEnd(Tok.getLocation());
2474 }
Chad Rosierc1183952012-06-26 22:30:43 +00002475
Douglas Gregordf593fb2011-11-07 17:33:42 +00002476 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002477 bool AttrsLastTime = false;
2478 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002479 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002480 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002481 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002482 unsigned DiagID = 0;
2483
Chris Lattner4d8f8732006-11-28 05:05:08 +00002484 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002485
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002486 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002487 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002488 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002489 if (!AttrsLastTime)
2490 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002491 else {
2492 // Reject C++11 attributes that appertain to decl specifiers as
2493 // we don't support any C++11 attributes that appertain to decl
2494 // specifiers. This also conforms to what g++ 4.8 is doing.
2495 ProhibitCXX11Attributes(attrs);
2496
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002497 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002498 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002499
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002500 // If this is not a declaration specifier token, we're done reading decl
2501 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002502 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002503 return;
Mike Stump11289f42009-09-09 15:08:12 +00002504
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002505 case tok::l_square:
2506 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002507 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002508 goto DoneWithDeclSpec;
2509
2510 ProhibitAttributes(attrs);
2511 // FIXME: It would be good to recover by accepting the attributes,
2512 // but attempting to do that now would cause serious
2513 // madness in terms of diagnostics.
2514 attrs.clear();
2515 attrs.Range = SourceRange();
2516
2517 ParseCXX11Attributes(attrs);
2518 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002519 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002520
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002521 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002522 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002523 if (DS.hasTypeSpecifier()) {
2524 bool AllowNonIdentifiers
2525 = (getCurScope()->getFlags() & (Scope::ControlScope |
2526 Scope::BlockScope |
2527 Scope::TemplateParamScope |
2528 Scope::FunctionPrototypeScope |
2529 Scope::AtCatchScope)) == 0;
2530 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002531 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002532 (DSContext == DSC_class && DS.isFriendSpecified());
2533
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002534 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002535 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002536 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002537 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002538 }
2539
Douglas Gregor80039242011-02-15 20:33:25 +00002540 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2541 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2542 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002543 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002544 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002545 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002546 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002547 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002548 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002549
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002550 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002551 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002552 }
2553
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002554 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002555 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002556 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002557 if (!DS.hasTypeSpecifier())
2558 DS.SetTypeSpecError();
2559 goto DoneWithDeclSpec;
2560 }
John McCall8bc2a702010-03-01 18:20:46 +00002561 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2562 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002563 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002564
2565 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002566 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002567 goto DoneWithDeclSpec;
2568
John McCall9dab4e62009-12-12 11:40:51 +00002569 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002570 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2571 Tok.getAnnotationRange(),
2572 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002573
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002574 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002575 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002576 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002577 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002578 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002579 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002580
2581 // C++ [class.qual]p2:
2582 // In a lookup in which the constructor is an acceptable lookup
2583 // result and the nested-name-specifier nominates a class C:
2584 //
2585 // - if the name specified after the
2586 // nested-name-specifier, when looked up in C, is the
2587 // injected-class-name of C (Clause 9), or
2588 //
2589 // - if the name specified after the nested-name-specifier
2590 // is the same as the identifier or the
2591 // simple-template-id's template-name in the last
2592 // component of the nested-name-specifier,
2593 //
2594 // the name is instead considered to name the constructor of
2595 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002596 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002597 // Thus, if the template-name is actually the constructor
2598 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002599 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002600 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002601 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002602 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002603 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002604 if (isConstructorDeclarator()) {
2605 // The user meant this to be an out-of-line constructor
2606 // definition, but template arguments are not allowed
2607 // there. Just allow this as a constructor; we'll
2608 // complain about it later.
2609 goto DoneWithDeclSpec;
2610 }
2611
2612 // The user meant this to name a type, but it actually names
2613 // a constructor with some extraneous template
2614 // arguments. Complain, then parse it as a type as the user
2615 // intended.
2616 Diag(TemplateId->TemplateNameLoc,
2617 diag::err_out_of_line_template_id_names_constructor)
2618 << TemplateId->Name;
2619 }
2620
John McCall9dab4e62009-12-12 11:40:51 +00002621 DS.getTypeSpecScope() = SS;
2622 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002623 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002624 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002625 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002626 continue;
2627 }
2628
Douglas Gregorc5790df2009-09-28 07:26:33 +00002629 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002630 DS.getTypeSpecScope() = SS;
2631 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002632 if (Tok.getAnnotationValue()) {
2633 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002634 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002635 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002636 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002637 if (isInvalid)
2638 break;
John McCallba7bf592010-08-24 05:47:05 +00002639 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002640 else
2641 DS.SetTypeSpecError();
2642 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2643 ConsumeToken(); // The typename
2644 }
2645
Douglas Gregor167fa622009-03-25 15:40:00 +00002646 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002647 goto DoneWithDeclSpec;
2648
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002649 // If we're in a context where the identifier could be a class name,
2650 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002651 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002652 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002653 &SS)) {
2654 if (isConstructorDeclarator())
2655 goto DoneWithDeclSpec;
2656
2657 // As noted in C++ [class.qual]p2 (cited above), when the name
2658 // of the class is qualified in a context where it could name
2659 // a constructor, its a constructor name. However, we've
2660 // looked at the declarator, and the user probably meant this
2661 // to be a type. Complain that it isn't supposed to be treated
2662 // as a type, then proceed to parse it as a type.
2663 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2664 << Next.getIdentifierInfo();
2665 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002666
John McCallba7bf592010-08-24 05:47:05 +00002667 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2668 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002669 getCurScope(), &SS,
2670 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002671 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002672 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002673
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002674 // If the referenced identifier is not a type, then this declspec is
2675 // erroneous: We already checked about that it has no type specifier, and
2676 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002677 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002678 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002679 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002680 ParsedAttributesWithRange Attrs(AttrFactory);
2681 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2682 if (!Attrs.empty()) {
2683 AttrsLastTime = true;
2684 attrs.takeAllFrom(Attrs);
2685 }
2686 continue;
2687 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002688 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002689 }
Mike Stump11289f42009-09-09 15:08:12 +00002690
John McCall9dab4e62009-12-12 11:40:51 +00002691 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002692 ConsumeToken(); // The C++ scope.
2693
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002694 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002695 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002696 if (isInvalid)
2697 break;
Mike Stump11289f42009-09-09 15:08:12 +00002698
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002699 DS.SetRangeEnd(Tok.getLocation());
2700 ConsumeToken(); // The typename.
2701
2702 continue;
2703 }
Mike Stump11289f42009-09-09 15:08:12 +00002704
Chris Lattnere387d9e2009-01-21 19:48:37 +00002705 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002706 // If we've previously seen a tag definition, we were almost surely
2707 // missing a semicolon after it.
2708 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2709 goto DoneWithDeclSpec;
2710
John McCallba7bf592010-08-24 05:47:05 +00002711 if (Tok.getAnnotationValue()) {
2712 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002714 DiagID, T);
2715 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002716 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002717
Chris Lattner005fc1b2010-04-05 18:18:31 +00002718 if (isInvalid)
2719 break;
2720
Chris Lattnere387d9e2009-01-21 19:48:37 +00002721 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2722 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002723
Chris Lattnere387d9e2009-01-21 19:48:37 +00002724 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2725 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002726 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002727 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002728 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002729
Chris Lattnere387d9e2009-01-21 19:48:37 +00002730 continue;
2731 }
Mike Stump11289f42009-09-09 15:08:12 +00002732
Douglas Gregor06873092011-04-28 15:48:45 +00002733 case tok::kw___is_signed:
2734 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2735 // typically treats it as a trait. If we see __is_signed as it appears
2736 // in libstdc++, e.g.,
2737 //
2738 // static const bool __is_signed;
2739 //
2740 // then treat __is_signed as an identifier rather than as a keyword.
2741 if (DS.getTypeSpecType() == TST_bool &&
2742 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002743 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2744 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002745
2746 // We're done with the declaration-specifiers.
2747 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002748
Chris Lattner16fac4f2008-07-26 01:18:38 +00002749 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002750 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002751 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002752 // In C++, check to see if this is a scope specifier like foo::bar::, if
2753 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002754 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002755 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002756 if (!DS.hasTypeSpecifier())
2757 DS.SetTypeSpecError();
2758 goto DoneWithDeclSpec;
2759 }
2760 if (!Tok.is(tok::identifier))
2761 continue;
2762 }
Mike Stump11289f42009-09-09 15:08:12 +00002763
Chris Lattner16fac4f2008-07-26 01:18:38 +00002764 // This identifier can only be a typedef name if we haven't already seen
2765 // a type-specifier. Without this check we misparse:
2766 // typedef int X; struct Y { short X; }; as 'short int'.
2767 if (DS.hasTypeSpecifier())
2768 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002769
John Thompson22334602010-02-05 00:12:22 +00002770 // Check for need to substitute AltiVec keyword tokens.
2771 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2772 break;
2773
Richard Smith3092a3b2012-05-09 18:56:43 +00002774 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2775 // allow the use of a typedef name as a type specifier.
2776 if (DS.isTypeAltiVecVector())
2777 goto DoneWithDeclSpec;
2778
John McCallba7bf592010-08-24 05:47:05 +00002779 ParsedType TypeRep =
2780 Actions.getTypeName(*Tok.getIdentifierInfo(),
2781 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002782
Chris Lattner6cc055a2009-04-12 20:42:31 +00002783 // If this is not a typedef name, don't parse it as part of the declspec,
2784 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002785 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002786 ParsedAttributesWithRange Attrs(AttrFactory);
2787 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2788 if (!Attrs.empty()) {
2789 AttrsLastTime = true;
2790 attrs.takeAllFrom(Attrs);
2791 }
2792 continue;
2793 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002794 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002795 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002796
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002797 // If we're in a context where the identifier could be a class name,
2798 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002799 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002800 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002801 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002802 goto DoneWithDeclSpec;
2803
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002804 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002805 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002806 if (isInvalid)
2807 break;
Mike Stump11289f42009-09-09 15:08:12 +00002808
Chris Lattner16fac4f2008-07-26 01:18:38 +00002809 DS.SetRangeEnd(Tok.getLocation());
2810 ConsumeToken(); // The identifier
2811
2812 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2813 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002814 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002815 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002816 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002817
Steve Naroffcd5e7822008-09-22 10:28:57 +00002818 // Need to support trailing type qualifiers (e.g. "id<p> const").
2819 // If a type specifier follows, it will be diagnosed elsewhere.
2820 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002821 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002822
2823 // type-name
2824 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002825 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002826 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002827 // This template-id does not refer to a type name, so we're
2828 // done with the type-specifiers.
2829 goto DoneWithDeclSpec;
2830 }
2831
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002832 // If we're in a context where the template-id could be a
2833 // constructor name or specialization, check whether this is a
2834 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002835 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002836 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002837 isConstructorDeclarator())
2838 goto DoneWithDeclSpec;
2839
Douglas Gregor7f741122009-02-25 19:37:18 +00002840 // Turn the template-id annotation token into a type annotation
2841 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002842 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002843 continue;
2844 }
2845
Chris Lattnere37e2332006-08-15 04:50:22 +00002846 // GNU attributes support.
2847 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002848 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002849 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002850
2851 // Microsoft declspec support.
2852 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002853 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002854 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002855
Steve Naroff44ac7772008-12-25 14:16:32 +00002856 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002857 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002858 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002859 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002860 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002861 // FIXME: This does not work correctly if it is set to be a declspec
2862 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002863 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2864 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002865 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002866 }
Eli Friedman53339e02009-06-08 23:27:34 +00002867
Aaron Ballman317a77f2013-05-22 23:25:32 +00002868 case tok::kw___sptr:
2869 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002870 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002871 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002872 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002873 case tok::kw___cdecl:
2874 case tok::kw___stdcall:
2875 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002876 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002877 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002878 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002879 continue;
2880
Dawn Perchik335e16b2010-09-03 01:29:35 +00002881 // Borland single token adornments.
2882 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002883 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002884 continue;
2885
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002886 // OpenCL single token adornments.
2887 case tok::kw___kernel:
2888 ParseOpenCLAttributes(DS.getAttributes());
2889 continue;
2890
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002891 // storage-class-specifier
2892 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002893 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2894 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002895 break;
2896 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002897 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002898 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002899 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2900 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002901 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002902 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002903 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2904 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002905 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002906 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002907 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002908 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002909 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2910 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002911 break;
2912 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002913 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002914 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002915 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2916 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002917 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002918 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002919 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002920 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002921 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2922 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002923 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002924 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2925 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002926 break;
2927 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002928 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2929 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002930 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002931 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002932 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2933 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002934 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002935 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002936 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2937 PrevSpec, DiagID);
2938 break;
2939 case tok::kw_thread_local:
2940 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2941 PrevSpec, DiagID);
2942 break;
2943 case tok::kw__Thread_local:
2944 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2945 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002946 break;
Mike Stump11289f42009-09-09 15:08:12 +00002947
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002948 // function-specifier
2949 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002950 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002951 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002952 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002953 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002954 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002955 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002956 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002957 break;
Richard Smith0015f092013-01-17 22:16:11 +00002958 case tok::kw__Noreturn:
2959 if (!getLangOpts().C11)
2960 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002961 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002962 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002963
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002964 // alignment-specifier
2965 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002966 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002967 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002968 ParseAlignmentSpecifier(DS.getAttributes());
2969 continue;
2970
Anders Carlssoncd8db412009-05-06 04:46:28 +00002971 // friend
2972 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002973 if (DSContext == DSC_class)
2974 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2975 else {
2976 PrevSpec = ""; // not actually used by the diagnostic
2977 DiagID = diag::err_friend_invalid_in_context;
2978 isInvalid = true;
2979 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002980 break;
Mike Stump11289f42009-09-09 15:08:12 +00002981
Douglas Gregor26701a42011-09-09 02:06:17 +00002982 // Modules
2983 case tok::kw___module_private__:
2984 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2985 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002986
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002987 // constexpr
2988 case tok::kw_constexpr:
2989 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2990 break;
2991
Chris Lattnere387d9e2009-01-21 19:48:37 +00002992 // type-specifier
2993 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002994 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2995 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002996 break;
2997 case tok::kw_long:
2998 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002999 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3000 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003001 else
John McCall49bfce42009-08-03 20:12:06 +00003002 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3003 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003004 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003005 case tok::kw___int64:
3006 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3007 DiagID);
3008 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003009 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003010 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3011 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003012 break;
3013 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003014 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3015 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003016 break;
3017 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003018 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3019 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003020 break;
3021 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003022 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3023 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003024 break;
3025 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003026 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3027 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003028 break;
3029 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003030 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3031 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003032 break;
3033 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3035 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003036 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003037 case tok::kw___int128:
3038 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3039 DiagID);
3040 break;
3041 case tok::kw_half:
3042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3043 DiagID);
3044 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003045 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003046 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3047 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003048 break;
3049 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003050 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3051 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003052 break;
3053 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3055 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003056 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003057 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003058 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3059 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003060 break;
3061 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003062 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3063 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003064 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003065 case tok::kw_bool:
3066 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003067 if (Tok.is(tok::kw_bool) &&
3068 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3069 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3070 PrevSpec = ""; // Not used by the diagnostic.
3071 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003072 // For better error recovery.
3073 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003074 isInvalid = true;
3075 } else {
3076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3077 DiagID);
3078 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003079 break;
3080 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003081 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3082 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003083 break;
3084 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3086 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003087 break;
3088 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3090 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003091 break;
John Thompson22334602010-02-05 00:12:22 +00003092 case tok::kw___vector:
3093 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3094 break;
3095 case tok::kw___pixel:
3096 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3097 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003098 case tok::kw_image1d_t:
3099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
3100 PrevSpec, DiagID);
3101 break;
3102 case tok::kw_image1d_array_t:
3103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
3104 PrevSpec, DiagID);
3105 break;
3106 case tok::kw_image1d_buffer_t:
3107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3108 PrevSpec, DiagID);
3109 break;
3110 case tok::kw_image2d_t:
3111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3112 PrevSpec, DiagID);
3113 break;
3114 case tok::kw_image2d_array_t:
3115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3116 PrevSpec, DiagID);
3117 break;
3118 case tok::kw_image3d_t:
3119 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3120 PrevSpec, DiagID);
3121 break;
Guy Benyei61054192013-02-07 10:55:47 +00003122 case tok::kw_sampler_t:
3123 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3124 PrevSpec, DiagID);
3125 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003126 case tok::kw_event_t:
3127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3128 PrevSpec, DiagID);
3129 break;
John McCall39439732011-04-09 22:50:59 +00003130 case tok::kw___unknown_anytype:
3131 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3132 PrevSpec, DiagID);
3133 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003134
3135 // class-specifier:
3136 case tok::kw_class:
3137 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003138 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003139 case tok::kw_union: {
3140 tok::TokenKind Kind = Tok.getKind();
3141 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003142
3143 // These are attributes following class specifiers.
3144 // To produce better diagnostic, we parse them when
3145 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003146 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003147 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003148 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003149
3150 // If there are attributes following class specifier,
3151 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003152 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003153 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003154 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003155 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003156 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003157 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003158
3159 // enum-specifier:
3160 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003161 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003162 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003163 continue;
3164
3165 // cv-qualifier:
3166 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003167 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003168 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003169 break;
3170 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003171 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003172 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003173 break;
3174 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003175 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003176 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003177 break;
3178
Douglas Gregor333489b2009-03-27 23:10:48 +00003179 // C++ typename-specifier:
3180 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003181 if (TryAnnotateTypeOrScopeToken()) {
3182 DS.SetTypeSpecError();
3183 goto DoneWithDeclSpec;
3184 }
3185 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003186 continue;
3187 break;
3188
Chris Lattnere387d9e2009-01-21 19:48:37 +00003189 // GNU typeof support.
3190 case tok::kw_typeof:
3191 ParseTypeofSpecifier(DS);
3192 continue;
3193
David Blaikie15a430a2011-12-04 05:04:18 +00003194 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003195 ParseDecltypeSpecifier(DS);
3196 continue;
3197
Alexis Hunt4a257072011-05-19 05:37:45 +00003198 case tok::kw___underlying_type:
3199 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003200 continue;
3201
3202 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003203 // C11 6.7.2.4/4:
3204 // If the _Atomic keyword is immediately followed by a left parenthesis,
3205 // it is interpreted as a type specifier (with a type name), not as a
3206 // type qualifier.
3207 if (NextToken().is(tok::l_paren)) {
3208 ParseAtomicSpecifier(DS);
3209 continue;
3210 }
3211 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3212 getLangOpts());
3213 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003214
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003215 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003216 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003217 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003218 goto DoneWithDeclSpec;
3219 case tok::kw___private:
3220 case tok::kw___global:
3221 case tok::kw___local:
3222 case tok::kw___constant:
3223 case tok::kw___read_only:
3224 case tok::kw___write_only:
3225 case tok::kw___read_write:
3226 ParseOpenCLQualifiers(DS);
3227 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003228
Steve Naroffcfdf6162008-06-05 00:02:44 +00003229 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003230 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003231 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3232 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003233 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003234 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003235
Douglas Gregor3a001f42010-11-19 17:10:50 +00003236 if (!ParseObjCProtocolQualifiers(DS))
3237 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3238 << FixItHint::CreateInsertion(Loc, "id")
3239 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003240
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003241 // Need to support trailing type qualifiers (e.g. "id<p> const").
3242 // If a type specifier follows, it will be diagnosed elsewhere.
3243 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003244 }
John McCall49bfce42009-08-03 20:12:06 +00003245 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003246 if (isInvalid) {
3247 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003248 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003249
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003250 if (DiagID == diag::ext_duplicate_declspec)
3251 Diag(Tok, DiagID)
3252 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3253 else
3254 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003255 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003256
Chris Lattner2e232092008-03-13 06:29:04 +00003257 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003258 if (DiagID != diag::err_bool_redeclaration)
3259 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003260
3261 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003262 }
3263}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003264
Chris Lattner70ae4912007-10-29 04:42:53 +00003265/// ParseStructDeclaration - Parse a struct declaration without the terminating
3266/// semicolon.
3267///
Chris Lattner90a26b02007-01-23 04:38:16 +00003268/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003269/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003270/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003271/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003272/// struct-declarator-list:
3273/// struct-declarator
3274/// struct-declarator-list ',' struct-declarator
3275/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3276/// struct-declarator:
3277/// declarator
3278/// [GNU] declarator attributes[opt]
3279/// declarator[opt] ':' constant-expression
3280/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3281///
Chris Lattnera12405b2008-04-10 06:46:29 +00003282void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003283ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003284
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003285 if (Tok.is(tok::kw___extension__)) {
3286 // __extension__ silences extension warnings in the subexpression.
3287 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003288 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003289 return ParseStructDeclaration(DS, Fields);
3290 }
Mike Stump11289f42009-09-09 15:08:12 +00003291
Steve Naroff97170802007-08-20 22:28:22 +00003292 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003293 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003294
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003295 // If there are no declarators, this is a free-standing declaration
3296 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003297 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003298 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3299 DS);
3300 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003301 return;
3302 }
3303
3304 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003305 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003306 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003307 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003308 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003309 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003310
Bill Wendling44426052012-12-20 19:22:21 +00003311 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003312 if (!FirstDeclarator)
3313 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003314
Steve Naroff97170802007-08-20 22:28:22 +00003315 /// struct-declarator: declarator
3316 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003317 if (Tok.isNot(tok::colon)) {
3318 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3319 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003320 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003321 }
Mike Stump11289f42009-09-09 15:08:12 +00003322
Chris Lattner76c72282007-10-09 17:33:22 +00003323 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003324 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003325 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003326 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003327 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003328 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003329 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003330 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003331
Steve Naroff97170802007-08-20 22:28:22 +00003332 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003333 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003334
John McCallcfefb6d2009-11-03 02:38:08 +00003335 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003336 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003337
Steve Naroff97170802007-08-20 22:28:22 +00003338 // If we don't have a comma, it is either the end of the list (a ';')
3339 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003340 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003341 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003342
Steve Naroff97170802007-08-20 22:28:22 +00003343 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003344 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003345
John McCallcfefb6d2009-11-03 02:38:08 +00003346 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003347 }
Steve Naroff97170802007-08-20 22:28:22 +00003348}
3349
3350/// ParseStructUnionBody
3351/// struct-contents:
3352/// struct-declaration-list
3353/// [EXT] empty
3354/// [GNU] "struct-declaration-list" without terminatoring ';'
3355/// struct-declaration-list:
3356/// struct-declaration
3357/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003358/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003359///
Chris Lattner1300fb92007-01-23 23:42:53 +00003360void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003361 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003362 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3363 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003364 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003365
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003366 BalancedDelimiterTracker T(*this, tok::l_brace);
3367 if (T.consumeOpen())
3368 return;
Mike Stump11289f42009-09-09 15:08:12 +00003369
Douglas Gregor658b9552009-01-09 22:42:13 +00003370 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003371 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003372
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003373 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003374
Chris Lattner7b9ace62007-01-23 20:11:08 +00003375 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003376 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003377 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003378
Chris Lattner736ed5d2007-06-09 05:59:07 +00003379 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003380 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003381 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003382 continue;
3383 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003384
Andy Gibbsc804e082013-04-03 09:46:04 +00003385 // Parse _Static_assert declaration.
3386 if (Tok.is(tok::kw__Static_assert)) {
3387 SourceLocation DeclEnd;
3388 ParseStaticAssertDeclaration(DeclEnd);
3389 continue;
3390 }
3391
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003392 if (Tok.is(tok::annot_pragma_pack)) {
3393 HandlePragmaPack();
3394 continue;
3395 }
3396
3397 if (Tok.is(tok::annot_pragma_align)) {
3398 HandlePragmaAlign();
3399 continue;
3400 }
3401
John McCallcfefb6d2009-11-03 02:38:08 +00003402 if (!Tok.is(tok::at)) {
3403 struct CFieldCallback : FieldCallback {
3404 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003405 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003406 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003407
John McCall48871652010-08-21 09:40:31 +00003408 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003409 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003410 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3411
Eli Friedman934dbbf2012-08-08 23:53:27 +00003412 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003413 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003414 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003415 FD.D.getDeclSpec().getSourceRange().getBegin(),
3416 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003417 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003418 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003419 }
John McCallcfefb6d2009-11-03 02:38:08 +00003420 } Callback(*this, TagDecl, FieldDecls);
3421
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003422 // Parse all the comma separated declarators.
3423 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003424 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003425 } else { // Handle @defs
3426 ConsumeToken();
3427 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3428 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003429 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003430 continue;
3431 }
3432 ConsumeToken();
3433 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3434 if (!Tok.is(tok::identifier)) {
3435 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003436 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003437 continue;
3438 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003439 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003440 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003441 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003442 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3443 ConsumeToken();
3444 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003445 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003446
Chris Lattner76c72282007-10-09 17:33:22 +00003447 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003448 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003449 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003450 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003451 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003452 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003453 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3454 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003455 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner245c5332010-02-02 00:37:27 +00003456 // If we stopped at a ';', eat it.
3457 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003458 }
3459 }
Mike Stump11289f42009-09-09 15:08:12 +00003460
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003461 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003462
John McCall084e83d2011-03-24 11:26:52 +00003463 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003464 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003465 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003466
Douglas Gregor0be31a22010-07-02 17:43:08 +00003467 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003468 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003469 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003470 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003471 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003472 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3473 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003474}
3475
Chris Lattner3b561a32006-08-13 00:12:11 +00003476/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003477/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003478/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003479///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003480/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3481/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003482/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3483/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003484/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003485/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003486///
Richard Smith7d137e32012-03-23 03:33:32 +00003487/// [C++11] enum-head '{' enumerator-list[opt] '}'
3488/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003489///
Richard Smith7d137e32012-03-23 03:33:32 +00003490/// enum-head: [C++11]
3491/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3492/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3493/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003494///
Richard Smith7d137e32012-03-23 03:33:32 +00003495/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003496/// 'enum'
3497/// 'enum' 'class'
3498/// 'enum' 'struct'
3499///
Richard Smith7d137e32012-03-23 03:33:32 +00003500/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003501/// ':' type-specifier-seq
3502///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003503/// [C++] elaborated-type-specifier:
3504/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3505///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003506void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003507 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003508 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003509 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003510 if (Tok.is(tok::code_completion)) {
3511 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003512 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003513 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003514 }
John McCallcb432fa2011-07-06 05:58:41 +00003515
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003516 // If attributes exist after tag, parse them.
3517 ParsedAttributesWithRange attrs(AttrFactory);
3518 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003519 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003520
3521 // If declspecs exist after tag, parse them.
3522 while (Tok.is(tok::kw___declspec))
3523 ParseMicrosoftDeclSpec(attrs);
3524
Richard Smith0f8ee222012-01-10 01:33:14 +00003525 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003526 bool IsScopedUsingClassTag = false;
3527
John McCallbeae29a2012-06-23 22:30:04 +00003528 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003529 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3530 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3531 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003532 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003533 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003534
Bill Wendling44426052012-12-20 19:22:21 +00003535 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003536 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003537 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003538
3539 // They are allowed afterwards, though.
3540 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003541 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003542 while (Tok.is(tok::kw___declspec))
3543 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003544 }
Richard Smith7d137e32012-03-23 03:33:32 +00003545
John McCall6347b682012-05-07 06:16:58 +00003546 // C++11 [temp.explicit]p12:
3547 // The usual access controls do not apply to names used to specify
3548 // explicit instantiations.
3549 // We extend this to also cover explicit specializations. Note that
3550 // we don't suppress if this turns out to be an elaborated type
3551 // specifier.
3552 bool shouldDelayDiagsInTag =
3553 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3554 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3555 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003556
Richard Smithbfdb1082012-03-12 08:56:40 +00003557 // Enum definitions should not be parsed in a trailing-return-type.
3558 bool AllowDeclaration = DSC != DSC_trailing;
3559
3560 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003561 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003562 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003563
Abramo Bagnarad7548482010-05-19 21:37:53 +00003564 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003565 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003566 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3567 // if a fixed underlying type is allowed.
3568 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003569
3570 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003571 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003572 return;
3573
3574 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003575 Diag(Tok, diag::err_expected_ident);
3576 if (Tok.isNot(tok::l_brace)) {
3577 // Has no name and is not a definition.
3578 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003579 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003580 return;
3581 }
3582 }
3583 }
Mike Stump11289f42009-09-09 15:08:12 +00003584
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003585 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003586 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003587 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003588 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003589
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003590 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003591 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003592 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003593 }
Mike Stump11289f42009-09-09 15:08:12 +00003594
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003595 // If an identifier is present, consume and remember it.
3596 IdentifierInfo *Name = 0;
3597 SourceLocation NameLoc;
3598 if (Tok.is(tok::identifier)) {
3599 Name = Tok.getIdentifierInfo();
3600 NameLoc = ConsumeToken();
3601 }
Mike Stump11289f42009-09-09 15:08:12 +00003602
Richard Smith0f8ee222012-01-10 01:33:14 +00003603 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003604 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3605 // declaration of a scoped enumeration.
3606 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003607 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003608 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003609 }
3610
John McCall6347b682012-05-07 06:16:58 +00003611 // Okay, end the suppression area. We'll decide whether to emit the
3612 // diagnostics in a second.
3613 if (shouldDelayDiagsInTag)
3614 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003615
Douglas Gregor0bf31402010-10-08 23:50:27 +00003616 TypeResult BaseType;
3617
Douglas Gregord1f69f62010-12-01 17:42:47 +00003618 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003619 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003620 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003621 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003622 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003623 // If we're in class scope, this can either be an enum declaration with
3624 // an underlying type, or a declaration of a bitfield member. We try to
3625 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003626 // (integer literal, sizeof); if it's still ambiguous, we then consider
3627 // anything that's a simple-type-specifier followed by '(' as an
3628 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003629 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003630 EnterExpressionEvaluationContext Unevaluated(Actions,
3631 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003632 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003633 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003634 // bit-field. This is the common case.
3635 if (TPR == TPResult::True())
3636 PossibleBitfield = true;
3637 // If the next token starts a type-specifier-seq, it may be either a
3638 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003639 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003640 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003641 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003642 GetLookAheadToken(2).getKind() == tok::semi) {
3643 // Consume the ':'.
3644 ConsumeToken();
3645 } else {
3646 // We have the start of a type-specifier-seq, so we have to perform
3647 // tentative parsing to determine whether we have an expression or a
3648 // type.
3649 TentativeParsingAction TPA(*this);
3650
3651 // Consume the ':'.
3652 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003653
3654 // If we see a type specifier followed by an open-brace, we have an
3655 // ambiguity between an underlying type and a C++11 braced
3656 // function-style cast. Resolve this by always treating it as an
3657 // underlying type.
3658 // FIXME: The standard is not entirely clear on how to disambiguate in
3659 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003660 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003661 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003662 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003663 // We'll parse this as a bitfield later.
3664 PossibleBitfield = true;
3665 TPA.Revert();
3666 } else {
3667 // We have a type-specifier-seq.
3668 TPA.Commit();
3669 }
3670 }
3671 } else {
3672 // Consume the ':'.
3673 ConsumeToken();
3674 }
3675
3676 if (!PossibleBitfield) {
3677 SourceRange Range;
3678 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003679
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003680 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003681 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003682 } else if (!getLangOpts().ObjC2) {
3683 if (getLangOpts().CPlusPlus)
3684 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3685 else
3686 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3687 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003688 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003689 }
3690
Richard Smith0f8ee222012-01-10 01:33:14 +00003691 // There are four options here. If we have 'friend enum foo;' then this is a
3692 // friend declaration, and cannot have an accompanying definition. If we have
3693 // 'enum foo;', then this is a forward declaration. If we have
3694 // 'enum foo {...' then this is a definition. Otherwise we have something
3695 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003696 //
3697 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3698 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3699 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3700 //
John McCallfaf5fb42010-08-26 23:41:50 +00003701 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003702 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003703 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003704 } else if (Tok.is(tok::l_brace)) {
3705 if (DS.isFriendSpecified()) {
3706 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3707 << SourceRange(DS.getFriendSpecLoc());
3708 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003709 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003710 TUK = Sema::TUK_Friend;
3711 } else {
3712 TUK = Sema::TUK_Definition;
3713 }
Richard Smith369b9f92012-06-25 21:37:02 +00003714 } else if (DSC != DSC_type_specifier &&
3715 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003716 (Tok.isAtStartOfLine() &&
3717 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003718 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3719 if (Tok.isNot(tok::semi)) {
3720 // A semicolon was missing after this declaration. Diagnose and recover.
3721 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3722 "enum");
3723 PP.EnterToken(Tok);
3724 Tok.setKind(tok::semi);
3725 }
John McCall6347b682012-05-07 06:16:58 +00003726 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003727 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003728 }
3729
3730 // If this is an elaborated type specifier, and we delayed
3731 // diagnostics before, just merge them into the current pool.
3732 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3733 diagsFromTag.redelay();
3734 }
Richard Smith7d137e32012-03-23 03:33:32 +00003735
3736 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003737 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003738 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003739 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003740 // Skip the rest of this declarator, up until the comma or semicolon.
3741 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003742 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003743 return;
3744 }
3745
3746 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3747 // Enumerations can't be explicitly instantiated.
3748 DS.SetTypeSpecError();
3749 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3750 return;
3751 }
3752
3753 assert(TemplateInfo.TemplateParams && "no template parameters");
3754 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3755 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003756 }
Chad Rosierc1183952012-06-26 22:30:43 +00003757
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003758 if (TUK == Sema::TUK_Reference)
3759 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003760
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003761 if (!Name && TUK != Sema::TUK_Definition) {
3762 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003763
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003764 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003765 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003766 return;
3767 }
Richard Smith7d137e32012-03-23 03:33:32 +00003768
Douglas Gregord6ab8742009-05-28 23:31:59 +00003769 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003770 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003771 const char *PrevSpec = 0;
3772 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003773 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003774 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003775 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003776 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003777 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003778
Douglas Gregorba41d012010-04-24 16:38:41 +00003779 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003780 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003781 // dependent tag.
3782 if (!Name) {
3783 DS.SetTypeSpecError();
3784 Diag(Tok, diag::err_expected_type_name_after_typename);
3785 return;
3786 }
Chad Rosierc1183952012-06-26 22:30:43 +00003787
Douglas Gregor0be31a22010-07-02 17:43:08 +00003788 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003789 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003790 NameLoc);
3791 if (Type.isInvalid()) {
3792 DS.SetTypeSpecError();
3793 return;
3794 }
Chad Rosierc1183952012-06-26 22:30:43 +00003795
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003796 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3797 NameLoc.isValid() ? NameLoc : StartLoc,
3798 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003799 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003800
Douglas Gregorba41d012010-04-24 16:38:41 +00003801 return;
3802 }
Mike Stump11289f42009-09-09 15:08:12 +00003803
John McCall48871652010-08-21 09:40:31 +00003804 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003805 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003806 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003807 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003808 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003809 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003810 }
Chad Rosierc1183952012-06-26 22:30:43 +00003811
Douglas Gregorba41d012010-04-24 16:38:41 +00003812 DS.SetTypeSpecError();
3813 return;
3814 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003815
Richard Smith369b9f92012-06-25 21:37:02 +00003816 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003817 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003818
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003819 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3820 NameLoc.isValid() ? NameLoc : StartLoc,
3821 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003822 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003823}
3824
Chris Lattnerc1915e22007-01-25 07:29:02 +00003825/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3826/// enumerator-list:
3827/// enumerator
3828/// enumerator-list ',' enumerator
3829/// enumerator:
3830/// enumeration-constant
3831/// enumeration-constant '=' constant-expression
3832/// enumeration-constant:
3833/// identifier
3834///
John McCall48871652010-08-21 09:40:31 +00003835void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003836 // Enter the scope of the enum body and start the definition.
3837 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003838 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003839
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003840 BalancedDelimiterTracker T(*this, tok::l_brace);
3841 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003842
Chris Lattner37256fb2007-08-27 17:24:30 +00003843 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003844 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003845 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003846
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003847 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003848
John McCall48871652010-08-21 09:40:31 +00003849 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003850
Chris Lattnerc1915e22007-01-25 07:29:02 +00003851 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003852 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003853 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3854 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003855
John McCall811a0f52010-10-22 23:36:17 +00003856 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003857 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003858 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003859 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003860 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003861
Chris Lattnerc1915e22007-01-25 07:29:02 +00003862 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003863 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003864 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003865
Chris Lattner76c72282007-10-09 17:33:22 +00003866 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003867 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003868 AssignedVal = ParseConstantExpression();
3869 if (AssignedVal.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003870 SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003871 }
Mike Stump11289f42009-09-09 15:08:12 +00003872
Chris Lattnerc1915e22007-01-25 07:29:02 +00003873 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003874 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3875 LastEnumConstDecl,
3876 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003877 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003878 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003879 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003880
Chris Lattner4ef40012007-06-11 01:28:17 +00003881 EnumConstantDecls.push_back(EnumConstDecl);
3882 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003883
Douglas Gregorce66d022010-09-07 14:51:08 +00003884 if (Tok.is(tok::identifier)) {
3885 // We're missing a comma between enumerators.
3886 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003887 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003888 << FixItHint::CreateInsertion(Loc, ", ");
3889 continue;
3890 }
Chad Rosierc1183952012-06-26 22:30:43 +00003891
Chris Lattner76c72282007-10-09 17:33:22 +00003892 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003893 break;
3894 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003895
Richard Smith5d164bc2011-10-15 05:09:34 +00003896 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003897 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003898 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3899 diag::ext_enumerator_list_comma_cxx :
3900 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003901 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003902 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003903 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3904 << FixItHint::CreateRemoval(CommaLoc);
3905 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003906 }
Mike Stump11289f42009-09-09 15:08:12 +00003907
Chris Lattnerc1915e22007-01-25 07:29:02 +00003908 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003909 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003910
Chris Lattnerc1915e22007-01-25 07:29:02 +00003911 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003912 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003913 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003914
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003915 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003916 EnumDecl, EnumConstantDecls,
3917 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003918 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003919
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003920 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003921 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3922 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003923
3924 // The next token must be valid after an enum definition. If not, a ';'
3925 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003926 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3927 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003928 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3929 // Push this token back into the preprocessor and change our current token
3930 // to ';' so that the rest of the code recovers as though there were an
3931 // ';' after the definition.
3932 PP.EnterToken(Tok);
3933 Tok.setKind(tok::semi);
3934 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003935}
Chris Lattner3b561a32006-08-13 00:12:11 +00003936
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003937/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003938/// start of a type-qualifier-list.
3939bool Parser::isTypeQualifier() const {
3940 switch (Tok.getKind()) {
3941 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003942
3943 // type-qualifier only in OpenCL
3944 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003945 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003946
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003947 // type-qualifier
3948 case tok::kw_const:
3949 case tok::kw_volatile:
3950 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003951 case tok::kw___private:
3952 case tok::kw___local:
3953 case tok::kw___global:
3954 case tok::kw___constant:
3955 case tok::kw___read_only:
3956 case tok::kw___read_write:
3957 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003958 return true;
3959 }
3960}
3961
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003962/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3963/// is definitely a type-specifier. Return false if it isn't part of a type
3964/// specifier or if we're not sure.
3965bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3966 switch (Tok.getKind()) {
3967 default: return false;
3968 // type-specifiers
3969 case tok::kw_short:
3970 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003971 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003972 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003973 case tok::kw_signed:
3974 case tok::kw_unsigned:
3975 case tok::kw__Complex:
3976 case tok::kw__Imaginary:
3977 case tok::kw_void:
3978 case tok::kw_char:
3979 case tok::kw_wchar_t:
3980 case tok::kw_char16_t:
3981 case tok::kw_char32_t:
3982 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003983 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003984 case tok::kw_float:
3985 case tok::kw_double:
3986 case tok::kw_bool:
3987 case tok::kw__Bool:
3988 case tok::kw__Decimal32:
3989 case tok::kw__Decimal64:
3990 case tok::kw__Decimal128:
3991 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003992
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003993 // OpenCL specific types:
3994 case tok::kw_image1d_t:
3995 case tok::kw_image1d_array_t:
3996 case tok::kw_image1d_buffer_t:
3997 case tok::kw_image2d_t:
3998 case tok::kw_image2d_array_t:
3999 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004000 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004001 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004002
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004003 // struct-or-union-specifier (C99) or class-specifier (C++)
4004 case tok::kw_class:
4005 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004006 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004007 case tok::kw_union:
4008 // enum-specifier
4009 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004010
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004011 // typedef-name
4012 case tok::annot_typename:
4013 return true;
4014 }
4015}
4016
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004017/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004018/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004019bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004020 switch (Tok.getKind()) {
4021 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004022
Chris Lattner020bab92009-01-04 23:41:41 +00004023 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004024 if (TryAltiVecVectorToken())
4025 return true;
4026 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004027 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004028 // Annotate typenames and C++ scope specifiers. If we get one, just
4029 // recurse to handle whatever we get.
4030 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004031 return true;
4032 if (Tok.is(tok::identifier))
4033 return false;
4034 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004035
Chris Lattner020bab92009-01-04 23:41:41 +00004036 case tok::coloncolon: // ::foo::bar
4037 if (NextToken().is(tok::kw_new) || // ::new
4038 NextToken().is(tok::kw_delete)) // ::delete
4039 return false;
4040
Chris Lattner020bab92009-01-04 23:41:41 +00004041 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004042 return true;
4043 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004044
Chris Lattnere37e2332006-08-15 04:50:22 +00004045 // GNU attributes support.
4046 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004047 // GNU typeof support.
4048 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004049
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004050 // type-specifiers
4051 case tok::kw_short:
4052 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004053 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004054 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004055 case tok::kw_signed:
4056 case tok::kw_unsigned:
4057 case tok::kw__Complex:
4058 case tok::kw__Imaginary:
4059 case tok::kw_void:
4060 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004061 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004062 case tok::kw_char16_t:
4063 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004064 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004065 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004066 case tok::kw_float:
4067 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004068 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004069 case tok::kw__Bool:
4070 case tok::kw__Decimal32:
4071 case tok::kw__Decimal64:
4072 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004073 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004074
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004075 // OpenCL specific types:
4076 case tok::kw_image1d_t:
4077 case tok::kw_image1d_array_t:
4078 case tok::kw_image1d_buffer_t:
4079 case tok::kw_image2d_t:
4080 case tok::kw_image2d_array_t:
4081 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004082 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004083 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004084
Chris Lattner861a2262008-04-13 18:59:07 +00004085 // struct-or-union-specifier (C99) or class-specifier (C++)
4086 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004087 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004088 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004089 case tok::kw_union:
4090 // enum-specifier
4091 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004092
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004093 // type-qualifier
4094 case tok::kw_const:
4095 case tok::kw_volatile:
4096 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004097
John McCallea0a39e2012-11-14 00:49:39 +00004098 // Debugger support.
4099 case tok::kw___unknown_anytype:
4100
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004101 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004102 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004103 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004104
Chris Lattner409bf7d2008-10-20 00:25:30 +00004105 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4106 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004107 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004108
Steve Naroff44ac7772008-12-25 14:16:32 +00004109 case tok::kw___cdecl:
4110 case tok::kw___stdcall:
4111 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004112 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004113 case tok::kw___w64:
4114 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004115 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004116 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004117 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004118
4119 case tok::kw___private:
4120 case tok::kw___local:
4121 case tok::kw___global:
4122 case tok::kw___constant:
4123 case tok::kw___read_only:
4124 case tok::kw___read_write:
4125 case tok::kw___write_only:
4126
Eli Friedman53339e02009-06-08 23:27:34 +00004127 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004128
4129 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004130 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00004131
Richard Smith8e1ac332013-03-28 01:55:44 +00004132 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004133 case tok::kw__Atomic:
4134 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004135 }
4136}
4137
Chris Lattneracd58a32006-08-06 17:24:14 +00004138/// isDeclarationSpecifier() - Return true if the current token is part of a
4139/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004140///
4141/// \param DisambiguatingWithExpression True to indicate that the purpose of
4142/// this check is to disambiguate between an expression and a declaration.
4143bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004144 switch (Tok.getKind()) {
4145 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004146
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004147 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004148 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004149
Chris Lattner020bab92009-01-04 23:41:41 +00004150 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004151 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004152 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004153 return false;
John Thompson22334602010-02-05 00:12:22 +00004154 if (TryAltiVecVectorToken())
4155 return true;
4156 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004157 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004158 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004159 // Annotate typenames and C++ scope specifiers. If we get one, just
4160 // recurse to handle whatever we get.
4161 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004162 return true;
4163 if (Tok.is(tok::identifier))
4164 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004165
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004166 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004167 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004168 // expression is permitted, then this is probably a class message send
4169 // missing the initial '['. In this case, we won't consider this to be
4170 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004171 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004172 isStartOfObjCClassMessageMissingOpenBracket())
4173 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004174
John McCall1f476a12010-02-26 08:45:28 +00004175 return isDeclarationSpecifier();
4176
Chris Lattner020bab92009-01-04 23:41:41 +00004177 case tok::coloncolon: // ::foo::bar
4178 if (NextToken().is(tok::kw_new) || // ::new
4179 NextToken().is(tok::kw_delete)) // ::delete
4180 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004181
Chris Lattner020bab92009-01-04 23:41:41 +00004182 // Annotate typenames and C++ scope specifiers. If we get one, just
4183 // recurse to handle whatever we get.
4184 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004185 return true;
4186 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004187
Chris Lattneracd58a32006-08-06 17:24:14 +00004188 // storage-class-specifier
4189 case tok::kw_typedef:
4190 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004191 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004192 case tok::kw_static:
4193 case tok::kw_auto:
4194 case tok::kw_register:
4195 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004196 case tok::kw_thread_local:
4197 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004198
Douglas Gregor26701a42011-09-09 02:06:17 +00004199 // Modules
4200 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004201
John McCallea0a39e2012-11-14 00:49:39 +00004202 // Debugger support
4203 case tok::kw___unknown_anytype:
4204
Chris Lattneracd58a32006-08-06 17:24:14 +00004205 // type-specifiers
4206 case tok::kw_short:
4207 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004208 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004209 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004210 case tok::kw_signed:
4211 case tok::kw_unsigned:
4212 case tok::kw__Complex:
4213 case tok::kw__Imaginary:
4214 case tok::kw_void:
4215 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004216 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004217 case tok::kw_char16_t:
4218 case tok::kw_char32_t:
4219
Chris Lattneracd58a32006-08-06 17:24:14 +00004220 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004221 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004222 case tok::kw_float:
4223 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004224 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004225 case tok::kw__Bool:
4226 case tok::kw__Decimal32:
4227 case tok::kw__Decimal64:
4228 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004229 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004230
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004231 // OpenCL specific types:
4232 case tok::kw_image1d_t:
4233 case tok::kw_image1d_array_t:
4234 case tok::kw_image1d_buffer_t:
4235 case tok::kw_image2d_t:
4236 case tok::kw_image2d_array_t:
4237 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004238 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004239 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004240
Chris Lattner861a2262008-04-13 18:59:07 +00004241 // struct-or-union-specifier (C99) or class-specifier (C++)
4242 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004243 case tok::kw_struct:
4244 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004245 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004246 // enum-specifier
4247 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004248
Chris Lattneracd58a32006-08-06 17:24:14 +00004249 // type-qualifier
4250 case tok::kw_const:
4251 case tok::kw_volatile:
4252 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004253
Chris Lattneracd58a32006-08-06 17:24:14 +00004254 // function-specifier
4255 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004256 case tok::kw_virtual:
4257 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004258 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004259
Richard Smith1dba27c2013-01-29 09:02:09 +00004260 // alignment-specifier
4261 case tok::kw__Alignas:
4262
Richard Smithd16fe122012-10-25 00:00:53 +00004263 // friend keyword.
4264 case tok::kw_friend:
4265
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004266 // static_assert-declaration
4267 case tok::kw__Static_assert:
4268
Chris Lattner599e47e2007-08-09 17:01:07 +00004269 // GNU typeof support.
4270 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004271
Chris Lattner599e47e2007-08-09 17:01:07 +00004272 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004273 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004274
Richard Smithd16fe122012-10-25 00:00:53 +00004275 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004276 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004277 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004278
Richard Smith8e1ac332013-03-28 01:55:44 +00004279 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004280 case tok::kw__Atomic:
4281 return true;
4282
Chris Lattner8b2ec162008-07-26 03:38:44 +00004283 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4284 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004285 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004286
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004287 // typedef-name
4288 case tok::annot_typename:
4289 return !DisambiguatingWithExpression ||
4290 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004291
Steve Narofff192fab2009-01-06 19:34:12 +00004292 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004293 case tok::kw___cdecl:
4294 case tok::kw___stdcall:
4295 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004296 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004297 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004298 case tok::kw___sptr:
4299 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004300 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004301 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004302 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004303 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004304 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004305
4306 case tok::kw___private:
4307 case tok::kw___local:
4308 case tok::kw___global:
4309 case tok::kw___constant:
4310 case tok::kw___read_only:
4311 case tok::kw___read_write:
4312 case tok::kw___write_only:
4313
Eli Friedman53339e02009-06-08 23:27:34 +00004314 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004315 }
4316}
4317
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004318bool Parser::isConstructorDeclarator() {
4319 TentativeParsingAction TPA(*this);
4320
4321 // Parse the C++ scope specifier.
4322 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004323 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004324 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004325 TPA.Revert();
4326 return false;
4327 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004328
4329 // Parse the constructor name.
4330 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4331 // We already know that we have a constructor name; just consume
4332 // the token.
4333 ConsumeToken();
4334 } else {
4335 TPA.Revert();
4336 return false;
4337 }
4338
Richard Smith43f340f2012-03-27 23:05:05 +00004339 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004340 if (Tok.isNot(tok::l_paren)) {
4341 TPA.Revert();
4342 return false;
4343 }
4344 ConsumeParen();
4345
Richard Smith43f340f2012-03-27 23:05:05 +00004346 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4347 // that we have a constructor.
4348 if (Tok.is(tok::r_paren) ||
4349 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004350 TPA.Revert();
4351 return true;
4352 }
4353
Richard Smithf2163662013-09-06 00:12:20 +00004354 // A C++11 attribute here signals that we have a constructor, and is an
4355 // attribute on the first constructor parameter.
4356 if (getLangOpts().CPlusPlus11 &&
4357 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4358 /*OuterMightBeMessageSend*/ true)) {
4359 TPA.Revert();
4360 return true;
4361 }
4362
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004363 // If we need to, enter the specified scope.
4364 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004365 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004366 DeclScopeObj.EnterDeclaratorScope();
4367
Francois Pichet79f3a872011-01-31 04:54:32 +00004368 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004369 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004370 MaybeParseMicrosoftAttributes(Attrs);
4371
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004372 // Check whether the next token(s) are part of a declaration
4373 // specifier, in which case we have the start of a parameter and,
4374 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004375 bool IsConstructor = false;
4376 if (isDeclarationSpecifier())
4377 IsConstructor = true;
4378 else if (Tok.is(tok::identifier) ||
4379 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4380 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4381 // This might be a parenthesized member name, but is more likely to
4382 // be a constructor declaration with an invalid argument type. Keep
4383 // looking.
4384 if (Tok.is(tok::annot_cxxscope))
4385 ConsumeToken();
4386 ConsumeToken();
4387
4388 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004389 // which must have one of the following syntactic forms (see the
4390 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004391 switch (Tok.getKind()) {
4392 case tok::l_paren:
4393 // C(X ( int));
4394 case tok::l_square:
4395 // C(X [ 5]);
4396 // C(X [ [attribute]]);
4397 case tok::coloncolon:
4398 // C(X :: Y);
4399 // C(X :: *p);
4400 case tok::r_paren:
4401 // C(X )
4402 // Assume this isn't a constructor, rather than assuming it's a
4403 // constructor with an unnamed parameter of an ill-formed type.
4404 break;
4405
4406 default:
4407 IsConstructor = true;
4408 break;
4409 }
4410 }
4411
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004412 TPA.Revert();
4413 return IsConstructor;
4414}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004415
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004416/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004417/// type-qualifier-list: [C99 6.7.5]
4418/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004419/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004420/// [ only if VendorAttributesAllowed=true ]
4421/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004422/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004423/// [ only if VendorAttributesAllowed=true ]
4424/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004425/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004426/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004427///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004428void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4429 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004430 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004431 bool AtomicAllowed,
4432 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004433 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004434 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004435 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004436 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004437 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004438 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004439
4440 SourceLocation EndLoc;
4441
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004442 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004443 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004444 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004445 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004446 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004447
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004448 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004449 case tok::code_completion:
4450 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004451 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004452
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004453 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004454 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004455 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004456 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004457 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004458 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004459 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004460 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004461 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004462 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004463 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004464 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004465 case tok::kw__Atomic:
4466 if (!AtomicAllowed)
4467 goto DoneWithTypeQuals;
4468 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4469 getLangOpts());
4470 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004471
4472 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004473 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004474 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004475 goto DoneWithTypeQuals;
4476 case tok::kw___private:
4477 case tok::kw___global:
4478 case tok::kw___local:
4479 case tok::kw___constant:
4480 case tok::kw___read_only:
4481 case tok::kw___write_only:
4482 case tok::kw___read_write:
4483 ParseOpenCLQualifiers(DS);
4484 break;
4485
Aaron Ballman317a77f2013-05-22 23:25:32 +00004486 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004487 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4488 // with the MS modifier keyword.
4489 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004490 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4491 if (TryKeywordIdentFallback(false))
4492 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004493 }
4494 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004495 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004496 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004497 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004498 case tok::kw___cdecl:
4499 case tok::kw___stdcall:
4500 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004501 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004502 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004503 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004504 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004505 continue;
4506 }
4507 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004508 case tok::kw___pascal:
4509 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004510 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004511 continue;
4512 }
4513 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004514 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004515 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004516 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004517 continue; // do *not* consume the next token!
4518 }
4519 // otherwise, FALL THROUGH!
4520 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004521 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004522 // If this is not a type-qualifier token, we're done reading type
4523 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004524 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004525 if (EndLoc.isValid())
4526 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004527 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004528 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004529
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004530 // If the specifier combination wasn't legal, issue a diagnostic.
4531 if (isInvalid) {
4532 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004533 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004534 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004535 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004536 }
4537}
4538
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004539
4540/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4541///
4542void Parser::ParseDeclarator(Declarator &D) {
4543 /// This implements the 'declarator' production in the C grammar, then checks
4544 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004545 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004546}
4547
Richard Smith0efa75c2012-03-29 01:16:42 +00004548static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4549 if (Kind == tok::star || Kind == tok::caret)
4550 return true;
4551
4552 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4553 if (!Lang.CPlusPlus)
4554 return false;
4555
4556 return Kind == tok::amp || Kind == tok::ampamp;
4557}
4558
Sebastian Redlbd150f42008-11-21 19:14:01 +00004559/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4560/// is parsed by the function passed to it. Pass null, and the direct-declarator
4561/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004562/// ptr-operator production.
4563///
Richard Smith09f76ee2011-10-19 21:33:05 +00004564/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004565/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4566/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004567///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004568/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4569/// [C] pointer[opt] direct-declarator
4570/// [C++] direct-declarator
4571/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004572///
4573/// pointer: [C99 6.7.5]
4574/// '*' type-qualifier-list[opt]
4575/// '*' type-qualifier-list[opt] pointer
4576///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004577/// ptr-operator:
4578/// '*' cv-qualifier-seq[opt]
4579/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004580/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004581/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004582/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004583/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004584void Parser::ParseDeclaratorInternal(Declarator &D,
4585 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004586 if (Diags.hasAllExtensionsSilenced())
4587 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004588
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004589 // C++ member pointers start with a '::' or a nested-name.
4590 // Member pointers get special handling, since there's no place for the
4591 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004592 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004593 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4594 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004595 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4596 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004597 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004598 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004599
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004600 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004601 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004602 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004603 if (D.mayHaveIdentifier())
4604 D.getCXXScopeSpec() = SS;
4605 else
4606 AnnotateScopeToken(SS, true);
4607
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004608 if (DirectDeclParser)
4609 (this->*DirectDeclParser)(D);
4610 return;
4611 }
4612
4613 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004614 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004615 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004616 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004617 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004618
4619 // Recurse to parse whatever is left.
4620 ParseDeclaratorInternal(D, DirectDeclParser);
4621
4622 // Sema will have to catch (syntactically invalid) pointers into global
4623 // scope. It has to catch pointers into namespace scope anyway.
4624 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004625 Loc),
4626 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004627 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004628 return;
4629 }
4630 }
4631
4632 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004633 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004634 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004635 if (DirectDeclParser)
4636 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004637 return;
4638 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004639
Sebastian Redled0f3b02009-03-15 22:02:01 +00004640 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4641 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004642 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004643 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004644
Chris Lattner9eac9312009-03-27 04:18:06 +00004645 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004646 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004647 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004648
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004649 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004650 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004651 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004652
Bill Wendling3708c182007-05-27 10:15:43 +00004653 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004654 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004655 if (Kind == tok::star)
4656 // Remember that we parsed a pointer type, and remember the type-quals.
4657 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004658 DS.getConstSpecLoc(),
4659 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004660 DS.getRestrictSpecLoc()),
4661 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004662 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004663 else
4664 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004665 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004666 Loc),
4667 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004668 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004669 } else {
4670 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004671 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004672
Sebastian Redl3b27be62009-03-23 00:00:23 +00004673 // Complain about rvalue references in C++03, but then go on and build
4674 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004675 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004676 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004677 diag::warn_cxx98_compat_rvalue_reference :
4678 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004679
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004680 // GNU-style and C++11 attributes are allowed here, as is restrict.
4681 ParseTypeQualifierListOpt(DS);
4682 D.ExtendWithDeclSpec(DS);
4683
Bill Wendling93efb222007-06-02 23:28:54 +00004684 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4685 // cv-qualifiers are introduced through the use of a typedef or of a
4686 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004687 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4688 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4689 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004690 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004691 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4692 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004693 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004694 // 'restrict' is permitted as an extension.
4695 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4696 Diag(DS.getAtomicSpecLoc(),
4697 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004698 }
Bill Wendling3708c182007-05-27 10:15:43 +00004699
4700 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004701 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004702
Douglas Gregor66583c52008-11-03 15:51:28 +00004703 if (D.getNumTypeObjects() > 0) {
4704 // C++ [dcl.ref]p4: There shall be no references to references.
4705 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4706 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004707 if (const IdentifierInfo *II = D.getIdentifier())
4708 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4709 << II;
4710 else
4711 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4712 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004713
Sebastian Redlbd150f42008-11-21 19:14:01 +00004714 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004715 // can go ahead and build the (technically ill-formed)
4716 // declarator: reference collapsing will take care of it.
4717 }
4718 }
4719
Richard Smith8e1ac332013-03-28 01:55:44 +00004720 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004721 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004722 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004723 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004724 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004725 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004726}
4727
Richard Smith0efa75c2012-03-29 01:16:42 +00004728static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4729 SourceLocation EllipsisLoc) {
4730 if (EllipsisLoc.isValid()) {
4731 FixItHint Insertion;
4732 if (!D.getEllipsisLoc().isValid()) {
4733 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4734 D.setEllipsisLoc(EllipsisLoc);
4735 }
4736 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4737 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4738 }
4739}
4740
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004741/// ParseDirectDeclarator
4742/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004743/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004744/// '(' declarator ')'
4745/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004746/// [C90] direct-declarator '[' constant-expression[opt] ']'
4747/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4748/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4749/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4750/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004751/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4752/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004753/// direct-declarator '(' parameter-type-list ')'
4754/// direct-declarator '(' identifier-list[opt] ')'
4755/// [GNU] direct-declarator '(' parameter-forward-declarations
4756/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004757/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4758/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004759/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4760/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4761/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004762/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004763/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004764///
4765/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004766/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004767/// '::'[opt] nested-name-specifier[opt] type-name
4768///
4769/// id-expression: [C++ 5.1]
4770/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004771/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004772///
4773/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004774/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004775/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004776/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004777/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004778/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004779///
Richard Smith1453e312012-03-27 01:42:32 +00004780/// Note, any additional constructs added here may need corresponding changes
4781/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004782void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004783 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004784
David Blaikiebbafb8a2012-03-11 07:00:24 +00004785 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004786 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004787 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004788 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4789 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004790 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004791 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004792 }
4793
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004794 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004795 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004796 // Change the declaration context for name lookup, until this function
4797 // is exited (and the declarator has been parsed).
4798 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004799 }
4800
Douglas Gregor27b4c162010-12-23 22:44:42 +00004801 // C++0x [dcl.fct]p14:
4802 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004803 // of a parameter-declaration-clause without a preceding comma. In
4804 // this case, the ellipsis is parsed as part of the
4805 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004806 // parameter pack that has not been expanded; otherwise, it is parsed
4807 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004808 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004809 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004810 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004811 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004812 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004813 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004814 !Actions.containsUnexpandedParameterPacks(D))) {
4815 SourceLocation EllipsisLoc = ConsumeToken();
4816 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4817 // The ellipsis was put in the wrong place. Recover, and explain to
4818 // the user what they should have done.
4819 ParseDeclarator(D);
4820 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4821 return;
4822 } else
4823 D.setEllipsisLoc(EllipsisLoc);
4824
4825 // The ellipsis can't be followed by a parenthesized declarator. We
4826 // check for that in ParseParenDeclarator, after we have disambiguated
4827 // the l_paren token.
4828 }
4829
Douglas Gregor7861a802009-11-03 01:35:08 +00004830 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4831 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4832 // We found something that indicates the start of an unqualified-id.
4833 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004834 bool AllowConstructorName;
4835 if (D.getDeclSpec().hasTypeSpecifier())
4836 AllowConstructorName = false;
4837 else if (D.getCXXScopeSpec().isSet())
4838 AllowConstructorName =
4839 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004840 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004841 else
4842 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4843
Abramo Bagnara7945c982012-01-27 09:46:47 +00004844 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004845 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4846 /*EnteringContext=*/true,
4847 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004848 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004849 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004850 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004851 D.getName()) ||
4852 // Once we're past the identifier, if the scope was bad, mark the
4853 // whole declarator bad.
4854 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004855 D.SetIdentifier(0, Tok.getLocation());
4856 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004857 } else {
4858 // Parsed the unqualified-id; update range information and move along.
4859 if (D.getSourceRange().getBegin().isInvalid())
4860 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4861 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004862 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004863 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004864 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004865 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004866 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004867 "There's a C++-specific check for tok::identifier above");
4868 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4869 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4870 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004871 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004872 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004873 // A virt-specifier isn't treated as an identifier if it appears after a
4874 // trailing-return-type.
4875 if (D.getContext() != Declarator::TrailingReturnContext ||
4876 !isCXX11VirtSpecifier(Tok)) {
4877 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4878 << FixItHint::CreateRemoval(Tok.getLocation());
4879 D.SetIdentifier(0, Tok.getLocation());
4880 ConsumeToken();
4881 goto PastIdentifier;
4882 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004883 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004884
Douglas Gregor7861a802009-11-03 01:35:08 +00004885 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004886 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004887 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004888 // Example: 'char (*X)' or 'int (*XX)(void)'
4889 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004890
4891 // If the declarator was parenthesized, we entered the declarator
4892 // scope when parsing the parenthesized declarator, then exited
4893 // the scope already. Re-enter the scope, if we need to.
4894 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004895 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004896 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004897 if (!D.isInvalidType() &&
4898 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004899 // Change the declaration context for name lookup, until this function
4900 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004901 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004902 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004903 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004904 // This could be something simple like "int" (in which case the declarator
4905 // portion is empty), if an abstract-declarator is allowed.
4906 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004907
4908 // The grammar for abstract-pack-declarator does not allow grouping parens.
4909 // FIXME: Revisit this once core issue 1488 is resolved.
4910 if (D.hasEllipsis() && D.hasGroupingParens())
4911 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4912 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004913 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004914 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004915 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004916 if (D.getContext() == Declarator::MemberContext)
4917 Diag(Tok, diag::err_expected_member_name_or_semi)
4918 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004919 else if (getLangOpts().CPlusPlus) {
4920 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4921 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004922 else {
4923 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4924 if (Tok.isAtStartOfLine() && Loc.isValid())
4925 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4926 << getLangOpts().CPlusPlus;
4927 else
4928 Diag(Tok, diag::err_expected_unqualified_id)
4929 << getLangOpts().CPlusPlus;
4930 }
Richard Trieu9c672672013-01-26 02:31:38 +00004931 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004932 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004933 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004934 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004935 }
Mike Stump11289f42009-09-09 15:08:12 +00004936
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004937 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004938 assert(D.isPastIdentifier() &&
4939 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004940
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004941 // Don't parse attributes unless we have parsed an unparenthesized name.
4942 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004943 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004944
Chris Lattneracd58a32006-08-06 17:24:14 +00004945 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004946 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004947 // Enter function-declaration scope, limiting any declarators to the
4948 // function prototype scope, including parameter declarators.
4949 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004950 Scope::FunctionPrototypeScope|Scope::DeclScope|
4951 (D.isFunctionDeclaratorAFunctionDeclaration()
4952 ? Scope::FunctionDeclarationScope : 0));
4953
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004954 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4955 // In such a case, check if we actually have a function declarator; if it
4956 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004957 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004958 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4959 // The name of the declarator, if any, is tentatively declared within
4960 // a possible direct initializer.
4961 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4962 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4963 TentativelyDeclaredIdentifiers.pop_back();
4964 if (!IsFunctionDecl)
4965 break;
4966 }
John McCall084e83d2011-03-24 11:26:52 +00004967 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004968 BalancedDelimiterTracker T(*this, tok::l_paren);
4969 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004970 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004971 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004972 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004973 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004974 } else {
4975 break;
4976 }
4977 }
Chad Rosierc1183952012-06-26 22:30:43 +00004978}
Chris Lattneracd58a32006-08-06 17:24:14 +00004979
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004980/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4981/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004982/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004983/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4984///
4985/// direct-declarator:
4986/// '(' declarator ')'
4987/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004988/// direct-declarator '(' parameter-type-list ')'
4989/// direct-declarator '(' identifier-list[opt] ')'
4990/// [GNU] direct-declarator '(' parameter-forward-declarations
4991/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004992///
4993void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004994 BalancedDelimiterTracker T(*this, tok::l_paren);
4995 T.consumeOpen();
4996
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004997 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004998
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004999 // Eat any attributes before we look at whether this is a grouping or function
5000 // declarator paren. If this is a grouping paren, the attribute applies to
5001 // the type being built up, for example:
5002 // int (__attribute__(()) *x)(long y)
5003 // If this ends up not being a grouping paren, the attribute applies to the
5004 // first argument, for example:
5005 // int (__attribute__(()) int x)
5006 // In either case, we need to eat any attributes to be able to determine what
5007 // sort of paren this is.
5008 //
John McCall084e83d2011-03-24 11:26:52 +00005009 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005010 bool RequiresArg = false;
5011 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005012 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005013
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005014 // We require that the argument list (if this is a non-grouping paren) be
5015 // present even if the attribute list was empty.
5016 RequiresArg = true;
5017 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005018
Steve Naroff44ac7772008-12-25 14:16:32 +00005019 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005020 ParseMicrosoftTypeAttributes(attrs);
5021
Dawn Perchik335e16b2010-09-03 01:29:35 +00005022 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005023 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005024 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005025
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005026 // If we haven't past the identifier yet (or where the identifier would be
5027 // stored, if this is an abstract declarator), then this is probably just
5028 // grouping parens. However, if this could be an abstract-declarator, then
5029 // this could also be the start of function arguments (consider 'void()').
5030 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005031
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005032 if (!D.mayOmitIdentifier()) {
5033 // If this can't be an abstract-declarator, this *must* be a grouping
5034 // paren, because we haven't seen the identifier yet.
5035 isGrouping = true;
5036 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005037 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5038 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005039 isDeclarationSpecifier() || // 'int(int)' is a function.
5040 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005041 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5042 // considered to be a type, not a K&R identifier-list.
5043 isGrouping = false;
5044 } else {
5045 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5046 isGrouping = true;
5047 }
Mike Stump11289f42009-09-09 15:08:12 +00005048
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005049 // If this is a grouping paren, handle:
5050 // direct-declarator: '(' declarator ')'
5051 // direct-declarator: '(' attributes declarator ')'
5052 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005053 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5054 D.setEllipsisLoc(SourceLocation());
5055
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005056 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005057 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005058 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005059 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005060 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005061 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005062 T.getCloseLocation()),
5063 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005064
5065 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005066
5067 // An ellipsis cannot be placed outside parentheses.
5068 if (EllipsisLoc.isValid())
5069 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5070
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005071 return;
5072 }
Mike Stump11289f42009-09-09 15:08:12 +00005073
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005074 // Okay, if this wasn't a grouping paren, it must be the start of a function
5075 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005076 // identifier (and remember where it would have been), then call into
5077 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005078 D.SetIdentifier(0, Tok.getLocation());
5079
David Blaikie15a430a2011-12-04 05:04:18 +00005080 // Enter function-declaration scope, limiting any declarators to the
5081 // function prototype scope, including parameter declarators.
5082 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005083 Scope::FunctionPrototypeScope | Scope::DeclScope |
5084 (D.isFunctionDeclaratorAFunctionDeclaration()
5085 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005086 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005087 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005088}
5089
5090/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5091/// declarator D up to a paren, which indicates that we are parsing function
5092/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005093///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005094/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5095/// immediately after the open paren - they should be considered to be the
5096/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005097///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005098/// If RequiresArg is true, then the first argument of the function is required
5099/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005100///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005101/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5102/// (C++11) ref-qualifier[opt], exception-specification[opt],
5103/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5104///
5105/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005106/// dynamic-exception-specification
5107/// noexcept-specification
5108///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005109void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005110 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005111 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005112 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005113 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005114 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005115 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005116 // lparen is already consumed!
5117 assert(D.isPastIdentifier() && "Should not call before identifier!");
5118
5119 // This should be true when the function has typed arguments.
5120 // Otherwise, it is treated as a K&R-style function.
5121 bool HasProto = false;
5122 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005123 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005124 // Remember where we see an ellipsis, if any.
5125 SourceLocation EllipsisLoc;
5126
5127 DeclSpec DS(AttrFactory);
5128 bool RefQualifierIsLValueRef = true;
5129 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005130 SourceLocation ConstQualifierLoc;
5131 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005132 ExceptionSpecificationType ESpecType = EST_None;
5133 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005134 SmallVector<ParsedType, 2> DynamicExceptions;
5135 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005136 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005137 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005138 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005139
James Molloy6f8780b2012-02-29 10:24:19 +00005140 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005141 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5142 EndLoc is the end location for the function declarator.
5143 They differ for trailing return types. */
5144 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005145 SourceLocation LParenLoc, RParenLoc;
5146 LParenLoc = Tracker.getOpenLocation();
5147 StartLoc = LParenLoc;
5148
Douglas Gregor9e66af42011-07-05 16:44:18 +00005149 if (isFunctionDeclaratorIdentifierList()) {
5150 if (RequiresArg)
5151 Diag(Tok, diag::err_argument_required_after_attribute);
5152
5153 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5154
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005155 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005156 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005157 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005158 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005159 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005160 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005161 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5162 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005163 else if (RequiresArg)
5164 Diag(Tok, diag::err_argument_required_after_attribute);
5165
David Blaikiebbafb8a2012-03-11 07:00:24 +00005166 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005167
5168 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005169 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005170 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005171 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005172 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005173
David Blaikiebbafb8a2012-03-11 07:00:24 +00005174 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005175 // FIXME: Accept these components in any order, and produce fixits to
5176 // correct the order if the user gets it wrong. Ideally we should deal
5177 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005178
5179 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005180 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5181 /*CXX11AttributesAllowed*/ false,
5182 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005183 if (!DS.getSourceRange().getEnd().isInvalid()) {
5184 EndLoc = DS.getSourceRange().getEnd();
5185 ConstQualifierLoc = DS.getConstSpecLoc();
5186 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5187 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005188
5189 // Parse ref-qualifier[opt].
5190 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005191 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005192 diag::warn_cxx98_compat_ref_qualifier :
5193 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005194
Douglas Gregor9e66af42011-07-05 16:44:18 +00005195 RefQualifierIsLValueRef = Tok.is(tok::amp);
5196 RefQualifierLoc = ConsumeToken();
5197 EndLoc = RefQualifierLoc;
5198 }
5199
Douglas Gregor3024f072012-04-16 07:05:22 +00005200 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005201 // If a declaration declares a member function or member function
5202 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005203 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005204 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005205 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005206 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005207 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005208 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005209 (D.getContext() == Declarator::MemberContext
5210 ? !D.getDeclSpec().isFriendSpecified()
5211 : D.getContext() == Declarator::FileContext &&
5212 D.getCXXScopeSpec().isValid() &&
5213 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005214 Sema::CXXThisScopeRAII ThisScope(Actions,
5215 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005216 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005217 (D.getDeclSpec().isConstexprSpecified() &&
5218 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005219 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005220 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005221
Douglas Gregor9e66af42011-07-05 16:44:18 +00005222 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005223 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005224 DynamicExceptions,
5225 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005226 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005227 if (ESpecType != EST_None)
5228 EndLoc = ESpecRange.getEnd();
5229
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005230 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5231 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005232 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005233
Douglas Gregor9e66af42011-07-05 16:44:18 +00005234 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005235 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005236 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005237 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005238 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5239 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005240 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005241 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005242 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005243 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005244 }
5245 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005246 }
5247
5248 // Remember that we parsed a function type, and remember the attributes.
5249 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005250 IsAmbiguous,
5251 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005252 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005253 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005254 DS.getTypeQualifiers(),
5255 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005256 RefQualifierLoc, ConstQualifierLoc,
5257 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005258 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005259 ESpecType, ESpecRange.getBegin(),
5260 DynamicExceptions.data(),
5261 DynamicExceptionRanges.data(),
5262 DynamicExceptions.size(),
5263 NoexceptExpr.isUsable() ?
5264 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005265 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005266 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005267 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005268
5269 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005270}
5271
5272/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5273/// identifier list form for a K&R-style function: void foo(a,b,c)
5274///
5275/// Note that identifier-lists are only allowed for normal declarators, not for
5276/// abstract-declarators.
5277bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005278 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005279 && Tok.is(tok::identifier)
5280 && !TryAltiVecVectorToken()
5281 // K&R identifier lists can't have typedefs as identifiers, per C99
5282 // 6.7.5.3p11.
5283 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5284 // Identifier lists follow a really simple grammar: the identifiers can
5285 // be followed *only* by a ", identifier" or ")". However, K&R
5286 // identifier lists are really rare in the brave new modern world, and
5287 // it is very common for someone to typo a type in a non-K&R style
5288 // list. If we are presented with something like: "void foo(intptr x,
5289 // float y)", we don't want to start parsing the function declarator as
5290 // though it is a K&R style declarator just because intptr is an
5291 // invalid type.
5292 //
5293 // To handle this, we check to see if the token after the first
5294 // identifier is a "," or ")". Only then do we parse it as an
5295 // identifier list.
5296 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5297}
5298
5299/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5300/// we found a K&R-style identifier list instead of a typed parameter list.
5301///
5302/// After returning, ParamInfo will hold the parsed parameters.
5303///
5304/// identifier-list: [C99 6.7.5]
5305/// identifier
5306/// identifier-list ',' identifier
5307///
5308void Parser::ParseFunctionDeclaratorIdentifierList(
5309 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005310 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005311 // If there was no identifier specified for the declarator, either we are in
5312 // an abstract-declarator, or we are in a parameter declarator which was found
5313 // to be abstract. In abstract-declarators, identifier lists are not valid:
5314 // diagnose this.
5315 if (!D.getIdentifier())
5316 Diag(Tok, diag::ext_ident_list_in_param);
5317
5318 // Maintain an efficient lookup of params we have seen so far.
5319 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5320
5321 while (1) {
5322 // If this isn't an identifier, report the error and skip until ')'.
5323 if (Tok.isNot(tok::identifier)) {
5324 Diag(Tok, diag::err_expected_ident);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005325 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005326 // Forget we parsed anything.
5327 ParamInfo.clear();
5328 return;
5329 }
5330
5331 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5332
5333 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5334 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5335 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5336
5337 // Verify that the argument identifier has not already been mentioned.
5338 if (!ParamsSoFar.insert(ParmII)) {
5339 Diag(Tok, diag::err_param_redefinition) << ParmII;
5340 } else {
5341 // Remember this identifier in ParamInfo.
5342 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5343 Tok.getLocation(),
5344 0));
5345 }
5346
5347 // Eat the identifier.
5348 ConsumeToken();
5349
5350 // The list continues if we see a comma.
5351 if (Tok.isNot(tok::comma))
5352 break;
5353 ConsumeToken();
5354 }
5355}
5356
5357/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5358/// after the opening parenthesis. This function will not parse a K&R-style
5359/// identifier list.
5360///
Richard Smith2620cd92012-04-11 04:01:28 +00005361/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5362/// caller parsed those arguments immediately after the open paren - they should
5363/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005364///
5365/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5366/// be the location of the ellipsis, if any was parsed.
5367///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005368/// parameter-type-list: [C99 6.7.5]
5369/// parameter-list
5370/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005371/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005372///
5373/// parameter-list: [C99 6.7.5]
5374/// parameter-declaration
5375/// parameter-list ',' parameter-declaration
5376///
5377/// parameter-declaration: [C99 6.7.5]
5378/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005379/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005380/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005381/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005382/// declaration-specifiers abstract-declarator[opt]
5383/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005384/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005385/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005386/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005387///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005388void Parser::ParseParameterDeclarationClause(
5389 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005390 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005391 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005392 SourceLocation &EllipsisLoc) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005393 while (1) {
5394 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005395 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5396 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005397 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005398 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005399 }
Mike Stump11289f42009-09-09 15:08:12 +00005400
Chris Lattner371ed4e2008-04-06 06:57:35 +00005401 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005402 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005403 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005404
Richard Smith2620cd92012-04-11 04:01:28 +00005405 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005406 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005407
John McCall53fa7142010-12-24 02:08:15 +00005408 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005409 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005410
5411 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005412
5413 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005414 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005415 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005416 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5417 // too much hassle.
5418 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005419
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005420 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005421
Faisal Vali2b391ab2013-09-26 19:54:12 +00005422
5423 // Parse the declarator. This is "PrototypeContext" or
5424 // "LambdaExprParameterContext", because we must accept either
5425 // 'declarator' or 'abstract-declarator' here.
5426 Declarator ParmDeclarator(DS,
5427 D.getContext() == Declarator::LambdaExprContext ?
5428 Declarator::LambdaExprParameterContext :
5429 Declarator::PrototypeContext);
5430 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005431
5432 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005433 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005434
Chris Lattner371ed4e2008-04-06 06:57:35 +00005435 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005436 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005437
Douglas Gregor4d87df52008-12-16 21:30:33 +00005438 // DefArgToks is used when the parsing of default arguments needs
5439 // to be delayed.
5440 CachedTokens *DefArgToks = 0;
5441
Chris Lattner371ed4e2008-04-06 06:57:35 +00005442 // If no parameter was specified, verify that *something* was specified,
5443 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005444 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5445 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005446 // Completely missing, emit error.
5447 Diag(DSStart, diag::err_missing_param);
5448 } else {
5449 // Otherwise, we have something. Add it and let semantic analysis try
5450 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005451
Chris Lattner371ed4e2008-04-06 06:57:35 +00005452 // Inform the actions module about the parameter declarator, so it gets
5453 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005454 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5455 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005456 // Parse the default argument, if any. We parse the default
5457 // arguments in all dialects; the semantic analysis in
5458 // ActOnParamDefaultArgument will reject the default argument in
5459 // C.
5460 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005461 SourceLocation EqualLoc = Tok.getLocation();
5462
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005463 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005464 if (D.getContext() == Declarator::MemberContext) {
5465 // If we're inside a class definition, cache the tokens
5466 // corresponding to the default argument. We'll actually parse
5467 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005468 // FIXME: Can we use a smart pointer for Toks?
5469 DefArgToks = new CachedTokens;
5470
Richard Smith1fff95c2013-09-12 23:28:08 +00005471 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005472 delete DefArgToks;
5473 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005474 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005475 } else {
5476 // Mark the end of the default argument so that we know when to
5477 // stop when we parse it later on.
5478 Token DefArgEnd;
5479 DefArgEnd.startToken();
5480 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5481 DefArgEnd.setLocation(Tok.getLocation());
5482 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005483 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005484 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005485 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005486 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005487 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005488 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005489
Chad Rosierc1183952012-06-26 22:30:43 +00005490 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005491 // used.
5492 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005493 Sema::PotentiallyEvaluatedIfUsed,
5494 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005495
Sebastian Redldb63af22012-03-14 15:54:00 +00005496 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005497 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005498 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005499 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005500 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005501 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005502 if (DefArgResult.isInvalid()) {
5503 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005504 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005505 } else {
5506 // Inform the actions module about the default argument
5507 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005508 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005509 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005510 }
5511 }
Mike Stump11289f42009-09-09 15:08:12 +00005512
5513 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005514 ParmDeclarator.getIdentifierLoc(),
5515 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005516 }
5517
5518 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005519 if (Tok.isNot(tok::comma)) {
5520 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005521 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005522
David Blaikiebbafb8a2012-03-11 07:00:24 +00005523 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005524 // We have ellipsis without a preceding ',', which is ill-formed
5525 // in C. Complain and provide the fix.
5526 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005527 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005528 }
5529 }
Chad Rosierc1183952012-06-26 22:30:43 +00005530
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005531 break;
5532 }
Mike Stump11289f42009-09-09 15:08:12 +00005533
Chris Lattner371ed4e2008-04-06 06:57:35 +00005534 // Consume the comma.
5535 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005536 }
Mike Stump11289f42009-09-09 15:08:12 +00005537
Chris Lattner6c940e62008-04-06 06:34:08 +00005538}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005539
Chris Lattnere8074e62006-08-06 18:30:15 +00005540/// [C90] direct-declarator '[' constant-expression[opt] ']'
5541/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5542/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5543/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5544/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005545/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5546/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005547void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005548 if (CheckProhibitedCXX11Attribute())
5549 return;
5550
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005551 BalancedDelimiterTracker T(*this, tok::l_square);
5552 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005553
Chris Lattner84a11622008-12-18 07:27:21 +00005554 // C array syntax has many features, but by-far the most common is [] and [4].
5555 // This code does a fast path to handle some of the most obvious cases.
5556 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005557 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005558 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005559 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005560
Chris Lattner84a11622008-12-18 07:27:21 +00005561 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005562 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005563 T.getOpenLocation(),
5564 T.getCloseLocation()),
5565 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005566 return;
5567 } else if (Tok.getKind() == tok::numeric_constant &&
5568 GetLookAheadToken(1).is(tok::r_square)) {
5569 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005570 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005571 ConsumeToken();
5572
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005573 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005574 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005575 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005576
Chris Lattner84a11622008-12-18 07:27:21 +00005577 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005578 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005579 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005580 T.getOpenLocation(),
5581 T.getCloseLocation()),
5582 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005583 return;
5584 }
Mike Stump11289f42009-09-09 15:08:12 +00005585
Chris Lattnere8074e62006-08-06 18:30:15 +00005586 // If valid, this location is the position where we read the 'static' keyword.
5587 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005588 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005589 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005590
Chris Lattnere8074e62006-08-06 18:30:15 +00005591 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005592 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005593 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005594 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005595
Chris Lattnere8074e62006-08-06 18:30:15 +00005596 // If we haven't already read 'static', check to see if there is one after the
5597 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005598 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005599 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005600
Chris Lattnere8074e62006-08-06 18:30:15 +00005601 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005602 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005603 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005604
Chris Lattner521ff2b2008-04-06 05:26:30 +00005605 // Handle the case where we have '[*]' as the array size. However, a leading
5606 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005607 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005608 // infrequent, use of lookahead is not costly here.
5609 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005610 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005611
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005612 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005613 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005614 StaticLoc = SourceLocation(); // Drop the static.
5615 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005616 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005617 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005618 // Note, in C89, this production uses the constant-expr production instead
5619 // of assignment-expr. The only difference is that assignment-expr allows
5620 // things like '=' and '*='. Sema rejects these in C89 mode because they
5621 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005622
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005623 // Parse the constant-expression or assignment-expression now (depending
5624 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005625 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005626 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005627 } else {
5628 EnterExpressionEvaluationContext Unevaluated(Actions,
5629 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005630 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005631 }
Chris Lattner62591722006-08-12 18:40:58 +00005632 }
Mike Stump11289f42009-09-09 15:08:12 +00005633
Chris Lattner62591722006-08-12 18:40:58 +00005634 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005635 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005636 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005637 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005638 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005639 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005640 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005641
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005642 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005643
John McCall084e83d2011-03-24 11:26:52 +00005644 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005645 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005646
Chris Lattner84a11622008-12-18 07:27:21 +00005647 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005648 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005649 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005650 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005651 T.getOpenLocation(),
5652 T.getCloseLocation()),
5653 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005654}
5655
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005656/// [GNU] typeof-specifier:
5657/// typeof ( expressions )
5658/// typeof ( type-name )
5659/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005660///
5661void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005662 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005663 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005664 SourceLocation StartLoc = ConsumeToken();
5665
John McCalle8595032010-01-13 20:03:27 +00005666 const bool hasParens = Tok.is(tok::l_paren);
5667
Eli Friedman15681d62012-09-26 04:34:21 +00005668 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5669 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005670
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005671 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005672 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005673 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005674 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5675 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005676 if (hasParens)
5677 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005678
5679 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005680 // FIXME: Not accurate, the range gets one token more than it should.
5681 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005682 else
5683 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005684
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005685 if (isCastExpr) {
5686 if (!CastTy) {
5687 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005688 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005689 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005690
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005691 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005692 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005693 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5694 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005695 DiagID, CastTy))
5696 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005697 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005698 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005699
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005700 // If we get here, the operand to the typeof was an expresion.
5701 if (Operand.isInvalid()) {
5702 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005703 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005704 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005705
Eli Friedmane0afc982012-01-21 01:01:51 +00005706 // We might need to transform the operand if it is potentially evaluated.
5707 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5708 if (Operand.isInvalid()) {
5709 DS.SetTypeSpecError();
5710 return;
5711 }
5712
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005713 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005714 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005715 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5716 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005717 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005718 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005719}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005720
Benjamin Kramere56f3932011-12-23 17:00:35 +00005721/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005722/// _Atomic ( type-name )
5723///
5724void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005725 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5726 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005727
5728 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005729 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005730 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005731 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005732
5733 TypeResult Result = ParseTypeName();
5734 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005735 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005736 return;
5737 }
5738
5739 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005740 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005741
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005742 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005743 return;
5744
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005745 DS.setTypeofParensRange(T.getRange());
5746 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005747
5748 const char *PrevSpec = 0;
5749 unsigned DiagID;
5750 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5751 DiagID, Result.release()))
5752 Diag(StartLoc, DiagID) << PrevSpec;
5753}
5754
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005755
5756/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5757/// from TryAltiVecVectorToken.
5758bool Parser::TryAltiVecVectorTokenOutOfLine() {
5759 Token Next = NextToken();
5760 switch (Next.getKind()) {
5761 default: return false;
5762 case tok::kw_short:
5763 case tok::kw_long:
5764 case tok::kw_signed:
5765 case tok::kw_unsigned:
5766 case tok::kw_void:
5767 case tok::kw_char:
5768 case tok::kw_int:
5769 case tok::kw_float:
5770 case tok::kw_double:
5771 case tok::kw_bool:
5772 case tok::kw___pixel:
5773 Tok.setKind(tok::kw___vector);
5774 return true;
5775 case tok::identifier:
5776 if (Next.getIdentifierInfo() == Ident_pixel) {
5777 Tok.setKind(tok::kw___vector);
5778 return true;
5779 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005780 if (Next.getIdentifierInfo() == Ident_bool) {
5781 Tok.setKind(tok::kw___vector);
5782 return true;
5783 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005784 return false;
5785 }
5786}
5787
5788bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5789 const char *&PrevSpec, unsigned &DiagID,
5790 bool &isInvalid) {
5791 if (Tok.getIdentifierInfo() == Ident_vector) {
5792 Token Next = NextToken();
5793 switch (Next.getKind()) {
5794 case tok::kw_short:
5795 case tok::kw_long:
5796 case tok::kw_signed:
5797 case tok::kw_unsigned:
5798 case tok::kw_void:
5799 case tok::kw_char:
5800 case tok::kw_int:
5801 case tok::kw_float:
5802 case tok::kw_double:
5803 case tok::kw_bool:
5804 case tok::kw___pixel:
5805 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5806 return true;
5807 case tok::identifier:
5808 if (Next.getIdentifierInfo() == Ident_pixel) {
5809 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5810 return true;
5811 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005812 if (Next.getIdentifierInfo() == Ident_bool) {
5813 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5814 return true;
5815 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005816 break;
5817 default:
5818 break;
5819 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005820 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005821 DS.isTypeAltiVecVector()) {
5822 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5823 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005824 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5825 DS.isTypeAltiVecVector()) {
5826 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5827 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005828 }
5829 return false;
5830}